Repository navigation
Conversation
bitwise_left_shift and bitwise_right_shift inferred the two's complement width from the magnitude of the value and masked the shift result back into it, so an INT32 or INT64 operand small enough to fit in 8 or 16 bits lost every bit that moved above that inferred width. bitwise_left_shift(1, 20) returned 0 instead of 1048576, and two rows of one column could be evaluated with different widths under the same shift amount. Each overload now shifts with the width of its declared operand type, which is what the column transformers already dispatch on: the int overloads are reached only for INT32 operands and the long overloads only for INT64 ones. The shift >= 32 and shift >= 64 guards stay, since Java uses only the five or six lowest bits of the shift count. bitwise_right_shift_arithmetic is unchanged: its preserveSign calls are the identity whenever their branch is taken.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19358
Problem
bitwise_left_shiftandbitwise_right_shiftchose the two's-complement width for the shift from the magnitude of the runtime value instead of the declared type of the operand, and masked the result back into that inferred width. Small operands were treated as 8-bit or 16-bit quantities, so every bit that moved above the inferred width was discarded and the functions returned silently wrong results.Because the width came from the value rather than the column, two rows of the same column under the same shift amount were evaluated with different semantics. The documented contract is
value << shiftwith "the return type is the same as the data type of value".Root cause
BitwiseUtilsselected the mask with theisTinyInt/isSmallInt/isInt32magnitude tests and applied it after the shift:The declared width was available all along:
TableMetadataImpl.getFunctionTypeaccepts only INT32/INT64 operands and returns the first argument's type, andBitwiseLeftShift(2)ColumnTransformer/BitwiseRightShift(2)ColumnTransformeralready dispatch oncolumn.getDataType(), so theintoverloads are reached only for INT32 operands and thelongoverloads only for INT64 operands. The utility then discarded that information.Change
The four affected overloads now use the width of their declared operand type, which is what Java's own operators give:
The existing
shift >= 32/shift >= 64guards are kept, because Java uses only the five or six lowest bits of the shift count and1 << 32would otherwise yield1.bitwiseRightShiftArithmeticTransformis not touched: itspreserveSigncalls are the identity whenever their branch is taken (a value inside[-2^(w-1), 2^(w-1)-1]already has every bit abovew-1equal to its sign bit), so that path was already correct. The mask constants and helpers it uses stay as they are.Negative shift amounts are out of scope here, as noted in the issue. Nothing specifies that case and it is better settled on its own.
Before and after
Same statements, same data, on a standalone cluster.
before
after
before, every row
0for all three columns; afterbefore, the INT32 and INT64 columns returned the same values (
-45gave105in both); afterA predicate over a shifted expression no longer drops matching rows:
before
-45000only; after1,100,-45000,1000.bitwise_right_shift_arithmeticis unchanged, before and after:The user guide's own example for
bitwise_left_shift(length, width)on(14,12),(15,10),(13,12)now returns57344, 15360, 53248instead of0, 0, 0, which is what the same page's prose requires. The published example output will need updating in apache/iotdb-docs.Tests
BitwiseUtilsTestgains five cases: left and right shift keeping the declared INT32 and INT64 width across the 8/16/32-bit boundaries the old code inferred, including(1,31),(1,32),(1L,62)and(1L,64)for the guards, and a property case asserting thatbitwiseLeftShiftTransform(v, s) == v << sandbitwiseRightShiftTransform(v, s) == v >>> sfor values on both sides of every old boundary and all shift amounts. The arithmetic right-shift case is extended with small-operand shifts so the unaffected path stays pinned down. All existing assertions are unchanged and still pass.IoTDBBitwiseFunctionTableITgains abitwise_shift_width_tablewith INT32 and INT64 columns and five tests covering the queries above, including the predicate case and the arithmetic right shift as a control.The failsafe report confirms all 23 methods ran, the 5 new ones together with the 18 that were already there.
Also run:
mvn test-compile -pl iotdb-core/calc-commons -am -P with-zh-locale(BUILD SUCCESS),mvn spotless:checkon the changed modules,python3 .github/scripts/check-it-selection.py, and the before/after SQL above on a standalone cluster.