Skip to content

Commit c2435ca

Browse files
groeneaiclaude
andcommitted
Do not prune Iceberg row lineage on the inherited block without lineage statistics
`addRowLineageHyperrectangles` took "some column of the manifest entry has `column_sizes`" as proof that a lineage column with no statistics is absent from the data file, and pruned `_row_id` / `_last_updated_sequence_number` on the inherited block `[first_row_id, first_row_id + record_count - 1]` / `[sequence_number, sequence_number]`. The Iceberg spec makes every metric map optional and a missing id means unknown, so a copy-on-write rewritten file whose materialized values lie below that block was skipped and its rows lost (#124271). The branch came with #115603. Without declared bounds a lineage column now gets the existing fallback `[0, top of the inherited block]`: a materialized row id is below the table's `next-row-id` and a copied sequence number belongs to an earlier commit. The new integration test strips only the lineage statistics from a Spark copy-on-write manifest. Seven count assertions that encoded the old lower bound are updated to the fallback numbers, and the wrapped-block probe moves to `_row_id >= 20`, which still distinguishes the overflow guard. Closes #124271 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 269d7ce commit c2435ca

2 files changed

Lines changed: 137 additions & 38 deletions

File tree

‎src/Storages/ObjectStorage/DataLakes/Iceberg/ManifestFileIterator.cpp‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,6 @@ namespace
102102
return DB::Range(*left, true, *right, true);
103103
}
104104

105-
bool isColumnPresenceKnown(const ParsedManifestFileEntry & parsed_entry)
106-
{
107-
for (const auto & [field_id, column_info] : parsed_entry.columns_infos)
108-
if (column_info.bytes_size.has_value())
109-
return true;
110-
return false;
111-
}
112-
113105
void addRowLineageHyperrectangles(
114106
std::unordered_map<Int32, DB::Range> & hyperrectangles,
115107
const ProcessedManifestFileEntry & entry,
@@ -126,31 +118,23 @@ namespace
126118
if (common::addOverflow<UInt64>(
127119
*entry.first_row_id, static_cast<UInt64>(parsed_entry.record_count) - 1, last_inherited_row_id))
128120
return;
129-
const bool column_presence_is_known = isColumnPresenceKnown(parsed_entry);
130121
const bool row_ids_are_readable = Poco::toUpper(parsed_entry.file_format) != "ORC";
131122

132123
for (const auto field_id : {row_id_field_id, last_updated_sequence_number_field_id})
133124
{
134125
const bool is_row_id = field_id == row_id_field_id;
135126
if (is_row_id && !row_ids_are_readable)
136127
continue;
137-
const UInt64 inherited_lower_bound = is_row_id ? *entry.first_row_id : inherited_sequence_number;
138128
const UInt64 inherited_upper_bound = is_row_id ? last_inherited_row_id : inherited_sequence_number;
139129

140-
if (!parsed_entry.columns_infos.contains(field_id))
141-
{
142-
if (column_presence_is_known)
143-
{
144-
hyperrectangles.emplace(field_id, DB::Range(inherited_lower_bound, true, inherited_upper_bound, true));
145-
continue;
146-
}
147-
}
148-
else if (auto range = getMaterializedRowLineageRange(parsed_entry, field_id, path_to_manifest_file))
130+
if (auto range = getMaterializedRowLineageRange(parsed_entry, field_id, path_to_manifest_file))
149131
{
150132
hyperrectangles.emplace(field_id, *range);
151133
continue;
152134
}
153135

136+
/// A missing statistic says nothing about the column, and a materialized value always predates this file, so only the
137+
/// upper end of the inherited block bounds every value the file can hold.
154138
hyperrectangles.emplace(field_id, DB::Range(UInt64(0), true, inherited_upper_bound, true));
155139
}
156140
}

‎tests/integration/test_storage_iceberg_with_spark/test_row_lineage_pruning.py‎

