Skip to content

Commit 93c147b

Browse files
committed
Merge remote-tracking branch 'origin/main' into pushdown-ip
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
2 parents 9f42c12 + 5316c0a commit 93c147b

110 files changed

Lines changed: 370 additions & 276 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.calcite.plan;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
import lombok.Getter;
11+
import org.apache.calcite.plan.Convention;
12+
import org.apache.calcite.plan.RelOptCluster;
13+
import org.apache.calcite.plan.RelTraitSet;
14+
import org.apache.calcite.rel.RelCollation;
15+
import org.apache.calcite.rel.RelCollationTraitDef;
16+
import org.apache.calcite.rel.RelNode;
17+
import org.apache.calcite.rel.RelWriter;
18+
import org.apache.calcite.rel.core.Sort;
19+
import org.apache.calcite.rel.hint.RelHint;
20+
import org.apache.calcite.rel.logical.LogicalSort;
21+
import org.apache.calcite.rex.RexNode;
22+
import org.checkerframework.checker.nullness.qual.Nullable;
23+
24+
/** System level limit logical plan, comparing to user level plan {@link LogicalSort}. */
25+
public class LogicalSystemLimit extends Sort {
26+
27+
public enum SystemLimitType {
28+
/**
29+
* System limit type for system level limit.
30+
*
31+
* <p>This type is used to indicate that the limit is applied to the system level.
32+
*/
33+
QUERY_SIZE_LIMIT
34+
}
35+
36+
@Getter private final SystemLimitType type;
37+
38+
private LogicalSystemLimit(
39+
SystemLimitType type,
40+
RelOptCluster cluster,
41+
RelTraitSet traitSet,
42+
RelNode input,
43+
RelCollation collation,
44+
@Nullable RexNode offset,
45+
@Nullable RexNode fetch) {
46+
this(type, cluster, traitSet, Collections.emptyList(), input, collation, offset, fetch);
47+
}
48+
49+
private LogicalSystemLimit(
50+
SystemLimitType type,
51+
RelOptCluster cluster,
52+
RelTraitSet traitSet,
53+
List<RelHint> hints,
54+
RelNode input,
55+
RelCollation collation,
56+
@Nullable RexNode offset,
57+
@Nullable RexNode fetch) {
58+
super(cluster, traitSet, hints, input, collation, offset, fetch);
59+
assert traitSet.containsIfApplicable(Convention.NONE);
60+
this.type = type;
61+
}
62+
63+
public static LogicalSystemLimit create(SystemLimitType type, RelNode input, RexNode fetch) {
64+
return create(type, input, input.getTraitSet().getCollation(), null, fetch);
65+
}
66+
67+
public static LogicalSystemLimit create(
68+
SystemLimitType type,
69+
RelNode input,
70+
RelCollation collation,
71+
@Nullable RexNode offset,
72+
@Nullable RexNode fetch) {
73+
RelOptCluster cluster = input.getCluster();
74+
collation = RelCollationTraitDef.INSTANCE.canonize(collation);
75+
RelTraitSet traitSet = input.getTraitSet().replace(Convention.NONE).replace(collation);
76+
return new LogicalSystemLimit(type, cluster, traitSet, input, collation, offset, fetch);
77+
}
78+
79+
@Override
80+
public Sort copy(
81+
RelTraitSet traitSet,
82+
RelNode newInput,
83+
RelCollation newCollation,
84+
@Nullable RexNode offset,
85+
@Nullable RexNode fetch) {
86+
return new LogicalSystemLimit(
87+
this.type, getCluster(), traitSet, hints, newInput, newCollation, offset, fetch);
88+
}
89+
90+
@Override
91+
public RelWriter explainTerms(RelWriter pw) {
92+
super.explainTerms(pw);
93+
// Show type in the explain
94+
pw.item("type", type);
95+
return pw;
96+
}
97+
}

core/src/main/java/org/opensearch/sql/calcite/plan/Scannable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
*/
1717
public interface Scannable {
1818

19-
public Enumerable<@Nullable Object> scanWithLimit();
19+
public Enumerable<@Nullable Object> scan();
2020
}

