Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) let anonymous alias same as user input #47093

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -240,7 +240,7 @@ public void testHavingAggregateFunction() {
).when(FieldChecker.check("projects", Lists.newArrayList(a1.toSlot(), sumA2.toSlot()))));

sql = "SELECT a1, sum(a1 + a2) FROM t1 GROUP BY a1 HAVING sum(a1 + a2) > 0";
Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), "sum((a1 + a2))");
Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), "sum(a1 + a2)");
PlanChecker.from(connectContext).analyze(sql)
.matches(
logicalProject(
Expand Down Expand Up @@ -363,7 +363,7 @@ void testComplexQueryWithHaving() {
Alias sumA1 = new Alias(new ExprId(9), new Sum(a1), "sum(a1)");
Alias countA1 = new Alias(new ExprId(13), new Count(a1), "count(a1)");
Alias countA11 = new Alias(new ExprId(10), new Add(countA1.toSlot(), Literal.of((byte) 1)), "(count(a1) + 1)");
Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), "sum((a1 + a2))");
Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), "sum(a1 + a2)");
Alias v1 = new Alias(new ExprId(12), new Count(a2), "v1");
PlanChecker.from(connectContext).analyze(sql)
.matches(
Expand Down Expand Up @@ -476,7 +476,7 @@ public void testSortAggregateFunction() {
).when(FieldChecker.check("projects", Lists.newArrayList(a1.toSlot(), sumA2.toSlot()))));

sql = "SELECT a1, sum(a1 + a2) FROM t1 GROUP BY a1 ORDER BY sum(a1 + a2)";
Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), "sum((a1 + a2))");
Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), "sum(a1 + a2)");
PlanChecker.from(connectContext).analyze(sql)
.matches(
logicalSort(
Expand Down Expand Up @@ -562,7 +562,7 @@ void testComplexQueryWithOrderBy() {
Alias sumA1 = new Alias(new ExprId(9), new Sum(a1), "sum(a1)");
Alias countA1 = new Alias(new ExprId(13), new Count(a1), "count(a1)");
Alias countA11 = new Alias(new ExprId(10), new Add(new Count(a1), Literal.of((byte) 1)), "(count(a1) + 1)");
Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), "sum((a1 + a2))");
Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), "sum(a1 + a2)");
Alias v1 = new Alias(new ExprId(12), new Count(a2), "v1");
PlanChecker.from(connectContext).analyze(sql)
.matches(logicalProject(logicalSort(logicalProject(logicalAggregate(logicalProject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PhysicalResultSink

-- !groupby_pushdown_having --
PhysicalResultSink
--filter((count(score) > 100))
--filter((count(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down Expand Up @@ -546,7 +546,7 @@ SyntaxError:

-- !with_hint_groupby_pushdown_having --
PhysicalResultSink
--filter((count(score) > 100))
--filter((count(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PhysicalResultSink

-- !groupby_pushdown_having --
PhysicalResultSink
--filter((count(score) > 100))
--filter((count(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down Expand Up @@ -572,7 +572,7 @@ SyntaxError:

-- !with_hint_groupby_pushdown_having --
PhysicalResultSink
--filter((count(score) > 100))
--filter((count(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PhysicalResultSink

-- !groupby_pushdown_having --
PhysicalResultSink
--filter((min(score) > 100))
--filter((min(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down Expand Up @@ -376,7 +376,7 @@ SyntaxError:

-- !with_hint_groupby_pushdown_having --
PhysicalResultSink
--filter((min(score) > 100))
--filter((min(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PhysicalResultSink

-- !groupby_pushdown_having --
PhysicalResultSink
--filter((sum(score) > 100))
--filter((sum(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down Expand Up @@ -366,7 +366,7 @@ SyntaxError:

-- !with_hint_groupby_pushdown_having --
PhysicalResultSink
--filter((sum(score) > 100))
--filter((sum(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ PhysicalResultSink

-- !groupby_pushdown_having --
PhysicalResultSink
--filter((sum(score) > 100))
--filter((sum(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down Expand Up @@ -392,7 +392,7 @@ SyntaxError:

-- !with_hint_groupby_pushdown_having --
PhysicalResultSink
--filter((sum(score) > 100))
--filter((sum(t1.score) > 100))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------hashJoin[INNER_JOIN] hashCondition=((t1.id = t2.id)) otherCondition=()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ PhysicalResultSink

-- !filter_aggregation_filtered_agg_func --
PhysicalResultSink
--filter((count(*) > 10))
--filter((count() > 10))
----hashAgg[GLOBAL]
------hashAgg[LOCAL]
--------PhysicalOlapScan[t1]
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ suite("test_duplicate_name_in_view") {
issue_19611_t1.c0 + 1
FROM issue_19611_t1 ) tmp;
"""
exception "Duplicated inline view column alias: '(c0 + 1)' in inline view: 'tmp'"
exception "Duplicated inline view column alias: 'issue_19611_t1.c0 + 1' in inline view: 'tmp'"
}

test {
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"
}
Loading