DataFusion plans a CTE once per reference, so ML queries grow exponentially with depth
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 64/100
- Issue-Typ
- Dokumentation
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- postgresql, python
- Bereich
- backend, data-engineering
Rechercherichtung
Beginne mit docs/limitations.md, das im Issue als der Ort genannt wird, an dem die wiederholte Planung von CTEs durch DataFusion und die Umgehungslösung, ein Zwischenergebnis als Tabelle zu registrieren, dokumentiert werden sollen. Gleiche das dokumentierte Verhalten mit der gemeldeten Einschränkung in DataFusion 54 ab und erkläre, dass die Unterstützung materialisierter CTEs und automatische Entscheidungen vorgelagerte Arbeiten sind. Fertig ist die Arbeit, wenn Leser die Einschränkung verstehen und die Umgehungslösung erkennen können.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
DataFusion plans a CTE once for every place that reads it, so a query whose CTEs read earlier CTEs more than once grows exponentially with its depth. DuckDB computes such a CTE once. ML models written in SQL hit this constantly: a layer norm reads its input three times, a residual connection twice, and a message-passing round reads the node states as sender, receiver and residual.
Reproduction
Each CTE joins the previous one with itself:
from datafusion import SessionContext
def query(depth):
ctes = ["c0 AS (SELECT i, x FROM t)"] + [
f"c{d} AS (SELECT a.i, a.x + b.x AS x FROM c{d-1} a JOIN c{d-1} b ON a.i = b.i)"
for d in range(1, depth + 1)
]
return f"WITH {', '.join(ctes)} SELECT SUM(x) FROM c{depth}"
ctx = SessionContext()
ctx.sql("CREATE TABLE t AS SELECT * FROM (VALUES (1, 1.0), (2, 2.0)) v(i, x)").collect()
plan = ctx.sql(query(8)).execution_plan().display_indent()
| depth | DataFusion operators | DataFusion table scans | DuckDB 1.5.5 operators |
|---|---|---|---|
| 2 | 16 | 4 | 13 |
| 4 | 64 | 16 | 23 |
| 6 | 256 | 64 | 33 |
| 8 | 1,024 | 256 | 43 |
In the ML benchmarks
The same queries, on DataFusion 54.0.0 through xarray_sql.Context and on DuckDB through xql.register, Intel i7-8700:
| Query | DataFusion operators | DuckDB operators | DataFusion | DuckDB |
|---|---|---|---|---|
| 1-layer GPT loss, batch 4 (112 lines of SQL) | 12,166 | 120 | 8.4 s | 0.055 s |
| GraphCast-shaped forecast, 1 processor round | 1,816 | 243 | 43 s | 17 s |
| GraphCast-shaped forecast, 2 processor rounds | 5,948 | 282 | 159 s | 13 s |
A table read through xarray-sql is read again for every inlined copy. With ERA5 registered lazily from GCS, the 1-round forecast spent most of its time waiting on the network, so the benchmark loads its three time steps into memory first.
A training step through ddx (forward and backward) takes 2.7 s on DataFusion, less than the 8.4 s forward query alone, because ddx stores each step of its program as a table.
Upstream
DataFusion is working on this. apache/datafusion#25580 added WITH ... AS MATERIALIZED, in the PostgreSQL dialect only (apache/datafusion#17737), and apache/datafusion#26237 proposes deciding automatically, as PostgreSQL and DuckDB do. Neither is in DataFusion 54.
Options for xarray-sql
- Once a DataFusion release has
AS MATERIALIZED, check that it works with xarray-sql's table providers and thedatafusiondialectContextuses, and document it. - Follow apache/datafusion#26237, which would fix this without changing queries.
- Until then, document the behavior in
docs/limitations.md, with the workaround: register an intermediate result as a table.
- Vorherrschende Sprache
- Python
- Sterne
- 142
- Forks
- 19
- Ø Merge
- 1 T. 9 Std.
- Gemergte PRs (30 T.)
- 10
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus xqlsystems/xarray-sql
-
documentation
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 85/100
xqlsystems/xarray-sql#268 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 74/100
xqlsystems/xarray-sql#243 ·
Maintainer antworten meist innerhalb von 1 Tag
-
publish.yml: the TestPyPI verification step is unreachable (and installs the wrong package name)Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
xqlsystems/xarray-sql#242 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
xqlsystems/xarray-sql#237 ·
Maintainer antworten meist innerhalb von 1 Tag
-
page not found errorOffen
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 72/100
xqlsystems/xarray-sql#231 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in xqlsystems/xarray-sql
Ähnliche Issues
-
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 68/100
FavioVazquez/showtime#101 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 83/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
rossant/awesome-math#199 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
nautechsystems/nautilus_trader#5365 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Examples say cnxOffendocumentation embedded
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 1 Tag