Lines changed: 134 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Pruning by the Iceberg v3 row lineage columns (`_row_id`, `_last_updated_sequence_number`).
22
3-
Both columns are derived from file-level metadata, so a filter on them is answerable from the
4-
manifest alone: a data file holds the row ids `[first_row_id, first_row_id + record_count)` and a
5-
single `_last_updated_sequence_number` (its data sequence number). That makes the incremental "give
6-
me everything changed after sequence number N" read - the way row lineage is meant to be consumed -
7-
a metadata-only file skip, and a `_row_id` point lookup a single-file read.
3+
Both columns are derived from file-level metadata: a data file inherits the row ids
4+
`[first_row_id, first_row_id + record_count)` and a single `_last_updated_sequence_number` (its data
5+
sequence number). That makes the incremental "give me everything changed after sequence number N"
6+
read - the way row lineage is meant to be consumed - a metadata-only file skip.
87
98
The trap these tests guard is the copy-on-write rewrite: a file whose rows carry materialized row
109
ids does NOT hold the contiguous range its manifest entry advertises, so pruning it by that range
11-
drops live rows. See `test_row_lineage.py` for how the values themselves are derived.
10+
drops live rows. Unless the manifest declares bounds for a lineage column, only the upper end of the
11+
inherited range bounds a file. See `test_row_lineage.py` for how the values themselves are derived.
1212
1313
NOTE: row lineage is written only by `iceberg-spark-runtime` 1.10.0 and later, see the version
1414
pinned in `ci/docker/integration/runner/Dockerfile`.
@@ -30,8 +30,10 @@
3030
get_uuid_str,
3131
)
3232

33-
# The reserved field id of `_row_id`, which a manifest uses to key statistics about it.
33+
# The reserved field ids of `_row_id` and `_last_updated_sequence_number`, which a manifest uses to
34+
# key statistics about them.
3435
ROW_ID_FIELD_ID = 2147483540
36+
LAST_UPDATED_SEQUENCE_NUMBER_FIELD_ID = 2147483539
3537

3638
# Both runs must read the same data, so only the metadata-level skip differs between them.
3739
PRUNING_DISABLED = {
@@ -113,14 +115,15 @@ def test_row_id_filter_prunes_files(started_cluster_iceberg_with_spark, storage_
113115
== 0
114116
)
115117

116-
# A point lookup touches the one file whose row id range contains the value.
118+
# Spark writes no lineage statistics for new rows, so a file may hold any row id up to the top of
119+
# its block: a point lookup skips only the files whose whole block is below the value.
117120
assert (
118121
_pruned_files(
119122
instance,
120123
TABLE_NAME,
121124
f"SELECT id FROM {table_expression} WHERE _row_id = 25 ORDER BY ALL",
122125
)
123-
== 4
126+
== 2
124127
)
125128

126129
# A half-open range keeps the two files above it.
@@ -139,7 +142,7 @@ def test_row_id_filter_prunes_files(started_cluster_iceberg_with_spark, storage_
139142
TABLE_NAME,
140143
f"SELECT id FROM {table_expression} WHERE _row_id < 10 ORDER BY ALL",
141144
)
142-
== 4
145+
== 0
143146
)
144147

145148
# A range spanning everything prunes nothing.
@@ -184,7 +187,7 @@ def test_incremental_read_by_sequence_number_prunes_files(
184187
TABLE_NAME,
185188
f"SELECT id FROM {table_expression} WHERE _last_updated_sequence_number = 2 ORDER BY ALL",
186189
)
187-
== 4
190+
== 1
188191
)
189192

190193
assert (
@@ -466,7 +469,7 @@ def declare_inverted_row_id_bounds(record):
466469
started_cluster_iceberg_with_spark, storage_type, TABLE_NAME
467470
)
468471

