Skip to content

Let modifiers and INTO formats only read the finished result #2699

Open

Description

@mathiasrw

What happens today

  • For INTO OBJECT(), compileInScope (src/select/select.js) sets query.intoObject (line 303), and compileSelect1 (src/select/columns.js, line 503) then compiles an arrow column without alias (a->b->c) into nested assignments.
  • It also sets query.preserveArrayResult (line 483). Without that flag queryfn (src/select/queryfn.js, line 72) turns a source's array into its length whenever the query has an INTO.
  • Every INTO target becomes generated code in compileInScope (lines 418-512). query.intofns and query.intoallfns are strings turned into functions with new Function, called at the end of queryfn3 (lines 526-540).
  • Modifiers are a switch in modify() (src/select/select.js, line 940).

What to change

  1. In src/select/select.js replace the switch in modify() with a table of functions that read {columns, rows, distinct}. columns comes from resultColumns (Decide the result columns of a SELECT in one function and let modify() read them from it #2651).

    const modifiers = {
      VALUE: r => (r.rows.length ? r.rows[0][firstKey(r)] : undefined),
      COLUMN: r => { const k = firstKey(r); const a = r.rows.map(t => t[k]); return r.distinct ? distinctArray(a) : a; },
      RECORDSET: r => new alasql.Recordset({columns: r.columns, data: r.rows}),
      // ROW, MATRIX, INDEX, TEXTSTRING, ALASQL_DETAILS the same way
    };
  2. Remove query.intoObject and the arrow branch of compileSelect1. The compiler records the path of an arrow column without alias on its column (coldef.path = ['a', 'b', 'c']). alasql.into.OBJECT in src/formats/into.js builds the nested objects from columns, plain columns first and nested ones after.

  3. Remove query.preserveArrayResult and the array-to-length conversion in queryfn. Each alasql.into function and the table, ? and @variable targets return what alasql() returns today. Turn intofns and intoallfns into plain functions of (result, params, cb).

  4. INTO formats keep taking their columns from the first row.

What stays the same

  • The output of every modifier and the return value and file content of every INTO format.
  • The key order of INTO OBJECT(), and an arrow column with an alias stays flat.
  • SELECT INTO a table keeps its type conversion.
  • No result changes.

Done when

  • A new test file named after this issue pins the return value of SELECT ... INTO for OBJECT, JSON, CSV, TXT, a table, ?, $name and @variable, synchronously and with a callback, and the output of every modifier over a table, ? and an empty result.
  • src/select/columns.js and src/select/queryfn.js have no code that depends on the kind of INTO or on the modifier.
  • 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

#2651

Activity

  1. added this to the v4.x Focus milestone on Nov 26, 2026
  2. changed the title [-]Review #2276 for code improvements: INTO OBJECT() changes how the SELECT list is compiled[/-] [+]Let modifiers and INTO formats only read the finished result[/+] 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