DOUBLE cast clamps out-of-range INT64 in Apache IoTDB

Two casts of the same string disagree in IoTDB: one raises error 701, the other quietly returns a different number. Here is how to spot it and stop it.

Binary Buddies · 5 min read · 2 views

A gateway import table stores counters as strings until a normalization stage converts them. One device sent 9223372036854775808, which is exactly one more than the INT64 maximum. Converting it directly is rejected. Converting it through DOUBLE first is not: the value comes back as 9223372036854775807, a valid-looking counter that was never sent.

This was reported against Apache IoTDB 2.0.8, table model. The cost is quiet data corruption. An out-of-range identifier or counter is replaced by a plausible one, with no error and no null.

Below: the reproduction, the likely cause, what we can and cannot conclude from the report, and how to keep the two-step cast from hiding bad input.

The reproduction

The reduced setup from the report:

CREATE TABLE imported_counters (
  gateway_id STRING TAG,
  raw_value STRING FIELD
);

INSERT INTO imported_counters(time, gateway_id, raw_value)
VALUES (1000, 'gw-a', '9223372036854775808');

The generic normalization expression is a two-step cast:

SELECT raw_value,
       CAST(CAST(raw_value AS DOUBLE) AS INT64) AS parsed_value
FROM imported_counters;

It returns 9223372036854775807 in parsed_value next to the original 9223372036854775808 in raw_value. The query succeeds and nothing flags the change.

The direct cast rejects the same text

Casting the stored string straight to INT64 behaves as you would want:

SELECT raw_value, CAST(raw_value AS INT64) AS parsed_value
FROM imported_counters;

Msg: org.apache.iotdb.jdbc.IoTDBSQLException:
701: Cannot cast 9223372036854775808 to INT64 type

So two paths for the same input disagree: one raises error 701, the other returns a number. That inconsistency is the real hazard. A pipeline author who tested the direct cast and saw it reject overflow has no reason to expect a generic stage that goes through DOUBLE to behave differently.

Why the DOUBLE path clamps

The value 9223372036854775808 is 263. A double can represent it exactly, so the STRING to DOUBLE step succeeds without complaint. Nothing is wrong yet. The problem appears on the second step, where a double that is out of range for a 64-bit signed integer has to become an INT64.

An answer on the question attributes the clamp to Java: IoTDB runs on the JVM, and the Java Language Specification defines a double-to-long narrowing conversion as saturating, so anything too large becomes Long.MAX_VALUE. That fits the output exactly. We have not verified it against the IoTDB source, and the report does not show which code path implements the DOUBLE to INT64 cast. Treat it as the most plausible explanation rather than a confirmed one.

The same answer says the direct cast rejects the value because it goes through string parsing, in the style of Long.parseLong, which checks bounds and throws. That is consistent with the 701 error, but again it is an inference about the implementation.

Making the pipeline fail instead of clamp

The question asks whether the two-step cast can be made to fail. The report does not show a configuration option that does this, and we are not aware of one, so the practical fixes sit around the cast:

  • Drop the DOUBLE hop for integer inputs. If the column holds integer counters, use CAST(raw_value AS INT64) and let error 701 reject out-of-range text.
  • Guard before converting. If the stage must be generic, validate the string first. In the ingest code, parse with an arbitrary-precision integer type (BigInteger in Java, plain int in Python) and reject anything outside -9223372036854775808 to 9223372036854775807 before it reaches the table.
  • Compare after the cast. A cheap tripwire is to flag any row whose converted value equals 9223372036854775807. A real counter could legitimately hold that value, so this finds suspects rather than proving corruption.

The first option is the cleanest because it keeps one code path and the database's own overflow check does the work.

A second problem behind the first

The clamp is the visible failure, but the DOUBLE hop has a quieter one even for in-range counters. A double has a 53-bit significand, so integers above 253 cannot all be represented. That is general floating-point behavior, not something specific to IoTDB, and it means a large counter can change in its low digits during the round trip with no overflow at all. The report does not test this, so we cannot quote an IoTDB output for it, but any counter or identifier that large deserves a round-trip comparison against the original string.

Where this does not fit, and what to watch for

Removing the DOUBLE step does not suit every input. If the staging column legitimately holds decimals or exponent notation such as 1e3, a direct INT64 cast may reject text that the DOUBLE path accepted. We have not tested which formats IoTDB's direct cast accepts, so check your own data before swapping it in.

The findings are also from a single report on IoTDB 2.0.8 in the table model. Other versions, or the tree model, may behave differently, and the saturation explanation is unconfirmed. Before relying on any of this, run the three-line reproduction above on your own version and see whether both paths still disagree.

Finally, audit existing data. Any generic stage that used the two-step cast may already have written 9223372036854775807 in place of out-of-range input. Because the original text is still in raw_value in this setup, a comparison of the two columns will show the affected rows.

This article draws on Casting an out-of-range counter through DOUBLE clamps it to INT64 max and an answer to it, on the Stack Exchange network, licensed under CC BY-SA 4.0.

  • #apache-iotdb
  • #int64 overflow
  • #integer
  • #sql

Comments

Add a comment
Sign in with Google to join the conversation