Skip to content

Improve selective reads for positional text formats - #105774

Open
niyue wants to merge 14 commits into
ClickHouse:masterfrom
niyue:perf/selective-read-csv
Open

niyue wants to merge 14 commits into
ClickHouse:masterfrom
niyue:perf/selective-read-csv

Conversation

@niyue

@niyue niyue commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Enable file-like storages to read only requested columns for positional row formats such as CSV, TSV, Raw, CustomSeparated, JSONCompactEachRow, and related aliases.

The change passes a ColumnMapping with the full input field order into input formats, so parsers can skip unrequested fields by position instead of deserializing them. This avoids parsing unused columns and allows queries like SELECT c2 to succeed even when an unrequested c1 contains data that would not parse as the declared type.

CustomSeparated enables this optimization only for CSV escaping, where skipped fields respect custom field and row delimiters.

This PR closes #62356

Changelog category (leave one):

  • Performance Improvement

Changelog entry:

  • Improve selective column reads from positional text formats such as CSV, TSV, Raw, CustomSeparated, and JSONCompactEachRow by skipping unrequested fields instead of deserializing them.

niyue added 5 commits May 25, 2026 16:13
which adds position-based subset column reading for formats such as `CSV`, `TSV`, `TabSeparatedRaw`, `Raw`, and `CustomSeparated`. File-like storages now pass a column mapping with the full input field order so unrequested fields can be skipped by the row parser instead of deserialized.
Use `SubsetOfColumnsByPositionSupportChecker` for the position-based subset column capability so the type name matches `subset_of_columns_by_position_support_checker`.
Add `checkIfFormatSupportsReadingSubsetOfColumns` to centralize the combination of normal subset column support and position-based subset column support. Keep direct `checkIfFormatSupportsSubsetOfColumnsByPosition` checks at call sites that need to build a positional `ColumnMapping`.
Register positional subset column support for `JSONCompactEachRow` and `JSONCompactStringsEachRow` so file-like reads can skip unrequested array positions.
Add a dynamic checker for by-position subset column support and use it so `CustomSeparated` enables the optimization only with `CSV` escaping, where skipped fields respect custom delimiters.
Extend the selective-read stateless test to cover `TabSeparated`, `TabSeparatedRaw`, `TSVRaw`, `Raw`, `CustomSeparated`, and `CustomSeparatedIgnoreSpaces`, including the `Raw` escaping non-support path.
@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label May 25, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b1a90a3]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (version 26.6.1.1), Stack trace: (STID: 2508-3fb1) FAIL cidb
AST fuzzer (amd_debug, targeted, old_compatibility) FAIL
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (version 26.6.1.1), Stack trace: (STID: 2508-3fb1) FAIL cidb
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 2/2) FAIL
Server died FAIL cidb, issue
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (version 26.6.1.1), Stack trace: (STID: 2508-3fb1) FAIL cidb
Stateless tests (amd_tsan, parallel, 1/2) FAIL
Server died FAIL cidb, issue
ThreadSanitizer: data race (STID: 5139-54f8) FAIL cidb
Stateless tests (arm_binary, parallel) FAIL
04033_iceberg_mismatched_location ERROR cidb
Server died FAIL cidb, issue
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (version 26.6.1.1), Stack trace: (STID: 2508-3fb1) FAIL cidb
Stateless tests (amd_llvm_coverage, 3/3) FAIL
Server died FAIL cidb, issue
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = vector (STID: 2508-436e) FAIL cidb
Integration tests (amd_llvm_coverage, 4/5) FAIL
test_distributed_format/test.py::test_single_file[test_cluster_internal_replication] FAIL cidb
test_distributed_format/test.py::test_single_file[test_cluster_no_internal_replication] FAIL cidb
test_distributed_format/test.py::test_two_files[test_cluster_internal_replication] FAIL cidb
test_distributed_format/test.py::test_two_files[test_cluster_no_internal_replication] FAIL cidb
test_distributed_format/test.py::test_single_file_old[test_cluster_internal_replication] FAIL cidb
test_distributed_format/test.py::test_single_file_old[test_cluster_no_internal_replication] FAIL cidb
Fast test (arm_darwin) ERROR
Build (amd_release) ERROR
Build (arm_release) ERROR

AI Review

Summary

This PR adds positional subset reads for file-like text formats by passing the full input field order into ColumnMapping, so selected columns can be parsed while unrequested fields are skipped. The correctness issues raised in earlier inline threads are addressed in the current code, and I did not find a new line-level correctness finding to post inline. The remaining gap is proof for the stated Performance Improvement.

Missing context / blind spots
  • ⚠️ No local build directory is available in this checkout, so I could not run the new stateless tests locally. Passing targeted CI would close this gap.
