Repository navigation
Conversation
A multi-value IN list on an INT32 or INT64 field converted its literals without the range and decimal handling that "=" already has. Out-of-range integers were narrowed with an unchecked int cast and matched unrelated rows, and decimal literals were cast to LongLiteral, which failed with a ClassCastException. Both the pushed-down filter and the projection evaluator were affected, and "x = a OR x = b" hit the same code after it is normalized into IN. Convert each literal through a shared helper that keeps integers inside the column range and integral decimals, and drops literals that can never match. An impossible list becomes an empty IN set, so NOT IN still excludes NULL rows. Closes apache#19276
JackieTien97
left a comment
There was a problem hiding this comment.
I ran the focused unit tests and the standalone table integration suite on this revision. All 27 tests passed. Additional SQL checks exposed two wrong-result cases that need to be addressed before merge.
An IN list on an INT32/INT64 field whose literals were all dropped produced an empty TsFile ValueIn. Its statistics shortcut reports allSatisfy() for an empty candidate set on a page whose column is entirely NULL, so once the data was flushed the pushed-down filter returned those NULL rows. Return FalseLiteralFilter instead, which is what "=" already does for a literal that can never match. NOT IN is unaffected because a NotExpression is never pushed into the scan and keeps excluding NULL rows in the column transformer. The pushed-down "=" also accepted a double literal of exactly 2^63 for an INT64 column, because (double) Long.MAX_VALUE rounds up to 2^63 and the cast back to long saturated onto Long.MAX_VALUE. The optimizer deduplicates an IN list and rewrites a single remaining value to "=", so the same literal matched in "IN (2^63, 2^63)" while "IN (2^63, 0)" correctly missed. Compare against 0x1p63 so the bound is exact.
The optimizer deduplicates "l IN (9223372036854775808.0, 9223372036854775808.0)" into "l = 9223372036854775808.0". CompareBinaryColumnTransformer evaluated every numeric comparison through getDouble, and (double) Long.MAX_VALUE rounds up to 2^63, so the projection returned true for Long.MAX_VALUE. When one side is INT64 or TIMESTAMP, compare integer pairs as longs and integer/floating-point pairs with an exact long-to-double comparison. Other numeric pairs are exact as doubles and keep the existing path.
|
Hi @JackieTien97 just following up, I've addressed your latest feedback and added the requested regression coverage. Could you please take another look when you get a chance |
JackieTien97
left a comment
There was a problem hiding this comment.
Thanks for addressing the previous feedback—the core literal-conversion logic, the flushed all-NULL FalseLiteralFilter fix, and the exact compareLongToDouble math look sound.
Before merge, please address the following items (details inline):
- Avoid per-row type-dispatch regression in
CompareBinaryColumnTransformer: hoist the comparison mode out of the per-row hot loop into the constructor and switch outside the loop indoTransform(and remove the redundantgetLonghelper). - Align constant folding: reuse
compareLongToDoubleinIrExpressionInterpreter.evaluateNumericComparisonso constant-folded queries likeSELECT 9223372036854775807 IN (9223372036854775808.0, 0)agree with runtime projections. - Test coverage & small cleanups: expand
ArithmeticAndCompareBinaryColumnTransformerTestfor negative fractions /NaN/Infinity/FLOAT/INT32/TIMESTAMP/ selection, add an argument check inTypeServices.getIntegralInListValue, and update the PR description (it still says projected comparisons are left for a separate change and omitsCompareBinaryColumnTransformer).
Related out-of-scope issues noticed during this review have been filed separately so this PR stays focused:
- #19435: Non-literal expressions in multi-value
IN(In*MultiColumnTransformerisNull(i)bug and numeric narrowing/truncation) - #19436: Multi-value
INonTIMESTAMP,FLOAT, andDOUBLEcolumns (andGenericLiteralitems) - #19437:
NULLliteral in multi-valueINthree-valued logic - #19438:
BETWEENonINT32/INT64fields - #19439:
NaNcomparison inconsistencies between pushdown, projection, andNOT IN
CompareBinaryColumnTransformer now picks its comparison mode once in the constructor from the operand types and dispatches on it outside the row loop in both doTransform overloads, so INT32/FLOAT/DOUBLE comparisons no longer pay per-row type checks. The INT32 getLong helper is dropped since IntType.getLong already widens. compareLongToDouble moves to CommonTransformUtils and is reused by IrExpressionInterpreter.evaluateNumericComparison, so a constant-folded "9223372036854775807 IN (9223372036854775808.0, 0)" returns false like the column projection does. getIntegralInListValue rejects types other than INT32/INT64, and an IN list reduced to one value after dropping impossible literals is pushed down as "=". The NotExpression assertions on a pushed-down filter are removed from TablePredicateConversionTest because NOT IN never reaches that path; the column transformer tests cover negative fractions, signed zero, NaN, infinities, the largest double below 2^63, FLOAT, INT32, TIMESTAMP, every operator, and evaluation with a selection.
|
@JackieTien97 Addressed the requested review changes, added the relevant regression tests, and validated the implementation. Updates have been pushed. |
Description
Closes #19276
Bug
A multi-value
INlist on an INT32 or INT64 field converted its literals without the range and decimal handling that=already has. The bug only shows up when the list has two or more values; a single value is rewritten to=.On a field
i INT32holding 1, 2, 3, INT32_MIN, INT32_MAX and NULL:where i in (1, 4294967298)where i in (1, -4294967293)where i in (1, 2147483648)where i = 1 or i = 4294967298(normalized to IN)where i not in (1, 4294967298)where i in (2, 2.5),where l in (2, 2.5)(INT64)301: DoubleLiteral cannot be cast to LongLiteralselect i in (2, 2.5),select l in (2, 2.5)ClassCastExceptiontruefor 2,nullfor NULLwhere i not in (2.5, 4294967298)ClassCastExceptionThe "Before" column comes from a standalone server built from master. The "After" column comes from a standalone server built from this branch, running the same statements as #19276.
Cause
There were two paths with the same problem:
ConvertPredicateToFilterVisitor.constructInSetconverted every literal throughRELATIONAL_CONVERT_PREDICATE_VALUE_PARSER_SERVICE. That is(int) ((LongLiteral) v).getParsedValue()for INT32. Out-of-range values wrapped, and anyDoubleLiteralfailed the cast.calc-commonsTypeServices.IN_MULTI_COLUMN_TRANSFORMER_SERVICE, already skipped out-of-range INT32 values. It still cast every literal toLongLiteral, though, so decimals failed for both INT32 and INT64.The analyzer does not insert casts for IN lists, so these literals reach both paths unchanged.
Fix
TypeServices.getIntegralInListValue(Literal, Type)in calc-commons converts each literal for an INT32/INT64 column. It keeps the value when it is an integer within the column range, or an integral decimal within that range. It returnsnullwhen the literal can never match. This is the same ruleconstructCompareFilteruses for=.constructInSetuses it for INT32/INT64 columns and drops literals that can never match. Other column types keep the existing conversion.IN_MULTI_COLUMN_TRANSFORMER_SERVICEuse the same helper. The INT32try/catch IllegalArgumentExceptionaroundgetParsedValue()is removed; that method cannot throw, so the block was dead code.constructInFilterreturnsFalseLiteralFilter, the same as=for a literal that can never match. An empty IN set cannot be used here: TsFile'sValueIn.allSatisfyreports a match for an empty candidate set on a page whose column is entirely NULL, so a flushed all-NULL chunk would be returned.NOT INis unaffected becausePredicateCombineIntoTableScanCheckerrejects aNotExpression, so it is evaluated by the column transformer and still excludes NULL rows.(double) Long.MAX_VALUErounds up to 2^63, so both the IN helper and the pushed-down=compare against the exact exclusive bound0x1p63. Previously=usedfloatPointValue > Long.MAX_VALUE, which let9223372036854775808.0through and saturated it ontoLong.MAX_VALUE; because the optimizer deduplicates an IN list and rewrites a single remaining value to=,IN (2^63, 2^63)matched whileIN (2^63, 0)did not.constructInFilterpushes down=instead of a one-element IN set, the same filter a single-valueINproduces.SELECT l IN (2^63, 2^63)becomesl = 9223372036854775808.0.CompareBinaryColumnTransformerused to compare every numeric pair throughgetDouble, and(double) Long.MAX_VALUErounds up to 2^63, so it returnedtrue. When one side is INT64/TIMESTAMP, integer pairs are now compared as longs and integer/floating-point pairs with an exact long-to-double comparison (CommonTransformUtils.compareLongToDouble); other numeric pairs are exact as doubles and keep the double path. The comparison mode is chosen once in the constructor and dispatched outside the row loop, so there are no per-row type checks.IrExpressionInterpreter.evaluateNumericComparisonuses the samecompareLongToDoublefor a long against a FLOAT/DOUBLE, soSELECT 9223372036854775807 IN (9223372036854775808.0, 0)andSELECT 9223372036854775807 = 9223372036854775808.0returnfalse, matching the column projection (they returnedtruebefore).getIntegralInListValuerejects types other than INT32/INT64 instead of treating them as INT64.CANNOT_CAST_TO_TYPEmessage instead of aClassCastException.Tests
TablePredicateConversionTest(new tests:testMultiValueInPredicateAndInt32Column,testMultiValueInPredicateAndInt64Column,testInt64ColumnAndUpperBoundDoubleLiteral):2147483648at the boundary);TypeServicesTest.testIntegralInListValue: the shared conversion used by the projection path, including the type check.ArithmeticAndCompareBinaryColumnTransformerTest(new tests:testInt64ComparisonDoesNotRoundThroughDouble,testInt64AndDoubleComparisonEdgeCases,testInt64ComparedWithOtherNumericTypes,testComparisonWithSelection): every comparison operator, with the operands in both orders, on negative fractions,-0.0,NaN,±Infinity,Math.nextDown(2^63), INT64 vs FLOAT (including0x1p63f), INT64 vs INT32, TIMESTAMP, andevaluateWithSelection.IoTDBPredicateConversionTableIT(new tests:testMultiValueInPredicateAndInt32Column,testMultiValueInPredicateAndInt64Column,testImpossibleInListAndFlushedAllNullColumn,testInt64UpperBoundInList). The last two use a second table whosenull_devdevice never writes the fields, with aFLUSHin setup, so the all-NULL chunk statistics are exercised:l IN (2^63, 2^63)projection;9223372036854775807 IN (9223372036854775808.0, 0)and9223372036854775807 = 9223372036854775808.0.Local runs (JDK 25, macOS arm64):
Without the main-code change, the two multi-value
TablePredicateConversionTestcases fail withClassCastException: ... DoubleLiteral cannot be cast to ... LongLiteral, the impossible-list assertion reportsmeasurements[0] IN [],testInt64ColumnAndUpperBoundDoubleLiteralreportsmeasurements[0] = 9223372036854775807, and both new IT cases fail on the rows that should not have been returned. With only theIrExpressionInterpreterchange reverted,testInt64UpperBoundInListfails on the constant-folded query withexpected:<false> but was:<true>.Not run locally:
TableClusterIT(I relied on the CI run for the cluster environment) and the full unit-test suites of the two modules.This PR has:
Key changed/added classes (or packages if there are too many classes) in this PR
org.apache.iotdb.calc.utils.TypeServicesorg.apache.iotdb.db.queryengine.plan.relational.analyzer.predicate.ConvertPredicateToFilterVisitororg.apache.iotdb.calc.transformation.dag.column.binary.CompareBinaryColumnTransformerorg.apache.iotdb.calc.transformation.dag.util.CommonTransformUtilsorg.apache.iotdb.db.queryengine.plan.relational.planner.IrExpressionInterpreter