This is an automated email from the ASF dual-hosted git repository.

starocean999 pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new 68c8bce8300 [opt](Nereids) support where, group by, having, order by 
clause without from clause in query statement (#34475)
68c8bce8300 is described below

commit 68c8bce8300a4f0ed6c457fa235923fc9ca8d9d6
Author: starocean999 <40539150+starocean...@users.noreply.github.com>
AuthorDate: Thu May 9 10:18:42 2024 +0800

    [opt](Nereids) support where, group by, having, order by clause without 
from clause in query statement (#34475)
---
 .../java/org/apache/doris/catalog/ScalarType.java  |  8 +++++++-
 .../doris/nereids/parser/LogicalPlanBuilder.java   | 19 +++++++++--------
 .../nereids/rules/analysis/BindExpression.java     |  7 +++++++
 .../rules/analysis/FunctionRegistryTest.java       | 18 ++++++++--------
 .../doris/nereids/trees/expressions/UdfTest.java   | 24 +++++++++++-----------
 .../select_no_from/sql/withWhereFalse.out          |  3 +++
 .../select_no_from/sql/withWhereFalse.sql          |  2 ++
 .../suites/nereids_syntax_p0/bind_priority.groovy  |  2 +-
 .../nereids_syntax_p0/one_row_relation.groovy      |  7 -------
 9 files changed, 51 insertions(+), 39 deletions(-)

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 cd635649fd1..3db3739d08a 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
@@ -1095,7 +1095,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 873d7f28c19..289097ca2ef 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
@@ -585,22 +585,23 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
         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 = new 
UnboundOneRowRelation(StatementScopeIdGenerator.newRelationId(),
+                        ImmutableList.of(new UnboundAlias(Literal.of(0))));
             } 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 e0f0d1baa78..72af439f452 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
@@ -468,6 +468,13 @@ public class BindExpression implements AnalysisRuleFactory 
{
                     return bindSort(sort, cteAnchor, ctx.cascadesContext);
                 })
             ),
