Skip to content

Keep the rows of a plain SELECT as tuples inside the engine and build objects only at output #3162

Backlog

Description

@mathiasrw

What happens today

A SELECT builds each result row as an object and keeps helper values in the same object. compileSelect1 (src/select/columns.js) generates var r={'a':...,'b':...}. compileSelect2 adds the sort keys $$$0 and the DISTINCT ON key $$distinctOn, and compileGroup (src/select/groupby.js) adds running aggregates such as _SUM_x. All are listed in query.removeKeys, and queryfn3 (src/select/queryfn.js) deletes them at the end. Until then DISTINCT, set operations and user functions see the helper keys, and a column with the same name collides with one.

What to change

Give the engine one internal shape for the rows of a result, {columns, rows} with each row an array in column order, and build objects once, at output.

  1. A SELECT takes the tuple path when it has no GROUP BY, no aggregate, no set operation, no ORDER BY, no DISTINCT or DISTINCT ON, no PIVOT or UNPIVOT, no window function, no ROWNUM, no INTO and no REMOVE COLUMNS, and the compiler knows every column (no dirtyColumns). Decide this once in compileInScope (src/select/select.js) and keep it as query.tuples = true. Everything else stays on the object path.

  2. Add compileSelectTuple(query) next to compileSelect1 in src/select/columns.js. It returns new Function('p,params,alasql', 'return [' + exprs + ']') in the order of query.columns, built with the same toJS calls as compileSelect1.

  3. In doJoin (src/select/dojoin.js) and the joinsorted branch of queryfn3, push the tuple when query.tuples is set. LIMIT and OFFSET work on the array of tuples.

  4. Add toObjects(columns, rows) next to resultColumns (Decide the result columns of a SELECT in one function and let modify() read them from it #2651). Call it at the end of queryfn3 before the result goes to the callback, so modify() and every modifier still receive objects.

    function toObjects(columns, rows) {
      const out = new Array(rows.length);
      for (let i = 0; i < rows.length; i++) {
        const t = rows[i], r = {};
        for (let j = 0; j < columns.length; j++) r[columns[j].columnid] = t[j];
        out[i] = r;
      }
      return out;
    }

Do not move the other paths here. ORDER BY is #3165, DISTINCT and set operations #3167, GROUP BY #2647 and #3168, modifiers and INTO #2699.

What stays the same

  • Every result, column name and order, key order of the returned objects, undefined versus a missing key, and the shape of every modifier. The parity check shows no difference.
  • SELECT a, a FROM ? gives [{a: 1}], and RECORDSET lists a twice.
  • Statements outside the scope above run as before.

Done when

  • A new test file named after this issue runs the queries in scope (a table, ? with known columns, a join, WHERE, LIMIT and OFFSET, expressions, aliases, a duplicate column name, an empty result) with each modifier. It compares each result with the same query forced onto the object path.
  • The same file checks that a query in scope never puts a key starting with $$ or _SUM_ into a row.
  • test/performance/ shows the tuple path is not slower than the object path on Node and Bun for a plain SELECT over 100,000 rows.
  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions