Skip to content

Decide the result columns of a SELECT in one function and let modify() read them from it #2651

Open

Description

@mathiasrw

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.

  1. With query.dirtyColumns (a * over a source whose columns are unknown at compile time), it reads the names of the first alasql.options.columnlookup rows and adds the new ones to query.columns.
  2. Without compiled columns, it reads the names of the first columnlookup rows in a copy of the same loop.
  3. Without compiled columns and without rows, it joins the columns of every source in query.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

  1. Add resultColumns(run, rows) next to modify() in src/select/select.js. It holds the three branches, with the loop over the first columnlookup rows 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.columns today. Keep that, or return a new list in the same order.

  2. modify() calls resultColumns(query, res) once, and every modifier reads that list. The Object.keys(res[0]) fallbacks of VALUE, COLUMN, INDEX and TEXTSTRING stay.

  3. 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 than columnlookup with a new key after that limit, and a statement from alasql.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

  1. added this to the v4.x Focus milestone on Nov 26, 2026
  2. changed the title [-]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[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions