Skip to content

Commit be046f9

Browse files
apurtellclaude
andauthored
PHOENIX-7922 Consolidate the EXPLAIN rewrite pass entry points (#2529)
Co-authored-by: Claude Opus 4.8[1m] <noreply@anthropic.com>
1 parent 66be274 commit be046f9

8 files changed

Lines changed: 27 additions & 34 deletions

File tree

phoenix-core-client/src/main/java/org/apache/phoenix/compile/DeleteCompiler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,7 @@ public MutationPlan compile(DeleteStatement delete, MutationState.ReturnResult r
598598
select = StatementNormalizer.normalize(select, resolverToBe);
599599
// Pre-build a context so the early rewrite pass records top of plan breadcrumbs that are
600600
// adopted by the DELETE data query plan's compilation context.
601-
StatementContext rewriteContext =
602-
new StatementContext(statement, FromCompiler.EMPTY_TABLE_RESOLVER,
603-
new BindManager(statement.getParameters()), new Scan(), new SequenceManager(statement));
601+
StatementContext rewriteContext = StatementContext.forRewrite(statement);
604602
SelectStatement transformedSelect =
605603
SubqueryRewriter.transform(select, resolverToBe, connection, rewriteContext);
606604
boolean hasPreProcessing = transformedSelect != select;

phoenix-core-client/src/main/java/org/apache/phoenix/compile/QueryCompiler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,7 @@ protected QueryPlan compileSubquery(SelectStatement subquery, boolean pushDownMa
724724

725725
protected QueryPlan compileSubquery(SelectStatement subquerySelectStatement,
726726
boolean pushDownMaxRows, StatementContext parentContext) throws SQLException {
727-
// Pre-build a context so the subquery's early rewrite pass records breadcrumbs that are then
728-
// adopted by the subquery's compilation context.
729-
StatementContext rewriteContext =
730-
new StatementContext(this.statement, FromCompiler.EMPTY_TABLE_RESOLVER, bindManager,
731-
new Scan(), new SequenceManager(this.statement));
727+
StatementContext rewriteContext = StatementContext.forRewrite(this.statement, bindManager);
732728
RewriteResult rewriteResult = ParseNodeUtil.rewrite(subquerySelectStatement, rewriteContext);
733729
int maxRows = this.statement.getMaxRows();
734730
// overwrite maxRows to avoid its impact on inner queries.

phoenix-core-client/src/main/java/org/apache/phoenix/compile/StatementContext.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ public StatementContext(PhoenixStatement statement) {
125125
this(statement, new Scan());
126126
}
127127

128+
/**
129+
* Build a top-level {@link StatementContext} suitable for the early
130+
* {@link SubselectRewriter}/{@link SubqueryRewriter} rewrite pass, accumulating rewrite
131+
* breadcrumbs.
132+
*/
133+
public static StatementContext forRewrite(PhoenixStatement statement) {
134+
return forRewrite(statement, new BindManager(statement.getParameters()));
135+
}
136+
137+
/**
138+
* Variant of {@link #forRewrite(PhoenixStatement)} that reuses an existing {@link BindManager}.
139+
*/
140+
public static StatementContext forRewrite(PhoenixStatement statement, BindManager bindManager) {
141+
return new StatementContext(statement, FromCompiler.EMPTY_TABLE_RESOLVER, bindManager,
142+
new Scan(), new SequenceManager(statement));
143+
}
144+
128145
public StatementContext(StatementContext context) {
129146
this.resolver = context.resolver;
130147
this.connection = context.connection;

phoenix-core-client/src/main/java/org/apache/phoenix/compile/SubqueryRewriter.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ public class SubqueryRewriter extends ParseNodeRewriter {
7373
private TableNode tableNode;
7474
private ParseNode topNode;
7575

76-
public static SelectStatement transform(SelectStatement select, ColumnResolver resolver,
77-
PhoenixConnection connection) throws SQLException {
78-
return transform(select, resolver, connection, null);
79-
}
80-
8176
public static SelectStatement transform(SelectStatement select, ColumnResolver resolver,
8277
PhoenixConnection connection, StatementContext context) throws SQLException {
8378
ParseNode where = select.getWhere();
@@ -90,11 +85,6 @@ public static SelectStatement transform(SelectStatement select, ColumnResolver r
9085
return NODE_FACTORY.select(select, rewriter.tableNode, normWhere);
9186
}
9287

93-
protected SubqueryRewriter(SelectStatement select, ColumnResolver resolver,
94-
PhoenixConnection connection) {
95-
this(select, resolver, connection, null);
96-
}
97-
9888
protected SubqueryRewriter(SelectStatement select, ColumnResolver resolver,
9989
PhoenixConnection connection, StatementContext context) {
10090
this.columnResolver = resolver;

phoenix-core-client/src/main/java/org/apache/phoenix/compile/SubselectRewriter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,6 @@ public static SelectStatement pruneSelectAliasedNodes(SelectStatement selectStat
298298
newSelectAliasedNodes);
299299
}
300300

301-
public static SelectStatement flatten(SelectStatement select, PhoenixConnection connection)
302-
throws SQLException {
303-
return flatten(select, connection, null);
304-
}
305-
306301
public static SelectStatement flatten(SelectStatement select, PhoenixConnection connection,
307302
StatementContext context) throws SQLException {
308303
TableNode from = select.getFrom();

phoenix-core-client/src/main/java/org/apache/phoenix/compile/UpsertCompiler.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,7 @@ public MutationPlan compile(UpsertStatement upsert) throws SQLException {
583583
assert (select != null);
584584
// Pre-build a context so the early rewrite pass records top-of-plan breadcrumbs that are
585585
// adopted by the UPSERT SELECT query plan's compilation context.
586-
StatementContext rewriteContext =
587-
new StatementContext(statement, FromCompiler.EMPTY_TABLE_RESOLVER,
588-
new BindManager(statement.getParameters()), new Scan(), new SequenceManager(statement));
586+
StatementContext rewriteContext = StatementContext.forRewrite(statement);
589587
select = SubselectRewriter.flatten(select, connection, rewriteContext);
590588
ColumnResolver selectResolver =
591589
FromCompiler.getResolverForQuery(select, connection, false, upsert.getTable().getName());

phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
import org.apache.hadoop.hbase.util.Pair;
8181
import org.apache.phoenix.call.CallRunner;
8282
import org.apache.phoenix.compile.BaseMutationPlan;
83-
import org.apache.phoenix.compile.BindManager;
8483
import org.apache.phoenix.compile.CloseStatementCompiler;
8584
import org.apache.phoenix.compile.ColumnProjector;
8685
import org.apache.phoenix.compile.CreateFunctionCompiler;
@@ -95,7 +94,6 @@
9594
import org.apache.phoenix.compile.ExplainPlan;
9695
import org.apache.phoenix.compile.ExplainPlanAttributes;
9796
import org.apache.phoenix.compile.ExpressionProjector;
98-
import org.apache.phoenix.compile.FromCompiler;
9997
import org.apache.phoenix.compile.GroupByCompiler.GroupBy;
10098
import org.apache.phoenix.compile.ListJarsQueryPlan;
10199
import org.apache.phoenix.compile.MutationPlan;
@@ -867,9 +865,7 @@ public QueryPlan compilePlan(PhoenixStatement phoenixStatement, Sequence.ValueOp
867865
// Pre-build the top-level StatementContext so the early SubselectRewriter/SubqueryRewriter
868866
// pass can record top of plan rewrite breadcrumbs onto it. The same accumulator is then
869867
// adopted by the compilation context via QueryCompiler.withRewriteContext.
870-
StatementContext rewriteContext = new StatementContext(phoenixStatement,
871-
FromCompiler.EMPTY_TABLE_RESOLVER, new BindManager(phoenixStatement.getParameters()),
872-
new Scan(), new SequenceManager(phoenixStatement));
868+
StatementContext rewriteContext = StatementContext.forRewrite(phoenixStatement);
873869
RewriteResult rewriteResult = ParseNodeUtil.rewrite(this, rewriteContext);
874870
QueryPlan queryPlan = new QueryCompiler(phoenixStatement,
875871
rewriteResult.getRewrittenSelectStatement(), rewriteResult.getColumnResolver(),

phoenix-core/src/test/java/org/apache/phoenix/util/TestUtil.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,15 +1445,18 @@ public static boolean hasFilter(Scan scan, Class<? extends Filter> filterClass)
14451445
public static JoinTable getJoinTable(String query, PhoenixConnection connection)
14461446
throws SQLException {
14471447
SQLParser parser = new SQLParser(query);
1448-
SelectStatement select = SubselectRewriter.flatten(parser.parseQuery(), connection);
1448+
PhoenixStatement stmt = connection.createStatement().unwrap(PhoenixStatement.class);
1449+
StatementContext rewriteContext = StatementContext.forRewrite(stmt);
1450+
SelectStatement select =
1451+
SubselectRewriter.flatten(parser.parseQuery(), connection, rewriteContext);
14491452
ColumnResolver resolver = FromCompiler.getResolverForQuery(select, connection);
14501453
select = StatementNormalizer.normalize(select, resolver);
1451-
SelectStatement transformedSelect = SubqueryRewriter.transform(select, resolver, connection);
1454+
SelectStatement transformedSelect =
1455+
SubqueryRewriter.transform(select, resolver, connection, rewriteContext);
14521456
if (transformedSelect != select) {
14531457
resolver = FromCompiler.getResolverForQuery(transformedSelect, connection);
14541458
select = StatementNormalizer.normalize(transformedSelect, resolver);
14551459
}
1456-
PhoenixStatement stmt = connection.createStatement().unwrap(PhoenixStatement.class);
14571460
return JoinCompiler.compile(stmt, select, resolver);
14581461
}
14591462

0 commit comments

Comments
 (0)