core/src/main/java/org/opensearch/sql/calcite/utils/CalciteToolsHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected PreparedResult implement(RelRoot root) {
302302
RelDataType resultType = root.rel.getRowType();
303303
boolean isDml = root.kind.belongsTo(SqlKind.DML);
304304
if (root.rel instanceof Scannable scannable) {
305-
final Bindable bindable = dataContext -> scannable.scanWithLimit();
305+
final Bindable bindable = dataContext -> scannable.scan();
306306

307307
return new PreparedResultImpl(
308308
resultType,

core/src/main/java/org/opensearch/sql/executor/QueryService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.opensearch.sql.calcite.CalcitePlanContext;
3737
import org.opensearch.sql.calcite.CalciteRelNodeVisitor;
3838
import org.opensearch.sql.calcite.OpenSearchSchema;
39+
import org.opensearch.sql.calcite.plan.LogicalSystemLimit;
40+
import org.opensearch.sql.calcite.plan.LogicalSystemLimit.SystemLimitType;
3941
import org.opensearch.sql.common.response.ResponseListener;
4042
import org.opensearch.sql.common.setting.Settings;
4143
import org.opensearch.sql.common.setting.Settings.Key;
@@ -100,7 +102,7 @@ public void executeWithCalcite(
100102
CalcitePlanContext.create(
101103
buildFrameworkConfig(), getQuerySizeLimit(), queryType);
102104
RelNode relNode = analyze(plan, context);
103-
RelNode optimized = optimize(relNode);
105+
RelNode optimized = optimize(relNode, context);
104106
RelNode calcitePlan = convertToCalcitePlan(optimized);
105107
executionEngine.execute(calcitePlan, context, listener);
106108
return null;
@@ -133,7 +135,7 @@ public void explainWithCalcite(
133135
CalcitePlanContext.create(
134136
buildFrameworkConfig(), getQuerySizeLimit(), queryType);
135137
RelNode relNode = analyze(plan, context);
136-
RelNode optimized = optimize(relNode);
138+
RelNode optimized = optimize(relNode, context);
137139
RelNode calcitePlan = convertToCalcitePlan(optimized);
138140
executionEngine.explain(calcitePlan, format, context, listener);
139141
return null;
@@ -250,8 +252,13 @@ public PhysicalPlan plan(LogicalPlan plan) {
250252
return planner.plan(plan);
251253
}
252254

253-
public RelNode optimize(RelNode plan) {
254-
return planner.customOptimize(plan);
255+
/**
256+
* Try to optimize the plan by appending a limit operator for QUERY_SIZE_LIMIT Don't add for
257+
* `EXPLAIN` to avoid changing its output plan.
258+
*/
259+
public RelNode optimize(RelNode plan, CalcitePlanContext context) {
260+
return LogicalSystemLimit.create(
261+
SystemLimitType.QUERY_SIZE_LIMIT, plan, context.relBuilder.literal(context.querySizeLimit));
255262
}
256263

257264
private boolean isCalciteFallbackAllowed() {

docs/user/ppl/cmd/expand.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ expand
1111

1212
Description
1313
============
14-
(From 3.1.0)
14+
| (Experimental)
1515
1616
Use the ``expand`` command on a nested array field to transform a single
1717
document into multiple documents—each containing one element from the array.

docs/user/ppl/index.rst

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,75 @@ The query start with search command and then flowing a set of command delimited
4040

4141
- `Cross-Cluster Search <admin/cross_cluster_search.rst>`_
4242

43+
* **Language Structure**
44+
45+
- `Identifiers <general/identifiers.rst>`_
46+
47+
- `Data Types <general/datatypes.rst>`_
48+
4349
* **Commands**
4450

4551
- `Syntax <cmd/syntax.rst>`_
4652

4753
- `ad command <cmd/ad.rst>`_
4854

55+
- `appendcol command <cmd/appendcol.rst>`_
56+
4957
- `dedup command <cmd/dedup.rst>`_
5058

5159
- `describe command <cmd/describe.rst>`_
5260

53-
- `show datasources command <cmd/showdatasources.rst>`_
54-
5561
- `eval command <cmd/eval.rst>`_
5662

63+
- `eventstats command <cmd/eventstats.rst>`_
64+
65+
- `expand command <cmd/expand.rst>`_
66+
67+
- `explain command <cmd/explain.rst>`_
68+
5769
- `fields command <cmd/fields.rst>`_
5870

71+
- `fillnull command <cmd/fillnull.rst>`_
72+
73+
- `flatten command <cmd/flatten.rst>`_
74+
5975
- `grok command <cmd/grok.rst>`_
6076

77+
- `head command <cmd/head.rst>`_
78+
79+
- `join command <cmd/join.rst>`_
80+
6181
- `kmeans command <cmd/kmeans.rst>`_
6282

83+
- `lookup command <cmd/lookup.rst>`_
84+
85+
- `metadata commands <cmd/information_schema.rst>`_
86+
6387
- `ml command <cmd/ml.rst>`_
6488

6589
- `parse command <cmd/parse.rst>`_
6690

6791
- `patterns command <cmd/patterns.rst>`_
6892

93+
- `rare command <cmd/rare.rst>`_
94+
6995
- `rename command <cmd/rename.rst>`_
7096

7197
- `search command <cmd/search.rst>`_
7298

99+
- `show datasources command <cmd/showdatasources.rst>`_
100+
73101
- `sort command <cmd/sort.rst>`_
74102

75103
- `stats command <cmd/stats.rst>`_
76104

77-
- `trendline command <cmd/trendline.rst>`_
78-
79-
- `where command <cmd/where.rst>`_
80-
81-
- `head command <cmd/head.rst>`_
82-
83-
- `rare command <cmd/rare.rst>`_
105+
- `subquery (aka subsearch) command <cmd/subquery.rst>`_
84106

85107
- `top command <cmd/top.rst>`_
86108

87-
- `metadata commands <cmd/information_schema.rst>`_
88-
89-
- `(Experimental)(From 3.0.0) join command <cmd/join.rst>`_
90-
91-
- `(Experimental)(From 3.0.0) lookup command <cmd/lookup.rst>`_
92-
93-
- `(Experimental)(From 3.0.0) subquery (aka subsearch) command <cmd/subquery.rst>`_
109+
- `trendline command <cmd/trendline.rst>`_
94110

95-
- `(Experimental)(From 3.1.0) eventstats command <cmd/eventstats.rst>`_
111+
- `where command <cmd/where.rst>`_
96112

97113
* **Functions**
98114

@@ -124,12 +140,6 @@ The query start with search command and then flowing a set of command delimited
124140

125141
- `Optimization <../../user/optimization/optimization.rst>`_
126142

127-
* **Language Structure**
128-
129-
- `Identifiers <general/identifiers.rst>`_
130-
131-
- `Data Types <general/datatypes.rst>`_
132-
133143
* **Limitations**
134144

135145
- `Limitations <limitations/limitations.rst>`_

docs/user/ppl/limitations/limitations.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,3 @@ For the following functionalities, the query will be forwarded to the V2 query e
102102
* Kmeans
103103

104104
* Commands with ``fetch_size`` parameter
105-
106-
* Search relevant functions
107-
108-
* match
109-
* match_phrase
110-
* match_bool_prefix
111-
* match_phrase_prefix
112-
* simple_query_string
113-
* query_string
114-
* multi_match

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLExplainIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public void testExplainCommandCost() throws IOException {
8080
? loadFromFile("expectedOutput/calcite/explain_filter_cost_w_pushdown.txt")
8181
: loadFromFile("expectedOutput/calcite/explain_filter_cost_wo_pushdown.txt");
8282
assertTrue(
83-
String.format("Got: %s\n, expected: %s", result, expected), result.contains(expected));
83+
String.format("Got: %s\n, expected: %s", result, expected),
84+
result.contains(expected.trim()));
8485
}
8586

8687
@Test

0 commit comments

Comments
 (0)