Commit 6ac2daa
authored
Unify arrow exports across all query result types (#495)
This PR unifies arrow exports across query result types, and makes sure
we always provide the schema from within a transaction.
We are dealing with 3 arrow export types:
- Arrow Table
- Arrow RecordBatch
- Arrow C Stream
... across 3 result types:
- StreamingQueryResult
- ArrowQueryResult
- StreamingQueryResult
The `StreamingQueryResult` paths are now unified. We re-feed the backing
ColumnDataCollection to the engine for parallel conversion into a
`ArrowQueryResult`, and then we delegate to the corresponding
`ArrowQueryResult` path.
The `ArrowQueryResult` paths deal with materialized data already, and we
have no way to plug into the transaction that generated it. The actual
fix for this is to cache the schema when creating the
`ArrowQueryResult`, during `Finalize`. This is a core change that we
will probably apply in v2.0. The workaround is to fetch the schema in a
separate transaction. For all paths, since we are already dealing with
materialized data, we create an arrow table. Then for the streaming
paths we return the corresponding stream types directly from the table.
The `StreamingQueryResult` paths always have access to a valid
transaction context, and can get the arrow schema on demand even when
that requires catalog access.
As a side effect of this PR, consuming an arrow c stream (reading from
`con.sql(q).__arrow_c_stream__()`) is now lazy, i.e. not materialized.
This makes consumption of course slower, but allows streaming much
larger datasets.
The materialized paths are overall a little faster, and the non-c stream
streaming paths as well.
```
┌───────────────────────────────────────────────────┬────────────────────┬───────────────────┬───────────────────┐
│ benchmark expression │ wall base→now (ms) │ CPU base→now (ms) │ mem base→now (MB) │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ r=con.sql(q); r.execute(); r.to_arrow_table() │ 159 → 161 │ 259 → 286 │ 847 → 875 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ r=con.sql(q); r.execute(); r.to_arrow_reader() │ 161 → 144 │ 255 → 263 │ 896 → 877 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ r=con.sql(q); r.execute(); r.__arrow_c_stream__() │ 157 → 136 │ 282 → 235 │ 854 → 881 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ con.sql(q).to_arrow_table() │ 52 → 35 │ 267 → 244 │ 855 → 854 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ con.execute(q).to_arrow_table() │ 202 → 174 │ 212 → 193 │ 548 → 554 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ con.sql(q).to_arrow_reader() │ 186 → 175 │ 199 → 187 │ 552 → 552 │
├───────────────────────────────────────────────────┼────────────────────┼───────────────────┼───────────────────┤
│ con.sql(q).__arrow_c_stream__() │ 48 → 173 │ 250 → 189 │ 857 → 554 │
└───────────────────────────────────────────────────┴────────────────────┴───────────────────┴───────────────────┘
```11 files changed
Lines changed: 571 additions & 365 deletions
File tree
- .github/workflows
- src/duckdb_py
- arrow
- include/duckdb_python
- arrow
- tests/fast/arrow
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | | - | |
| 20 | + | |
22 | 21 | | |
23 | 22 | | |
24 | 23 | | |
25 | | - | |
26 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
27 | 39 | | |
28 | 40 | | |
29 | | - | |
30 | | - | |
31 | | - | |
| 41 | + | |
32 | 42 | | |
33 | 43 | | |
34 | 44 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | | - | |
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
47 | | - | |
| 45 | + | |
48 | 46 | | |
49 | 47 | | |
50 | 48 | | |
| |||
71 | 69 | | |
72 | 70 | | |
73 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
74 | 85 | | |
75 | 86 | | |
76 | 87 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
960 | 960 | | |
961 | 961 | | |
962 | 962 | | |
963 | | - | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
964 | 967 | | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | 968 | | |
969 | 969 | | |
970 | 970 | | |
| |||
991 | 991 | | |
992 | 992 | | |
993 | 993 | | |
994 | | - | |
995 | | - | |
996 | | - | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
1005 | | - | |
1006 | | - | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
1007 | 997 | | |
1008 | 998 | | |
1009 | 999 | | |
| |||
1049 | 1039 | | |
1050 | 1040 | | |
1051 | 1041 | | |
| 1042 | + | |
1052 | 1043 | | |
1053 | 1044 | | |
1054 | 1045 | | |
| |||
0 commit comments