Skip to content

Fix WITH using an async source like CSV() to close #1276 - #2571

Merged
mathiasrw merged 1 commit into
AlaSQL:developfrom
SahinurDEV:fix/with-csv-source
Oct 8, 2026
Merged

mathiasrw merged 1 commit into
AlaSQL:developfrom
SahinurDEV:fix/with-csv-source

Conversation

@SahinurDEV

Copy link
Copy Markdown
Contributor

Problem

A CTE that reads from an asynchronous source fails:

WITH c AS (SELECT * FROM CSV('data.csv', {headers:true}))
SELECT * FROM c

throws Error: Data source number 0 in undefined (uncaught, even when a callback or alasql.promise is used), while the same query written as a subselect works.

Cause

WithSelect.prototype.execute filled each temporary CTE table with tb.data = w.select.execute(databaseid, params), i.e. without a callback. CSV() (and other file sources) load the data asynchronously, so that call returns undefined and the main select then reads a table without data.

Fix

The CTEs are now executed one after another, each with a completion callback. The temporary table is filled once its select has finished, and only then is the next CTE (or the main select) run. Errors from a CTE are passed to the callback and the temporary tables are restored.

For synchronous sources the callbacks still run immediately, so alasql('WITH ... SELECT ...') keeps returning its result directly (the statement returns its own result rather than the callback's, so the result of the rest of the chain is kept and returned). Recursive CTEs are unchanged.

Tests

  • Added test/test1276.js: CSV source in a CTE with a callback and with alasql.promise, a CTE that uses a previous CSV-backed CTE, cleanup of the temporary table, and a synchronous multi-CTE query that still returns directly. The 4 async cases fail on develop and pass with this change.
  • yarn test-only (full suite): 2251 pass, 213 skip, 0 fail. yarn test-format passes.

Note: while testing I noticed that a non-recursive CTE with a column list (WITH c(x) AS (...)) fails with renameColumns is not defined. That happens on develop too and isn't touched here.

Fixes #1276

@mathiasrw

Copy link
Copy Markdown
Member

Thank you!

@mathiasrw
mathiasrw merged commit bc7005c into AlaSQL:develop Oct 8, 2026
10 checks passed
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.

WITH using a CSV source throws Error: Data source number 0 in undefined

2 participants