Skip to content

Commit

Permalink
[fix](Nereids) let anonymous alias same as user input
Browse files Browse the repository at this point in the history
  • Loading branch information
morrySnow committed Jan 16, 2025
1 parent 93f8da1 commit 62ba654
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,29 @@
*/
public class UnboundAlias extends NamedExpression implements UnaryExpression, Unbound, PropagateNullable {

private Optional<String> alias;
private final Optional<String> alias;
private final boolean nameFromChild;

public UnboundAlias(Expression child) {
super(ImmutableList.of(child));
this.alias = Optional.empty();
this(ImmutableList.of(child), Optional.empty());
}

public UnboundAlias(Expression child, String alias) {
super(ImmutableList.of(child));
this.alias = Optional.of(alias);
this(ImmutableList.of(child), Optional.of(alias));
}

public UnboundAlias(Expression child, String alias, boolean nameFromChild) {
this(ImmutableList.of(child), Optional.of(alias), nameFromChild);
}

private UnboundAlias(List<Expression> children, Optional<String> alias) {
this(children, alias, false);
}

private UnboundAlias(List<Expression> children, Optional<String> alias, boolean nameFromChild) {
super(children);
this.alias = alias;
this.nameFromChild = nameFromChild;
}

@Override
Expand Down Expand Up @@ -89,4 +97,8 @@ public UnboundAlias withChildren(List<Expression> children) {
public Optional<String> getAlias() {
return alias;
}

public boolean isNameFromChild() {
return nameFromChild;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2130,10 +2130,16 @@ public NamedExpression visitNamedExpression(NamedExpressionContext ctx) {
if (ctx.identifierOrText() == null) {
if (expression instanceof NamedExpression) {
return (NamedExpression) expression;
} else if (expression instanceof Literal) {
return new Alias(expression);
} else {
return new UnboundAlias(expression);
int start = ctx.expression().start.getStartIndex();
int stop = ctx.expression().stop.getStopIndex();
String alias = ctx.start.getInputStream()
.getText(new org.antlr.v4.runtime.misc.Interval(start, stop));
if (expression instanceof Literal) {
return new Alias(expression, alias, true);
} else {
return new UnboundAlias(expression, alias, true);
}
}
}
String alias = visitIdentifierOrText(ctx.identifierOrText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static Variable resolveUnboundVariable(UnboundVariable unboundVariable) t
public Expression visitUnboundAlias(UnboundAlias unboundAlias, ExpressionRewriteContext context) {
Expression child = unboundAlias.child().accept(this, context);
if (unboundAlias.getAlias().isPresent()) {
return new Alias(child, unboundAlias.getAlias().get());
return new Alias(child, unboundAlias.getAlias().get(), unboundAlias.isNameFromChild());
// TODO: the variant bind element_at(slot, 'name') will return a slot, and we should
// assign an Alias to this function, this is trick and should refactor it
} else if (!(unboundAlias.child() instanceof ElementAt) && child instanceof NamedExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public class Alias extends NamedExpression implements UnaryExpression {
* @param name alias name
*/
public Alias(Expression child, String name) {
this(StatementScopeIdGenerator.newExprId(), child, name, false);
this(child, name, false);
}

public Alias(Expression child, String name, boolean nameFromChild) {
this(StatementScopeIdGenerator.newExprId(), child, name, nameFromChild);
}

public Alias(Expression child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ PhysicalResultSink
----------------PhysicalProject
------------------hashAgg[LOCAL]
--------------------PhysicalProject
----------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
----------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
----------------------------PhysicalProject
------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=()
--------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ PhysicalResultSink
----------------PhysicalProject
------------------hashAgg[LOCAL]
--------------------PhysicalProject
----------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
----------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
----------------------------PhysicalProject
------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
--------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=()
------------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
------------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
------------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
------------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
------------------------------------PhysicalProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ PhysicalResultSink
--------------------PhysicalDistribute[DistributionSpecHash]
----------------------hashAgg[LOCAL]
------------------------PhysicalProject
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= (d_month_seq + 3))
--------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) <= d_month_seq+3)
----------------------------PhysicalProject
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= (d_month_seq + 1))
------------------------------NestedLoopJoin[INNER_JOIN](cast(d_month_seq as BIGINT) >= d_month_seq+1)
--------------------------------PhysicalProject
----------------------------------hashJoin[INNER_JOIN broadcast] hashCondition=((store_sales.ss_sold_date_sk = date_dim.d_date_sk)) otherCondition=() build RFs:RF7 d_date_sk->[ss_sold_date_sk]
------------------------------------PhysicalProject
Expand Down
2 changes: 2 additions & 0 deletions regression-test/suites/nereids_syntax_p0/select_const.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ suite("select_with_const") {
"""

sql "select all 1"

sql "select `1 + 2` from (select 1 + 2) t"
}

0 comments on commit 62ba654

Please sign in to comment.