This repository has been archived by the owner on May 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the custom implementation of broadcast(), and just call the base method on the lift()ed method. This implements the blacklist approach: methods with a custom lifting behavior like isnull() should override lift() to get passed Nullable values directly; if they do not return a Nullable, the result is a standard Array rather than a NullableArray. Performance will probably be worse than before, but at least the semantics will be correct. We can always re-implement the custom and faster versions later when broadcast() has stabilized in Base and Nullable support has settled.
- Loading branch information
Showing
4 changed files
with
165 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import Base: null_safe_op | ||
|
||
############################################################################## | ||
## | ||
## Standard lifting semantics | ||
## | ||
## For a function call f(xs...), return null if any x in xs is null; | ||
## otherwise, return f applied to values of xs. | ||
## | ||
############################################################################## | ||
|
||
@inline function lift(f, x) | ||
if null_safe_op(f, typeof(x)) | ||
return @compat Nullable(f(x.value), !isnull(x)) | ||
else | ||
U = Core.Inference.return_type(f, Tuple{eltype(typeof(x))}) | ||
if isnull(x) | ||
return Nullable{U}() | ||
else | ||
return Nullable(f(unsafe_get(x))) | ||
end | ||
end | ||
end | ||
|
||
@inline function lift(f, x1, x2) | ||
if null_safe_op(f, typeof(x1), typeof(x2)) | ||
return @compat Nullable( | ||
f(x1.value, x2.value), !(isnull(x1) | isnull(x2)) | ||
) | ||
else | ||
U = Core.Inference.return_type( | ||
f, Tuple{eltype(typeof(x1)), eltype(typeof(x2))} | ||
) | ||
if isnull(x1) | isnull(x2) | ||
return Nullable{U}() | ||
else | ||
return Nullable(f(unsafe_get(x1), unsafe_get(x2))) | ||
end | ||
end | ||
end | ||
|
||
hasnulls(xs...) = any(isnull.(xs)) | ||
|
||
@inline function lift(f, xs...) | ||
if null_safe_op(f, map(typeof, xs)...) | ||
return @compat Nullable( | ||
f(map(unsafe_get, xs)...), !(mapreduce(isnull, |, xs)) | ||
) | ||
else | ||
U = Core.Inference.return_type( | ||
f, Tuple{map(x->eltype(typeof(x)), xs)...} | ||
) | ||
if hasnulls(xs...) | ||
return Nullable{U}() | ||
else | ||
return Nullable(f(map(unsafe_get, xs)...)) | ||
end | ||
end | ||
end | ||
|
||
############################################################################## | ||
## | ||
## Non-standard lifting semantics | ||
## | ||
############################################################################## | ||
|
||
# three-valued logic implementation | ||
@inline function lift(::typeof(&), x, y)::Nullable{Bool} | ||
return ifelse( isnull(x), | ||
ifelse( isnull(y), | ||
Nullable{Bool}(), # x, y null | ||
ifelse( unsafe_get(y), | ||
Nullable{Bool}(), # x null, y == true | ||
Nullable(false) # x null, y == false | ||
) | ||
), | ||
ifelse( isnull(y), | ||
ifelse( unsafe_get(x), | ||
Nullable{Bool}(), # x == true, y null | ||
Nullable(false) # x == false, y null | ||
), | ||
Nullable(unsafe_get(x) & unsafe_get(y)) # x, y not null | ||
) | ||
) | ||
end | ||
|
||
# three-valued logic implementation | ||
@inline function lift(::typeof(|), x, y)::Nullable{Bool} | ||
return ifelse( isnull(x), | ||
ifelse( isnull(y), | ||
Nullable{Bool}(), # x, y null | ||
ifelse( unsafe_get(y), | ||
Nullable(true), # x null, y == true | ||
Nullable{Bool}() # x null, y == false | ||
) | ||
), | ||
ifelse( isnull(y), | ||
ifelse( unsafe_get(x), | ||
Nullable(true), # x == true, y null | ||
Nullable{Bool}() # x == false, y null | ||
), | ||
Nullable(unsafe_get(x) | unsafe_get(y)) # x, y not null | ||
) | ||
) | ||
end | ||
|
||
# TODO: Decide on semantics for isequal and uncomment the following | ||
# @inline function lift(::typeof(isequal), x, y) | ||
# return ifelse( isnull(x), | ||
# ifelse( isnull(y), | ||
# true, # x, y null | ||
# false # x null, y not null | ||
# ), | ||
# ifelse( isnull(y), | ||
# false, # x not null, y null | ||
# isequal(unsafe_get(x), unsafe_get(y)) # x, y not null | ||
# ) | ||
# ) | ||
# end | ||
|
||
@inline function lift(::typeof(isless), x, y)::Bool | ||
if null_safe_op(isless, typeof(x), typeof(y)) | ||
return ifelse( isnull(x), | ||
false, # x null | ||
ifelse( isnull(y), | ||
true, # x not null, y null | ||
isless(unsafe_get(x), unsafe_get(y)) # x, y not null | ||
) | ||
) | ||
else | ||
return isnull(x) ? false : | ||
isnull(y) ? true : isless(unsafe_get(x), unsafe_get(y)) | ||
end | ||
end | ||
|
||
@inline lift(::typeof(isnull), x) = isnull(x) | ||
@inline lift(::typeof(get), x::Nullable) = get(x) | ||
@inline lift(::typeof(get), x::Nullable, y) = get(x, y) | ||
|
Oops, something went wrong.