469-
# The file is read, so the four whose blocks cannot hold row id 25 are the only ones skipped.
472+
# The file is read, so the two whose blocks lie below row id 25 are the only ones skipped.
470473
# Swapping the pair instead would exclude 25 and skip the file holding it, and pruning on the
471474
# pair as declared would skip it too, both losing the row the filter asks for.
472475
assert (
@@ -475,7 +478,7 @@ def declare_inverted_row_id_bounds(record):
475478
TABLE_NAME,
476479
f"SELECT id FROM {table_expression} WHERE _row_id = 25 ORDER BY ALL",
477480
)
478-
== 4
481+
== 2
479482
)
480483

481484
assert (
@@ -524,12 +527,13 @@ def declare_negative_first_row_id(record):
524527
)
525528

526529
# The file whose block does not fit is read whatever the filter says, while the second file,
527-
# whose block is well formed, still prunes.
530+
# whose block is well formed, still prunes. The first row of the former has row id 2^64 - 1, so
531+
# pruning it on its wrapped block would drop that row.
528532
assert (
529533
_pruned_files(
530534
instance,
531535
TABLE_NAME,
532-
f"SELECT id FROM {table_expression} WHERE _row_id = 5 ORDER BY ALL",
536+
f"SELECT id FROM {table_expression} WHERE _row_id >= 20 ORDER BY ALL",
533537
)
534538
== 1
535539
)
@@ -546,6 +550,116 @@ def declare_negative_first_row_id(record):
546550
)
547551

548552

