Repository navigation
Split the data written into files and objects by size - #116234
alexey-milovidov wants to merge 163 commits into
Conversation
`INSERT` into the `File`, `S3`, `AzureBlobStorage` and `HDFS` table engines and the corresponding table functions writes everything into a single file. Now the output can be limited in size: as soon as at least `engine_file_split_on_write_by_size_bytes` (`s3_...`, `azure_...`, `hdfs_...`) bytes have been written into the current file, the rest of the data goes into a new one. The new files are named with the same scheme as `*_create_new_file_on_insert`: `data.Parquet` -> `data.1.Parquet` -> `data.2.Parquet`, and they are added to the list of the files of the table, so they are visible for reading. Every partition of a partitioned write is split on its own. If the name of the first file already contains a number in this scheme, the numbering continues from it, so `data.5.Parquet` is followed by `data.6.Parquet`. It allows to start the numbering from an arbitrary offset, and to have the number in the first file as well. The same rule now applies to `*_create_new_file_on_insert`, which previously produced `data.5.1.Parquet`. The check is performed after writing a block, so a file can be larger than the requested size - the block that crossed the limit is written in full. The amount of the data as it is written to the file is taken into account, which means the size of the compressed data for a compressed file. Formats that buffer the data internally, such as `Parquet`, are taken into account only when they write the data out, on the boundary of a row group. If a file with the generated name already exists, the behavior is controlled by `*_create_new_file_on_insert` / `engine_file_allow_create_multiple_files`: if enabled, the number is skipped and the next unused name is taken, otherwise an exception is thrown, and the files written before that are left as is. With `*_truncate_on_insert` the existing files are overwritten instead. With the parallel formatting, the data is written into the buffer by a background thread, so the amount of the written data cannot be checked after every block without a data race. The output is formatted by a single thread while splitting is enabled. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [2102eb2] Summary: ✅
AI ReviewSummaryAdds opt-in splitting by written size for Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Co-Authored-By: Claude Fable 5 <[email protected]>
… Parquet Co-Authored-By: Claude Fable 5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes52 object files changed (+604.39 KiB total), 1 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units185 translation units recompiled, 1408 s compile time in total, 184 of them have a recent master baseline. |
nellicus
left a comment
There was a problem hiding this comment.
thanks for making this happen!
`findSequenceNumber` passed `npos` from `find_last_of` into `find_first_of`, so for top-level object storage keys such as `data.tsv` the extension was never found, and the number was appended at the end: `data.tsv.1` instead of `data.1.tsv`. This broke the `test_respect_object_existence_on_partitioned_write` and `test_insert_create_new_file` integration tests: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116234&sha=62e9c44e8ca7f51eff452cb6d1974e2da9858e93&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%20old%20analyzer%2C%201%2F6%29 Also add a unit test for `NumberedFileName`. #116234 Co-Authored-By: Claude Fable 5 <[email protected]>
…list For `File` and object storage engine tables, the split sequence was seeded from the persistent list of paths, which already contained the split files of the previous inserts. A second insert continued the numbering instead of starting over, so with `engine_file_truncate_on_insert` / `s3_truncate_on_insert` the files of the previous insert were left stale instead of being overwritten, and for object storage the sink even started writing into the last old shard. Now a truncating insert starts from the base name and resets the numbering, overwriting the previously split files, and the numbering of the split files is derived per insert from the name of the file the insert starts with. Add stateless tests for repeated inserts into the same `File` and `S3` engine tables. #116234 Co-Authored-By: Claude Fable 5 <[email protected]>
# Conflicts: # src/Storages/StorageFile.cpp
A truncating insert overwrites the whole dataset of the table. If a previous insert has produced more numbered files or objects than the current one, the leftovers were kept on disk and in the bucket: they were only removed from the in-memory list of the visible paths, so a wildcard reader over the directory or the prefix, as well as any external consumer, still saw the stale rows. Now the numbered tail of the previous insert is deleted before a truncating split write starts, for `File`, `S3`, `AzureBlobStorage`, and `HDFS`, including the partitioned writes.
`removeStaleSplitFiles` treated every `std::filesystem` error as the end of the numbered sequence, so a permission or I/O error on `data.N.tsv` let the truncating insert succeed with the stale shards still visible. Now the loop stops only when the next name does not exist, and a failed removal throws `CANNOT_UNLINK`. Test `05099_file_split_on_write_cleanup_error` replaces a numbered name with a non-empty directory and checks that the insert fails and the base file is left untouched. Addresses #116234 (comment)
The feature is wired for `AzureBlobStorage` as well, but the functional coverage exercised only `File` and `S3`. The test covers the split, the numbering offset, the engine table with a truncating insert that removes the leftovers, the taken-name exception, and skipping the taken names with `azure_create_new_file_on_insert`.
|
🕵 Merged master (clean), fixed the review finding about swallowed removal errors in The only red on the previous run, |
Address review: the cleanup and the reset of the path list of a truncating insert no longer depend on the current insert being split by size. If one insert produced `data.tsv`, `data.1.tsv`, `data.2.tsv` and the next one rewrites the table with `*_truncate_on_insert = 1` while `*_split_on_write_by_size_bytes` is back at 0, the numbered tail is deleted and dropped from the visible path list, so neither the table itself nor a wildcard reader over the same directory or prefix sees the stale rows. The cleanup stays attributed to the table: it runs either when the current insert is split by size (and therefore claims the numbered sequence anyway) or when the numbered tail is registered in the path list of this table. Nothing is attributed to a table that has no numbered tail of its own - that is the separately declined durable-metadata question. Also fix the `Build (wasm64)` failure `-Werror,-Wlifetime-safety-dangling-field-moved` in `StorageObjectStorageSink` the same way as it was fixed in `StorageFileSink`: the sink owns the buffer that writes into the object storage, and `write_buf` holds only the non-owning compressing wrapper around it, which is empty when the data is written uncompressed. Tests `05136_file_split_on_write_truncate_without_split` and `05137_s3_split_on_write_truncate_without_split`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ls to initialize In HDFS, opening the destination already creates the file. If creating the compressing wrapper or the output format failed afterwards (e.g. with `output_format_compression_zstd_window_log = 1`), the buffer was destroyed without the cancellation, which finalized it and left an empty file that was not a part of the table, but was read by a wildcard path. Now the write is cancelled when `initialize` fails, both in the constructor of `StorageObjectStorageSink` and when starting the next object of a split insert in `consume`, where `path_is_published` is reset before the initialization, as in the `File` sink. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Storages/StorageFile.cpp
|
🕵 Merged The only red check on the previous head was |
Conflicts: master added `[[nodiscard]]` to the configuration accessors (`modernize-use-nodiscard`), keep the PR's new path accessors and mark the `const` ones `[[nodiscard]]` too. Master added `format_settings_` to `StorageFileSource::FilesIterator`, the PR added `read_lock_`; keep both, with `read_lock_` last. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master enabled `modernize-use-nodiscard`, so the `const` member functions added by this PR need the attribute as well. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ace on `TRUNCATE TABLE` Address review: `TRUNCATE TABLE` of a `File` table deleted every file after the first one, including the files created by `engine_file_allow_create_multiple_files` alone. Before, they were truncated in place, which needs only the permission to write the files, not the directory. Now only the files written by an insert split by size are deleted, the other ones are truncated in place as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…starting key is reserved Address review: a truncating insert took the list of the keys to delete from a snapshot made before it reserved its starting key. A concurrent insert could complete and publish its keys in between, and the truncating insert then either deleted them by number without retiring them from the list of the paths (so later reads failed with `FILE_DOESNT_EXIST`), or did not delete the extra keys at all (so their rows stayed visible after the rewrite). The cleanup set is now taken from the live list after the reservation, and `removeStaleSplitObjectsByNumber` retires every key it deletes while it still holds its removal reservation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The conflicts are with #129605, which checks the written keys of an insert against a filtered `WRITE ON` grant. The check is applied to the starting key of an insert, to the keys `checkAndGetNewFileOnInsertIfNeeded` steps aside into, and also to the numbered keys of this PR: `getNextKeyForSplittingBySize` and `removeStaleSplitObjectsByNumber` check every key before it is looked up, written or removed. The check is captured by value, because the sink calls it for every next object. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A truncating insert that is not split starts from the last key of the table that is not a part of a split tail (e.g. `data.1.tsv` written with `s3_create_new_file_on_insert`), while a split one starts from the raw key `data.tsv`. They reserved only their starting keys, so both could run at once: the split one stepped over the key the other one was writing in its cleanup, and the table read the rows of both rewrites. Every truncating insert now reserves the raw key of the table as well, and fails if another insert holds it. Address review: #116234 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ts starting key A truncating insert that is not split selected its starting key (the last key of the table that is not a part of a split tail, e.g. `data.1.tsv`) before it reserved the raw key of the table. A split truncating insert could rewrite the table and finish in between, removing `data.1.tsv`, and the first insert would then write and publish that stale key again next to the objects of the other rewrite. Now every truncating insert reserves the raw key first, and selects its starting key from the paths of the table read after the reservation. `checkAndGetNewFileOnInsertIfNeeded` does not reserve a key the insert already holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it insert A grant that allows only `data.tsv` must not allow writing `data.1.tsv` on a rollover, or deleting it in the cleanup of a truncating split insert, also for a partitioned insert. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lit_on_write_restricted_write_grant`
The grant `part/p[0-9]/data[.]tsv` did not match the path template
`part/p{_partition_id}/data.tsv`, which is checked when the table function is
executed, so the partitioned insert was refused before reaching the partition
writer, and the numbered-key check of the partitioned sink was not exercised.
Use `part/p[^/]+/data[.]tsv`, which allows the template and the base keys of
every partition but not their numbered keys, and assert that the base objects
are written before the rollover is refused.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… succeeds If registering a completed shard in the table throws (e.g. `MEMORY_LIMIT_EXCEEDED` while allocating the path or its provenance entry), the shard is no longer kept: - `StorageFileSink` sets `path_is_published` only after the publication callback returns, so the destructor removes the complete but unregistered file. - `StorageObjectStorageConfiguration::appendPath` is now transactional, and the publication callback of `StorageObjectStorage` removes the committed object of a new key that it has not managed to register. Otherwise a wildcard reader would see the rows of the failed insert, and the next smaller truncating insert would not delete the shard: it cleans only the remembered tail when the tail is not empty. The new failpoint `split_on_write_fail_to_publish_third_path` fails the registration of a new path when the table already lists two, which reproduces the scenario from the review for both `File` and `S3`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… without `fail` in their names After merging master the current version is `26.12`, so the history records of the `*_split_on_write_by_size_bytes` settings move there. The style check rejects test names containing `fail`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116234&sha=535277ff8e8a721c376633b3023172d1e2afc327&name_0=PR&name_1=Style%20check #116234 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged
|
|
🕵 The only failing check was |
|
🕵 The only failing check is |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=4 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
INSERTinto theFile,S3,AzureBlobStorageandHDFStable engines and the corresponding table functions writes everything into a single file, and there was no way to bound its size - onlyPARTITION BY(splitting by a key, not by size) or a new file perINSERTwith*_create_new_file_on_insert. Exporting a large table therefore produces one huge object, which is inconvenient to move, to read in parallel by other tools, and sometimes impossible to store.This adds four settings:
engine_file_split_on_write_by_size_bytess3_split_on_write_by_size_bytesazure_split_on_write_by_size_byteshdfs_split_on_write_by_size_bytesIf the value is not zero, as soon as at least this number of bytes has been written into the current file, the rest of the data goes into a new one. The new files are named with the same scheme as
*_create_new_file_on_insert:data.Parquet->data.1.Parquet->data.2.Parquet, and they are added to the list of the files of the table, so they are visible for reading from the same table. Every partition of a partitioned write is split on its own. If the numbered names of a partition can be the names of another partition (e.g.p_{_partition_id}.tsvwith aStringkey:p_a.1.tsvis both the first numbered file ofaand the first file ofa.1), an insert split by size is refused before anything is written; a non-digit character between{_partition_id}and the extension,{_partition_id}in a directory, or a partition key of an integer, date orUUIDtype makes the names unambiguous.The number is always inserted before the first dot of the name of the file, and a number that is already in the name is kept as is, so
data.5.Parquetis followed bydata.1.5.Parquet- the naming of*_create_new_file_on_insertis unchanged, with two exceptions that fix misplaced numbers. ForS3,AzureBlobStorageandHDFS, the number is placed into the name of the file, not into a dotted directory: the next object afterexports.v1/data.Parquetis nowexports.v1/data.1.Parquet(it used to beexports.1.v1/data.Parquet, under another prefix), asFilealready did. For a partitioned write with{_partition_id}, the number is placed into the path pattern before the partition id is substituted: for the partitiona.bofp_{_partition_id}_part.tsv, the next file is nowp_a.b_part.1.tsv(it used to bep_a.1.b_part.tsv, which is the name of a file of the partitiona.1.b).The decision is made after writing a block, so a file can be larger than the requested size - the block that crossed the limit is written in full. The amount of the data as it is written to the file is taken into account, which means the size of the compressed data for a compressed file, and the initial size as well for a file that is appended to. Formats that buffer the data internally, such as
Parquet, are taken into account only when they write the data out, on the boundary of a row group.If a file with the generated name already exists, the behavior is controlled by
*_create_new_file_on_insert(engine_file_allow_create_multiple_filesforFile): if it is enabled, the number is skipped and the next unused name is taken, otherwise an exception is thrown, and the files written before that are left as is. With*_truncate_on_insertthe existing files are overwritten instead, and the numbered files of a previous, larger insert are deleted, so that a wildcard reader over the directory or the prefix does not see the stale data. Only the files that the table knows it has written are deleted: a table keeps the list of its numbered files for as long as it stays in memory, and the deletion goes over that list, so the names that a previous insert had to skip with*_create_new_file_on_insertare left alone. When there is no such list - anINSERTinto the table function, a partitioned write, or a table that was reloaded since the previous insert - a truncating insert that is split by size claims and deletes the whole numbered sequence of its path instead, and even that is not done if*_create_new_file_on_insertis enabled at the same time.Two notes on the implementation:
ENGINE = File(TSV, stdout)) is not possible, and it throwsNOT_IMPLEMENTEDinstead of silently ignoring the setting.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added settings
engine_file_split_on_write_by_size_bytes,s3_split_on_write_by_size_bytes,azure_split_on_write_by_size_bytesandhdfs_split_on_write_by_size_bytes. If not zero,INSERTinto the corresponding table engine or table function starts a new file as soon as at least this number of bytes has been written into the current one, naming the new filesdata.1.Parquet,data.2.Parquet, etc.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/116234]