Skip to content

Commit 2cf5183

Browse files
committed
Use AST visitor for index name extraction
Replace manual tree walking (findRelation) with IndexNameExtractor visitor extending AbstractNodeVisitor. The visitor's visitRelation() is called automatically by the AST accept/visitChildren pattern, which handles tree traversal. Signed-off-by: Kai Huang <kaihuang@amazon.com> Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent 022461a commit 2cf5183

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed

plugin/src/main/java/org/opensearch/sql/plugin/rest/RestUnifiedQueryAction.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.opensearch.core.action.ActionListener;
2121
import org.opensearch.sql.api.UnifiedQueryContext;
2222
import org.opensearch.sql.api.UnifiedQueryPlanner;
23+
import org.opensearch.sql.ast.AbstractNodeVisitor;
2324
import org.opensearch.sql.ast.tree.Relation;
2425
import org.opensearch.sql.ast.tree.UnresolvedPlan;
2526
import org.opensearch.sql.calcite.CalcitePlanContext;
@@ -156,35 +157,24 @@ private UnifiedQueryContext buildContext(QueryType queryType, boolean profiling)
156157
}
157158

158159
/**
159-
* Extract the source index name by parsing the query using the context's parser and finding the
160-
* Relation node in the AST. Works for both PPL and SQL via {@link
161-
* UnifiedQueryContext#getParser()}.
160+
* Extract the source index name by parsing the query and visiting the AST to find the Relation
161+
* node. Uses the context's parser which supports both PPL and SQL.
162162
*/
163-
@SuppressWarnings("unchecked")
164163
private static String extractIndexName(String query, UnifiedQueryContext context) {
165164
Object parseResult = context.getParser().parse(query);
166165
if (parseResult instanceof UnresolvedPlan unresolvedPlan) {
167-
Relation relation = findRelation(unresolvedPlan);
168-
return relation != null ? relation.getTableQualifiedName().toString() : null;
166+
return unresolvedPlan.accept(new IndexNameExtractor(), null);
169167
}
170168
// TODO: handle SQL SqlNode for table extraction when unified SQL is enabled
171169
return null;
172170
}
173171

174-
/** Walk the AST to find the Relation (table scan) node. */
175-
private static Relation findRelation(UnresolvedPlan plan) {
176-
if (plan instanceof Relation) {
177-
return (Relation) plan;
172+
/** AST visitor that extracts the source index name from a Relation node. */
173+
private static class IndexNameExtractor extends AbstractNodeVisitor<String, Void> {
174+
@Override
175+
public String visitRelation(Relation node, Void context) {
176+
return node.getTableQualifiedName().toString();
178177
}
179-
for (var child : plan.getChild()) {
180-
if (child instanceof UnresolvedPlan unresolvedChild) {
181-
Relation found = findRelation(unresolvedChild);
182-
if (found != null) {
183-
return found;
184-
}
185-
}
186-
}
187-
return null;
188178
}
189179

190180
private static RelNode addQuerySizeLimit(RelNode plan, CalcitePlanContext context) {

0 commit comments

Comments
 (0)