Skip to content

Fix GROUP BY with table-prefixed columns in arithmetic expressions - #2297

Merged
mathiasrw merged 3 commits into
developfrom
copilot/fix-group-by-integer-type-loss
Dec 2, 2025
Merged

mathiasrw merged 3 commits into
developfrom
copilot/fix-group-by-integer-type-loss

Conversation

Copilot AI commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor

Description

GROUP BY stores values by column name only (g['id']), but expressions with table prefixes attempted to access them with the full qualifier (g['a.id']), resulting in undefined values. For example:

// This would return undefined for a.id + 1
alasql(`SELECT a.id, a.id + 1 
        FROM data AS a, data AS b 
        WHERE a.id < b.id AND a.grp = b.grp 
        GROUP BY a.id`);

Changes

src/50expression.js

  • Modified Column.toJS() to fall back to columnid when nick is undefined in grouped context ('g')
  • Ensures table-prefixed columns in expressions correctly resolve to their grouped values

test/test536-B.js

  • Added test coverage for GROUP BY with table-prefixed columns in arithmetic expressions
  • Validates fix against the exact failure case and workaround scenarios

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>GROUP BY on field of type INTEGER sometimes loses type</issue_title>
<issue_description>```
create table data( id INTEGER PRIMARY KEY, grp INTEGER);
insert into data select range._ as id , range._ % 3 as grp from RANGE(0,9)as range;
select id, id +1 from data group by id;
select a.id, a.id +1, CAST(a.id AS INTEGER) +1 from data as a, data as b where a.id < b.id and a.grp = b.grp group by a.id

</issue_description>

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

<comments>
<comment_new><author>@agershun</author>
Please use variables without table prefix (at current moment it gives correct answer):

``` sql
    select a.id, (id +1), CAST(id AS INTEGER) +1 from data as a, data as b 
              where a.id < b.id and a.grp = b.grp group by a.id

I will check details later.
</comment_new>

Original prompt

This section details on the original issue you should resolve

<issue_title>GROUP BY on field of type INTEGER sometimes loses type</issue_title>
<issue_description>```
create table data( id INTEGER PRIMARY KEY, grp INTEGER);
insert into data select range._ as id , range._ % 3 as grp from RANGE(0,9)as range;
select id, id +1 from data group by id;
select a.id, a.id +1, CAST(a.id AS INTEGER) +1 from data as a, data as b where a.id < b.id and a.grp = b.grp group by a.id

</issue_description>

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

<comments>
<comment_new><author>@agershun</author><body>
Please use variables without table prefix (at current moment it gives correct answer):

``` sql
    select a.id, (id +1), CAST(id AS INTEGER) +1 from data as a, data as b 
              where a.id < b.id and a.grp = b.grp group by a.id

I will check details later.


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

When using table-prefixed columns (e.g., a.id) in arithmetic expressions
with GROUP BY, the type was being lost because the column nick resolution
didn't match the group storage key. Fixed by using columnid as fallback
when nick is not set in grouped context.

Co-authored-by: mathiasrw <[email protected]>
Copilot AI changed the title [WIP] Fix GROUP BY losing integer type issue in SQL query Fix GROUP BY with table-prefixed columns in arithmetic expressions Dec 2, 2025
Copilot AI requested a review from mathiasrw December 2, 2025 12:56
Comment thread test/test536-B.js Resolved
@mathiasrw

Copy link
Copy Markdown
Member

@copilot continue

Renamed test file from test926.js to test536-B.js following the
repository convention of naming tests after issue numbers.

Co-authored-by: mathiasrw <[email protected]>

Copilot AI commented Dec 2, 2025

Copy link
Copy Markdown
Contributor Author

@copilot continue

Renamed test926.js to test536-B.js in commit 5c279e7.

@mathiasrw
mathiasrw marked this pull request as ready for review December 2, 2025 22:57
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.

GROUP BY on field of type INTEGER sometimes loses type

2 participants