Skip to content

fix: the Iceberg FDW no longer prunes a decimal read as int on its bounds (#2086) - #2152

Open
jdatcmd wants to merge 4 commits into
mainfrom
fix/2086-iceberg-decimal-pruning
Open

jdatcmd wants to merge 4 commits into
mainfrom
fix/2086-iceberg-decimal-pruning

Conversation

@jdatcmd

@jdatcmd jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #2086.

Problem

The pgcolumnar_iceberg foreign data wrapper (FDW) returned no rows, and no error, for a filter that matched rows. This happened when an Iceberg decimal column was declared int or bigint. Spark stores a decimal of precision 18 or less as a Parquet INT32 or INT64, and the reader returns its unscaled integer for such a declaration.

  • decimal(9,2) declared int: d = 20000000 returned nothing.
  • decimal(18,2) declared bigint: d = 20000000 returned nothing, although the full scan reads the column correctly.
  • decimal(9,2) declared bigint: the full scan fails with 42804, but a filtered scan returned an empty result.
  • bucket[8] on a decimal declared int: d = 10000000 returned nothing.

Cause

Both lines are in the FDW's pruning setup.

  • Min/max pruning chose a column by its PostgreSQL type alone (int2, int4, int8, bool), and ice_fdw_bound_int decoded every bound as a little-endian integer. Iceberg writes a decimal bound as the unscaled value in big-endian two's complement. So 0x00989680 (10000000) decoded as a negative number, and the file was pruned.
  • Bucket pruning hashed an integer constant as an 8-byte long. Iceberg hashes a decimal's unscaled big-endian bytes, so the bucket was wrong and the file was pruned.

Because the file was pruned before it was opened, the reader's type check never ran. That is why the refused bigint declaration returned nothing instead of 42804.

The fix

The FDW now reads the Iceberg field type from the table's current schema before it prunes.

  • PgColumnarIcebergColumnFieldIds gives a column no field id unless its Iceberg field is int, long or boolean. No min/max qual is compiled without a field id.
  • PgColumnarIcebergBucketMap maps a bucket field only for an int2/int4/int8 column over an Iceberg int or long, or a text/varchar column over an Iceberg string. Those are the pairs ice_fdw_bucket_of_const hashes the way Iceberg does.

A decimal column is no longer pruned by metrics or by bucket. It is read, so it returns the right rows, and a refused declaration fails with 42804 with or without a filter. The long column of the same file still prunes on min/max (a control check pins this).

A new fixture, test/fixtures/iceberg/warehouse_decimal, is written by gen_iceberg_decimal.py with pyiceberg 0.12.0. It holds decimal(9,2) as INT32, decimal(18,2) as INT64, and a bucket[8] partition on the decimal. The tables have the unscaled values 10000000 (id 1) and 20000000 (id 2).

docs/features.md now states which Iceberg types metrics pruning covers.

Red, on main's code

PG18, d52f6e8, with only the tests and fixture added. test/iceberg_fdw.sh:

PASS  decimal: dec9 read as int returns both unscaled values (premise)
FAIL  decimal: d = 20000000 on decimal(9,2) read as int returns id 2: got [none] want [2]
FAIL  decimal: count where d > 15000000 on decimal(9,2) read as int is 1: got [0] want [1]
FAIL  decimal: d = 20000000 on decimal(18,2) read as bigint returns id 2: got [none] want [2]
FAIL  decimal: d >= 15000000 on decimal(18,2) read as bigint returns id 2: got [none] want [2]
PASS  decimal: a full scan of decimal(9,2) read as bigint is refused (42804)
FAIL  decimal: a filtered scan of decimal(9,2) read as bigint is refused the same way, not empty: got [no error ()] want [42804]
FAIL  decimal: d = 10000000 on bucket[8](decimal) read as int returns id 1: got [none] want [1]
FAIL  decimal: d = 20000000 on bucket[8](decimal) read as int returns id 2: got [none] want [2]
PASS  decimal: id = 5 on the long column of dec9 still prunes its file (Files Pruned: 1)
accounting: 95 passed + 7 failed + 0 unrunnable + 0 skipped = 102

test_iceberg_fdw.py -k decimal:

E           AssertionError: decimal: d = 20000000 on decimal(9,2) read as int returns id 2: got 'none' want '2'
E           AssertionError: decimal: d = 20000000 on decimal(18,2) read as bigint returns id 2: got 'none' want '2'
E           AssertionError: decimal: a filtered scan of decimal(9,2) read as bigint is refused the same way, not empty: got 'no error ([])' want '42804'
E           AssertionError: decimal: d = 10000000 on bucket[8](decimal) read as int returns id 1: got 'none' want '1'
4 failed, 1 passed, 67 deselected in 7.88s

The three passing checks are controls: the premise, the full-scan refusal, and the long column still pruning.

The bucket gate is needed on its own. With the metric fix in place and the bucket gate disabled, the bucket test still failed:

E           AssertionError: decimal: d = 10000000 on bucket[8](decimal) read as int returns id 1: got 'none' want '1'
1 failed, 4 passed, 67 deselected in 3.51s

Green

PG18, test/iceberg_fdw.sh:

PASS  decimal: dec9 read as int returns both unscaled values (premise)
PASS  decimal: d = 20000000 on decimal(9,2) read as int returns id 2
PASS  decimal: count where d > 15000000 on decimal(9,2) read as int is 1
PASS  decimal: d = 20000000 on decimal(18,2) read as bigint returns id 2
PASS  decimal: d >= 15000000 on decimal(18,2) read as bigint returns id 2
PASS  decimal: a full scan of decimal(9,2) read as bigint is refused (42804)
PASS  decimal: a filtered scan of decimal(9,2) read as bigint is refused the same way, not empty
PASS  decimal: d = 10000000 on bucket[8](decimal) read as int returns id 1
PASS  decimal: d = 20000000 on bucket[8](decimal) read as int returns id 2
PASS  decimal: id = 5 on the long column of dec9 still prunes its file (Files Pruned: 1)

PG18, test_iceberg_fdw.py -k decimal -v:

test_iceberg_fdw.py::test_a_decimal_9_read_as_int_is_not_pruned_on_its_bounds PASSED [ 20%]
test_iceberg_fdw.py::test_a_decimal_18_read_as_bigint_is_not_pruned_on_its_bounds PASSED [ 40%]
test_iceberg_fdw.py::test_a_refused_decimal_declaration_is_refused_with_a_filter_too PASSED [ 60%]
test_iceberg_fdw.py::test_a_bucket_partition_on_a_decimal_read_as_int_is_not_pruned PASSED [ 80%]
test_iceberg_fdw.py::test_the_long_column_beside_a_decimal_still_prunes PASSED [100%]
accounting: 11 pass + 0 fail + 0 unrun = 11
====================== 5 passed, 67 deselected in 16.95s =======================

The build gives no compiler warnings (make 2>err; grep warning: err is empty on PG18).

Suites run

All on the final source.

Shell suites, PG18 (18.6) and PG17 (17.11), every one exit 0, same counts on both majors: fdw_plan_privilege 8, iceberg_catalog 12, iceberg_data_files 42, iceberg_deletes 73, iceberg_dv 26, iceberg_fdw 102, iceberg_fdw_estimate 4, iceberg_fdw_projection 7, iceberg_malformed 68, iceberg_name_mapping 22, iceberg_name_mapping_memory 7, iceberg_objstore 19, iceberg_rest 16, iceberg_rest_scan 128, iceberg_rest_server 39, iceberg_rest_vended 18, iceberg_scan 46, objstore_error_advice 30, planner_choice_quality 11. All passed, 0 failed.

test/iceberg_fdw.sh on PG15, PG16 and PG19 (19beta4) as well, for the ledger: 102 passed + 0 failed on each.

pytest, -n 4, test_iceberg_*.py, test_objstore_error_advice.py, test_planner_choice_quality.py, test_fdw_plan_privilege.py:

pg18: accounting: 698 pass + 0 fail + 0 unrun = 698
      656 passed in 123.06s (0:02:03)
pg17: accounting: 698 pass + 0 fail + 0 unrun = 698
      656 passed in 233.59s (0:03:53)

Guard half, as the README shows: 894 passed, accounting: 2440 pass + 0 fail + 0 unrun = 2440.

docs_style.sh: 54 passed.

Registries: cluster_tests 5376 -> 5381, by collection of the cluster half. compare_to_bash.py reports 104 literal matches and 0 missing for iceberg_fdw.sh against test_iceberg_fdw.py. The ledger has 10 new rows from one merge of five green logs (PG15 to PG19), then the PG18 red log with --reds-are-real, a --mutation and a --target for each of the 7 failed checks. checks_never_observed_red 4894 -> 4897. census-commits reports 0 refused.

Not verified

  • I did not run the complete shell and pytest corpus, only the Iceberg and FDW suites listed above. The C change touches only columnar_iceberg.c, whose two changed functions the Iceberg FDW alone calls.
  • I ran the guard half in a venv that has psycopg. CI runs it without psycopg.
  • Truncate and identity partitions on a decimal were not tested. A decimal partition value arrives as bytes, and the FDW already declines to prune on bytes for an integer column, but no check pins that.
  • test_objstore_module.py, test_extension_upgrade.py and test_format_downgrade.py were not run; this change does not touch the code they cover.

🤖 Generated with Claude Code

…unds

The Iceberg foreign data wrapper returned no rows, and no error, for a
filter that matched rows when an Iceberg decimal column was declared int
or bigint. Spark stores a decimal of precision 18 or less as a Parquet
INT32 or INT64, and the reader returns the unscaled integer for such a
declaration. On main, d = 20000000 on decimal(9,2) declared int returned
nothing, and so did decimal(18,2) declared bigint.

Cause: the FDW chose min/max pruning by the PostgreSQL type alone and
decoded every bound as a little-endian integer. Iceberg writes a decimal
bound as the unscaled value in big-endian two's complement, so the range
came out wrong and the file was pruned. Bucket pruning had the same
fault: it hashed the constant as an 8-byte long, where Iceberg hashes the
decimal's bytes.

The fix reads the Iceberg field type from the current schema.
PgColumnarIcebergColumnFieldIds gives a column no field id unless the
field is an int, long or boolean, so no metric qual is compiled for it.
PgColumnarIcebergBucketMap maps a bucket field only when an int column
sits over an int or long field, or a text column over a string field.
A bigint declaration over an INT32 decimal now fails with a filter too
(42804), as the full scan always did, because no file is pruned first.

Tests: 10 checks in test/iceberg_fdw.sh and their pytest twins in
test_iceberg_fdw.py, plus one pytest-only oracle comparison. A new
fixture, warehouse_decimal, written by gen_iceberg_decimal.py with
pyiceberg 0.12.0, holds decimal(9,2) as INT32, decimal(18,2) as INT64 and
a bucket[8] partition on the decimal.

Red on PG18 with main d52f6e8: 7 checks failed, for example
"FAIL  decimal: d = 20000000 on decimal(18,2) read as bigint returns id
2: got [none] want [2]". With the metric fix alone, the bucket check
still failed: "got 'none' want '1'". The three controls passed on main.

Registries: cluster_tests 5376 -> 5381 by collection. The ledger merged
five green iceberg_fdw.sh logs, PG15 to PG19, in one merge, then the
PG18 red log with --mutation, --target for each of the 7 failed checks
and --reds-are-real. 10 new rows, 3 never observed red, so
checks_never_observed_red moves 4894 -> 4897.

Fixes #2086

Ledger-census: rows=9670 never=4897 dated=4773
Co-Authored-By: Claude Opus 5.5 <[email protected]>
jdatcmd and others added 3 commits November 24, 2026 06:34
ubuntu-latest moved from ubuntu-24.04 to ubuntu-26.04 between 2026-11-12
and 2026-11-22. Since then `sudo -E` no longer carries GITHUB_BASE_REF
into run_all_versions.sh. The ledger gate then cannot find the PR's base
and refuses: "no trustworthy prior ceiling: GITHUB_BASE_REF is unset".
Every PR fails suites (PG 17) and suites (PG 18) although every suite
passes (#2056, and every PR from #2127 to #2159).

The step now names the variable in the env list, so it reaches the gate
whatever sudo preserves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The suites (PG 17) and suites (PG 18) jobs fail on every PR with "ledger
integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF is unset",
because ubuntu-latest moved to 26.04 and sudo -E no longer passes the
variable. PR #2162 (845c83e) fixes the workflow but is not merged into main
yet, so this branch takes it directly. The merge had no conflicts; CHANGELOG.md
keeps both entries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test/fixtures/iceberg/gen_iceberg_decimal.py starts with a #! line but was
committed with mode 100644. harness_selftest part 300 requires every script
that declares an interpreter to be executable, so suites (PG 17) and
suites (PG 18) failed on this PR with:

  FAIL  every script that declares an interpreter is executable:
        got [[1: test/fixtures/iceberg/gen_iceberg_decimal.py]] want [[]]

The file is now 100755, like the other generators in that directory. The
test was right; only the file mode changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jdatcmd

jdatcmd commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Fix-up pushed: two commits on top of 6f3614d (new head f4180a6)

1. Merged fix/ci-sudo-base-ref (#2162) into this branch: d055093

On this PR, both suites (PG 17) and suites (PG 18) failed with the ledger refusal that every PR hits since ubuntu-latest moved to 26.04:

ledger integrity failure: no trustworthy prior ceiling: GITHUB_BASE_REF is unset ...

#2162 (845c83e) fixes the workflow but is not in main yet, so I merged that branch. There were no conflicts. CHANGELOG.md keeps both entries. When #2162 lands in main, this merge commit adds nothing new.

2. A real failure, separate from the ledger one: f4180a6

Both suites jobs also failed harness_selftest (jobs 138202826684 and 138202826596):

FAIL  every script that declares an interpreter is executable: got [[1: test/fixtures/iceberg/gen_iceberg_decimal.py]] want [[]]
accounting: 1298 passed + 1 failed + 0 unrunnable + 0 skipped = 1299

gen_iceberg_decimal.py starts with #!/usr/bin/env python3 but was committed as mode 100644. Every other generator in test/fixtures/iceberg/ is 100755. I changed the mode to 100755. The test was right, and the file contents are unchanged.

Selftest part 300 run alone, with a stub check:

  • on 6f3614d: FAIL every script that declares an interpreter is executable: got [[1: test/fixtures/iceberg/gen_iceberg_decimal.py]] want [[]]
  • on f4180a6: PASS every script that declares an interpreter is executable, 15 checks, 0 failed

Nothing else failed in either job. The 13 other checks were green.

Local runs on PG 18.6, before pushing

What Result
make after make clean 38 compiles, 0 warnings
Guard half (test/pytest/README.md) 894 passed, expected guard_tests 894
test/iceberg_fdw.sh accounting: 102 passed + 0 failed + 0 unrunnable + 0 skipped = 102
test_iceberg_fdw.py (--pg-config set to the PG 18 build) 72 passed, accounting: 107 pass + 0 fail + 0 unrun = 107
census-commits --against origin/main 1 change test/check_ledger.tsv since d52f6e8c ..., 0 refused

I also ran test/run_all_versions.sh on PG 18 with GITHUB_BASE_REF=main, so the ledger gate ran the way CI runs it, with --registered built by the script. The gate passed:

ledger census: rows=9670 | never observed red=4897, ever red=4773, new this run=0
  census stated 4897, ledger holds 4897: they agree
ledger coverage: registered=312 | covered=293, not covered=19, ceiling=19
  ceiling against refs/remotes/origin/main (4 commits behind HEAD): 19 -> 19, which does not rise
census commits: 1 change test/check_ledger.tsv since d52f6e8c (merge base with refs/remotes/origin/main), 0 refused

That run still ended FAIL PG18, for reasons that come from my machine, not from this PR:

  • audit, phase2 to phase6 and smoke could not connect (createdb: error: connection to server on socket ".../fx-iceberg-0/.s.PGSQL.NNNNN" failed). My sandbox's PGHOST setting leaked into these suites.
  • logical_subscriber failed with FATAL: role "postgres" does not exist, because I ran it as a non-root user.
  • harness_selftest skipped for the same reason.

None of these suites failed in CI on this PR.

Not verified

CI on f4180a6 has not finished yet.

This branch has not been deployed

No deployments
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.

iceberg: a filter on a decimal column declared int or bigint returns no rows, because min/max pruning reads decimal bounds as little-endian

1 participant