Tests
  • ⚠️ The PR is categorized as Performance Improvement, but I did not find targeted before/after evidence for the affected path. The available cloud benchmark comment is off-path for file-like CSV/TSV/JSONCompactEachRow reads. Please provide a focused measurement, for example reading a wide positional text file while selecting a small subset of columns, showing the old versus new parsing cost.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: add targeted before/after performance evidence for positional text-format subset reads.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 25, 2026
Comment thread src/Storages/StorageFile.cpp Resolved
Rebuild pre-populated `ColumnMapping` from detected input headers so auto header detection works with projected format headers.

Exclude hive path columns from file-like by-position mappings because they are not present in `File` and `URL` data files.
Comment thread src/Storages/StorageFile.cpp Resolved
niyue added 3 commits May 26, 2026 20:10
Create a fresh `ColumnMapping` when detected input headers need to rebuild positional mappings, so parallel parsers do not mutate shared mapping state.

Reset row input parsers by replacing their `ColumnMapping` while preserving the `is_set` state.
# Conflicts:
#	src/Processors/Formats/Impl/CSVRowInputFormat.cpp
Comment thread src/Formats/ColumnMapping.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR adds positional subset-of-columns reading for text formats (CSV, TSV, JSONCompactEachRow, CustomSeparated) when reading from file, URL, and object-storage sources, plus the column-mapping plumbing to skip unrequested input fields. ClickBench Q4 (-12.1%) and Q15 (-17.5%) are MergeTree table scans that never exercise that parsing path, so both flagged improvements are downgraded to not-sure: the deltas are run-to-run variance, not a real effect of this change. No query that this PR could plausibly affect was measured, so there is no performance signal to act on here.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 4 not_sure 264 232 -12.1% <0.0001 PR only touches CSV/TSV/JSONCompact subset-read plumbing for file/URL/S3; Q4 is a MergeTree scan it can't speed up
⚠️ 15 not_sure 251 207 -17.5% <0.0001 Same: text-format column-mapping change doesn't touch Q15's MergeTree execution path; -17.5% is run-to-run variance

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: fec5da15-1c8c-4114-b5c6-e4d1b21bcec7
  • MIRAI run: 0418d73c-fba8-46a3-b808-bafb689d2b98
  • PR check IDs:
    • clickbench_127445_1782214710
    • clickbench_127459_1782214710
    • clickbench_127465_1782214710
    • tpch_adapted_1_official_127472_1782214710
    • tpch_adapted_1_official_127482_1782214710
    • tpch_adapted_1_official_127492_1782214710

Keep `ReadFromFormatInfo` serialization byte-compatible by not serializing derived `ColumnMapping` state.

Rebuild header mappings through the same case-aware logic as `ColumnMapping::addColumns`, while still skipping known but unrequested input fields for position-based reads.

Add coverage for case-insensitive `CSVWithNames` header matching and unknown `CSV` header rejection.
niyue added 2 commits June 23, 2026 16:05
Register the by-position subset checker for `WithNames` and `WithNamesAndTypes` variants created by `registerWithNamesAndTypes`.

Keep `CustomSeparated` variants gated by `format_custom_escaping_rule = 'CSV'`.

Extend coverage for explicit header formats under `input_format_skip_unknown_fields = 0`.
Mirror the `StorageObjectStorageQueue::streamToViews` path when preparing direct `SELECT` reads.

This keeps hive partition columns out of the on-file positional mapping while still materializing them from the file path.
niyue added 2 commits June 23, 2026 18:44
Do not use by-position-only subset support for `file` table function structure hints.

The full inferred file structure is still needed to build positional `ColumnMapping` for formats such as `CSV`.

Add coverage for `use_structure_from_insertion_table_in_table_functions = 2` with `CSV`.
Do not use by-position-only subset support for object-storage table function structure hints.

The full inferred object structure is still needed to build positional `ColumnMapping` for formats such as `CSV`.
@abashkeev abashkeev added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 4, 2026
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
m7kss1 pushed a commit to m7kss1/ClickHouse that referenced this pull request Nov 20, 2026
…ueue

A direct SELECT from an S3Queue/AzureQueue table with use_hive_partitioning = 1
called prepareReadingFromFormat without PrepareReadingFromFormatHiveParams, so
the Hive columns were treated as file columns: the format returned the file's
value or the column default, and ObjectStorageQueueSource never appended the
value from the path. streamToViews already passes these parameters, which is
why a materialized view over the same table was correct.

Pass the same parameters in StorageObjectStorageQueue::read. A table without
Hive columns has empty file_columns and an empty map, which prepareReadingFromFormat
treats like the previous default call.

The same call change is part of the open ClickHouse#105774, where positional reads need it.

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

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

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance inprovement - parse only requested columns in TSV/CSV and similar text formats without header

4 participants