Skip to content

Commit d7603d3

Browse files
authored
* Bump Calcite to 1.41.0 * Workaround the Calcite codegen bug * upgrade calcite-testkit --------- (cherry picked from commit 23dc638) Signed-off-by: Lantao Jin <ltjin@amazon.com>
1 parent ac44f8b commit d7603d3

51 files changed

Lines changed: 231 additions & 281 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.

api/build.gradle

Whitespace-only changes.

core/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ dependencies {
4747
api group: 'com.google.code.gson', name: 'gson', version: '2.8.9'
4848
api group: 'com.tdunning', name: 't-digest', version: '3.2'
4949
api "net.minidev:json-smart:2.5.2"
50-
api('org.apache.calcite:calcite-core:1.38.0') {
50+
api('org.apache.calcite:calcite-core:1.41.0') {
5151
exclude group: 'net.minidev', module: 'json-smart'
52-
exclude group: 'commons-lang', module: 'commons-lang'
5352
}
54-
api 'org.apache.calcite:calcite-linq4j:1.38.0'
53+
api 'org.apache.calcite:calcite-linq4j:1.41.0'
5554
api project(':common')
5655
implementation "com.github.seancfoley:ipaddress:5.4.2"
5756
implementation "com.jayway.jsonpath:json-path:2.9.0"
5857
implementation "com.googlecode.aviator:aviator:5.4.3"
5958

6059
annotationProcessor('org.immutables:value:2.8.8')
61-
compileOnly('org.immutables:value-annotations:2.8.8')
60+
compileOnly 'org.immutables:value-annotations:2.8.8'
61+
compileOnlyApi 'com.google.code.findbugs:jsr305:3.0.2'
6262

6363
testImplementation('org.junit.jupiter:junit-jupiter:5.9.3')
6464
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: '2.1'

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Set;
4040
import java.util.stream.Collectors;
4141
import java.util.stream.Stream;
42+
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
4243
import org.apache.calcite.plan.RelOptTable;
4344
import org.apache.calcite.plan.ViewExpanders;
4445
import org.apache.calcite.rel.RelNode;
@@ -60,6 +61,8 @@
6061
import org.apache.calcite.rex.RexWindowBounds;
6162
import org.apache.calcite.sql.SqlKind;
6263
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
64+
import org.apache.calcite.sql.type.ArraySqlType;
65+
import org.apache.calcite.sql.type.MapSqlType;
6366
import org.apache.calcite.sql.type.SqlTypeFamily;
6467
import org.apache.calcite.sql.type.SqlTypeName;
6568
import org.apache.calcite.tools.RelBuilder;
@@ -906,7 +909,7 @@ private boolean isCountField(RexCall call) {
906909

907910
/**
908911
* Resolve the aggregation with trimming unused fields to avoid bugs in {@link
909-
* org.apache.calcite.sql2rel.RelDecorrelator#decorrelateRel(Aggregate, boolean)}
912+
* org.apache.calcite.sql2rel.RelDecorrelator#decorrelateRel(Aggregate, boolean, boolean)}
910913
*
911914
* @param groupExprList group by expression list
912915
* @param aggExprList aggregate expression list
@@ -2623,6 +2626,21 @@ private void buildParseRelNode(Parse node, CalcitePlanContext context) {
26232626
projectPlusOverriding(newFields, groupCandidates, context);
26242627
}
26252628

2629+
/**
2630+
* CALCITE-6981 introduced a stricter type checking for Array type in {@link RexToLixTranslator}.
2631+
* We defined a MAP(VARCHAR, ANY) in {@link UserDefinedFunctionUtils#nullablePatternAggList}, when
2632+
* we convert the value type to ArraySqlType, it will check the source data type by {@link
2633+
* RelDataType#getComponentType()} which will return null due to the source type is ANY.
2634+
*/
2635+
private RexNode explicitMapType(
2636+
CalcitePlanContext context, RexNode origin, SqlTypeName targetType) {
2637+
MapSqlType originalMapType = (MapSqlType) origin.getType();
2638+
ArraySqlType newValueType =
2639+
new ArraySqlType(context.rexBuilder.getTypeFactory().createSqlType(targetType), true);
2640+
MapSqlType newMapType = new MapSqlType(originalMapType.getKeyType(), newValueType, true);
2641+
return new RexInputRef(((RexInputRef) origin).getIndex(), newMapType);
2642+
}
2643+
26262644
private void flattenParsedPattern(
26272645
String originalPatternResultAlias,
26282646
RexNode parsedNode,
@@ -2683,7 +2701,7 @@ private void flattenParsedPattern(
26832701
PPLFuncImpTable.INSTANCE.resolve(
26842702
context.rexBuilder,
26852703
BuiltinFunctionName.INTERNAL_ITEM,
2686-
parsedNode,
2704+
explicitMapType(context, parsedNode, SqlTypeName.VARCHAR),
26872705
context.rexBuilder.makeLiteral(PatternUtils.SAMPLE_LOGS)),
26882706
true,
26892707
true);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ public static List<Expression> convertToExprValues(
187187
types.stream().map(OpenSearchTypeFactory::convertRelDataTypeToExprType).collect(Collectors.toList());
188188
List<Expression> exprValues = new ArrayList<>();
189189
for (int i = 0; i < operands.size(); i++) {
190-
Expression operand = Expressions.convert_(operands.get(i), Object.class);
190+
// TODO a workaround of Apache Calcite bug in 1.41.0:
191+
// If you call Expressions.convert_(expr, Number.class) or
192+
// Expressions.convert_(expr, Object.class),
193+
// you must change to Expressions.convert_(Expressions.box(expr), Number.class/Object.class).
194+
// Because the codegen in Janino.UnitCompiler, "(Object) -1" will be mistakenly treated to
195+
// "Object subtracting one" instead of "type casting on native one".
196+
Expression operand = Expressions.convert_(Expressions.box(operands.get(i)), Object.class);
191197
exprValues.add(
192198
i,
193199
Expressions.call(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void explainWithCalcite(
156156
} else {
157157
if (t instanceof Error) {
158158
// Calcite may throw AssertError during query execution.
159-
listener.onFailure(new CalciteUnsupportedException(t.getMessage()));
159+
listener.onFailure(new CalciteUnsupportedException(t.getMessage(), t));
160160
} else {
161161
listener.onFailure((Exception) t);
162162
}

core/src/main/java/org/opensearch/sql/expression/function/PPLBuiltinOperators.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import org.opensearch.sql.expression.function.udf.ip.CidrMatchFunction;
9595
import org.opensearch.sql.expression.function.udf.ip.CompareIpFunction;
9696
import org.opensearch.sql.expression.function.udf.ip.IPFunction;
97-
import org.opensearch.sql.expression.function.udf.math.CRC32Function;
9897
import org.opensearch.sql.expression.function.udf.math.ConvFunction;
9998
import org.opensearch.sql.expression.function.udf.math.DivideFunction;
10099
import org.opensearch.sql.expression.function.udf.math.EulerFunction;
@@ -128,7 +127,6 @@ public class PPLBuiltinOperators extends ReflectiveSqlOperatorTable {
128127
public static final SqlOperator E = new EulerFunction().toUDF("E");
129128
public static final SqlOperator CONV = new ConvFunction().toUDF("CONVERT");
130129
public static final SqlOperator MOD = new ModFunction().toUDF("MOD");
131-
public static final SqlOperator CRC32 = new CRC32Function().toUDF("CRC32");
132130
public static final SqlOperator DIVIDE = new DivideFunction().toUDF("DIVIDE");
133131
public static final SqlOperator SHA2 = CryptographicFunction.sha2().toUDF("SHA2");
134132
public static final SqlOperator CIDRMATCH = new CidrMatchFunction().toUDF("CIDRMATCH");

core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ void populate() {
841841
registerOperator(LOG2, SqlLibraryOperators.LOG2);
842842
registerOperator(MD5, SqlLibraryOperators.MD5);
843843
registerOperator(SHA1, SqlLibraryOperators.SHA1);
844+
registerOperator(CRC32, SqlLibraryOperators.CRC32);
844845
registerOperator(INTERNAL_REGEXP_REPLACE_3, SqlLibraryOperators.REGEXP_REPLACE_3);
845846
registerOperator(INTERNAL_REGEXP_REPLACE_PG_4, SqlLibraryOperators.REGEXP_REPLACE_PG_4);
846847
registerOperator(INTERNAL_REGEXP_REPLACE_5, SqlLibraryOperators.REGEXP_REPLACE_5);
@@ -861,7 +862,6 @@ void populate() {
861862
registerOperator(MOD, PPLBuiltinOperators.MOD);
862863
registerOperator(MODULUS, PPLBuiltinOperators.MOD);
863864
registerOperator(MODULUSFUNCTION, PPLBuiltinOperators.MOD);
864-
registerOperator(CRC32, PPLBuiltinOperators.CRC32);
865865
registerDivideFunction(DIVIDE);
866866
registerDivideFunction(DIVIDEFUNCTION);
867867
registerOperator(SHA2, PPLBuiltinOperators.SHA2);

core/src/main/java/org/opensearch/sql/expression/function/udf/binning/MinspanBucketFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ public Expression implement(
6464
return Expressions.call(
6565
MinspanBucketImplementor.class,
6666
"calculateMinspanBucket",
67-
Expressions.convert_(fieldValue, Number.class),
68-
Expressions.convert_(minSpan, Number.class),
69-
Expressions.convert_(dataRange, Number.class),
70-
Expressions.convert_(maxValue, Number.class));
67+
Expressions.convert_(Expressions.box(fieldValue), Number.class),
68+
Expressions.convert_(Expressions.box(minSpan), Number.class),
69+
Expressions.convert_(Expressions.box(dataRange), Number.class),
70+
Expressions.convert_(Expressions.box(maxValue), Number.class));
7171
}
7272

7373
/** Minspan bucket calculation. */

core/src/main/java/org/opensearch/sql/expression/function/udf/binning/RangeBucketFunction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public Expression implement(
6969
return Expressions.call(
7070
RangeBucketImplementor.class,
7171
"calculateRangeBucket",
72-
Expressions.convert_(fieldValue, Number.class),
73-
Expressions.convert_(dataMin, Number.class),
74-
Expressions.convert_(dataMax, Number.class),
75-
Expressions.convert_(startParam, Number.class),
76-
Expressions.convert_(endParam, Number.class));
72+
Expressions.convert_(Expressions.box(fieldValue), Number.class),
73+
Expressions.convert_(Expressions.box(dataMin), Number.class),
74+
Expressions.convert_(Expressions.box(dataMax), Number.class),
75+
Expressions.convert_(Expressions.box(startParam), Number.class),
76+
Expressions.convert_(Expressions.box(endParam), Number.class));
7777
}
7878

7979
/** Range bucket calculation with expansion algorithm and magnitude-based width. */

core/src/main/java/org/opensearch/sql/expression/function/udf/binning/SpanBucketFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public Expression implement(
6060
return Expressions.call(
6161
SpanBucketImplementor.class,
6262
"calculateSpanBucket",
63-
Expressions.convert_(fieldValue, Number.class),
64-
Expressions.convert_(spanValue, Number.class));
63+
Expressions.convert_(Expressions.box(fieldValue), Number.class),
64+
Expressions.convert_(Expressions.box(spanValue), Number.class));
6565
}
6666

6767
/** Span bucket calculation. */

0 commit comments

Comments
 (0)