Skip to content

Use native Arrow batches and parallelize C++ table scans - #1144

Open
ColinLeeo wants to merge 3 commits into
developfrom
arrow-query-scan
Open

ColinLeeo wants to merge 3 commits into
developfrom
arrow-query-scan

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

C++ table scans now build Arrow buffers directly. Python imports those buffers as PyArrow record batches and reuses the schema across batches. Callers of get_next_tsblock() still receive a TsBlock through an adapter.

Parallel reads now start during device metadata lookup and column page planning. For aligned field predicates, the scanner prunes pages using statistics, decodes predicate columns to identify matching rows, then reads projected values for those rows. Each column releases its page buffers after its last selected row. This bounds the number of decoded pages kept alive when matches are sparse.

Batch semantics

batch_size counts output rows after filtering, offset and limit. A batch can span pages, chunks and devices. Only the final batch can be shorter. For example, 83 matching rows with batch_size=32 produce batches of 32, 32 and 19 rows.

Performance

ALP prototype and PyArrow Parquet

An isolated prototype of the scan path had higher median throughput and lower median peak RSS in all 12 workloads below. The margin on a full scan was 1.20× for 128 columns and 1.25× for 5,000 columns. The largest gain was projecting 16 of 5,000 columns (7.80×). These measurements used a benchmark bridge into the C++ executor. A full timing run through the public SDK at PR head f19a829a is still outstanding.

Both readers streamed the same selected rows and columns into Arrow batches. The benchmark checked column values, nulls and output batch sizes. Speedup is Parquet median elapsed time divided by TsFile median elapsed time. RSS is the median of per-process peaks, including Python and PyArrow.

Dataset Query TsFile ms Parquet ms Throughput speedup TsFile peak MiB Parquet peak MiB
128 columns Full scan 67.7 81.3 1.20× 220 378
128 columns Project 16 columns 16.3 19.6 1.20× 121 148
128 columns Time range (~10%) 25.3 36.4 1.44× 128 275
128 columns Scattered matches (~1%) 81.3 105.7 1.30× 129 408
128 columns Contiguous matches (12.5%) 27.3 96.4 3.53× 119 401
5,000 columns Full scan 152.3 190.2 1.25× 488 1,084
5,000 columns Project 16 columns 3.9 30.2 7.80× 111 130
5,000 columns Scattered matches (~1%) 114.9 288.3 2.51× 148 1,283
5,000 columns Contiguous matches (12.5%) 59.0 252.1 4.28× 221 1,329
256 devices × 32 columns Full scan 43.7 64.2 1.47× 108 210
256 devices × 32 columns Tag filter (12.5%) 6.1 18.6 3.05× 107 157
256 devices × 32 columns Scattered matches (~1%) 53.3 71.6 1.34× 117 212

Test setup

  • Apple M5 Pro, 24 GiB RAM, macOS 26.6.2, Python 3.11.15 and PyArrow 26.0.1. Six threads, warm file cache, fresh processes and randomized interleaving over 11 rounds per configuration.
  • TsFile used ALP for DOUBLE values, LZ4 compression and pread. The fixture writer used a 1 GiB flush threshold. Parquet used BYTE_STREAM_SPLIT for DOUBLE values and LZ4 compression. PyArrow Dataset scanned with projection and predicates. The timer included assembling the requested output batches. For the 5,000-column dataset we tested default and bounded readahead and used the faster Parquet median for each query.
  • Output batches contained 1,024 matching rows for the 5,000-column dataset and 4,096 for the others, except for the final partial batch.
Devices Time rows per device DOUBLE columns Total rows DOUBLE cells including nulls
16 32,768 128 524,288 67,108,864
2 8,192 5,000 16,384 81,920,000
256 2,048 32 524,288 16,777,216

Contributions of Arrow output and parallel scanning

A separate seven-round experiment used the earlier 64 MiB writer threshold, six threads and ALP data. Each row compares successive builds on the same input, starting at develop@6c4ad792, then native Arrow output (ed16d6a0), then query-wide parallel scanning (10dc1db1). Each timing is a median in milliseconds.

Query develop ms Arrow output ms Arrow + parallel ms Speedup over develop
128 columns · Full scan 1,045.0 273.7 123.4 8.47×
5,000 columns · Full scan 2,379.8 802.3 363.5 6.55×
5,000 columns · Scattered matches (~1%) 612.6 819.1 220.6 2.78×

For scattered matches across 5,000 columns, median peak RSS went from 389 MiB on develop to 798 MiB with Arrow output alone, then down to 153 MiB with parallel scanning and early release of completed page buffers. Wide full scans made a different tradeoff. In the 1 GiB writer experiment, reusing compressed input reduced a 5,000-column full scan from 100,033 to 19 pread calls and from 552.3 to 277.8 MiB of cumulative reads. Peak RSS rose from 223 to 488 MiB.

Verification

  • All 197 focused C++ tests passed on macOS and on Linux with ASan and UBSan. Nine scan tests also passed ten consecutive runs each under parallel CTest execution on both platforms.
  • All 12 Python Arrow and row-query interface tests passed. All 15 Python sanitizer ownership tests passed.
  • Cross-language reads passed for 1,672 Java-written fixtures read by C++ and 1,626 C++-written fixtures read by Java.

The broader Linux UBSan run reported signed integer overflow in the SPRINTZ predictor's IntFire::train. The same overflow was reproduced with the implementation from the develop base commit, 6c4ad792.

Prepare device metadata and column page plans with a bounded shared-pool scheduler. Prune aligned pages by statistics, evaluate predicate columns first, and decode output columns directly into selected Arrow rows.

Retain page decoder cursors across output batches, keep metadata allocations task-owned, and cover concurrent reads, deferred I/O, terminal errors, buffer ownership, and exact result batches.
@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.42152% with 188 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.18%. Comparing base (6c4ad79) to head (f19a829).

Files with missing lines Patch % Lines
cpp/src/reader/arrow/query_scan_executor.cc 85.58% 32 Missing and 33 partials ⚠️
cpp/src/reader/arrow/scan_page.cc 81.97% 14 Missing and 39 partials ⚠️
cpp/src/common/arrow_batch.cc 84.46% 12 Missing and 20 partials ⚠️
cpp/src/reader/table_query_executor.cc 51.72% 11 Missing and 3 partials ⚠️
cpp/src/common/tsblock/tuple_desc.h 0.00% 6 Missing ⚠️
cpp/src/cwrapper/tsfile_cwrapper.cc 66.66% 2 Missing and 3 partials ⚠️
cpp/src/reader/table_result_set.cc 89.13% 2 Missing and 3 partials ⚠️
cpp/src/reader/tsfile_executor.cc 71.42% 0 Missing and 4 partials ⚠️
cpp/src/file/tsfile_io_reader.cc 87.50% 0 Missing and 2 partials ⚠️
cpp/src/common/container/bit_map.h 75.00% 0 Missing and 1 partial ⚠️
... and 1 more
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1144      +/-   ##
===========================================
- Coverage    69.10%   68.18%   -0.92%     
===========================================
  Files          790      795       +5     
  Lines        58327    59325     +998     
  Branches      9453     9755     +302     
===========================================
+ Hits         40305    40451     +146     
- Misses       16292    17074     +782     
- Partials      1730     1800      +70     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Pair aligned value chunks with their shared clocks, keep the original column schema in TsBlock results, and stop planning chunks and devices the query cannot use. The CLI inherits the corrected reader behavior without changes to commands or options.
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.

2 participants