Skip to content

Add test for UNION with ORDER BY (issue #671) - #2277

Merged
mathiasrw merged 2 commits into
developfrom
copilot/fix-union-order-by-issue
Nov 30, 2025
Merged

mathiasrw merged 2 commits into
developfrom
copilot/fix-union-order-by-issue

Conversation

Copilot AI commented Nov 30, 2025 •

Copy link
Copy Markdown
Contributor

Thank you for the time you are putting into AlaSQL!

Issue #671 reported UNION ignoring ORDER BY. After investigation, this has been resolved in the current codebase.

Changes:

  • Added test/test671.js covering UNION/UNION ALL with ORDER BY scenarios

Test coverage:

// UNION ALL with ORDER BY on alias
alasql('SELECT id, Date1 as theDate FROM $0 UNION ALL SELECT id, Date2 as theDate FROM $0 ORDER BY theDate', [data])

// UNION ALL with DESC
alasql('SELECT id, Date1 as theDate FROM $0 UNION ALL SELECT id, Date2 as theDate FROM $0 ORDER BY theDate DESC', [data])

// UNION (dedup) with ORDER BY
alasql('SELECT a FROM $0 WHERE a < 3 UNION SELECT a FROM $0 WHERE a > 1 ORDER BY a DESC', [data])

// Multiple ? parameters with ORDER BY
alasql('SELECT id, value FROM ? UNION ALL SELECT id, value FROM ? ORDER BY id', [data1, data2])

Note: Original issue's Data source number 0 in undefined error was a usage issue—using two ? placeholders requires passing data twice [projects, projects] or using $0 reference.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • upload.wikimedia.org
    • Triggering command: /usr/local/bin/node /usr/local/bin/node node_modules/mocha/bin/mocha.js ./test --reporter dot --bail (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>UNION ignores ORDER BY</issue_title>
<issue_description>I'm trying to use UNION ALL for selecting two different columns of sometimes the same rows in order to create a list of dates, see below a simplified version of what I'm trying to do:

alasql('SELECT [projectID], [Date1] FROM ? UNION ALL SELECT [projectID], [Date2] FROM ?',[projects])

As far as I know, this should work. However, no matter what I do, as long as UNION or UNION ALL is present in the code I get the following error:

alasql.js:6580 Uncaught Error: Data source number 0 in undefined
    doJoin                      @ alasql.js:6580
    queryfn3                    @ alasql.js:6086
    queryfn2                    @ alasql.js:6061
    (anonymous function)        @ VM3835:3
    (anonymous function)        @ alasql.js:6012
    queryfn                     @ alasql.js:6009
    tatement                    @ alasql.js:7031
    queryfn3                    @ alasql.js:6172
    queryfn2                    @ alasql.js:6061
    (anonymous function)        @ VM3833:3
    (anonymous function)        @ alasql.js:6012
    queryfn                     @ alasql.js:6009
    statement                   @ alasql.js:7031
    alasql.dexec                @ alasql.js:4304
    alasql.exec                 @ alasql.js:4255
    alasql                      @ alasql.js:128
    (anonymous function)        @ functions.js:1635     => this is where I have the above code
    (anonymous function)        @ alasql.js:7044
    queryfn3                    @ alasql.js:6330
    queryfn2                    @ alasql.js:6061
    (anonymous function)        @ alasql.js:15353
    xhr.onload                  @ alasql.js:3443

(I used the latest alasql.js 0.2.6 version.)

Am I doing something wrong, or is this a bug?
</issue_description>

<agent_instructions>Please add a new testfile named test/test671.js replicating this issue.

I have a feeling this is solved by now. Can you please confirm if its working?

If its working please PR the test file. If its not working plesea work on a solution.

</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@mathiasrw Thank you for a really well described error.

Does @agershun have any inputs on this?

@mathiasrw just saw your original sql

alasql('SELECT [projectID], [Date1] FROM ? UNION ALL SELECT [projectID], [Date2] FROM ?',[projects])

first ? will give the first index (projects) and second ? will give the second index - but there is no data in the second index.

Please try again with something like

alasql('SELECT [projectID], [Date1] FROM $0 UNION ALL SELECT [projectID], [Date2] FROM $0',[projects])

or

alasql('SELECT [projectID], [Date1] FROM $projects UNION ALL SELECT [projectID], [Date2] FROM $projects',{projects:projects})

Depending on what style you prefer...

@mathiasrw As you have no `WHERE` statement the order by semanticly belongs to the second select of the union all.

I tried to put a (...) around, but the parser can not handle it.

So - the solution for you is to add WHERE 1 before your ORDER BY

https://jsfiddle.net/gf845cfp/1/

UPDATE: I need new glasses...

@mathiasrw Omg. Im so sorry - I looked in the wrong collumn to verify it worked !

Ill mark it as a bug again

@mathiasrw Hi @tmrk

Had a look with fresh eyes. I dont know how I could miss this. The problem is that its sorting on an alias - and for some reason its not working for UNION

The solution is th have the union as a subquery - not both results have same sorting:

https://jsfiddle.net/hu0hzsrn/

@mathiasrw Glad I could help.

Sorting on alias shouldn't be a problem otherwise, as it's valid SQL.

True. I will make an "fresh" issue on the matter.

@mathiasrw Hmm. Looks like it works in when there is no sub or union

https://jsfiddle.net/0dozpgvq/

I will reopen this to make sure I remember to identify the problem.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix UNION ignores ORDER BY in query execution Add test for UNION with ORDER BY (issue #671) Nov 30, 2025
Copilot AI requested a review from mathiasrw November 30, 2025 04:23
@mathiasrw
mathiasrw marked this pull request as ready for review November 30, 2025 04:31
@mathiasrw
mathiasrw merged commit e602611 into develop Nov 30, 2025
@mathiasrw
mathiasrw deleted the copilot/fix-union-order-by-issue branch November 30, 2025 12:23
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.

UNION ignores ORDER BY

2 participants