diff --git a/src/Lookups/lookup_arrays.jl b/src/Lookups/lookup_arrays.jl index 9f69dc1cb..6df8a4470 100644 --- a/src/Lookups/lookup_arrays.jl +++ b/src/Lookups/lookup_arrays.jl @@ -852,21 +852,23 @@ promote_first(x1, x2, xs...) = # Fallback NoLookup if not identical type promote_first(l1::Lookup) = l1 promote_first(l1::L, ls::L...) where L<:Lookup = rebuild(l1; metadata=NoMetadata) -function promote_first(l1::L, ls::Lookup...) where {L<:Lookup} - ls = _remove(Length1NoLookup, l1, ls...) - if length(ls) > 1 - l1, ls... = ls - else +function promote_first(l1::Lookup, ls1::Lookup...) + ls = _remove(Length1NoLookup, l1, ls1...) + if length(ls) != length(ls1) + 1 + # If anything was removed, start again + return promote_first(ls...) + elseif length(ls) == 1 + # If there is only one left, use it return first(ls) end - if all(map(l -> typeof(l) == L, ls)) - if length(ls) > 0 - rebuild(l1; metadata=NoMetadata()) - else - l1 # Keep metadata if there is only one lookup - end + # Otherwise see if these have the same type + l2, ls2... = ls + if all(map(l -> typeof(l) == typeof(l2), ls2)) + # If so, just simplify the metadata + rebuild(l2; metadata=NoMetadata()) else - NoLookup(Base.OneTo(length(l1))) + # And if not, use NoLookup + NoLookup(Base.OneTo(length(l2))) end end # Categorical lookups diff --git a/src/array/broadcast.jl b/src/array/broadcast.jl index 20bbd44ae..d3592c94f 100644 --- a/src/array/broadcast.jl +++ b/src/array/broadcast.jl @@ -174,7 +174,9 @@ macro d(expr::Expr, options::Union{Expr,Nothing}=nothing) dims = $DimensionalData.dims(found_dims, order_dims) end else - :(dims = _find_dims(vars)) + quote + dims = _find_dims(vars) + end end quote let @@ -404,7 +406,6 @@ _broadcasted_dims(a) = () # its dimensions to match the rest of the @d broadcast, otherwise do nothing. _maybe_dimensional_broadcast(x, _, _) = x function _maybe_dimensional_broadcast(A::AbstractBasicDimArray, dest_dims, options) - len1s = basedims(otherdims(dest_dims, dims(A))) # Reshape first to avoid a ReshapedArray wrapper if possible A1 = _maybe_insert_length_one_dims(A, dest_dims) # Then permute and reorder diff --git a/test/broadcast.jl b/test/broadcast.jl index ba0502f56..f76080a29 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -371,6 +371,21 @@ end p(da1, da2, permutedims(da3, (X, Y, Z))) end + @testset "Lookups are maintained" begin + x = format(X(-5.0:5.0)) + y = format(Y(-10.0:2:12.0)) + z = format(Z(-3.0:0.5:4.0)) + + u = @d x .* y + v = @d x .* z + w = @d y .* z + + f(u, v, w) = u + v + w + + A = @d f.(u, v, w) + @test dims(A) == (x, y, z) + end + @testset "strict" begin @test_nowarn @d rand(X(1:3)) .* rand(X([:a, :b, :c])) strict=false @test_throws DimensionMismatch @d rand(X(1:3)) .* rand(X([:a, :b, :c])) strict=true