Skip to content

fix(iceberg): a pushed FILTER literal keeps only rows it can compare exactly - #2186

Merged
aaj3f merged 5 commits into
mainfrom
fix/iceberg-exact-pushdown-literals
Nov 3, 2026
Merged

aaj3f merged 5 commits into
mainfrom
fix/iceberg-exact-pushdown-literals

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Iceberg: a pushed FILTER literal keeps only the rows it can compare exactly

Fixes #2185.

The Arrow row filter evaluates a pushed comparison on each decoded batch before the engine sees it, so it may keep extra rows but must never drop a true match: a dropped row never reaches the in-engine FILTER. It used to cast the literal into the column's type with Arrow's safe cast, which panicked for a decimal literal below one (FILTER(?d >= 0.05) on BigQuery's decimal(38,9)), rounded when it reduced a decimal's scale (?d < 1.0500000001 lost the row 1.050000000), and turned an out-of-range literal into NULL, which dropped every row (?d < 1000 on decimal(5,2)). The last two were wrong results on main with no error at all.

  • The literal is converted once per file into the column's comparison type (prepare_comparisons, fluree-db-iceberg/src/io/arrow_reader.rs) and only used if the result is non-null and casts back to the original unchanged (exact_literal). Anything else keeps every row for that comparison, with a debug! breadcrumb, and the in-engine FILTER decides. The check doesn't know about decimals specifically, so integer narrowing and a sub-millisecond instant against a millisecond timestamp column are covered the same way.
  • A decimal literal is built at decimal128(38, s) (decimal_literal), with a negative scale rescaled exactly to 0 and anything no decimal128 can hold refused, so building it can no longer panic.
  • precision is gone from ScanValue::Decimal, LiteralValue::Decimal and TypedValue::Decimal. A value has no precision of its own; the only reader was the Arrow builder above, and every other consumer (decimal_cmp, the SQL renderer, the Delta bridge, the operator) already ignored it. Column types (FieldType::Decimal, Column::Decimal) keep theirs. The "precision is cosmetic" comment that led here goes with it.

I didn't run a benchmark for this. The literal cast moves from once per batch to once per file, and the per-batch work is otherwise the same comparison, so I don't expect a regression, but that's an argument, not a measurement.

Tests

  • row_filter_tests::decimal_literals_keep_exactly_the_true_matches_or_everything. Every op over a decimal(38,9) column, against literals on, between and beside its grid (0.05, -0.05, 0.0500000001, 0.0499999999, 1.0500000001, 4E+2, 1E-50). An exact literal must keep exactly the true matches (computed in integer arithmetic, not by Arrow); one the column can't hold must keep every row.
  • an_out_of_range_literal_keeps_every_row, a_lossy_conversion_of_any_type_is_refused, an_exact_literal_still_filters. Overflow (decimal and Int64 into decimal(5,2) / int), a timestamp truncation, and a check that the exactness test isn't a blanket keep-all.
  • it_iceberg_local_fs::decimal_filters_answer_exactly. End to end over a new pyiceberg fixture, silver.amounts (decimal(38,9) and decimal(5,2) columns; the generator script gains the table), in SPARQL for six filters and in JSON-LD for the integer case. JSON-LD has no decimal FILTER literal (a fractional number parses as a double, which isn't pushed against a decimal column), so the integer-overflow case is its only route into this filter.

Run against origin/main's source with this PR's tests, the integration test fails on every case it should: the panic, < 1.0500000001 → [1, 2, 3, 5], > 1.0499999999 → [], and < 1000 → [] in both SPARQL and JSON-LD.

Locally: fluree-db-iceberg (all targets), the fluree-db-query R2RML tests, the fluree-db-api graph-source unit tests, and it_graph_source_r2rml, it_iceberg_local_fs, it_iceberg_policy, it_iceberg_warehouse_root all pass, plus it_iceberg_direct with aws-testcontainers (Docker), which CI doesn't run.

…exactly

The Arrow row filter evaluates a pushed comparison on each decoded batch
before the engine sees it, so it may keep extra rows but must never drop a
true match: a dropped row never reaches the in-engine FILTER. It cast the
literal into the column type with Arrow's safe cast, which is lossy:

- A decimal literal below one built a Decimal128 whose scale exceeded its
  digit-count precision and panicked the reader (`FILTER(?d >= 0.05)` on a
  decimal(38,9) column, BigQuery's NUMERIC).
- Reducing a decimal's scale rounds, so `?d < 1.0500000001` on decimal(38,9)
  lost the row 1.050000000.
- A literal outside the column's range casts to NULL, not an error, so the
  keep-all fallback never fired and `?d < 1000` on decimal(5,2) lost every
  row. The same shape applies to any narrowing cast.

The literal is now converted once per file into the column's comparison type
and accepted only if the result is non-null and casts back unchanged;
otherwise that comparison keeps every row and the in-engine FILTER decides.

A literal has no precision of its own, so `ScanValue::Decimal`,
`LiteralValue::Decimal` and `TypedValue::Decimal` no longer carry one. Its
only reader was the Arrow literal builder, which now builds at decimal128's
full precision; every other consumer ignored it. Column types keep theirs.
@aaj3f aaj3f added bug Something isn't working as expected area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize labels Oct 26, 2026
@aaj3f
aaj3f requested a review from bplatz October 28, 2026 18:42
@bplatz

bplatz commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Two follow-ups — neither is a regression from this PR, so a follow-up issue is fine for both.

1. A lossless conversion can still compare differently. exact_literal proves the literal survives the conversion, not that the column's type orders values the way the in-engine FILTER does. Two cases in this row filter pass the round-trip check and can still drop a true match:

  • An integer literal against a physically string column. int_pushdown_literal falls through to Int64 for any column type it doesn't name, string included (r2rml.rs L357), and pattern_scan_filters pushes a FILTER comparison without looking at the object map's datatype (operator.rs L1968). Arrow casts 1000 to "1000" and compares lexically, so "999" < "1000" is false and the row is dropped, where a column mapped xsd:integer compares numerically in the engine. Date already has exactly this guard (L905: "on a physically string column the operator would keep such a row while the row filter drops it"); integers don't. I traced this through the code rather than reproducing it end to end, so it's worth confirming the engine side compares those as numbers.
  • −0.0 against a double column. arrow-ord 58's cmp kernels use IEEE totalOrder, which puts −0.0 below 0.0, so FILTER(?d >= 0) drops a −0.0 row the engine keeps. Needs −0.0 in the data, so rare.

A type-family check in prepare_comparisons — push only when the literal and the comparison type are both numeric, both string, or both temporal — would close the first; normalizing zeros on float columns before comparing would close the second. The first fits this PR's theme if you'd rather take it here.

2. Keep narrowing when a literal is inexact (optional, perf). A literal with no exact value in the column now keeps every row for that comparison. Range ops could still narrow by rounding in the safe direction and adjusting the operator — < 1.0500000001 on decimal(38,9) is <= 1.050000000, > 1.0499999999 is >= 1.050000000 — and an Eq against a value no column entry can hold matches nothing. Low value for BigQuery's decimal(38,9), where literals finer than nine places are rare, so only if it shows up.

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The round-trip check closes all three failure modes in #2185 — the panic, the rounding cast, and overflow to NULL — decimal_literal can no longer panic, and dropping precision is safe since nothing serializes these types. The unit test computing the oracle in integer arithmetic rather than through Arrow is the right call.

Some feedback in the comment above: two follow-ups, neither a regression from this PR.

aaj3f added 2 commits October 30, 2026 09:21
…rders as the engine

The exact-conversion check proves a literal survives the conversion into
the column's type, not that the column then orders values as the in-engine
FILTER does. Two shapes passed it and still dropped true matches:

- A literal of another kind than the column. An integer converts exactly to
  a string column's "1000", which then compares as text, where an
  `xsd:integer` map over that column compares numbers: `FILTER(?n < 1000)`
  lost "999", "50" and "7". `true` against a column holding "1" lost the
  row an `xsd:boolean` map reads as true. A comparison now runs only
  between a literal and a column of one kind (integers also with decimals
  and floats); a string against a date or timestamp column, which the
  engine orders by parsing it as that type; and a string by `=` against any
  other column, where the round trip makes it the rendering of the value it
  matches. A decimal and a float no longer compare: 0.1 round-trips through
  a double by rounding back, and the engine compares the two exactly.
- A zero against a float column. arrow-ord orders floats by IEEE
  totalOrder, so -0.0 < 0.0, where the engine holds them equal:
  `FILTER(?d >= 0)` lost the -0.0 row, and `= 0` too. The ordering
  operators now compare with the zero of the sign on which totalOrder
  agrees (`< -0.0`, `>= -0.0`, `<= 0.0`, `> 0.0`); equality with a zero
  keeps every row. Chosen per file when the comparison is prepared, so the
  per-row kernels are unchanged.

The local fixtures gain `silver.literals`: numeric and boolean text, and a
double column with both zeros. Every case runs on SPARQL and JSON-LD.
…the row filter

A literal a `decimal(p, s)` column cannot hold exactly kept every row for
its comparison. It is now restated on the column's grid, in integer
arithmetic rather than by a cast: a literal between grid points `lo < x <
hi` holds for the cells at or below `lo` (`<`, `<=`) or at or above `hi`
(`>`, `>=`), so `< 1.0500000001` on decimal(38,9) is `<= 1.050000000` and
`> 1.0499999999` is `>= 1.050000000`; no cell equals it, so `=` drops every
row. A literal on the grid but beyond the column's precision compares as
itself (`< 1000` on decimal(5,2) keeps every row, `> 1000` none), and one
beyond i128 saturates, which can only keep extra rows. Integer literals
against a decimal column take the same path at scale 0.

An exhaustive test checks every value of a decimal(2,1) column against
literals of scale -2 to 4 on, between and around its grid points, its
limits and zero, and beyond i128, for every operator, against an exact
oracle.
@aaj3f

aaj3f commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @bplatz — both of these were right, and since they fit the PR's theme I folded them in here rather than filing follow-ups (17c30fc2c, 063377ffc).

1a, an integer literal against a physically string column — confirmed end to end. Over a new silver.literals fixture that maps xsd:integer and xsd:boolean over string columns, FILTER(?n < 1000) came back [4] where the engine's answer is [1, 3, 4, 5], ?n >= 50 lost "1000", and ?b = true / ?b = false lost "1" / "0". prepare_comparisons now pushes a comparison only when the literal and the column are the same kind. I did stop a little short of the strict numeric/string/temporal rule, though, because it would have dropped two pushes that are sound on main: a string against a date or timestamp column (the engine reads the string as the temporal there, and the reader casts it the same way), and a string = against any column, which the existing round trip already guarantees renders exactly as the cell. Decimal against float is closed in both directions too — a decimal 0.1 survives a round trip through a double by rounding while the engine compares the two exactly — though nothing reaches that pair today since the provider blocks it.

1b, −0.0 on a double column — confirmed. ?d >= 0, = 0 and the JSON-LD <= -0.0 each dropped a zero row. Rather than normalize the column per batch, the literal's zero is swapped once per file to the signed zero on which Arrow's totalOrder agrees with the engine (< -0.0, >= -0.0, <= 0.0, > 0.0), and equality against a zero keeps every row — exact for the ordering ops and nothing added per row.

2, narrowing an inexact literal. I know you flagged this as optional, but it turned out bounded enough to do properly: decimal_check puts the literal on the column's grid in integer arithmetic (no cast), so between two grid points </<= become <= the lower one and >/>= become >= the upper one, = keeps nothing and != keeps everything; a literal on the grid but past the column's precision compares as itself, and anything past i128 saturates, which can only keep extra rows. decimal_rounding_is_exact_at_every_boundary checks all 199 values of a decimal(2,1) column against ~3,100 literals across scales −2..4 (zero, the limits, every grid point, the i128 extremes, 1E-50) and all six operators, against an exact integer oracle.

Every new test fails with its fix reverted. One merge note: this and #2193 both edit the decimal pushdown — this PR drops precision from LiteralValue::Decimal / ScanValue::Decimal and #2193 still builds it — so whichever lands second gets a small compile fix-up; I'll do that on whichever it is.

aaj3f added 2 commits November 2, 2026 15:46
…hdown-literals

Conflicts: it_iceberg_local_fs.rs and write_local_iceberg_table.py, both
sides' additions kept (the decimal and literal FILTER tests and the subgraph
crawl tests; the amounts, literals and cells fixture tables).

Seam: a decimal literal carries no precision on this branch, so the
rendered-cell equality literal from main is built as `{ unscaled, scale }`.
The cell it renders through keeps the column's precision, which
`decimal_precision_scale` still supplies.
…hdown-literals

Conflict: it_iceberg_local_fs.rs, both sides' tests kept (the decimal and
literal FILTER tests, and the manifest row-count tests).
@aaj3f
aaj3f merged commit 149d02d into main Nov 3, 2026
19 checks passed
@aaj3f
aaj3f deleted the fix/iceberg-exact-pushdown-literals branch November 3, 2026 14:57
bplatz added a commit that referenced this pull request Nov 4, 2026
Brings #2247, #2232, #2246, #2248, #2245, #2230, #2205, #2231, #2255,
#2219, #2209, #2207, #2190, #2216, #2186, #2215, #2213, #2184, #2210,
#2206, #2198, #2187, #2183, #2200, #2193, #2199, #2196 and #2145 into next.

Resolutions:
- Novelty: main's fact-state trim (a cutoff plus a bounded sweep) and the
  dictionary's coverage watermark sit on next's graph slots and per-graph
  OrdMaps. A commit's reification links count toward coverage. Planner
  stats from segment sums decline under annotations, whose link deltas
  only the walk counts, and otherwise carry the indexed link counts.
- Stats-view identity: the index root's id reaches the snapshot through
  next's root decoder (`decode` takes the id, as `take_snapshot_metadata`
  does). `LedgerState::load_record` has a decoding-root form beside
  `load_decoding_root`.
