diff --git a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java index 79e9d4b4ab..a4972d9ced 100644 --- a/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java +++ b/fe/fe-common/src/main/java/org/apache/doris/catalog/ScalarType.java @@ -1060,7 +1060,13 @@ public class ScalarType extends Type { if (t1.type == PrimitiveType.STRING || t2.type == PrimitiveType.STRING) { return createStringType(); } - return createVarcharType(Math.max(t1.len, t2.len)); + int minLength = Math.min(t1.len, t2.len); + if (minLength < 0) { + // If < 0 which means max length, use firstly + return createVarcharType(minLength); + } + int length = Math.max(t1.len, t2.len); + return createVarcharType(length == 0 ? MAX_VARCHAR_LENGTH : length); } if (((t1.isDecimalV3() || t1.isDecimalV2()) && (t2.isDateV2() || t2.isDate())) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index a1118525b8..9a8fe4bba2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -992,22 +992,22 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { return ParserUtils.withOrigin(ctx, () -> { SelectClauseContext selectCtx = ctx.selectClause(); LogicalPlan selectPlan; + LogicalPlan relation; if (ctx.fromClause() == null) { SelectColumnClauseContext columnCtx = selectCtx.selectColumnClause(); if (columnCtx.EXCEPT() != null) { throw new ParseException("select-except cannot be used in one row relation", selectCtx); } - selectPlan = withOneRowRelation(columnCtx); + relation = withOneRowRelation(columnCtx); } else { - LogicalPlan relation = visitFromClause(ctx.fromClause()); - selectPlan = withSelectQuerySpecification( - ctx, relation, - selectCtx, - Optional.ofNullable(ctx.whereClause()), - Optional.ofNullable(ctx.aggClause()), - Optional.ofNullable(ctx.havingClause()) - ); + relation = visitFromClause(ctx.fromClause()); } + selectPlan = withSelectQuerySpecification( + ctx, relation, + selectCtx, + Optional.ofNullable(ctx.whereClause()), + Optional.ofNullable(ctx.aggClause()), + Optional.ofNullable(ctx.havingClause())); selectPlan = withQueryOrganization(selectPlan, ctx.queryOrganization()); return withSelectHint(selectPlan, selectCtx.selectHint()); }); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java index 9f267cd471..dd967984c3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java @@ -446,6 +446,12 @@ public class BindExpression implements AnalysisRuleFactory { LogicalCTEAnchor cteAnchor = sort.child(); return bindSort(sort, cteAnchor, ctx.cascadesContext); }) + ), RuleType.BINDING_SORT_SLOT.build( + logicalSort(logicalOneRowRelation()).thenApply(ctx -> { + LogicalSort sort = ctx.root; + LogicalOneRowRelation oneRowRelation = sort.child(); + return bindSort(sort, oneRowRelation, ctx.cascadesContext); + }) ), RuleType.BINDING_SORT_SET_OPERATION_SLOT.build( logicalSort(logicalSetOperation()).thenApply(ctx -> { diff --git a/regression-test/data/nereids_p0/select_no_from/sql/withGroupOrderHaving.out b/regression-test/data/nereids_p0/select_no_from/sql/withGroupOrderHaving.out new file mode 100644 index 0000000000..636cae7e3f --- /dev/null +++ b/regression-test/data/nereids_p0/select_no_from/sql/withGroupOrderHaving.out @@ -0,0 +1,10 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withGroupOrderHaving -- +0 + +-- !withGroupOrderHaving_2 -- +10 3 + +-- !withGroupOrderHaving_3 -- +1 1 2 1.0 1 + diff --git a/regression-test/data/nereids_p0/select_no_from/sql/withWhereClause.out b/regression-test/data/nereids_p0/select_no_from/sql/withWhereClause.out new file mode 100644 index 0000000000..0b6d028a24 --- /dev/null +++ b/regression-test/data/nereids_p0/select_no_from/sql/withWhereClause.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withWhereClause -- +0 + +-- !withWhereClause_2 -- +1 + diff --git a/regression-test/data/nereids_p0/select_no_from/sql/withWhereFalse.out b/regression-test/data/nereids_p0/select_no_from/sql/withWhereFalse.out new file mode 100644 index 0000000000..bd030d90fa --- /dev/null +++ b/regression-test/data/nereids_p0/select_no_from/sql/withWhereFalse.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withWhereFalse -- +0 + +-- !withWhereFalse_2 -- +0 1 + diff --git a/regression-test/data/query_p0/select_no_from/sql/withGroupOrderHaving.out b/regression-test/data/query_p0/select_no_from/sql/withGroupOrderHaving.out new file mode 100644 index 0000000000..636cae7e3f --- /dev/null +++ b/regression-test/data/query_p0/select_no_from/sql/withGroupOrderHaving.out @@ -0,0 +1,10 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withGroupOrderHaving -- +0 + +-- !withGroupOrderHaving_2 -- +10 3 + +-- !withGroupOrderHaving_3 -- +1 1 2 1.0 1 + diff --git a/regression-test/data/query_p0/select_no_from/sql/withWhereClause.out b/regression-test/data/query_p0/select_no_from/sql/withWhereClause.out new file mode 100644 index 0000000000..0b6d028a24 --- /dev/null +++ b/regression-test/data/query_p0/select_no_from/sql/withWhereClause.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withWhereClause -- +0 + +-- !withWhereClause_2 -- +1 + diff --git a/regression-test/data/query_p0/select_no_from/sql/withWhereFalse.out b/regression-test/data/query_p0/select_no_from/sql/withWhereFalse.out new file mode 100644 index 0000000000..bd030d90fa --- /dev/null +++ b/regression-test/data/query_p0/select_no_from/sql/withWhereFalse.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !withWhereFalse -- +0 + +-- !withWhereFalse_2 -- +0 1 + diff --git a/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_cast.out b/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_cast.out index cf64c4da4a..1fb288a763 100644 --- a/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_cast.out +++ b/regression-test/data/query_p0/sql_functions/conditional_functions/test_if_cast.out @@ -15,5 +15,5 @@ ["1970-01-01"] -- !select -- -[null] +[NULL] diff --git a/regression-test/suites/nereids_p0/select_no_from/sql/withGroupOrderHaving.sql b/regression-test/suites/nereids_p0/select_no_from/sql/withGroupOrderHaving.sql index 679d822040..868e42d59a 100644 --- a/regression-test/suites/nereids_p0/select_no_from/sql/withGroupOrderHaving.sql +++ b/regression-test/suites/nereids_p0/select_no_from/sql/withGroupOrderHaving.sql @@ -1,4 +1,8 @@ -/* -- database: presto; groups: no_from -SELECT MIN(10), 3 as col1 GROUP BY 2 HAVING 6 > 5 ORDER BY 1 -*/ +SET enable_nereids_planner = TRUE; +SELECT MIN(10), 3 as col1 GROUP BY 2 HAVING 6 > 5 ORDER BY 1; +SELECT 1 AS a, COUNT(*), SUM(2), AVG(1), RANK() OVER() AS w_rank +WHERE 1 = 1 +GROUP BY a, w_rank +HAVING COUNT(*) IN (1, 2) AND w_rank = 1 +ORDER BY a; \ No newline at end of file diff --git a/regression-test/suites/nereids_p0/select_no_from/sql/withWhereClause.sql b/regression-test/suites/nereids_p0/select_no_from/sql/withWhereClause.sql index 3798fa8d56..12b501a559 100644 --- a/regression-test/suites/nereids_p0/select_no_from/sql/withWhereClause.sql +++ b/regression-test/suites/nereids_p0/select_no_from/sql/withWhereClause.sql @@ -1,4 +1,3 @@ -/* -- database: presto; groups: no_from -SELECT 1 WHERE TRUE AND 2=2 -*/ +SET enable_nereids_planner = TRUE; +SELECT 1 WHERE TRUE AND 2=2; diff --git a/regression-test/suites/nereids_p0/select_no_from/sql/withWhereFalse.sql b/regression-test/suites/nereids_p0/select_no_from/sql/withWhereFalse.sql index c4096c2811..3ffe305885 100644 --- a/regression-test/suites/nereids_p0/select_no_from/sql/withWhereFalse.sql +++ b/regression-test/suites/nereids_p0/select_no_from/sql/withWhereFalse.sql @@ -1,4 +1,3 @@ -/* -- database: presto; groups: no_from -SELECT COUNT(*), 1 WHERE FALSE -*/ +SET enable_nereids_planner = TRUE; +SELECT COUNT(*), 1 WHERE FALSE; diff --git a/regression-test/suites/nereids_syntax_p0/bind_priority.groovy b/regression-test/suites/nereids_syntax_p0/bind_priority.groovy index 536dcddf06..072587ed28 100644 --- a/regression-test/suites/nereids_syntax_p0/bind_priority.groovy +++ b/regression-test/suites/nereids_syntax_p0/bind_priority.groovy @@ -116,7 +116,7 @@ suite("bind_priority") { test{ sql "SELECT a,2 as a FROM (SELECT '1' as a) b HAVING a=1" - exception "a is ambiguous: a#0, a#1." + exception "a is ambiguous: a#1, a#2." } sql "drop table if exists duplicate_slot"; diff --git a/regression-test/suites/query_p0/select_no_from/sql/withGroupOrderHaving.sql b/regression-test/suites/query_p0/select_no_from/sql/withGroupOrderHaving.sql index 679d822040..868e42d59a 100644 --- a/regression-test/suites/query_p0/select_no_from/sql/withGroupOrderHaving.sql +++ b/regression-test/suites/query_p0/select_no_from/sql/withGroupOrderHaving.sql @@ -1,4 +1,8 @@ -/* -- database: presto; groups: no_from -SELECT MIN(10), 3 as col1 GROUP BY 2 HAVING 6 > 5 ORDER BY 1 -*/ +SET enable_nereids_planner = TRUE; +SELECT MIN(10), 3 as col1 GROUP BY 2 HAVING 6 > 5 ORDER BY 1; +SELECT 1 AS a, COUNT(*), SUM(2), AVG(1), RANK() OVER() AS w_rank +WHERE 1 = 1 +GROUP BY a, w_rank +HAVING COUNT(*) IN (1, 2) AND w_rank = 1 +ORDER BY a; \ No newline at end of file diff --git a/regression-test/suites/query_p0/select_no_from/sql/withWhereClause.sql b/regression-test/suites/query_p0/select_no_from/sql/withWhereClause.sql index 3798fa8d56..2f413308df 100644 --- a/regression-test/suites/query_p0/select_no_from/sql/withWhereClause.sql +++ b/regression-test/suites/query_p0/select_no_from/sql/withWhereClause.sql @@ -1,4 +1,3 @@ -/* -- database: presto; groups: no_from -SELECT 1 WHERE TRUE AND 2=2 -*/ +SET enable_nereids_planner = TRUE; +SELECT 1 WHERE TRUE AND 2=2; \ No newline at end of file diff --git a/regression-test/suites/query_p0/select_no_from/sql/withWhereFalse.sql b/regression-test/suites/query_p0/select_no_from/sql/withWhereFalse.sql index c4096c2811..774532dc0d 100644 --- a/regression-test/suites/query_p0/select_no_from/sql/withWhereFalse.sql +++ b/regression-test/suites/query_p0/select_no_from/sql/withWhereFalse.sql @@ -1,4 +1,3 @@ -/* -- database: presto; groups: no_from -SELECT COUNT(*), 1 WHERE FALSE -*/ +SET enable_nereids_planner = TRUE; +SELECT COUNT(*), 1 WHERE FALSE; \ No newline at end of file