Skip to content

feat(db): add opt-in Arrow ingest via SupersetResultSet.from_arrow - #46353

Open
aoelvp94 wants to merge 15 commits into
apache:masterfrom
aoelvp94:feat/arrow-native-result-set
Open

aoelvp94 wants to merge 15 commits into
apache:masterfrom
aoelvp94:feat/arrow-native-result-set

Conversation

@aoelvp94

@aoelvp94 aoelvp94 commented Nov 19, 2026 •

Copy link
Copy Markdown

SUMMARY

I designed this change and drove it end to end: the scope comes from SIP #46289 and my demo repo, and I reviewed every commit, ran the tests, and checked the parity cases myself, including a local run of SQL Lab with the flag on and off. I used an AI coding assistant (Claude Code) to draft code, tests and this description under my direction. Here's the AI summary of the changes, I reviewed this and agree with it:

This is the Phase 2 (ingest) part of SIP #46289. It adds an opt-in path that builds SupersetResultSet from a pyarrow.Table instead of Python rows. The existing fetchall row path stays the default and the fallback. DuckDB is the only engine that opts in here.

  • SupersetResultSet.from_arrow(table, db_engine_spec, *, truncated=False) accepts a Table from any producer. It does not fetch anything and does not require ADBC. It normalizes like the row constructor: deduplicated column names, decoded dictionary columns, nested values stringified as JSON (and kept in nested_columns), JSON extension columns kept as text until to_pandas_df or nested_columns parses them, UUID stringified, opaque NUMERIC read as Decimal, other extension types stringified, and uint64 columns stringified when a value exceeds 2**63 - 1. Engine specs can adjust a column through a new normalize_arrow_column(field, column) hook, and vendor <VENDOR>:type field metadata is used for type names.
  • New feature flag ARROW_NATIVE_RESULT_SET (default False) is the global rollback. New engine-spec attribute supports_arrow_result (default False) is the per-engine opt-in. BaseEngineSpec.fetch_arrow(cursor, limit) reads from to_arrow_reader, fetch_record_batch, to_arrow_table or fetch_arrow_table, and stops reading a stream once limit rows are in. When a limit is set and the cursor can only fetch a full table, it returns None so the caller uses fetchmany.
  • fetch_arrow_result(db_engine_spec, cursor, limit) in superset/result_set.py is the single entry point. It checks the flag, fetches limit + 1 rows, and reports whether the result was truncated. Without the flag, an opt-in, or an Arrow-capable cursor, it returns (None, False) without touching the cursor.
  • Database.get_df (charts and datasets), SQL Lab execution (sql_lab._read_sqllab_payload), and the statement executor (_fetch_result_set) call it first and fall back to fetch_data. limiting_factor and truncation behave as before. The executor checks the query deadline after fetching and before building the result set, as the row path does.
  • DuckDB opts in. Its reader batches are capped at the row limit, INTERVAL becomes a timedelta with 30-day months (what DuckDB's Python fetch returns), and HUGEINT / UHUGEINT, which DuckDB exports as decimal128(38, 0), become int64, or strings when a value does not fit.

Out of scope: the /api/v1/chart/data Arrow wire format and frontend shim (Phase 1, #45363), Postgres/ADBC (a later PR), a generic Flight SQL engine spec, and any new required dependency.

Known, intentional differences from the row path, pinned in tests: an all-NULL column keeps its declared Arrow type, int32 keeps its width, and DuckDB DECIMAL(38, 0) reads as an integer because it has the same Arrow type as HUGEINT.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A, backend only. With the flag off nothing changes. With it on, SQL Lab and chart results are identical to the row path.

Ingest-path benchmark from the SIP, on apache/superset master f81191d, 1M rows, median of 3 rounds, total time through Arrow IPC: DuckDB 0.22s with from_arrow vs 5.83s on the row path (26.8×).

Re-run on this branch at 149303a, 100k rows, median of 3 rounds, total time through write_ipc_buffer (Linux VM, Intel Xeon). DuckDB is the only engine opted in on this PR; the Postgres and Flight SQL Arrow figures use ADBC + from_arrow in the same harness:

backend rows path from_arrow speedup
Postgres (psycopg2 vs ADBC) 2.317s 0.212s 10.9x
DuckDB 1.518s 0.067s 22.6x
Flight SQL (flightsql-dbapi vs ADBC) 2.560s 0.090s 28.6x

Harness and data: https://github.com/aoelvp94/superset-adbc-arrow-demo (see the reproduction comment on #46289).

TESTING INSTRUCTIONS

  1. Default config (ARROW_NATIVE_RESULT_SET off): charts and SQL Lab behave as today.
  2. Set FEATURE_FLAGS = {"ARROW_NATIVE_RESULT_SET": True}. DuckDB connections also need PREVENT_UNSAFE_DB_CONNECTIONS = False. Add a DuckDB database (duckdb:///:memory:).
  3. In SQL Lab, run with the flag on and then off:
SELECT range AS id,
       INTERVAL 1 MONTH + INTERVAL (range % 3) DAY + INTERVAL 3 HOUR AS iv,
       CASE WHEN range = 0 THEN 170141183460469231731687303715884105727::HUGEINT
            ELSE range::HUGEINT END AS big,
       (range % 7)::DECIMAL(10, 2) AS amount,
       DATE '2024-01-01' + range::INTEGER AS d,
       CASE WHEN range % 5 = 0 THEN NULL ELSE range END AS maybe,
       [range, range + 1] AS arr,
       {'k': range} AS st
FROM range(1000)

The results, column types and row count are the same both times. iv shows as 30 days 03:00:00, and the first big value keeps all 39 digits.
4. Unit tests:

pytest tests/unit_tests/result_set_from_arrow_test.py tests/unit_tests/result_set_test.py \
  tests/unit_tests/db_engine_specs/test_base.py tests/unit_tests/db_engine_specs/test_duckdb.py \
  tests/unit_tests/sql_lab_test.py tests/unit_tests/sql/execution/ tests/unit_tests/models/core_test.py

ADDITIONAL INFORMATION

  • Has associated issue: [SIP-242] An opt-in Apache Arrow path from query engines to the browser #46289
  • Required feature flags: ARROW_NATIVE_RESULT_SET (default False)
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

hold:sip: This PR is the Phase 2 reference implementation for SIP #46289 and should not be merged until the SIP vote passes. Could a committer add the hold:sip label? It does not duplicate #45363, which stays the Phase 1 chart-data work.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the doc Namespace | Anything related to documentation label Nov 19, 2026
Comment thread superset/db_engine_specs/base.py Resolved
Comment thread superset/result_set.py Resolved
@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟢  Low Risk

  • The PR appears safe to merge; limited reads avoid full-table materialization, and scalar JSON values are converted before consumers receive them.

Assessed commit: ea79bd151e88

@aoelvp94 aoelvp94 changed the title Add opt-in Arrow ingest via SupersetResultSet.from_arrow feat(db): add opt-in Arrow ingest via SupersetResultSet.from_arrow Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.28125% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.08%. Comparing base (93ace00) to head (4aa8ffc).
⚠️ Report is 12 commits behind head on master.

Files with missing lines Patch % Lines
superset/db_engine_specs/base.py 80.82% 8 Missing and 6 partials ⚠️
superset/result_set.py 89.65% 6 Missing and 6 partials ⚠️
superset/db_engine_specs/duckdb.py 90.90% 1 Missing and 1 partial ⚠️
superset/sql_lab.py 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #46353    +/-   ##
========================================
  Coverage   86.08%   86.08%            
========================================
  Files        3140     3140            
  Lines      219083   219318   +235     
  Branches    51921    51976    +55     
========================================
+ Hits       188600   188803   +203     
- Misses      27055    27072    +17     
- Partials     3428     3443    +15     
Flag Coverage Δ
hive 33.66% <19.92%> (-0.04%) ⬇️
mysql 51.40% <21.09%> (-0.07%) ⬇️
postgres 51.43% <21.09%> (-0.09%) ⬇️
presto 35.34% <21.09%> (-0.04%) ⬇️
python 88.57% <88.28%> (-0.01%) ⬇️
sqlite 51.11% <21.09%> (-0.07%) ⬇️
unit 84.14% <88.28%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

aoelvp94 and others added 5 commits November 23, 2026 21:12
Engine specs can set supports_arrow_result to fetch query results as a
pyarrow.Table when the DB-API cursor exposes an Arrow method; any other
cursor returns None and keeps fetch_data. Also adds the
ARROW_NATIVE_RESULT_SET feature flag (default off) that callers check
before asking for Arrow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Build a result set from a pyarrow.Table with the same column names,
nested-value handling, uint64 overflow rule and column metadata as the
row constructor, with an engine-spec hook for engine-specific types.
fetch_arrow_result fetches Arrow only when ARROW_NATIVE_RESULT_SET is on
and reports truncation from one extra row. Tests compare both paths down
to to_pandas_df(), and pin the two intended dtype differences (int32
width, all-NULL columns).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
duckdb-engine cursors return Arrow natively, so no ADBC driver or new
dependency is needed. INTERVAL and HUGEINT/UHUGEINT are converted to what
DuckDB's Python fetch returns, and limited reads cap the reader's batch
size. A parity test runs the same SQL through both paths on a real
DuckDB cursor. The inherited MotherDuck spec opts in too. Off unless
ARROW_NATIVE_RESULT_SET is enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With ARROW_NATIVE_RESULT_SET on and an opted-in engine, SQL Lab and the
statement executor fetch Arrow and use SupersetResultSet.from_arrow. One
extra row is still requested to detect truncation, as on the row path.
With the flag off the existing fetch_data path runs unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Chart and dataset queries that go through get_df build their DataFrame
via SupersetResultSet.from_arrow when ARROW_NATIVE_RESULT_SET is on and
the engine returns Arrow. The DataFrame, cache and response format are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aoelvp94
aoelvp94 force-pushed the feat/arrow-native-result-set branch from 96d47ca to 2d09484 Compare November 24, 2026 05:30
@aoelvp94

Copy link
Copy Markdown
Author

I replaced the branch with a reworked version:

  • DuckDB is the only engine that opts in here. The Postgres/ADBC part moves to a later PR, since a normal psycopg2 connection never used it.
  • DuckDB parity fixes: INTERVAL now matches DuckDB's Python fetch (it rendered as DateOffset(...) before), and HUGEINT/UHUGEINT keep their values and types.
  • The DuckDB reader no longer reads a full 1M-row batch when a row limit is set.
  • The SQL executor checks the query deadline before building the result set.
  • The flag check and the truncation logic moved into one helper, fetch_arrow_result, instead of being repeated in three callers. Tests sit next to each module.

I updated the description to match. Local checks: unit tests, the superset/sql 100% coverage gate and pre-commit pass, and SQL Lab returns the same results with the flag on and off.

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aoelvp94! Added hold:sip! until the SIP #46289 vote, as asked. Couple of small things inline in the meantime.

Comment thread superset/db_engine_specs/base.py Outdated
Comment on lines +1835 to +1837
table = table_method()
if limit is not None:
return table.slice(0, limit)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the rework lost the fix from codeant's thread, where a limited read with only to_arrow_table/fetch_arrow_table returned None so fetchmany could apply the limit. test_fetch_arrow_limits_a_table pins the materialize-then-slice version now. Was that intentional? DuckDB doesn't hit it, but the next engine to opt in without a streaming reader would.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, not intentional. Fixed in aoelvp94@e7fa6fa: with a limit and only a table fetch method, fetch_arrow now returns None so the caller uses fetchmany. The tests cover that case now.

Comment thread superset/db_engine_specs/duckdb.py Resolved
aoelvp94 and others added 6 commits November 25, 2026 03:39
Co-authored-by: Evan Rusackas <evan@preset.io>
to_arrow_table/fetch_arrow_table materialize the whole result, so a
limited read returns None and the caller uses fetchmany. Restore the
behavior the rework dropped.
The rework sent arrow.json, arrow.uuid and opaque NUMERIC through
stringify_extension_columns, a Python loop over to_pylist(). That made
from_arrow about 9x slower on Postgres and left JSON as text in
to_pandas_df. Restore the previous handling: JSON text stays in the
table until to_pandas_df or nested_columns, UUIDs are stringified with
a vectorized conversion, and NUMERIC becomes a decimal.
aoelvp94 and others added 3 commits November 26, 2026 09:54
Arrow's string-to-decimal cast does not reject values that overflow the
target precision: an 80-digit NUMERIC came back as a different number with
the wrong sign. Count the integer and fraction digits first, pick
decimal128 or decimal256 only when the value fits, and keep the exact text
otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

doc Namespace | Anything related to documentation hold:sip! size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants