GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Feature] support recursive cte (#65932) · APIJSON/starrocks@28ca796

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • fe
    • fe-core/src
      • main/java/com/starrocks
        • connector/parser/trino
          • AstBuilder.java
        • qe
          • SessionVariable.java
          • StmtExecutor.java
          • recursivecte
            • RecursiveCTEAstCheck.java
            • RecursiveCTEExecutor.java
        • sql
          • analyzer
            • QueryAnalyzer.java
          • ast
            • AstTraverser.java
            • CTERelation.java
            • QueryRelation.java
            • SelectRelation.java
          • formatter
            • AST2StringVisitor.java
          • optimizer/transformer
            • RelationTransformer.java
          • parser
            • AstBuilder.java
      • test/java/com/starrocks/sql
        • analyzer
          • AnalyzeCTETest.java
        • plan
          • RecursiveCTETest.java
    • fe-grammar/src/main/antlr/com/starrocks/grammar
      • StarRocks.g4
      • StarRocksLex.g4
    • fe-parser/src/main/java/com/starrocks/sql/ast
      • QualifiedName.java
  • test/sql/test_cte
    • R
      • test_recursive_cte
    • T
      • test_recursive_cte
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java‎

Copy file name to clipboardExpand all lines: fe/fe-core/src/main/java/com/starrocks/connector/parser/trino/AstBuilder.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ protected ParseNode visitWithQuery(WithQuery node, ParseTreeContext context) {
387387
RelationId.of(queryStatement.getQueryRelation()).hashCode(),
388388
node.getName().getValue().toLowerCase(),
389389
getColumnNames(node.getColumnNames()),
390-
queryStatement);
390+
queryStatement,
391+
false,
392+
true);
391393
}
392394

