Skip to content

Fix ORDER BY with numeric column references for SELECT * - #2189

Merged
mathiasrw merged 7 commits into
developfrom
copilot/fix-order-by-bug
Nov 19, 2025
Merged

mathiasrw merged 7 commits into
developfrom
copilot/fix-order-by-bug

Conversation

Copilot AI commented Nov 19, 2025 •

Copy link
Copy Markdown
Contributor

Fix ORDER BY with numeric column reference when using SELECT *

  • Created test file test125B.js to reproduce the issue
  • Confirmed that ORDER BY 1 works with explicit column names but not with SELECT *
  • Analyzed the root cause in compileOrder method
  • Implemented fix to handle SELECT * case in ORDER BY numeric references
    • Modified src/426orderby.js to detect wildcard columns and create a marker object for positional ordering
    • Modified src/424select.js (compileSelect2 and compileSelectGroup2) to handle positional column references at runtime
  • Improved validation logic to:
    • Handle ORDER BY with column numbers > 1 when using SELECT *
    • Validate column numbers are >= 1
    • Validate column numbers don't exceed column count for explicit columns
  • Run tests to validate the fix - all test125B tests passing
  • Run comprehensive tests - all passing with ORDER BY 1, 2, 3 with SELECT *
  • Run edge case tests - all passing including validation tests
  • Run full test suite - 494 passing (1 unrelated network timeout)
  • Parser changes not needed (only runtime JS changes)
  • Security scan completed - no vulnerabilities found
  • Fixed code formatting to match project standards
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug in ORDER BY 1</issue_title>
<issue_description>This sequence

    alasql('SELECT * FROM TSV("sensor_data.txt",{headers:true}) ORDER BY 1',[],function(res){
        console.log(res);
    });

gives error:

    alasql.min.js:5 Uncaught TypeError: Cannot read property 'reduced' of undefined
    yy.Expression.toJavaScriptalasql.min.js:5 (anonymous function)
    yy.Select.compileOrder

</issue_description>

<agent_instructions>Please make a testfile named test125B.js testing this issue, if its still a problem, then please look into solving it.

If you think the best way to provide SQL-99 compatible syntax for alasql you can do precise and small udpates to alasqlparser.jison. RUn yarn jison && yarn test to confirm. </agent_instructions>

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

@agershun :) The problem was that I sed star in the query. AlaSQL could not find any column name, of course. Now all works fine. @agershun Sample:
     -- works fine
      var res = alasql('SELECT COLUMN DISTINCT city#country#name AS country\
           FROM Person ORDER BY country');

     -- does not work
       var res = alasql('SELECT COLUMN DISTINCT city#country#name\
           FROM Person ORDER BY 1');

error

     ReferenceError: alasql is not defined 
@mathiasrw Can you elaborate on the `#` part of the `city#country#name` notation?

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits November 19, 2025 09:55
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug in ORDER BY clause handling Fix ORDER BY with numeric column references for SELECT * Nov 19, 2025
Copilot AI requested a review from mathiasrw November 19, 2025 10:08
@mathiasrw

Copy link
Copy Markdown
Member

@copilot

Run yarn test-format-all
yarn run v1.22.22
$ prettier --list-different '{.,src,test}/*.{js,ts,json}' || (echo 'Please correct file formatting using yarn format-all and try again.' && exit 1)
src/424select.js
Please correct file formatting using yarn format-all and try again.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

@mathiasrw

Copy link
Copy Markdown
Member

@copilot continue

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>

Copilot AI commented Nov 19, 2025

Copy link
Copy Markdown
Contributor Author

@copilot continue

Fixed code formatting in commit 0832f40.

@mathiasrw
mathiasrw marked this pull request as ready for review November 19, 2025 13:06
@mathiasrw
mathiasrw merged commit 28b1be2 into develop Nov 19, 2025
10 checks passed
@mathiasrw
mathiasrw deleted the copilot/fix-order-by-bug branch November 30, 2025 12:24
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.

Bug in ORDER BY 1

2 participants