553+
@pytest.mark.parametrize("storage_type", ["s3"])
554+
def test_materialized_row_lineage_without_statistics_is_not_pruned_away(
555+
started_cluster_iceberg_with_spark, storage_type
556+
):
557+
instance = started_cluster_iceberg_with_spark.instances["node1"]
558+
spark = started_cluster_iceberg_with_spark.spark_session
559+
TABLE_NAME = "test_row_lineage_no_lineage_stats_" + storage_type + "_" + get_uuid_str()
560+
561+
# The rewritten file of `test_materialized_row_ids_are_not_pruned_away`: it keeps the row ids 0..3
562+
# and the sequence number 1 of its untouched rows, below its inherited block 4..7 and number 2.
563+
spark.sql(
564+
f"CREATE TABLE {TABLE_NAME} (id bigint, data string) USING iceberg "
565+
f"TBLPROPERTIES ('format-version' = '3', 'write.update.mode' = 'copy-on-write')"
566+
)
567+
spark.sql(f"INSERT INTO {TABLE_NAME} select id, 'a' from range(0, 4)")
568+
spark.sql(f"UPDATE {TABLE_NAME} SET data = 'z' WHERE id = 1")
569+
570+
lineage_field_ids = (ROW_ID_FIELD_ID, LAST_UPDATED_SEQUENCE_NUMBER_FIELD_ID)
571+
sizes_left_for_table_columns = []
572+
573+
def drop_row_lineage_statistics(record):
574+
# Every metric map is optional and a missing id means "unknown", so a writer may keep the
575+
# statistics of the table columns and leave out those of the reserved lineage columns.
576+
data_file = record["data_file"]
577+
dropped = False
578+
for name in (
579+
"column_sizes",
580+
"value_counts",
581+
"null_value_counts",
582+
"nan_value_counts",
583+
"lower_bounds",
584+
"upper_bounds",
585+
):
586+
stats = data_file.get(name)
587+
if stats is None:
588+
continue
589+
kept = [item for item in stats if item["key"] not in lineage_field_ids]
590+
dropped = dropped or len(kept) != len(stats)
591+
data_file[name] = kept
592+
if dropped:
593+
sizes_left_for_table_columns.append(len(data_file["column_sizes"] or []))
594+
return dropped
595+
596+
with open(_current_manifest_list(TABLE_NAME), "rb") as f:
597+
reader = avro.datafile.DataFileReader(f, avro.io.DatumReader())
598+
data_manifests = [
599+
entry["manifest_path"] for entry in reader if entry["content"] == 0
600+
]
601+
reader.close()
602+
603+
# Only the rewritten file has lineage statistics to drop, and it must keep sizes for its table
604+
# columns: the manifest then knows about some columns of the file, but nothing about lineage.
605+
assert (
606+
sum(_rewrite_avro(path, drop_row_lineage_statistics) for path in data_manifests)
607+
== 1
608+
)
609+
assert sizes_left_for_table_columns[0] > 0
610+
611+
_publish(started_cluster_iceberg_with_spark, storage_type, TABLE_NAME)
612+
table_expression = _table_function(
613+
started_cluster_iceberg_with_spark, storage_type, TABLE_NAME
614+
)
615+
616+
for row_key in range(4):
617+
assert (
618+
instance.query(
619+
f"SELECT id FROM {table_expression} WHERE _row_id = {row_key}",
620+
settings=PRUNING_ENABLED,
621+
).strip()
622+
== str(row_key)
623+
)
624+
625+
assert (
626+
instance.query(
627+
f"SELECT id FROM {table_expression} WHERE _last_updated_sequence_number = 1 ORDER BY ALL",
628+
settings=PRUNING_ENABLED,
629+
).strip()
630+
== "0\n2\n3"
631+
)
632+
633+
assert (
634+
_pruned_files(
635+
instance,
636+
TABLE_NAME,
637+
f"SELECT id FROM {table_expression} WHERE _row_id = 1 ORDER BY ALL",
638+
)
639+
== 0
640+
)
641+
642+
# Every value the file can hold is still at most the top of its inherited block, so a filter
643+
# above it prunes the file.
644+
assert (
645+
_pruned_files(
646+
instance,
647+
TABLE_NAME,
648+
f"SELECT id FROM {table_expression} WHERE _row_id >= 8 ORDER BY ALL",
649+
)
650+
== 1
651+
)
652+
653+
assert (
654+
_pruned_files(
655+
instance,
656+
TABLE_NAME,
657+
f"SELECT id FROM {table_expression} WHERE _last_updated_sequence_number > 2 ORDER BY ALL",
658+
)
659+
== 1
660+
)
661+
662+
549663
# The tests above read what Spark wrote. The ones below prune over metadata ClickHouse wrote itself:
550664
# the inherited row id range of a manifest entry is only as good as the `first_row_id` the writer put
551665
# into the manifest list, so the same filters are replayed against a table of its own making.
@@ -583,12 +697,13 @@ def test_row_id_filter_prunes_files_clickhouse(started_cluster_iceberg_with_spar
583697

584698
assert _pruned_files(instance, TABLE_NAME, f"SELECT id FROM {TABLE_NAME} ORDER BY ALL") == 0
585699

586-
# A point lookup touches the one file whose row id range contains the value.
700+
# No lineage statistics are written for new rows, so a point lookup skips only the files whose
701+
# whole block is below the value.
587702
assert (
588703
_pruned_files(
589704
instance, TABLE_NAME, f"SELECT id FROM {TABLE_NAME} WHERE _row_id = 25 ORDER BY ALL"
590705
)
591-
== 4
706+
== 2
592707
)
593708

594709
# A half-open range keeps the two files above it.
@@ -603,7 +718,7 @@ def test_row_id_filter_prunes_files_clickhouse(started_cluster_iceberg_with_spar
603718
_pruned_files(
604719
instance, TABLE_NAME, f"SELECT id FROM {TABLE_NAME} WHERE _row_id < 10 ORDER BY ALL"
605720
)
606-
== 4
721+
== 0
607722
)
608723

609724
# A range spanning everything prunes nothing.
@@ -641,7 +756,7 @@ def test_incremental_read_by_sequence_number_prunes_files_clickhouse(
641756
TABLE_NAME,
642757
f"SELECT id FROM {TABLE_NAME} WHERE _last_updated_sequence_number = 2 ORDER BY ALL",
643758
)
644-
== 4
759+
== 1
645760
)
646761

647762
assert (

0 commit comments

Comments
 (0)