This is an automated email from the ASF dual-hosted git repository.
yashmayya pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new f5641918e36 Fix MSE literal folding regressions for INT values beyond
the INT range, arrays and timestamp casts (#20277)
f5641918e36 is described below
commit f5641918e36d65f2e981353dfa490417008736d8
Author: Yash Mayya <[email protected]>
AuthorDate: Wed Nov 18 00:24:11 2026 +0530
Fix MSE literal folding regressions for INT values beyond the INT range,
arrays and timestamp casts (#20277)
---
.../rel/rules/PinotEvaluateLiteralRule.java | 112 +++++++++++++++++++--
.../resources/queries/LiteralEvaluationPlans.json | 92 +++++++++++++++++
.../test/resources/queries/LiteralExpressions.json | 90 ++++++++++++++++-
3 files changed, 287 insertions(+), 7 deletions(-)
diff --git
a/pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotEvaluateLiteralRule.java
b/pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotEvaluateLiteralRule.java
index 270550cc473..7ed72d243a1 100644
---
a/pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotEvaluateLiteralRule.java
+++
b/pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotEvaluateLiteralRule.java
@@ -67,6 +67,8 @@ import org.apache.pinot.sql.parsers.SqlCompilationException;
/// The function gets each literal as the Java value that the runtime passes
for it, so that the result is the same as
/// the result of the call at runtime.
public class PinotEvaluateLiteralRule {
+ private static final BigDecimal MIN_INT =
BigDecimal.valueOf(Integer.MIN_VALUE);
+ private static final BigDecimal MAX_INT =
BigDecimal.valueOf(Integer.MAX_VALUE);
public static class Project extends RelOptRule {
public static final Project INSTANCE = new
Project(PinotRuleUtils.PINOT_REL_FACTORY, null);
@@ -190,9 +192,11 @@ public class PinotEvaluateLiteralRule {
return rexCall;
}
argumentTypes[i] =
RelToPlanNodeConverter.convertToColumnDataType(rexNode.getType());
- if (TransformFunctionType.isJsonExtractScalar(canonicalName) && i == 3) {
- argumentTypes[i] =
RexExpressionUtils.fromJsonExtractScalarDefaultLiteral((RexLiteral)
rexNode).getDataType();
- }
+ }
+ widenIntArgumentTypes(operands, argumentTypes);
+ if (TransformFunctionType.isJsonExtractScalar(canonicalName) &&
numArguments > 3) {
+ argumentTypes[3] =
+ RexExpressionUtils.fromJsonExtractScalarDefaultLiteral((RexLiteral)
operands.get(3)).getDataType();
}
if (isDatetimeIntervalArithmetic(rexCall)) {
@@ -201,13 +205,13 @@ public class PinotEvaluateLiteralRule {
if (isIntervalArithmetic(rexCall)) {
return evaluateIntervalArithmetic(rexCall, rexBuilder);
}
- String castTargetType = null;
+ ColumnDataType castTargetType = null;
if (rexCall.getKind() == SqlKind.CAST) {
// Handle separately because the CAST operator only has one operand (the
value to be cast) and the type to be cast
// to is determined by the operator's return type. Pinot's CAST function
implementation requires two arguments:
// the value to be cast and the target type.
argumentTypes = new ColumnDataType[]{argumentTypes[0],
ColumnDataType.STRING};
- castTargetType =
RelToPlanNodeConverter.convertToColumnDataType(rexCall.getType()).name();
+ castTargetType =
RelToPlanNodeConverter.convertToColumnDataType(rexCall.getType());
}
FunctionInfo functionInfo =
FunctionRegistry.lookupFunctionInfo(canonicalName, argumentTypes);
if (functionInfo == null || !functionInfo.isDeterministic()) {
@@ -227,7 +231,11 @@ public class PinotEvaluateLiteralRule {
}
}
if (castTargetType != null) {
- arguments[1] = castTargetType;
+ arguments[0] = getCastValue(arguments[0], castTargetType);
+ arguments[1] = castTargetType.name();
+ }
+ if (rexCall.getKind() == SqlKind.ARRAY_VALUE_CONSTRUCTOR) {
+ keepExactArrayElements(operands, arguments);
}
RelDataType rexNodeType = rexCall.getType();
if (rexNodeType.getSqlTypeName() == SqlTypeName.DECIMAL) {
@@ -426,6 +434,71 @@ public class PinotEvaluateLiteralRule {
}
}
+ /// Makes the INT arguments of a call LONG if one of them is an integer
literal beyond the INT range.
+ ///
+ /// This rule makes such a literal. Calcite gives INT arithmetic the type
INTEGER, but Pinot evaluates it as LONG, so
+ /// the literal for `3600000 * 100000` has the type INTEGER and the value
360000000000. A call on that literal (for
+ /// example `NOW() - 3600000 * 100000`) must get the full value, not the
value that wraps around as an INT. All the
+ /// INT arguments change, so that the arguments of one type (for example the
elements of an array) keep one Java
+ /// class.
+ private static void widenIntArgumentTypes(List<RexNode> operands,
ColumnDataType[] argumentTypes) {
+ int numArguments = argumentTypes.length;
+ boolean beyondIntRange = false;
+ for (int i = 0; i < numArguments; i++) {
+ if (isIntegerLiteral((RexLiteral) operands.get(i), argumentTypes[i])) {
+ BigDecimal value = (BigDecimal) ((RexLiteral)
operands.get(i)).getValue();
+ if (value.compareTo(MIN_INT) < 0 || value.compareTo(MAX_INT) > 0) {
+ beyondIntRange = true;
+ break;
+ }
+ }
+ }
+ if (!beyondIntRange) {
+ return;
+ }
+ for (int i = 0; i < numArguments; i++) {
+ if (isIntegerLiteral((RexLiteral) operands.get(i), argumentTypes[i])) {
+ argumentTypes[i] = ColumnDataType.LONG;
+ }
+ }
+ }
+
+ /// Returns whether the literal is a number with an integer type (for
example INTEGER or SMALLINT) that the planner
+ /// maps to INT. A DECIMAL is not an integer type here, see
[#canExceedArgumentType]. A NULL literal is not a number.
+ /// A literal that is not a value (for example the Sarg of a SEARCH on
INTEGER values) is not a number.
+ private static boolean isIntegerLiteral(RexLiteral rexLiteral,
ColumnDataType literalType) {
+ return literalType == ColumnDataType.INT && rexLiteral.getValue()
instanceof BigDecimal
+ &&
SqlTypeName.INT_TYPES.contains(rexLiteral.getType().getSqlTypeName());
+ }
+
+ /// Gives the number elements of an array constructor their exact values
(the `BigDecimal` that Calcite stores) if
+ /// one element is NULL or if the elements do not have one Java class.
+ ///
+ /// The array constructor makes a primitive array from the class of its
first element, so it cannot take a NULL
+ /// element or an element of a different class. Elements of one array type
can have different classes: this rule
+ /// gives the literal that it makes for a DECIMAL(20, 0) call the type
BIGINT (a `Long`), but a DECIMAL(20, 0)
+ /// literal from Calcite keeps its exact value (see
[#canExceedArgumentType]).
+ private static void keepExactArrayElements(List<RexNode> operands, Object[]
arguments) {
+ Class<?> elementClass = null;
+ boolean oneClass = true;
+ for (Object argument : arguments) {
+ if (argument == null || (elementClass != null && argument.getClass() !=
elementClass)) {
+ oneClass = false;
+ break;
+ }
+ elementClass = argument.getClass();
+ }
+ if (oneClass) {
+ return;
+ }
+ for (int i = 0; i < arguments.length; i++) {
+ Object value = ((RexLiteral) operands.get(i)).getValue();
+ if (value instanceof BigDecimal) {
+ arguments[i] = value;
+ }
+ }
+ }
+
/// Returns the value that the runtime passes to a function for the literal
(see `FunctionOperand`): the value from
/// [RexExpressionUtils#fromRexLiteral], converted with
[ColumnDataType#toExternal]. For example, an INTEGER literal
/// becomes an `Integer` (Calcite stores a `BigDecimal`), and a TIMESTAMP
literal becomes a `Timestamp`.
@@ -435,6 +508,12 @@ public class PinotEvaluateLiteralRule {
// The runtime cannot represent this literal (for example an INTERVAL),
so keep the value that Calcite stores
return rexLiteral.getValue();
}
+ if (argumentType == ColumnDataType.LONG && isIntegerLiteral(rexLiteral,
+ RelToPlanNodeConverter.convertToColumnDataType(rexLiteral.getType())))
{
+ // See widenIntArgumentTypes(). The value comes from an INT literal or
from a LONG result, so it is in the LONG
+ // range.
+ return ((BigDecimal) rexLiteral.getValue()).longValue();
+ }
if (canExceedArgumentType(rexLiteral, argumentType)) {
// Keep the exact value, which the conversion to INT or LONG can wrap
around. A value that fits (for example
// CAST(5 AS DECIMAL(10, 0))) then stays a BigDecimal, unlike at
runtime, but no value wraps around.
@@ -445,6 +524,27 @@ public class PinotEvaluateLiteralRule {
return value != null ? literal.getDataType().toExternal(value) : null;
}
+ /// Returns the value to pass to the CAST function for a CAST to the given
type.
+ ///
+ /// The CAST function cannot convert between TIMESTAMP and INT or FLOAT (the
leaf stage can). For such a CAST, returns
+ /// the same value with a type that the function can convert: a LONG for an
INT or a TIMESTAMP, and a DOUBLE for a
+ /// FLOAT.
+ @Nullable
+ private static Object getCastValue(@Nullable Object value, ColumnDataType
targetType) {
+ if (targetType == ColumnDataType.TIMESTAMP) {
+ if (value instanceof Integer) {
+ return ((Integer) value).longValue();
+ }
+ if (value instanceof Float) {
+ return ((Float) value).doubleValue();
+ }
+ } else if (value instanceof Timestamp && (targetType == ColumnDataType.INT
+ || targetType == ColumnDataType.FLOAT)) {
+ return ((Timestamp) value).getTime();
+ }
+ return value;
+ }
+
/// Returns whether the literal is a DECIMAL whose type can hold numbers
beyond the range of its INT or LONG argument
/// type. The planner gives DECIMAL(p, 0) the type INT or LONG by its
precision only, for example INT to
/// DECIMAL(10, 0), and LONG to an integer literal beyond the BIGINT range.
The decision depends on the type, not on
diff --git
a/pinot-query-planner/src/test/resources/queries/LiteralEvaluationPlans.json
b/pinot-query-planner/src/test/resources/queries/LiteralEvaluationPlans.json
index 31184cc8e72..be3a586ff98 100644
--- a/pinot-query-planner/src/test/resources/queries/LiteralEvaluationPlans.json
+++ b/pinot-query-planner/src/test/resources/queries/LiteralEvaluationPlans.json
@@ -310,6 +310,98 @@
"\n"
]
},
+ {
+ "description": "INT arithmetic with a result beyond the INT range,
which Pinot evaluates as LONG, keeps its value as the argument of BIGINT
arithmetic",
+ "sql": "EXPLAIN PLAN FOR SELECT CAST(1 AS BIGINT) + 1000 * 60 * 60 *
100000, 1000 * 60 * 60 * 100000 * 2 FROM a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[360000000001:BIGINT],
EXPR$1=[720000000000])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "INT arithmetic with a result beyond the INT range
keeps its value in a filter that is evaluated to TRUE",
+ "sql": "EXPLAIN PLAN FOR SELECT col1 FROM a WHERE 1500000000000 - 1000
* 60 * 60 * 100000 < 1200000000000",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(col1=[$0])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "INT arithmetic with a result beyond the INT range
keeps its value as the argument of CAST and ABS, and as an array element with
an INT literal",
+ "sql": "EXPLAIN PLAN FOR SELECT CAST(1000 * 60 * 60 * 100000 AS
VARCHAR), ABS(-1000 * 60 * 60 * 100000), ARRAY[1000 * 60 * 60 * 100000, 1] FROM
a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[_UTF-8'360000000000':VARCHAR CHARACTER SET
\"UTF-8\"], EXPR$1=[360000000000], EXPR$2=[ARRAY(360000000000, 1)])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "INT arithmetic at the limits of the INT range",
+ "sql": "EXPLAIN PLAN FOR SELECT 2147483647 + 1 - 1, -2147483648 - 1 +
1, 2147483647 + 0 FROM a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[2147483647], EXPR$1=[-2147483648],
EXPR$2=[2147483647])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "an evaluated INT literal with IN on INT literals,
which becomes a SEARCH whose Sarg literal has the type INTEGER",
+ "sql": "EXPLAIN PLAN FOR SELECT col1 FROM a WHERE LENGTH('abc') IN (1,
2, 3)",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(col1=[$0])",
+ "\n LogicalFilter(condition=[SEARCH(3, Sarg[1, 2, 3])])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "array literal with a NULL element",
+ "sql": "EXPLAIN PLAN FOR SELECT ARRAY[1, NULL], ARRAY[CAST(1 AS
BIGINT), NULL], ARRAY[1.5, NULL], ARRAY[1000 * 60 * 60 * 100000, CAST(NULL AS
INT)] FROM a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[ARRAY(1, null:INTEGER)],
EXPR$1=[ARRAY(1:BIGINT, null:BIGINT)], EXPR$2=[ARRAY(1.5:DECIMAL(2, 1),
null:DECIMAL(2, 1))], EXPR$3=[ARRAY(360000000000, null:INTEGER)])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "array literal of an evaluated integer and a DECIMAL
literal that keeps its exact value, which share a DECIMAL type",
+ "sql": "EXPLAIN PLAN FOR SELECT ARRAY[1 + 1, CAST(5 AS DECIMAL(10,
0))], ARRAY[99999999999999999999, 1 + 1] FROM a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[ARRAY(2, 5:DECIMAL(10, 0))],
EXPR$1=[ARRAY(99999999999999999999:DECIMAL(20, 0), 2:BIGINT)])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "CAST between TIMESTAMP and INT or REAL of an evaluated
literal",
+ "sql": "EXPLAIN PLAN FOR SELECT CAST(86400 * 1000 AS TIMESTAMP),
CAST(CAST(1.5 AS REAL) + 1 AS TIMESTAMP), CAST(fromDateTime('1970-01-02',
'yyyy-MM-dd') AS INT), CAST(fromDateTime('1970-01-02', 'yyyy-MM-dd') AS REAL)
FROM a",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(EXPR$0=[1970-01-02 00:00:00:TIMESTAMP(3)],
EXPR$1=[1970-01-01 00:00:00.002:TIMESTAMP(3)], EXPR$2=[86400000],
EXPR$3=[8.64E7:REAL])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
+ {
+ "description": "filter that compares a TIMESTAMP column with INT
arithmetic, which is evaluated and cast to TIMESTAMP",
+ "sql": "EXPLAIN PLAN FOR SELECT col1 FROM a WHERE ts_timestamp > 86400
* 1000",
+ "output": [
+ "Execution Plan",
+ "\nLogicalProject(col1=[$0])",
+ "\n LogicalFilter(condition=[>($8, 1970-01-02 00:00:00)])",
+ "\n PinotLogicalTableScan(table=[[default, a]])",
+ "\n"
+ ]
+ },
{
"description": "a DECIMAL literal with a scale is evaluated as a
DOUBLE value, as at runtime, so jsonFormat drops the trailing zero",
"sql": "EXPLAIN PLAN FOR SELECT jsonFormat(1.50) FROM a",
diff --git
a/pinot-query-runtime/src/test/resources/queries/LiteralExpressions.json
b/pinot-query-runtime/src/test/resources/queries/LiteralExpressions.json
index e41ed0ffdcc..05cc223b478 100644
--- a/pinot-query-runtime/src/test/resources/queries/LiteralExpressions.json
+++ b/pinot-query-runtime/src/test/resources/queries/LiteralExpressions.json
@@ -69,5 +69,93 @@
"h2Sql": "SELECT doubleCol, doubleCol = 1.5 FROM (SELECT doubleCol
FROM {tbl} GROUP BY doubleCol) t"
}
]
+ },
+ "literal_int_arithmetic_beyond_int_range": {
+ "comments": "Calcite gives INT arithmetic the type INTEGER, but Pinot
evaluates it as LONG. The planner must pass the full result to the call around
it, not the value that wraps around as an INT.",
+ "tables": {
+ "tbl": {
+ "schema": [
+ {"name": "intCol", "type": "INT"}
+ ],
+ "inputs": [
+ [1],
+ [5]
+ ]
+ }
+ },
+ "queries": [
+ {
+ "description": "INT product beyond the INT range as the argument of
BIGINT arithmetic",
+ "sql": "SELECT CAST(1 AS BIGINT) + 1000 * 60 * 60 * 100000 FROM {tbl}
LIMIT 1",
+ "outputs": [[360000000001]]
+ },
+ {
+ "description": "INT product beyond the INT range in a filter on
literals",
+ "sql": "SELECT intCol FROM {tbl} WHERE 1500000000000 - 1000 * 60 * 60
* 100000 < 1200000000000 ORDER BY intCol",
+ "outputs": [[1], [5]]
+ },
+ {
+ "description": "INT product beyond the INT range subtracted from
NOW(), which must give a time in the past",
+ "sql": "SELECT intCol FROM {tbl} WHERE NOW() - 1000 * 60 * 60 * 100000
< NOW() ORDER BY intCol",
+ "outputs": [[1], [5]]
+ },
+ {
+ "description": "evaluated INT literal with IN on INT literals, which
becomes a SEARCH with only literal operands",
+ "sql": "SELECT intCol FROM {tbl} WHERE LENGTH('abc') IN (1, 2, 3)
ORDER BY intCol",
+ "outputs": [[1], [5]]
+ }
+ ]
+ },
+ "literal_array_elements": {
+ "comments": "The planner evaluates an array constructor on literals. It
must accept a NULL element, and elements that the planner gives different Java
classes.",
+ "tables": {
+ "tbl": {
+ "schema": [
+ {"name": "intCol", "type": "INT"}
+ ],
+ "inputs": [
+ [1]
+ ]
+ }
+ },
+ "queries": [
+ {
+ "description": "array literal with a NULL element, which becomes the
default value",
+ "sql": "SELECT ARRAY[1, NULL], ARRAY[1 + 1, 3, CAST(NULL AS INT)] FROM
{tbl}",
+ "outputs": [[[1, 0], [2, 3, 0]]]
+ },
+ {
+ "description": "array literal of an evaluated integer and a
DECIMAL(10, 0) literal",
+ "sql": "SELECT ARRAY[1 + 1, CAST(5 AS DECIMAL(10, 0))], ARRAY[CAST(5
AS DECIMAL(10, 0)), 1 + 1] FROM {tbl}",
+ "outputs": [[[2, 5], [5, 2]]]
+ }
+ ]
+ },
+ "literal_cast_between_timestamp_and_int": {
+ "comments": "The planner evaluates a CAST on a literal. It must also
evaluate a CAST between TIMESTAMP and INT, as the leaf stage does.",
+ "tables": {
+ "tbl": {
+ "schema": [
+ {"name": "intCol", "type": "INT"},
+ {"name": "tsCol", "type": "TIMESTAMP"}
+ ],
+ "inputs": [
+ [1, "2020-01-01 00:00:00"],
+ [5, "1970-01-01 00:00:00"]
+ ]
+ }
+ },
+ "queries": [
+ {
+ "description": "evaluated INT literal cast to TIMESTAMP, and evaluated
TIMESTAMP literal cast to INT",
+ "sql": "SELECT CAST(CAST(86400 * 1000 AS TIMESTAMP) AS BIGINT),
CAST(fromDateTime('1970-01-02', 'yyyy-MM-dd') AS INT) FROM {tbl} LIMIT 1",
+ "outputs": [[86400000, 86400000]]
+ },
+ {
+ "description": "TIMESTAMP column compared with INT arithmetic, which
is evaluated and cast to TIMESTAMP",
+ "sql": "SELECT intCol FROM {tbl} WHERE tsCol > 86400 * 1000",
+ "outputs": [[1]]
+ }
+ ]
}
-}
\ No newline at end of file
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]