Skip to content

Commit

Permalink
[improvement](nereids) Support where, group by, having, order by clau…
Browse files Browse the repository at this point in the history
…se without from clause in query statement
  • Loading branch information
JingDas authored and seawinde committed Nov 15, 2023
1 parent 6183b29 commit bf92ab4
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -987,22 +987,22 @@ public LogicalPlan visitRegularQuerySpecification(RegularQuerySpecificationConte
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());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,12 @@ protected boolean condition(Rule rule, Plan plan) {
LogicalCTEAnchor<Plan, Plan> cteAnchor = sort.child();
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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*
-- database: presto; groups: no_from
SELECT COUNT(*), 1 WHERE FALSE
*/
SET enable_nereids_planner = TRUE;
SELECT COUNT(*), 1 WHERE FALSE;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*
-- database: presto; groups: no_from
SELECT COUNT(*), 1 WHERE FALSE
*/
SET enable_nereids_planner = TRUE;
SELECT COUNT(*), 1 WHERE FALSE;

0 comments on commit bf92ab4

Please sign in to comment.