Repository navigation
Description
What happens today
FROM calls a file source through its datafn in compileFrom (src/select/from.js) as alasql.from[NAME](arg1, arg2, cb, idx, query), with queryfn2 as cb. Each file source in src/formats/from.js reads with alasql.utils.loadFile or loadBinaryFile (src/utils/fileio.js), parses, and handles errors on its own.
alasql.from.JSONand thejsonlfactory for JSONL and NDJSON each have their own handler that callsquery.cb(null, error). A bad JSONL line throws inside the load callback, outside any handler.alasql.from.TXTpasses no error handler toloadFile.alasql.from.CSVpassese => query.cb(null, e), which fails whenqueryis not given.XLSXLSX(XLS, XLSX, ODS) has a third copy of the handler.
This also fixes #3142.
What to change
-
Add
src/engine/source.jswith the interface and the one place that reports an error.// A source gives the rows of one run. columns is null when known only once rows arrive. // open returns an array (sync) or a promise of an array (async). // { columns: Column[] | null, open(ctx, args) } export function reportError(err, query) { const error = err instanceof Error ? err : new Error(err); if (query && query.cb) return query.cb(null, error); throw error; }
-
Add
fileSource({ext, binary, parse})insrc/formats/from.js.open(ctx, [filename, opts])appliesautoExtFilename, reads withloadFileorloadBinaryFile, runsparse(data, opts)inside atry, and resolves with the rows or rejects with the read or parse error. It reads synchronously when the caller passes no callback, as today. -
Write each format as a parse function only. These are
parseJSON,parseJSONL(keeps the messageCould not parse JSON at line N: ...),parseTXT,parseCSV(the currentparseText, with TSV and TAB setting the separator) andparseSheetfor XLS, XLSX and ODS (the currentprocessSheet).alasql.from.FILEkeeps choosing the format by extension. -
alasql.from.JSON,JSONL,NDJSON,TXT,CSV,TSV,TAB,XLS,XLSXandODSstay callable with(filename, opts, cb, idx, query). One adapter turns a source into that function.function fromSource(source) { return function (arg, opts, cb, idx, query) { let rows; const done = r => { rows = r; if (cb) rows = cb(r, idx, query); }; let opened; try { opened = source.open(query && query.ctx, [arg, opts], !!cb); } catch (e) { return reportError(e, query); } if (opened && typeof opened.then === 'function') opened.then(done, e => reportError(e, query)); else done(opened); return rows; }; }
Do not catch an error thrown by
cbhere. That is Run a statement through one runner so every error reaches the caller, and key the statement cache on every compile option #2652.
Only file sources move in this pull request.
What stays the same
- The rows, column names and error texts of every file format, for a found file and a missing one.
alasql.from.X(...)called directly returns what it returns today, also synchronously in Node.- A user function added to
alasql.fromworks as before. - A plain
alasql(sql)without a callback over a file still returnsundefined. - Results change only for alasql.promise never settles for TXT with a missing file or JSONL and NDJSON with a bad line #3142.
Done when
- A new test file named after this issue checks JSON, JSONL, NDJSON, TXT, CSV, TSV and XLSX.
alasql.promiseand the callback form reject with the error for a missing file and for content that cannot be parsed, and give today's rows for a good file. Each case has a time limit, so a promise that never settles fails the test. src/formats/from.jshas no error handler of its own left. Every file source goes throughreportError.- The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Fixes
Fixes #3142.
Activity
[-]Review #2082 for code improvements: each file source handles its own errors[/-][+]Give file sources one source interface that opens rows for a run and reports errors in one place[/+]