Skip to content

Commit 6a30697

Browse files
committed
Pass on bucket_nullable flag to sql node and back to rel node (1929/2066)
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent bab48d7 commit 6a30697

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/validate/converters/PplRelToSqlNodeConverter.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@
55

66
package org.opensearch.sql.calcite.validate.converters;
77

8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
12+
import org.apache.calcite.rel.core.Aggregate;
813
import org.apache.calcite.rel.core.Correlate;
914
import org.apache.calcite.rel.core.Join;
1015
import org.apache.calcite.rel.core.JoinRelType;
16+
import org.apache.calcite.rel.hint.RelHint;
1117
import org.apache.calcite.rel.rel2sql.RelToSqlConverter;
1218
import org.apache.calcite.sql.JoinConditionType;
1319
import org.apache.calcite.sql.JoinType;
1420
import org.apache.calcite.sql.SqlDialect;
21+
import org.apache.calcite.sql.SqlHint;
22+
import org.apache.calcite.sql.SqlIdentifier;
1523
import org.apache.calcite.sql.SqlJoin;
1624
import org.apache.calcite.sql.SqlLiteral;
1725
import org.apache.calcite.sql.SqlNode;
26+
import org.apache.calcite.sql.SqlNodeList;
27+
import org.apache.calcite.sql.parser.SqlParserPos;
1828

1929
/**
2030
* An extension of {@link RelToSqlConverter} to convert a relation algebra tree, translated from a
@@ -107,4 +117,56 @@ protected Result visitAntiOrSemiJoin(Join e) {
107117

108118
return result(join, leftResult, rightResult);
109119
}
120+
121+
@Override
122+
public Result visit(Aggregate e) {
123+
Result r = super.visit(e);
124+
if (!e.getHints().isEmpty()) {
125+
List<SqlNode> hints =
126+
e.getHints().stream()
127+
.map(relHint -> (SqlNode) toSqlHint(relHint, POS))
128+
.collect(Collectors.toCollection(ArrayList::new));
129+
r.asSelect().setHints(SqlNodeList.of(POS, hints));
130+
}
131+
return r;
132+
}
133+
134+
/**
135+
* Converts a RelHint to a SqlHint.
136+
*
137+
* <p>Copied from {@link RelToSqlConverter#toSqlHint(RelHint, SqlParserPos)} (as Calcite 1.41) as
138+
* it is private there
139+
*/
140+
private static SqlHint toSqlHint(RelHint hint, SqlParserPos pos) {
141+
if (hint.kvOptions != null) {
142+
return new SqlHint(
143+
pos,
144+
new SqlIdentifier(hint.hintName, pos),
145+
SqlNodeList.of(
146+
pos,
147+
hint.kvOptions.entrySet().stream()
148+
.flatMap(
149+
e ->
150+
Stream.of(
151+
new SqlIdentifier(e.getKey(), pos),
152+
SqlLiteral.createCharString(e.getValue(), pos)))
153+
.collect(Collectors.toList())),
154+
SqlHint.HintOptionFormat.KV_LIST);
155+
} else if (hint.listOptions != null) {
156+
return new SqlHint(
157+
pos,
158+
new SqlIdentifier(hint.hintName, pos),
159+
SqlNodeList.of(
160+
pos,
161+
hint.listOptions.stream()
162+
.map(e -> SqlLiteral.createCharString(e, pos))
163+
.collect(Collectors.toList())),
164+
SqlHint.HintOptionFormat.LITERAL_LIST);
165+
}
166+
return new SqlHint(
167+
pos,
168+
new SqlIdentifier(hint.hintName, pos),
169+
SqlNodeList.EMPTY,
170+
SqlHint.HintOptionFormat.EMPTY);
171+
}
110172
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.opensearch.sql.calcite.SysLimit;
4747
import org.opensearch.sql.calcite.plan.LogicalSystemLimit;
4848
import org.opensearch.sql.calcite.plan.LogicalSystemLimit.SystemLimitType;
49+
import org.opensearch.sql.calcite.utils.PPLHintStrategyTable;
4950
import org.opensearch.sql.calcite.validate.OpenSearchSparkSqlDialect;
5051
import org.opensearch.sql.calcite.validate.PplConvertletTable;
5152
import org.opensearch.sql.calcite.validate.ValidationUtils;
@@ -328,12 +329,15 @@ private RelNode validate(RelNode relNode, CalcitePlanContext context) {
328329

329330
// 1. Do not remove sort in subqueries so that the orders for queries like `... | sort a |
330331
// fields b` is preserved
331-
// 2. Disable automatic JSON_TYPE_OPERATOR wrapping for nested JSON functions
332-
// (See CALCITE-4989: Calcite wraps nested JSON functions with JSON_TYPE by default)
332+
// 2. Disable automatic JSON_TYPE_OPERATOR wrapping for nested JSON functions.
333+
// See CALCITE-4989: Calcite wraps nested JSON functions with JSON_TYPE by default
334+
// 3. Set hint strategy so that hints can be properly propagated.
335+
// See SqlToRelConverter.java#convertSelectImpl
333336
SqlToRelConverter.Config sql2relConfig =
334337
SqlToRelConverter.config()
335338
.withRemoveSortInSubQuery(false)
336-
.withAddJsonTypeOperatorEnabled(false);
339+
.withAddJsonTypeOperatorEnabled(false)
340+
.withHintStrategyTable(PPLHintStrategyTable.getHintStrategyTable());
337341
SqlToRelConverter sql2rel =
338342
new PplSqlToRelConverter(
339343
context.config.getViewExpander(),

0 commit comments

Comments
 (0)