Skip to content

Shift at the declared operand width in bitwise shift functions - #19359

Open
Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-bitwise-shift-declared-width
Open

Karman580 wants to merge 1 commit into
apache:masterfrom
Karman580:fix-bitwise-shift-declared-width

Conversation

@Karman580

@Karman580 Karman580 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #19358

Problem

bitwise_left_shift and bitwise_right_shift chose 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 << shift with "the return type is the same as the data type of value".

Root cause

BitwiseUtils selected the mask with the isTinyInt / isSmallInt / isInt32 magnitude tests and applied it after the shift:

int shifted = (value << shift);
if (isTinyInt(value)) {
  return preserveSign(shifted, TINYINT_MASK_IN_INT, TINYINT_SIGNED_BIT_IN_INT);
} else if (isSmallInt(value)) {
  return preserveSign(shifted, SMALLINT_MASK_IN_INT, SMALLINT_SIGNED_BIT_IN_INT);
} else {
  return shifted;
}

The declared width was available all along: TableMetadataImpl.getFunctionType accepts only INT32/INT64 operands and returns the first argument's type, and BitwiseLeftShift(2)ColumnTransformer / BitwiseRightShift(2)ColumnTransformer already dispatch on column.getDataType(), so the int overloads are reached only for INT32 operands and the long overloads 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:

public static int bitwiseLeftShiftTransform(int value, long shift) {
  if (shift >= 32) {
    return 0;
  }
  return value << shift;
}

The existing shift >= 32 / shift >= 64 guards are kept, because Java uses only the five or six lowest bits of the shift count and 1 << 32 would otherwise yield 1.

bitwiseRightShiftArithmeticTransform is not touched: its preserveSign calls are the identity whenever their branch is taken (a value inside [-2^(w-1), 2^(w-1)-1] already has every bit above w-1 equal 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.

create table t(device_id STRING TAG, i32 INT32 FIELD, i64 INT64 FIELD);
insert into t(time,device_id,i32,i64) values
  (1,'d1',1,1),(2,'d1',100,100),(3,'d1',-45,-45),(4,'d1',-45000,-45000),(5,'d1',1000,1000);

select i32, bitwise_left_shift(i32,1) as sh1, bitwise_left_shift(i32,7) as sh7,
            bitwise_left_shift(i32,20) as sh20 from t order by time;

before

+------+------+--------+--------+
|   i32|   sh1|     sh7|    sh20|
+------+------+--------+--------+
|     1|     2|    -128|       0|
|   100|   -56|       0|       0|
|   -45|   -90|    -128|       0|
|-45000|-90000|-5760000|58720256|
|  1000|  2000|   -3072|       0|
+------+------+--------+--------+

after

+------+------+--------+----------+
|   i32|   sh1|     sh7|      sh20|
+------+------+--------+----------+
|     1|     2|     128|   1048576|
|   100|   200|   12800| 104857600|
|   -45|   -90|   -5760| -47185920|
|-45000|-90000|-5760000|  58720256|
|  1000|  2000|  128000|1048576000|
+------+------+--------+----------+
select i64, bitwise_left_shift(i64,31) as sh31, bitwise_left_shift(i64,40) as sh40,
            bitwise_left_shift(i64,62) as sh62 from t order by time;

before, every row 0 for all three columns; after

+------+---------------+------------------+--------------------+
|   i64|           sh31|              sh40|                sh62|
+------+---------------+------------------+--------------------+
|     1|     2147483648|     1099511627776| 4611686018427387904|
|   100|   214748364800|   109951162777600|                   0|
|   -45|   -96636764160|   -49478023249920|-4611686018427387904|
|-45000|-96636764160000|-49478023249920000|                   0|
|  1000|  2147483648000|  1099511627776000|                   0|
+------+---------------+------------------+--------------------+
select i32, bitwise_right_shift(i32,1) as rs32, i64, bitwise_right_shift(i64,1) as rs64
from t order by time;

before, the INT32 and INT64 columns returned the same values (-45 gave 105 in both); after

+------+----------+------+-------------------+
|   i32|      rs32|   i64|               rs64|
+------+----------+------+-------------------+
|     1|         0|     1|                  0|
|   100|        50|   100|                 50|
|   -45|2147483625|   -45|9223372036854775785|
|-45000|2147461148|-45000|9223372036854753308|
|  1000|       500|  1000|                500|
+------+----------+------+-------------------+

A predicate over a shifted expression no longer drops matching rows:

select i32 from t where bitwise_left_shift(i32,20) > 0 order by time;

before -45000 only; after 1, 100, -45000, 1000.

bitwise_right_shift_arithmetic is unchanged, before and after:

+------+------+------+
|   i32|  ar32|  ar64|
+------+------+------+
|     1|     0|     0|
|   100|    50|    50|
|   -45|   -23|   -23|
|-45000|-22500|-22500|
|  1000|   500|   500|
+------+------+------+

The user guide's own example for bitwise_left_shift(length, width) on (14,12),(15,10),(13,12) now returns 57344, 15360, 53248 instead of 0, 0, 0, which is what the same page's prose requires. The published example output will need updating in apache/iotdb-docs.

Tests

BitwiseUtilsTest gains 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 that bitwiseLeftShiftTransform(v, s) == v << s and bitwiseRightShiftTransform(v, s) == v >>> s for 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.

IoTDBBitwiseFunctionTableIT gains a bitwise_shift_width_table with INT32 and INT64 columns and five tests covering the queries above, including the predicate case and the arithmetic right shift as a control.

$ mvn test -pl iotdb-core/datanode -am -Dtest=BitwiseUtilsTest
[INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 s -- in org.apache.iotdb.db.queryengine.transformation.dag.util.BitwiseUtilsTest
[INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
$ mvn verify -DskipUTs -Dit.test=IoTDBBitwiseFunctionTableIT -pl integration-test -am -PTableSimpleIT -P with-integration-tests
[INFO] Tests run: 23, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.834 s -- in org.apache.iotdb.relational.it.query.old.builtinfunction.scalar.IoTDBBitwiseFunctionTableIT
[INFO] BUILD SUCCESS

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:check on the changed modules, python3 .github/scripts/check-it-selection.py, and the before/after SQL above on a standalone cluster.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Table model] bitwise_left_shift and bitwise_right_shift take the shift width from the operand's value instead of its declared INT32/INT64 type

1 participant