- Transact: #2184's parallel-relationship check runs in next's link
  cascade, counting the `rdf:reifies` links on the retracted edge that the
  transaction does not retract itself. Main's bundle `edge_reifiers` is
  dropped, and `subjects_with_prior_rows` and
  `enforce_single_target_reifiers` stay deleted. The upsert pre-check
  takes main's dictionary probe and bounded seek.
- Query: geo search resolves the graph's POST tree once at open (pooled
  graphs keep their filter), an empty manifest when there is none.
  `branch_or_none` reads the dedicated branch, and the batched joins keep
  next's pooled lanes. The stats-view key hashes the root id.
- Indexer: `apply_dict_novelty` and `compact_forward_streams` take the
  cycle's compaction budget, so the term dictionary keeps its reserved
  share. `Prefetched` holds `ContentBytes`. `relocalize_sorted_commit`
  rewrites next's 20-byte types records.
- Disk cache: main's in-flight write registration, with an empty entry
  still read as crash debris that a write replaces. The process-wide
  eviction walk stays removed.
- Graph import (#2232): fenced CAS and publishes, the seed carries the
  record's namespace and fence, 32-bit graph ids and next's loaders. An
  import into a graph of an existing ledger refuses triple terms and edge
  annotations: the splice merges no triple-term dictionary yet.
- Iceberg: `create_iceberg_graph_source` goes with main (a source needs a
  mapping); the R2RML path keeps next's `model_instance`.
- CLI: main's shared import flags without the leaflet flags; `graph
  import` routes through next's process-wide server routing, and
  `upload_source_import` serves next's `Destination`. Doc sources read a
  ledger through the command's open instance.
- Tests: sibling ledger ids become distinct names, `genesis_ledger` is
  awaited, Cypher state dumps include each relationship's `rdf:reifies`
  link, and the graph-import arena test checks the refusal instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:iceberg Iceberg catalogs/credentials, R2RML, virtual datasets, materialize bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Iceberg: a pushed decimal FILTER literal panics the reader or silently drops matching rows

2 participants