Repository navigation
Description
Found this while querying BigQuery-managed Iceberg tables (BigQuery exports NUMERIC as decimal(38,9)), and it turned out to be more than the panic I first saw.
The Arrow row filter in fluree-db-iceberg/src/io/arrow_reader.rs evaluates a pushed FILTER comparison on each decoded batch before the engine sees it, and the in-engine FILTER only runs on the rows it keeps (fluree-db-query/src/r2rml/rewrite.rs:655-683, operator.rs:529-541). So it can keep extra rows harmlessly, but a row it drops is gone. eval_comparison casts the literal into the column's type with Arrow's default (safe) cast, and that cast is lossy in three ways. Claude traced the mechanism and reproduced each of these end to end against 9420db4ca:
- A panic, for a decimal literal below one.
scan_value_from_bigdecimal(rewrite.rs:1025) derivesprecisionfrom the unscaled digit count only, so0.05decomposes tounscaled 5, precision 1, scale 2.literal_to_array(arrow_reader.rs:604) then calls.with_precision_and_scale(1, 2).expect(..), andFILTER(?d >= 0.05)panics the reader withscale 2 is greater than precision 1. The comment there says precision "is cosmetic for pruning", which is true fordecimal_cmpbut not for Arrow's builder. - Silent row loss from rounding. When the literal has enough digits to get a valid precision, the cast to the column's scale rounds (half away from zero).
?d < 1.0500000001ondecimal(38,9)loses the row1.050000000, and?d > 1.0499999999loses it too. No panic, just wrong results. - Silent row loss from overflow. A literal outside the column's range casts to NULL rather than erroring, so the keep-all fallback (
arrow_reader.rs:565) never fires and the NULL mask drops every row:?d < 1000on adecimal(5,2)column returns nothing. This one reaches JSON-LD too, since an integer FILTER literal against a decimal column is pushed as a scale-0 decimal (fluree-db-api/src/graph_source/r2rml.rs:353).
My first instinct was to fix the precision (max(digits, scale)), but that would've turned (1) into (2), fwiw, so I don't think patching the precision is the right fix. What seems right to me:
- convert the literal into the column's comparison type once per file and only use it if the conversion is non-null and casts back unchanged; otherwise that comparison keeps every row and the in-engine FILTER decides. That check is type-generic, so it also covers integer narrowing and timestamp truncation, which have the same shape;
- drop
precisionfromScanValue::Decimal/LiteralValue::Decimal(and the manifestTypedValue::Decimal). A literal doesn't have one, its only reader was the Arrow builder, and the column's owndecimal(p, s)is what any comparison should use.
Tests should cover SPARQL and JSON-LD, though JSON-LD has no decimal FILTER literal (a fractional number parses as a double, which isn't pushed against a decimal column), so its twin is the integer-overflow case.
Activity