393395
public List<String> getColumnNames(Optional<List<Identifier>> columnNames) {
Collapse file

‎fe/fe-core/src/main/java/com/starrocks/qe/SessionVariable.java‎

Copy file name to clipboardExpand all lines: fe/fe-core/src/main/java/com/starrocks/qe/SessionVariable.java
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ public class SessionVariable implements Serializable, Writable, Cloneable {
470470

471471
public static final String CHOOSE_EXECUTE_INSTANCES_MODE = "choose_execute_instances_mode";
472472

473+
public static final String ENABLE_RECURSIVE_CTE = "enable_recursive_cte";
474+
public static final String RECURSIVE_CTE_MAX_DEPTH = "recursive_cte_max_depth";
475+
public static final String RECURSIVE_CTE_THROW_LIMIT_EXCEPTION = "recursive_cte_throw_limit_exception";
476+
public static final String RECURSIVE_CTE_FINALIZE_TEMPORAL_TABLE = "recursive_cte_finalize_temporal_table";
477+
473478
// -------- New planner session variables end --------
474479

475480
// Type of compression of transmitted data
@@ -2111,6 +2116,42 @@ public static MaterializedViewRewriteMode parse(String str) {
21112116
@VarAttr(name = ENABLE_DROP_TABLE_CHECK_MV_DEPENDENCY)
21122117
public boolean enableDropTableCheckMvDependency = false;
21132118

2119+
@VarAttr(name = ENABLE_RECURSIVE_CTE)
2120+
private boolean enableRecursiveCTE = false;
2121+
2122+
@VarAttr(name = RECURSIVE_CTE_MAX_DEPTH)
2123+
private int recursiveCteMaxDepth = 5;
2124+
2125+
@VarAttr(name = RECURSIVE_CTE_FINALIZE_TEMPORAL_TABLE, flag = VariableMgr.INVISIBLE)
2126+
private boolean recursiveCteFinalizeTemporalTable = true;
2127+
2128+
@VarAttr(name = RECURSIVE_CTE_THROW_LIMIT_EXCEPTION)
2129+
private boolean recursiveCteThrowLimitException = true;
2130+
2131+
public boolean isEnableRecursiveCTE() {
2132+
return enableRecursiveCTE;
2133+
}
2134+
2135+
public void setEnableRecursiveCTE(boolean enableRecursiveCTE) {
2136+
this.enableRecursiveCTE = enableRecursiveCTE;
2137+
}
2138+
2139+
public boolean isRecursiveCteFinalizeTemporalTable() {
2140+
return recursiveCteFinalizeTemporalTable;
2141+
}
2142+
2143+
public boolean isRecursiveCteThrowLimitException() {
2144+
return recursiveCteThrowLimitException;
2145+
}
2146+
2147+
public int getRecursiveCteMaxDepth() {
2148+
return recursiveCteMaxDepth;
2149+
}
2150+
2151+
public void setRecursiveCteMaxDepth(int recursiveCteMaxDepth) {
2152+
this.recursiveCteMaxDepth = recursiveCteMaxDepth;
2153+
}
2154+
21142155
public boolean isEnableSplitTopNAgg() {
21152156
return enableSplitTopNAgg;
21162157
}
Collapse file

‎fe/fe-core/src/main/java/com/starrocks/qe/StmtExecutor.java‎

Copy file name to clipboardExpand all lines: fe/fe-core/src/main/java/com/starrocks/qe/StmtExecutor.java
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
import com.starrocks.qe.feedback.analyzer.PlanTuningAnalyzer;
127127
import com.starrocks.qe.feedback.skeleton.SkeletonBuilder;
128128
import com.starrocks.qe.feedback.skeleton.SkeletonNode;
129+
import com.starrocks.qe.recursivecte.RecursiveCTEAstCheck;
130+
import com.starrocks.qe.recursivecte.RecursiveCTEExecutor;
129131
import com.starrocks.qe.scheduler.Coordinator;
130132
import com.starrocks.qe.scheduler.FeExecuteCoordinator;
131133
import com.starrocks.qe.scheduler.dag.ExecutionFragment;
@@ -724,6 +726,7 @@ public void execute() throws Exception {
724726
WarehouseIdleChecker.increaseRunningSQL(originWarehouseId);
725727
}
726728

729+
RecursiveCTEExecutor cteExecutor = null;
727730
try {
728731
context.getState().setIsQuery(context.isQueryStmt(parsedStmt));
729732
if (parsedStmt.isExistQueryScopeHint()) {
@@ -742,6 +745,26 @@ public void execute() throws Exception {
742745
context.setExplainLevel(null);
743746
}
744747

748+
if (RecursiveCTEAstCheck.hasRecursiveCte(parsedStmt, context)) {
749+
redirectStatus = RedirectStatus.FORWARD_WITH_SYNC;
750+
if (isForwardToLeader()) {
751+
context.setIsForward(true);
752+
forwardToLeader();
753+
return;
754+
}
755+
cteExecutor = new RecursiveCTEExecutor(context);
756+
parsedStmt = cteExecutor.splitOuterStmt(parsedStmt);
757+
if (parsedStmt.isExplain()) {
758+
// Every time set no send flag and clean all data in buffer
759+
context.getMysqlChannel().reset();
760+
String explainString = cteExecutor.explainCTE(parsedStmt);
761+
handleExplainStmt(explainString);
762+
return;
763+
} else {
764+
cteExecutor.prepareRecursiveCTE();
765+
}
766+
}
767+
745768
redirectStatus = RedirectStatus.getRedirectStatus(parsedStmt);
746769
// for explain query, we can trace even if optimizer fails and execPlan is null
747770
// for refresh materialized view, use handleDdlStmt instead.
@@ -1037,6 +1060,9 @@ public void execute() throws Exception {
10371060

10381061
// process post-action after query is finished
10391062
context.onQueryFinished();
1063+
if (cteExecutor != null) {
1064+
cteExecutor.finalizeRecursiveCTE();
1065+
}
10401066
}
10411067
}
10421068

Collapse file

‎fe/fe-core/src/main/java/com/starrocks/qe/recursivecte/RecursiveCTEAstCheck.java‎

Copy file name to clipboard
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2021-present StarRocks, Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.starrocks.qe.recursivecte;
16+
17+
import com.starrocks.qe.ConnectContext;
18+
import com.starrocks.sql.analyzer.SemanticException;
19+
import com.starrocks.sql.ast.AstTraverser;
20+
import com.starrocks.sql.ast.CTERelation;
21+
import com.starrocks.sql.ast.StatementBase;
22+
import com.starrocks.sql.ast.TableRelation;
23+
24+
import java.util.Stack;
25+
26+
public class RecursiveCTEAstCheck extends AstTraverser<Void, Void> {
27+
private final Stack<String> cteNameStack = new Stack<>();
28+
private boolean isRecursiveCte = false;
29+
30+
public static boolean hasRecursiveCte(StatementBase stmt, ConnectContext context) {
31+
if (!context.getSessionVariable().isEnableRecursiveCTE()) {
32+
return false;
33+
}
34+
RecursiveCTEAstCheck check = new RecursiveCTEAstCheck();
35+
stmt.accept(check, null);
36+
return check.isRecursiveCte;
37+
}
38+
39+
@Override
40+
public Void visitCTE(CTERelation node, Void context) {
41+
if (node.isAnchor()) {
42+
cteNameStack.push(node.getName());
43+
}
44+
super.visitCTE(node, context);
45+
if (node.isAnchor()) {
46+
cteNameStack.pop();
47+
}
48+
return null;
49+
}
50+
51+
@Override
52+
public Void visitTable(TableRelation node, Void context) {
53+
if (cteNameStack.isEmpty()) {
54+
return null;
55+
}
56+
if (node.getName().getDb() == null && node.getName().getTbl().equalsIgnoreCase(cteNameStack.peek())) {
57+
if (cteNameStack.size() != 1) {
58+
throw new SemanticException("Doesn't support multi-level recursive CTE");
59+
}
60+
isRecursiveCte = true;
61+
}
62+
return null;
63+
}
64+
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.