Skip to content

Commit b6c3a63

Browse files
authored
[fix](dereference) Resolve relation-qualified columns before output alias fields in ORDER BY / HAVING / QUALIFY (apache#68260)
Follow-up of apache#67438. ## The problem A multipart name is either a relation-qualified column (`t.col`) or a nested field reference (`col.field`). When a select output alias has the same name as a relation alias, a legal relation-qualified reference fails: ```sql SELECT q.v AS q FROM (SELECT 7 AS v) q ORDER BY q.v; -- No such field 'v' in 'q' SELECT q.v FROM (SELECT 7 AS v) q ORDER BY q.v; -- ok, 7 SELECT q.v AS q FROM (SELECT 7 AS v) q ORDER BY v; -- ok, 7 ``` ORDER BY, HAVING and QUALIFY do not bind against one scope. They bind against layered local scopes, for ORDER BY the select output first and then its child output. apache#67438 added a pass that tries the relation-qualified reading in every visible scope before any `col.field` reading, and threaded it through all these layered binders, but the pass only ran when a correlated outer scope existed. At the top level the nearer output alias `q` was taken as the first part of `col.field`, and because `q` is a scalar the lookup threw before the child output was ever tried. The same HAVING inside a subquery already worked, which is what pointed at the gate. It is worse when the alias is a struct, map or variant: nothing fails, `q.v` silently binds to `element_at(alias q, 'v')` and the query returns wrong results. Shapes that were broken at the top level (all fixed here): | Clause | Example | Before | |---|---|---| | ORDER BY | `select q.v as q from t q order by q.v` (also with an expression, `DISTINCT`, `LIMIT`, a join, inside an `IN` subquery, `db.tbl.col`) | No such field | | ORDER BY over aggregate | `select max(q.v) as q from t q group by q.id order by q.id` / `... order by max(q.v)` | No such field | | HAVING without aggregate | `select q.v as q from t q having q.v > 0` | No such field | | HAVING / QUALIFY after `GROUP BY <expr>` | `select q.id + 1 as q from t q group by q.id + 1 having q.id + 1 > 0` | No such field | | struct / map / variant alias | `select q.s as q from t q order by q.v` (`s` is `struct<v:int>`, `t` has a column `v`) | **wrong result**, sorted by `s.v` | While auditing the same family, one more bypass of the layered binders showed up, and it needs no alias at all: a **lambda body** inside these clauses resolved names against the default scope of the enclosing analyzer only, so it could not see the child output. ```sql SELECT id FROM t ORDER BY array_sum(array_map(x -> x + v, arr)); -- Unknown lambda slot 'v in lambda arguments[x] (the same expression works in WHERE) ``` Letting a lambda body follow its clause made one more failure reachable, and it turned out to exist on master independently of this PR: a nested loop join whose condition has a lambda that references **both sides** fails in the translator. ```sql SELECT q.id, p.id FROM t q, t p WHERE array_sum(array_map(x -> x + p.v + q.v, [0])) > 40; -- java.lang.NullPointerException: Cannot read field "type" because "e" is null ``` `ProjectOtherJoinConditionForNestedLoopJoin` moves a sub-expression of the join condition into a Project under the join when all of its input slots come from one side. A lambda argument is not an input slot (no child outputs it), so `x + p.v` was taken as a right-only expression and moved out of the lambda body into a Project where `x` does not exist. Before this PR a lambda in JOIN ON or in a correlated EXISTS failed during analysis, so only the WHERE-over-join shape could reach it; with the lambda fix those two shapes reach it too, so it is fixed here. ## The fix No new mechanism is added. 1. `visitUnboundSlot`: run the relation-qualified pass over the local scopes for **every** multipart name. Only the lookup in the outer scope stays conditional on a correlated subquery. For an analyzer with a single scope the result is identical to before, the pass is just the first stage of the normal lookup. 2. Lambda analyzer: bind the lambda arguments first as the nearest lexical scope, and delegate every other name to the enclosing analyzer, so a lambda body resolves names exactly like the clause around it. A nested lambda delegates to the lambda around it. The variant schema auto cast (`enable_variant_schema_auto_cast`) casts only the outermost `element_at` of a chain, and tracks the chain in a counter of the analyzer instance. The chain around a lambda body name is visited by the lambda analyzer, so the enclosing analyzer binds the delegated name under the counter of the lambda analyzer and restores its own afterwards (`visitUnboundSlotOfLambdaBody`). This keeps the casts of a lambda body exactly as they were before the delegation, in both directions: a chain inside the lambda body, and a chain around the higher order function. 3. `ProjectOtherJoinConditionForNestedLoopJoin`: do not descend into a lambda. A higher order function whose inputs all come from one side is still projected as a whole. ## Binding priority after this PR For every name: 1. Multipart name only: try the **relation-qualified** reading (`t.col`, `db.t.col`, `ctl.db.t.col`) through the local scope chain of the clause (table below). If nothing matches, the clause is in a correlated subquery, and no local relation has that name, try it in the outer scope. 2. Full reading through the same local scope chain. Inside one scope: `ctl.db.t.col` → `db.t.col` → `t.col` → `col.field`. 3. Full reading in the outer scope (one level up), if the clause may bind it. 4. 0 matches → error, 1 → bound, more → the exact qualifier match if the clause enables it, otherwise ambiguous. Local scope chain per clause (near → far). Only step 1 is new for the top level, the chains themselves are unchanged: | Clause | Local scope chain | Outer scope | |---|---|---| | SELECT list, WHERE, JOIN ON, aggregate output, window, LATERAL VIEW | child output | yes | | GROUP BY | child output (exactly one match) → aggregate output alias, replaced by its expression | yes | | HAVING without aggregate | select output → its child output | yes | | HAVING over aggregate, outside an aggregate function | group by slots → aggregate output → aggregate child | yes | | HAVING over aggregate, inside an aggregate function | aggregate child | yes | | QUALIFY over project | project child output → project output | yes | | QUALIFY over aggregate | group by slots → aggregate output → aggregate child | yes | | ORDER BY | select output → its child output (skips QUALIFY, HAVING, DISTINCT project over aggregate); an integer literal is an ordinal | no | | ORDER BY, key contains an aggregate function | aggregate output without aggregate functions → child output | no | | ORDER BY over a set operation | set operation output | yes | | Lambda body | lambda arguments → **the enclosing clause, by the rules above** (new) | as the clause | What changes in priority: for a multipart name, a relation-qualified column in **any** local layer now wins over a `col.field` reading in a nearer layer. A single-part name is untouched, `ORDER BY q` still prefers the alias. A lambda argument keeps lexical priority over a relation of the same name (`array_map(x -> x.value, x.items) from t x`). ## Behavior changes - `q.v` prefers column `v` of relation `q` over field `v` of a same-named struct / map / variant output alias in HAVING, QUALIFY and ORDER BY. A query that relied on the alias reading while a same-named relation has a matching column now binds to the column. (GROUP BY looks at the child output first, its result does not change.) - The relation reading wins even when it then fails: with a relation `q` that has a scalar column `v` and a struct output alias `q` that has the path `v.b`, `ORDER BY q.v.b` used to read the alias and now reports `No such field 'b' in 'v'`. MySQL agrees on the priority: with an output alias `s` and a table `s`, it reads `s.a` as column `a` of the table. - Lambda bodies: - an unresolved name reports the error of the enclosing clause (`Unknown column ... in SORT clause`) instead of `Unknown lambda slot`; - a lambda body in a correlated subquery can reference an outer column, which is registered as a correlated slot; - a lambda body in a join condition no longer reports `Unsupported correlated subquery with correlated slot in join conjuncts` for a column of the join itself; - an ambiguous name follows the exact-match setting of the enclosing clause. The lambda analyzer used to pick the exact match on its own, so `select q.id as id, p.id from ... having array_sum(array_map(x -> x + id, q.arr)) > 0` used to bind `id` to the alias and now reports `id is ambiguous`, exactly as `having id > 0` does outside a lambda; - analyzers that override name resolution (generated columns, alias functions) now apply it inside lambda bodies too. ## Test matrix `U` = FE unit test (`TestDereference` unless another class is named; analysis only), `R` = regression case in `query_p0/test_dereference` (executed, result in `.out`). | Scenario | U | R | |---|---|---| | Reported repro: `select q.v as q from (select 7 as v) q order by q.v` | ✓ | ✓ | | ORDER BY on a table, scalar alias | ✓ | ✓ | | ORDER BY with DISTINCT / join / inside an `IN` subquery | ✓ | | | ORDER BY `db.tbl.col`, alias named like the database | ✓ | | | ORDER BY with an aggregate function in the key | ✓ | ✓ | | ORDER BY over aggregate, key is a group by column | ✓ | ✓ | | ORDER BY over aggregate, key is the group by expression | ✓ | | | HAVING without aggregate | ✓ | ✓ | | HAVING over aggregate, group by column / group by expression | ✓ | ✓ (expression) | | QUALIFY over project / over aggregate with a group by expression | ✓ | ✓ (aggregate) | | struct alias, ORDER BY — result must follow column `v`, not `s.v` | ✓ (no `element_at`) | ✓ (returns id 3, the wrong binding returns 1) | | struct alias, HAVING | ✓ | ✓ (returns id 1, the wrong binding returns nothing) | | struct alias, QUALIFY | ✓ | | | Fallback kept: alias only, `select q.s as a ... order by a.v` | ✓ (`element_at`) | | | Fallback kept: alias named like a relation that has no such column | ✓ | ✓ | | Negative: name is only a scalar output alias → still `No such field 'v' in 'q'` | ✓ | ✓ | | Negative: `q.v.b`, relation column `v` is scalar, alias `q` has the path `v.b` → `No such field 'b' in 'v'` | ✓ | ✓ | | Lambda in ORDER BY, column not in the select list, unqualified / qualified | ✓ | ✓ | | Lambda in ORDER BY / HAVING with the alias shadow | ✓ | ✓ | | Lambda inside an aggregate function in HAVING, lambda in QUALIFY | ✓ | | | Nested lambda referencing a column of the clause | ✓ | | | Lambda in JOIN ON referencing both sides | ✓ | ✓ (NPE before the rule fix) | | Lambda in a correlated EXISTS referencing the outer and the inner relation | ✓ (correlated slot registered) | ✓ (NPE before the rule fix) | | Lambda in a correlated IN referencing an outer column | | ✓ | | Nested loop join rule: a lambda body is not projected, a one-sided higher order function still is | ✓ (`ProjectOtherJoinConditionForNestedLoopJoinTest`) | | | Negative: ambiguous name in HAVING, outside and inside a lambda → `id is ambiguous` | ✓ | | | Variant auto cast: `array_map(x -> data.num_nested['l1']['l2'], arr)`, the dotted prefix in a chain inside the lambda body is not cast, same as outside a lambda | ✓ (`ExpressionAnalyzerVariantAutoCastTest`) | | | Variant auto cast: `array_map(x -> array(data.num_a), arr)[1][1]`, a chain around the higher order function does not suppress the cast in the lambda body | ✓ (`ExpressionAnalyzerVariantAutoCastTest`) | | | Lambda argument keeps priority over a same-named relation (existing test) | ✓ | ✓ | | Negative: unknown name in a lambda body → `Unknown column` | ✓ | ✓ | How it was run: - The unit tests of the broken shapes fail without the fix (`No such field 'v' in 'q'`, an `element_at` binding for the struct alias, a projected lambda body for the join rule) and pass with it. A few cases in the same tests pass either way and only pin the behavior down (HAVING on a group by column, QUALIFY over a project, the negatives). - FE unit tests under `nereids/rules/analysis`, the lambda tests and `nereids/rules/rewrite/*Join*` were run locally before the variant cast commit: 68 of 70 classes pass. `PushDownLimitDistinctThroughJoinTest` and `PushDownTopNThroughJoinTest` fail locally in `runBeforeAll` while creating their tables (`available backend num is 0`), before any test method runs; they passed in CI on an earlier head. After that commit `TestDereference` (18), `ExpressionAnalyzerVariantAutoCastTest` (18) and `ProjectOtherJoinConditionForNestedLoopJoinTest` (3) pass, and the two variant cast tests fail without the fix. - The new regression cases were executed, and their expected output generated with `run-regression-test.sh -genOut`, as a standalone copy of the new section against a local cluster of this FE with a **4.1.3 BE** (no master BE was available). The generated blocks were appended to `test_dereference.out`. The `test { sql; exception }` cases were executed on the same cluster and report the expected messages. The whole `test_dereference` suite cannot run end to end locally, because the existing `parse_to_variant` cases need a master BE; it passed in CI (P0) on an earlier head of this PR, before the negative and the join / correlated lambda cases were added. Not covered, left for a follow-up: - A lambda in a correlated **scalar** subquery with a non-equal correlated predicate reports `Unsupported correlated subquery with correlated predicate`, which is the existing limitation for such predicates and not specific to lambdas. - A regression case for the variant auto cast inside a lambda body, and a unit test for passing the cast counter through a nested lambda (traced in the code only). - A regression case for a variant-typed alias. It takes the same code path as the struct alias. - Lambdas in generated columns and alias functions. ## Known and deliberately not changed An output alias that shadows a same-named **column** (not a relation) for nested access still prefers the alias in ORDER BY and HAVING: ```sql select id, var.name as var from t order by cast(var.name as int), id; -- `var.name` reads field `name` of the alias `var`, i.e. var.name.name, which is NULL: the sort is a no-op ``` This is the "column vs alias" priority, a separate rule from the "relation qualifier vs alias" one fixed here (QUALIFY and GROUP BY already prefer the underlying column, HAVING and ORDER BY prefer the alias). Changing it is a semantic decision with its own corner cases under aggregation, so it is left for a separate PR.
1 parent d657d62 commit b6c3a63

7 files changed

Lines changed: 493 additions & 17 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,20 @@ public Expression visitUnboundSlot(UnboundSlot unboundSlot, ExpressionRewriteCon
326326
boolean relationQualifierOccupied = false;
327327

328328
// A multipart name can be either a relation-qualified column (t.col) or a nested field
329-
// reference (col.field). In a correlated subquery, try the relation-qualified interpretation
330-
// in both visible scopes first, so an inner column named "t" does not hide an outer alias "t".
331-
if (canBindOuterScope && shouldPrioritizeRelationQualifier()
332-
&& unboundSlot.getNameParts().size() > 1) {
329+
// reference (col.field). Try the relation-qualified interpretation in every visible scope
330+
// first, so a nearer name "t" does not hide a farther relation alias "t". The visible scopes
331+
// are the local ones, which HAVING, QUALIFY and ORDER BY layer from the select output and its
332+
// child output in a clause specific order, and then the outer scope of a correlated subquery:
333+
// select q.v as q from t q order by q.v -- q.v is the column of relation q, not alias q
334+
if (shouldPrioritizeRelationQualifier() && unboundSlot.getNameParts().size() > 1) {
333335
SlotBinding localRelationBinding = bindSlotByRelationQualifierInThisScope(unboundSlot);
334336
bounded = localRelationBinding.getBoundSlots();
335337
foundInThisScope = !bounded.isEmpty();
336-
if (!foundInThisScope) {
338+
if (!foundInThisScope && canBindOuterScope) {
337339
relationQualifierOccupied = localRelationBinding.isRelationQualifierOccupied();
338-
}
339-
if (!foundInThisScope && !relationQualifierOccupied) {
340-
bounded = bindSlotsByRelationQualifier(unboundSlot, outerScope.get());
340+
if (!relationQualifierOccupied) {
341+
bounded = bindSlotsByRelationQualifier(unboundSlot, outerScope.get());
342+
}
341343
}
342344
}
343345

@@ -474,34 +476,57 @@ private UnboundFunction processHighOrderFunction(UnboundFunction unboundFunction
474476
Lambda lambda = (Lambda) unboundFunction.children().get(0);
475477
Expression lambdaFunction = lambda.getLambdaFunction();
476478
LambdaBinding binding = bindingSpec.bind(unboundFunction.getName(), lambda, subChildren);
477-
lambdaFunction = analyzeLambdaFunction(
478-
lambda, lambdaFunction, binding.getAnalysisSlots(), context);
479+
lambdaFunction = analyzeLambdaFunction(lambdaFunction, binding.getAnalysisSlots(), context);
479480
Lambda lambdaClosure = binding.close(lambdaFunction);
480481

481482
// We don't add the ArrayExpression in high order function at all
482483
return unboundFunction.withChildren(ImmutableList.of(lambdaClosure));
483484
}
484485

485-
private Expression analyzeLambdaFunction(Lambda lambda, Expression lambdaFunction,
486+
private Expression analyzeLambdaFunction(Expression lambdaFunction,
486487
List<Slot> boundedSlots, ExpressionRewriteContext context) {
488+
ExpressionAnalyzer enclosingAnalyzer = this;
487489
ExpressionAnalyzer lambdaAnalyzer = new ExpressionAnalyzer(currentPlan, new Scope(Optional.of(getScope()),
488490
boundedSlots), context == null ? null : context.cascadesContext,
489491
true, true) {
490492
@Override
491-
protected boolean shouldPrioritizeRelationQualifier() {
492-
return false;
493+
public Expression visitUnboundSlot(UnboundSlot unboundSlot, ExpressionRewriteContext context) {
494+
// The lambda arguments are the nearest lexical scope. Every other name is resolved by the
495+
// enclosing analyzer rather than by its default scope, because ORDER BY, HAVING and QUALIFY
496+
// layer several local scopes and a correlated subquery sees its outer scope:
497+
// select id from t order by array_sum(array_map(x -> x + v, arr)) -- v is not in the output
498+
if (bindSlotByThisScope(unboundSlot).isEmpty()) {
499+
return enclosingAnalyzer.visitUnboundSlotOfLambdaBody(unboundSlot, context, this);
500+
}
501+
return super.visitUnboundSlot(unboundSlot, context);
493502
}
494503

495504
@Override
496-
protected void couldNotFoundColumn(UnboundSlot unboundSlot, String tableName) {
497-
throw new AnalysisException("Unknown lambda slot '"
498-
+ unboundSlot.getNameParts().get(unboundSlot.getNameParts().size() - 1)
499-
+ " in lambda arguments" + lambda.getLambdaArgumentNames());
505+
protected boolean shouldPrioritizeRelationQualifier() {
506+
// a name that starts with a lambda argument is a field of it, even if a relation of the
507+
// enclosing scope has the same name: array_map(x -> x.value, x.items) from t x
508+
return false;
500509
}
501510
};
502511
return lambdaAnalyzer.analyze(lambdaFunction, context);
503512
}
504513

514+
/**
515+
* Bind a name of a lambda body that is not a lambda argument. The element_at chain around the name is
516+
* visited by the lambda analyzer, so whether the variant cast of the name is suppressed is the state of
517+
* the lambda analyzer, not the state of this analyzer, which may be in a chain around the lambda.
518+
*/
519+
private Expression visitUnboundSlotOfLambdaBody(UnboundSlot unboundSlot, ExpressionRewriteContext context,
520+
ExpressionAnalyzer lambdaAnalyzer) {
521+
int enclosingDepth = suppressVariantElementAtCastDepth;
522+
suppressVariantElementAtCastDepth = lambdaAnalyzer.suppressVariantElementAtCastDepth;
523+
try {
524+
return visitUnboundSlot(unboundSlot, context);
525+
} finally {
526+
suppressVariantElementAtCastDepth = enclosingDepth;
527+
}
528+
}
529+
505530
/** Whether relation-qualified columns should be resolved across scopes before nested fields. */
506531
protected boolean shouldPrioritizeRelationQualifier() {
507532
return true;

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ProjectOtherJoinConditionForNestedLoopJoin.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.doris.nereids.trees.expressions.Expression;
2424
import org.apache.doris.nereids.trees.expressions.NamedExpression;
2525
import org.apache.doris.nereids.trees.expressions.Slot;
26+
import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda;
2627
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
2728
import org.apache.doris.nereids.trees.plans.Plan;
2829
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
@@ -103,6 +104,15 @@ public ReplacerContext(Set<Slot> leftSlots, Set<Slot> rightSlots) {
103104
private static class AliasReplacer extends DefaultExpressionRewriter<ReplacerContext> {
104105
public static AliasReplacer INSTANCE = new AliasReplacer();
105106

107+
@Override
108+
public Expression visitLambda(Lambda lambda, ReplacerContext ctx) {
109+
// A lambda body is evaluated per array item. An expression in it may reference the lambda
110+
// arguments, which are not input slots and which no child of the join outputs, so it can
111+
// not be evaluated in a child Project:
112+
// array_map(x -> x + t2.b, [0]) > t1.a -- `x + t2.b` must stay inside the lambda
113+
return lambda;
114+
}
115+
106116
@Override
107117
public Expression visit(Expression expression, ReplacerContext ctx) {
108118
Set<Slot> input = expression.getInputSlots();

‎fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzerVariantAutoCastTest.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.doris.nereids.CascadesContext;
2121
import org.apache.doris.nereids.analyzer.Scope;
22+
import org.apache.doris.nereids.analyzer.UnboundFunction;
2223
import org.apache.doris.nereids.analyzer.UnboundSlot;
2324
import org.apache.doris.nereids.trees.expressions.Alias;
2425
import org.apache.doris.nereids.trees.expressions.Between;
@@ -34,9 +35,11 @@
3435
import org.apache.doris.nereids.trees.expressions.functions.agg.Min;
3536
import org.apache.doris.nereids.trees.expressions.functions.agg.Sum;
3637
import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt;
38+
import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda;
3739
import org.apache.doris.nereids.trees.expressions.functions.scalar.TryParseToVariant;
3840
import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
3941
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
42+
import org.apache.doris.nereids.types.ArrayType;
4043
import org.apache.doris.nereids.types.BigIntType;
4144
import org.apache.doris.nereids.types.IntegerType;
4245
import org.apache.doris.nereids.types.StringType;
@@ -49,6 +52,8 @@
4952
import org.junit.jupiter.api.Assertions;
5053
import org.junit.jupiter.api.Test;
5154

55+
import java.util.List;
56+
5257
public class ExpressionAnalyzerVariantAutoCastTest {
5358

5459
@AfterEach
@@ -320,6 +325,52 @@ public void testAggregateMinMaxAvgCountDistinct() {
320325
assertCastElementAt(((Count) countResult).child(0));
321326
}
322327

328+
private SlotReference buildArraySlot() {
329+
return new SlotReference(new ExprId(2), "arr", ArrayType.of(BigIntType.INSTANCE), true, ImmutableList.of());
330+
}
331+
332+
private List<Cast> collectCastElementAt(Expression expr) {
333+
return expr.collectToList(node -> node instanceof Cast && ((Cast) node).child() instanceof ElementAt);
334+
}
335+
336+
@Test
337+
public void testLambdaBodyElementAtChainSuppressesCastOfDottedPrefix() {
338+
// array_map(x -> data.num_nested['l1']['l2'], arr)
339+
// data is not a lambda argument, so the enclosing analyzer binds data.num_nested, but the element_at
340+
// chain around it is in the lambda body: data.num_nested is a prefix of the path and is not cast to
341+
// the type of 'num_*', same as outside a lambda
342+
SlotReference data = buildVariantSlot(buildVariantType());
343+
Scope scope = new Scope(ImmutableList.of(data, buildArraySlot()));
344+
345+
Expression chain = new ElementAt(
346+
new ElementAt(new UnboundSlot("data", "num_nested"), new StringLiteral("l1")),
347+
new StringLiteral("l2"));
348+
Expression outsideLambda = analyze(chain, scope, true);
349+
Assertions.assertTrue(collectCastElementAt(outsideLambda).isEmpty(), outsideLambda.toSql());
350+
351+
Lambda lambda = new Lambda(ImmutableList.of("x"), chain);
352+
Expression result = analyze(
353+
new UnboundFunction("array_map", ImmutableList.of(lambda, new UnboundSlot("arr"))), scope, true);
354+
Assertions.assertTrue(collectCastElementAt(result).isEmpty(), result.toSql());
355+
}
356+
357+
@Test
358+
public void testLambdaBodyIsNotSuppressedByElementAtChainAroundLambda() {
359+
// array_map(x -> array(data.num_a), arr)[1][1]
360+
// the element_at chain is around the higher order function, data.num_a in the lambda body is not
361+
// a part of it and is still cast
362+
SlotReference data = buildVariantSlot(buildVariantType());
363+
Scope scope = new Scope(ImmutableList.of(data, buildArraySlot()));
364+
365+
Lambda lambda = new Lambda(ImmutableList.of("x"),
366+
new UnboundFunction("array", ImmutableList.of(new UnboundSlot("data", "num_a"))));
367+
Expression arrayMap = new UnboundFunction("array_map", ImmutableList.of(lambda, new UnboundSlot("arr")));
368+
Expression result = analyze(
369+
new ElementAt(new ElementAt(arrayMap, new BigIntLiteral(1)), new BigIntLiteral(1)), scope, true);
370+
371+
Assertions.assertEquals(1, collectCastElementAt(result).size(), result.toSql());
372+
}
373+
323374
@Test
324375
public void testAutoCastDisabled() {
325376
VariantType variantType = buildVariantType();

0 commit comments

Comments
 (0)