|
8 | 8 | import static org.opensearch.sql.executor.QueryType.PPL; |
9 | 9 |
|
10 | 10 | import com.google.common.collect.ImmutableList; |
| 11 | +import java.util.List; |
11 | 12 | import lombok.Builder; |
12 | 13 | import lombok.Data; |
13 | 14 | import lombok.RequiredArgsConstructor; |
| 15 | +import org.opensearch.sql.ast.Node; |
14 | 16 | import org.opensearch.sql.ast.expression.AllFields; |
15 | 17 | import org.opensearch.sql.ast.statement.Explain; |
16 | 18 | import org.opensearch.sql.ast.statement.Query; |
@@ -70,18 +72,22 @@ public static class StatementBuilderContext { |
70 | 72 | } |
71 | 73 |
|
72 | 74 | /** |
73 | | - * Recursively checks if the AST contains a {@link Head} node. When the user's query already |
74 | | - * includes an explicit {@code head} command, we should not inject an additional Head for |
75 | | - * fetch_size so that the user's explicit limit takes precedence. |
| 75 | + * Checks if the main pipeline contains a {@link Head} node by walking the first-child chain. Only |
| 76 | + * the main pipeline is checked — subqueries in joins or nested structures are not traversed. When |
| 77 | + * the user's query already includes an explicit {@code head} command, we should not inject an |
| 78 | + * additional Head for fetch_size so that the user's explicit limit takes precedence. |
76 | 79 | */ |
77 | 80 | private boolean containsHead(UnresolvedPlan plan) { |
78 | | - if (plan instanceof Head) { |
79 | | - return true; |
80 | | - } |
81 | | - for (var child : plan.getChild()) { |
82 | | - if (child instanceof UnresolvedPlan && containsHead((UnresolvedPlan) child)) { |
| 81 | + UnresolvedPlan current = plan; |
| 82 | + while (current != null) { |
| 83 | + if (current instanceof Head) { |
83 | 84 | return true; |
84 | 85 | } |
| 86 | + List<? extends Node> children = current.getChild(); |
| 87 | + if (children.isEmpty() || !(children.get(0) instanceof UnresolvedPlan)) { |
| 88 | + break; |
| 89 | + } |
| 90 | + current = (UnresolvedPlan) children.get(0); |
85 | 91 | } |
86 | 92 | return false; |
87 | 93 | } |
|
0 commit comments