Repository navigation
Description
What happens today
modify() in src/select/select.js decides the columns of a result at run time, in three branches that each build their own list.
- With
query.dirtyColumns(a*over a source whose columns are unknown at compile time), it reads the names of the firstalasql.options.columnlookuprows and adds the new ones toquery.columns. - Without compiled columns, it reads the names of the first
columnlookuprows in a copy of the same loop. - Without compiled columns and without rows, it joins the
columnsof every source inquery.sources.
Every modifier reads the list from there, so nothing can get the columns of a result without running the modifier. src/graph/search.js calls the same modify().
const data = [{a: 1}, {a: 2, b: 3}];
alasql('SELECT RECORDSET * FROM ?', [data]).columns; // a, b
alasql('SELECT RECORDSET *, a+1 AS c FROM ?', [data]).columns; // c, a, b
alasql('SELECT RECORDSET * FROM ?', [[]]).columns; // []What to change
-
Add
resultColumns(run, rows)next tomodify()insrc/select/select.js. It holds the three branches, with the loop over the firstcolumnlookuprows written once as a helper.// The columns of the result of one run. Compiled columns when the compiler knows them, // otherwise read from the first rows, otherwise the columns of the sources. function resultColumns(run, rows) { const compiled = run.columns; if (run.dirtyColumns && rows.length > 0) return mergeColumns(compiled, namesInRows(rows)); if (compiled && compiled.length > 0) return compiled; if (rows.length > 0) return namesInRows(rows); return columnsOfSources(run.sources); }
Branch 1 pushes into
run.columnstoday. Keep that, or return a new list in the same order. -
modify()callsresultColumns(query, res)once, and every modifier reads that list. TheObject.keys(res[0])fallbacks of VALUE, COLUMN, INDEX and TEXTSTRING stay. -
Export it as
internal.resultColumns.
What stays the same
- Every result, column name, column order, modifier shape and the columns of an empty result. The parity check shows no difference.
- The INTO formats in
src/formats/keep taking their columns from the first row. - The columns a subquery gives the outer query (
src/select/from.js,src/select/join.js) and PIVOT (src/select/pivot.js).
Done when
- A new test file named after this issue pins the column lists of RECORDSET, MATRIX, ALASQL_DETAILS and COLUMN for these cases. They are
*over?with rows that have different keys,*plus a named column, an empty?, an empty table, a table with rows, more rows thancolumnlookupwith a new key after that limit, and a statement fromalasql.compile()run twice over data with different keys. All pass on main before the change. modify()has no loop over rows to find column names.- 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.
Depends on
Nothing, can start now.
Activity
[-]Review #2299 for code improvements: RECORDSET columns decided in three places[/-][+]Decide the result columns of a SELECT in one function and let modify() read them from it[/+]