+            RuleType.BINDING_SORT_SLOT.build(
+                logicalSort(logicalOneRowRelation()).thenApply(ctx -> {
+                    LogicalSort<LogicalOneRowRelation> sort = ctx.root;
+                    LogicalOneRowRelation oneRowRelation = sort.child();
+                    return bindSort(sort, oneRowRelation, ctx.cascadesContext);
+                })
+            ),
             RuleType.BINDING_SORT_SET_OPERATION_SLOT.build(
                 logicalSort(logicalSetOperation()).thenApply(ctx -> {
                     LogicalSort<LogicalSetOperation> sort = ctx.root;
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FunctionRegistryTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FunctionRegistryTest.java
index 5f2c839d722..e797e4dd49b 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FunctionRegistryTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FunctionRegistryTest.java
@@ -55,8 +55,8 @@ public class FunctionRegistryTest implements 
MemoPatternMatchSupported {
         PlanChecker.from(connectContext)
                 .analyze("select year('2021-01-01')")
                 .matches(
-                        logicalOneRowRelation().when(r -> {
-                            Year year = (Year) r.getProjects().get(0).child(0);
+                        logicalProject().when(project -> {
+                            Year year = (Year) 
project.getProjects().get(0).child(0);
                             Assertions.assertEquals("2021-01-01",
                                     ((Literal) 
year.getArguments().get(0).child(0)).getValue());
                             return true;
@@ -72,14 +72,14 @@ public class FunctionRegistryTest implements 
MemoPatternMatchSupported {
         PlanChecker.from(connectContext)
                 .analyze("select substring('abc', 1, 2), 
substr(substring('abcdefg', 4, 3), 1, 2)")
                 .matches(
-                        logicalOneRowRelation().when(r -> {
-                            Substring firstSubstring = (Substring) 
r.getProjects().get(0).child(0);
+                        logicalProject().when(project -> {
+                            Substring firstSubstring = (Substring) 
project.getProjects().get(0).child(0);
                             Assertions.assertEquals("abc", ((Literal) 
firstSubstring.getSource()).getValue());
                             Assertions.assertEquals(1, ((Literal) 
firstSubstring.getPosition()).getValue());
                             Assertions.assertEquals(2, ((Literal) 
firstSubstring.getLength().get()).getValue());
 
-                            Substring secondSubstring = (Substring) 
r.getProjects().get(1).child(0);
-                            Assertions.assertTrue(secondSubstring.getSource() 
instanceof Substring);
+                            Substring secondSubstring = (Substring) 
project.getProjects().get(1).child(0);
+                            Assertions.assertInstanceOf(Substring.class, 
secondSubstring.getSource());
                             Assertions.assertEquals(1, ((Literal) 
secondSubstring.getPosition()).getValue());
                             Assertions.assertEquals(2, ((Literal) 
secondSubstring.getLength().get()).getValue());
                             return true;
@@ -95,13 +95,13 @@ public class FunctionRegistryTest implements 
MemoPatternMatchSupported {
         PlanChecker.from(connectContext)
                 .analyze("select substr('abc', 1), substring('def', 2, 3)")
                 .matches(
-                        logicalOneRowRelation().when(r -> {
-                            Substring firstSubstring = (Substring) 
r.getProjects().get(0).child(0);
+                        logicalProject().when(project -> {
+                            Substring firstSubstring = (Substring) 
project.getProjects().get(0).child(0);
                             Assertions.assertEquals("abc", ((Literal) 
firstSubstring.getSource()).getValue());
                             Assertions.assertEquals(1, ((Literal) 
firstSubstring.getPosition()).getValue());
                             
Assertions.assertTrue(firstSubstring.getLength().isPresent());
 
-                            Substring secondSubstring = (Substring) 
r.getProjects().get(1).child(0);
+                            Substring secondSubstring = (Substring) 
project.getProjects().get(1).child(0);
                             Assertions.assertEquals("def", ((Literal) 
secondSubstring.getSource()).getValue());
                             Assertions.assertEquals(2, ((Literal) 
secondSubstring.getPosition()).getValue());
                             Assertions.assertEquals(3, ((Literal) 
secondSubstring.getLength().get()).getValue());
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/UdfTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/UdfTest.java
index d6121f05888..adb07ebf95f 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/UdfTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/UdfTest.java
@@ -71,8 +71,8 @@ public class UdfTest extends TestWithFeService implements 
PlanPatternMatchSuppor
         PlanChecker.from(connectContext)
                 .analyze(sql)
                 .matches(
-                        logicalOneRowRelation()
-                                .when(relation -> 
relation.getProjects().get(0).child(0).equals(expected))
+                        logicalProject()
+                                .when(project -> 
project.getProjects().get(0).child(0).equals(expected))
                 );
 
         connectContext.setDatabase("default_cluster:test_1");
@@ -80,8 +80,8 @@ public class UdfTest extends TestWithFeService implements 
PlanPatternMatchSuppor
         PlanChecker.from(connectContext)
                 .analyze(sql)
                 .matches(
-                        logicalOneRowRelation()
-                                .when(relation -> 
relation.getProjects().get(0).child(0).equals(expected1))
+                        logicalProject()
+                                .when(project -> 
project.getProjects().get(0).child(0).equals(expected1))
                 );
 
         sql = "select test.f(3)";
@@ -89,8 +89,8 @@ public class UdfTest extends TestWithFeService implements 
PlanPatternMatchSuppor
         PlanChecker.from(connectContext)
                 .analyze(sql)
                 .matches(
-                        logicalOneRowRelation()
-                                .when(relation -> 
relation.getProjects().get(0).child(0).equals(expected2))
+                        logicalProject()
+                                .when(project -> 
project.getProjects().get(0).child(0).equals(expected2))
                 );
     }
 
@@ -129,9 +129,9 @@ public class UdfTest extends TestWithFeService implements 
PlanPatternMatchSuppor
         PlanChecker.from(connectContext)
                 .analyze(sql)
                 .matches(
-                        logicalOneRowRelation()
-                                .when(relation -> 
relation.getProjects().size() == 1
-                                        && 
relation.getProjects().get(0).child(0).equals(expected))
+                        logicalProject()
+                                .when(project -> project.getProjects().size() 
== 1
+                                        && 
project.getProjects().get(0).child(0).equals(expected))
                 );
     }
 
@@ -172,9 +172,9 @@ public class UdfTest extends TestWithFeService implements 
PlanPatternMatchSuppor
         PlanChecker.from(connectContext)
                 .analyze(sql)
                 .matches(
-                        logicalOneRowRelation()
-                                .when(relation -> 
relation.getProjects().size() == 1
-                                        && 
relation.getProjects().get(0).child(0).equals(expected))
+                        logicalProject()
+                                .when(project -> project.getProjects().size() 
== 1
+                                        && 
project.getProjects().get(0).child(0).equals(expected))
                 );
     }
 
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 00000000000..3f1ba34f761
--- /dev/null
+++ b/regression-test/data/nereids_p0/select_no_from/sql/withWhereFalse.out
@@ -0,0 +1,3 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !withWhereFalse --
+
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 c4096c2811a..6ea99d0c8da 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
@@ -2,3 +2,5 @@
 -- database: presto; groups: no_from
 SELECT COUNT(*), 1 WHERE FALSE
 */
+
+SELECT 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 c60cce38678..351d18535c7 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 "Unexpected exception: a is ambiguous: a#0, a#1."
+        exception "Unexpected exception: a is ambiguous"
     }
 
     sql "drop table if exists duplicate_slot";
diff --git a/regression-test/suites/nereids_syntax_p0/one_row_relation.groovy 
b/regression-test/suites/nereids_syntax_p0/one_row_relation.groovy
index 9b814cc4fe0..e389960e79e 100644
--- a/regression-test/suites/nereids_syntax_p0/one_row_relation.groovy
+++ b/regression-test/suites/nereids_syntax_p0/one_row_relation.groovy
@@ -31,11 +31,4 @@ suite("one_row_relation") {
         )a"""
         result([[100, "abc", "ab", "de", null]])
     }
-
-    test {
-        sql """
-            select sum(1);
-        """
-        exception "OneRowRelation can not contains any aggregate function"     
       
-    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to