Skip to content

Commit 23dc638

Browse files
authored
Bump Calcite to 1.41.0 (#4714)
* Bump Calcite to 1.41.0 Signed-off-by: Lantao Jin <[email protected]> * Workaround the Calcite codegen bug Signed-off-by: Lantao Jin <[email protected]> * upgrade calcite-testkit Signed-off-by: Lantao Jin <[email protected]> --------- Signed-off-by: Lantao Jin <[email protected]>
1 parent 3a42c51 commit 23dc638

File tree

51 files changed

+233
-283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+233
-283
lines changed

api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies {
1515
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
1616
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: "${hamcrest_version}"
1717
testImplementation group: 'org.mockito', name: 'mockito-core', version: "${mockito_version}"
18-
testImplementation group: 'org.apache.calcite', name: 'calcite-testkit', version: '1.38.0'
18+
testImplementation group: 'org.apache.calcite', name: 'calcite-testkit', version: '1.41.0'
1919
}
2020

2121
spotless {

core/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ dependencies {
5858
api group: 'com.google.code.gson', name: 'gson', version: '2.8.9'
5959
api group: 'com.tdunning', name: 't-digest', version: '3.3'
6060
api "net.minidev:json-smart:${versions.json_smart}"
61-
api('org.apache.calcite:calcite-core:1.38.0') {
61+
api('org.apache.calcite:calcite-core:1.41.0') {
6262
exclude group: 'net.minidev', module: 'json-smart'
63-
exclude group: 'commons-lang', module: 'commons-lang'
6463
}
65-
api 'org.apache.calcite:calcite-linq4j:1.38.0'
64+
api 'org.apache.calcite:calcite-linq4j:1.41.0'
6665
api project(':common')
6766
implementation "com.github.seancfoley:ipaddress:5.4.2"
6867
implementation "com.jayway.jsonpath:json-path:2.9.0"
6968

7069
annotationProcessor('org.immutables:value:2.8.8')
71-
compileOnly('org.immutables:value-annotations:2.8.8')
70+
compileOnly 'org.immutables:value-annotations:2.8.8'
71+
compileOnlyApi 'com.google.code.findbugs:jsr305:3.0.2'
7272

73-
testImplementation('org.junit.jupiter:junit-jupiter:5.9.3')
73+
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
7474
testImplementation group: 'org.hamcrest', name: 'hamcrest-library', version: "${hamcrest_version}"
7575
testImplementation group: 'org.mockito', name: 'mockito-core', version: "${mockito_version}"
7676
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: "${mockito_version}"

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
@@ -2614,6 +2617,21 @@ private void buildParseRelNode(Parse node, CalcitePlanContext context) {
26142617
projectPlusOverriding(newFields, groupCandidates, context);
26152618
}
26162619

2620+
/**
2621+
* CALCITE-6981 introduced a stricter type checking for Array type in {@link RexToLixTranslator}.
2622+
* We defined a MAP(VARCHAR, ANY) in {@link UserDefinedFunctionUtils#nullablePatternAggList}, when
2623+
* we convert the value type to ArraySqlType, it will check the source data type by {@link
2624+
* RelDataType#getComponentType()} which will return null due to the source type is ANY.
2625+
*/
2626+
private RexNode explicitMapType(
2627+
CalcitePlanContext context, RexNode origin, SqlTypeName targetType) {
2628+
MapSqlType originalMapType = (MapSqlType) origin.getType();
2629+
ArraySqlType newValueType =
2630+
new ArraySqlType(context.rexBuilder.getTypeFactory().createSqlType(targetType), true);
2631+
MapSqlType newMapType = new MapSqlType(originalMapType.getKeyType(), newValueType, true);
2632+
return new RexInputRef(((RexInputRef) origin).getIndex(), newMapType);
2633+
}
2634+
26172635
private void flattenParsedPattern(
26182636
String originalPatternResultAlias,
26192637
RexNode parsedNode,
@@ -2674,7 +2692,7 @@ private void flattenParsedPattern(
26742692
PPLFuncImpTable.INSTANCE.resolve(
26752693
context.rexBuilder,
26762694
BuiltinFunctionName.INTERNAL_ITEM,
2677-
parsedNode,
2695+
explicitMapType(context, parsedNode, SqlTypeName.VARCHAR),
26782696
context.rexBuilder.makeLiteral(PatternUtils.SAMPLE_LOGS)),
26792697
true,
26802698
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
@@ -178,7 +178,13 @@ public static List<Expression> convertToExprValues(
178178
types.stream().map(OpenSearchTypeFactory::convertRelDataTypeToExprType).toList();
179179
List<Expression> exprValues = new ArrayList<>();
180180
for (int i = 0; i < operands.size(); i++) {
181-
Expression operand = Expressions.convert_(operands.get(i), Object.class);
181+
// TODO a workaround of Apache Calcite bug in 1.41.0:
182+
// If you call Expressions.convert_(expr, Number.class) or
183+
// Expressions.convert_(expr, Object.class),
184+
// you must change to Expressions.convert_(Expressions.box(expr), Number.class/Object.class).
185+
// Because the codegen in Janino.UnitCompiler, "(Object) -1" will be mistakenly treated to
186+
// "Object subtracting one" instead of "type casting on native one".
187+
Expression operand = Expressions.convert_(Expressions.box(operands.get(i)), Object.class);
182188
exprValues.add(
183189
i,
184190
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
@@ -832,6 +832,7 @@ void populate() {
832832
registerOperator(LOG2, SqlLibraryOperators.LOG2);
833833
registerOperator(MD5, SqlLibraryOperators.MD5);
834834
registerOperator(SHA1, SqlLibraryOperators.SHA1);
835+
registerOperator(CRC32, SqlLibraryOperators.CRC32);
835836
registerOperator(INTERNAL_REGEXP_REPLACE_3, SqlLibraryOperators.REGEXP_REPLACE_3);
836837
registerOperator(INTERNAL_REGEXP_REPLACE_PG_4, SqlLibraryOperators.REGEXP_REPLACE_PG_4);
837838
registerOperator(INTERNAL_REGEXP_REPLACE_5, SqlLibraryOperators.REGEXP_REPLACE_5);
@@ -852,7 +853,6 @@ void populate() {
852853
registerOperator(MOD, PPLBuiltinOperators.MOD);
853854
registerOperator(MODULUS, PPLBuiltinOperators.MOD);
854855
registerOperator(MODULUSFUNCTION, PPLBuiltinOperators.MOD);
855-
registerOperator(CRC32, PPLBuiltinOperators.CRC32);
856856
registerDivideFunction(DIVIDE);
857857
registerDivideFunction(DIVIDEFUNCTION);
858858
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)