[opt](Nereids) support where, group by, having, order by clause without from clause in query statement (#27006)

Support where, group by, having, order by clause without from clause in query statement.
For example as following:

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;

this will return result:

| a  |count(*)|sum(2)|avg(1)|w_rank|
+----+--------+------+------+------+
| 1  |       1|     2|   1.0|     1|


For another example as following:

select 1 c1, 2 union (select "hell0", "") order by c1
the second column datatype will be varchar(65533), 65533 is the default varchar length.

this will return result:

|c1    | 2 |
+------+---+
|1     | 2 |
|hell0 |   |
This commit is contained in:
seawinde
2023-11-27 12:05:14 +08:00
committed by GitHub
parent 331effdb20
commit 1b4cd24b36
17 changed files with 94 additions and 30 deletions

View File

@ -992,22 +992,22 @@ 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 = 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());
});

View File

@ -446,6 +446,12 @@ public class BindExpression implements AnalysisRuleFactory {
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 -> {