Skip to content

Built-in functions read an unqualified column from the first table in a JOIN #2615

Description

@mathiasrw

In a query with more than one source, an unqualified column used as an argument of a built-in function such as UPPER, LENGTH, ROUND or IFNULL is always read from the first source. If the column belongs to another source, the function gets undefined. No error is raised.

Reproduced on develop:

alasql("CREATE TABLE one (a INT, b INT); INSERT INTO one VALUES (1,10)");
alasql("CREATE TABLE two (a INT, w STRING); INSERT INTO two VALUES (1,'x')");

alasql('SELECT UPPER(w) AS u FROM one t0 JOIN two t1 ON t0.a = t1.a');                    // [{}]   expected [{u: 'X'}]
alasql('SELECT LENGTH(w) AS n FROM one t0 JOIN (SELECT a, w FROM two) t1 ON t0.a = t1.a'); // [{}]   expected [{n: 1}]
alasql('SELECT UPPER(t1.w) AS u FROM one t0 JOIN two t1 ON t0.a = t1.a');                 // [{u: 'X'}]  works when qualified

Cause

yy.FuncValue.prototype.toJS (src/55functions.js) compiles the arguments of alasql.stdlib functions with arg.toJS(context, tableid), without defcols. Without defcols, an unqualified column is looked up in the default (first) source. The other two branches of the same function (alasql.stdfn and user functions from alasql.fn) already pass defcols.

The 34 functions in alasql.stdlib are affected: ABS, CEIL/CEILING, CLONEDEEP, EXP, FLOOR, GREATEST, IFNULL, IIF, INSTR, ISNULL, LCASE/LOWER, LEAST, LEN/LENGTH, LTRIM, MAX, MID, MIN, NULLIF, POWER, RANDOM, ROUND, ROWNUM, ROW_NUMBER, GROUP_ROW_NUMBER, RTRIM, SQRT, SUBSTR/SUBSTRING, TRIM, UCASE/UPPER.

Direction

Pass defcols in the alasql.stdlib branch too: arg.toJS(context, tableid, defcols).

This fixes a result nobody can rely on, so it can go into 4.x.

Tests: each example above, plus one function per group (string, number, NULL handling) with a column from the second table and from a joined subquery.

Related: #832 / #2579 (unqualified columns from joined subqueries), #2578 (its UPPER(db.t1.v) case fails for this reason).

Activity

  1. added a commit that references this issue on Oct 29, 2026
    99c92b8
  2. added a commit that references this issue on Nov 25, 2026
    a2204a6
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