|
20 | 20 | import org.opensearch.core.action.ActionListener; |
21 | 21 | import org.opensearch.sql.api.UnifiedQueryContext; |
22 | 22 | import org.opensearch.sql.api.UnifiedQueryPlanner; |
| 23 | +import org.opensearch.sql.ast.AbstractNodeVisitor; |
23 | 24 | import org.opensearch.sql.ast.tree.Relation; |
24 | 25 | import org.opensearch.sql.ast.tree.UnresolvedPlan; |
25 | 26 | import org.opensearch.sql.calcite.CalcitePlanContext; |
@@ -156,35 +157,24 @@ private UnifiedQueryContext buildContext(QueryType queryType, boolean profiling) |
156 | 157 | } |
157 | 158 |
|
158 | 159 | /** |
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. |
162 | 162 | */ |
163 | | - @SuppressWarnings("unchecked") |
164 | 163 | private static String extractIndexName(String query, UnifiedQueryContext context) { |
165 | 164 | Object parseResult = context.getParser().parse(query); |
166 | 165 | 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); |
169 | 167 | } |
170 | 168 | // TODO: handle SQL SqlNode for table extraction when unified SQL is enabled |
171 | 169 | return null; |
172 | 170 | } |
173 | 171 |
|
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(); |
178 | 177 | } |
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; |
188 | 178 | } |
189 | 179 |
|
190 | 180 | private static RelNode addQuerySizeLimit(RelNode plan, CalcitePlanContext context) { |
|
0 commit comments