Skip to content

Let a source column win over a SELECT alias of the same name in HAVING to close #2878 - #3095

Closed
toyeshhm wants to merge 1 commit into
AlaSQL:mainfrom
toyeshhm:fix-2878-having-alias-source-column
Closed

toyeshhm wants to merge 1 commit into
AlaSQL:mainfrom
toyeshhm:fix-2878-having-alias-source-column

Conversation

@toyeshhm

Copy link
Copy Markdown

Fixes #2878.

The problem

A name in HAVING that is both a SELECT alias and a column of the source should read the source column. That only held for a table with column definitions. With a subquery, FROM ? or a table without columns the alias replaced the column:

var data = [{a: 1, cnt: 0}, {a: 1, cnt: 0}, {a: 2, cnt: 5}];
alasql('SELECT a, COUNT(*) AS cnt FROM (SELECT a, cnt FROM ?) s GROUP BY a HAVING cnt > 1', [data]);
// [{a: 1, cnt: 2}], expected [{a: 2, cnt: 1}]

And for a table with column definitions the alias was not used, but the column could not be read either, so the same query returned [].

The cause

resolveHavingAliases (src/select/having.js) asked query.defcols[columnid] whether the name is a source column, and defcols only lists the columns of tables that have a schema. When it did leave the name alone, the name compiled to g['cnt'], and the group object holds only the GROUP BY keys and the aggregates.

The fix

For a name that is a SELECT alias, HAVING now reads the column from the row the group keeps in query.groupSources when a source has that column, and the alias when none has it.

  • A table with column definitions is decided at compile time from the column sets in defcols['.'].
  • A subquery, a parameter and a table without columns are decided at run time with an in check on the row. Not substituting at all for these sources would break the alias cases that already work (test887 G, test2558), since nothing at compile time says whether n in FROM ? ... HAVING n > 1 is a column.

Names that are a GROUP BY key or ambiguous between two tables compile as before. The decision is made again on every compile, so a compiled statement follows the data of each run.

Tests

test/test2878.js covers a subquery, FROM ?, a table without column definitions, a table with them, a joined parameter, the alias when no source has the name, and a compiled statement run on data with and without the column. Cases A to E and G fail before the change (A to C return [{a: 1, cnt: 2}], D returns []).

Test D of test887.js now asserts the result, [{a: 2, cnt: 1}], in place of notDeepStrictEqual. It fails before the change too.

yarn test passes (2723 pass, 0 fail), and yarn test-format-all is clean.

Note on #3003

#3003 edits the same function for #2769. The two do not depend on each other, and whichever lands second needs a small rebase.

Copilot AI balanced review requested due to automatic review settings November 22, 2026 02:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mathiasrw

Copy link
Copy Markdown
Member

Thank you for this, and for the clear tests. The behaviour you propose (a source column wins over a SELECT alias of the same name in HAVING) matches SQLite, and it is what we want for the next major version.

We can't take it into the current releases, though. It changes the result of a common pattern:

SELECT city, SUM(amount) AS amount FROM ? GROUP BY city HAVING amount > 100

which filters on the sum today and would filter on a single row's amount with this change. Code relies on that, so it is a breaking change. #2878 is now in the 5.0.0 milestone and marked as breaking, and the fix there will come with the column-resolution work already under way, so I'm closing this PR. Your tests and the cases in #2878 will be used for it, and you'll be credited.

@mathiasrw mathiasrw closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A HAVING alias replaces a source column of the same name when the source is a subquery or FROM ?

3 participants