Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 2, 2025

Description

The issue reported that SELECT Name FROM ? WHERE 1=0 GROUP BY Name was returning [{Name: 'undefined'}] instead of an empty result set. This behavior has already been fixed in the codebase.

The fix exists in src/38query.js:102 where the condition if (query.groups.length === 0 && query.allgroups.length === 0) ensures artificial groups are only created when no GROUP BY clause exists. This correctly implements SQL standard behavior:

  • GROUP BY with no matches → []
  • Aggregates without GROUP BY with no matches → one row with defaults (e.g., [{cnt: 0}])

Changes

Added comprehensive test suite in test/test79-B.js covering:

  • GROUP BY returning empty results when WHERE matches no rows
  • GROUP BY with aggregates on empty result sets
  • GROUP BY on empty datasets
  • Multi-column GROUP BY with no matches
  • Aggregates without GROUP BY correctly returning default values
  • Verification that GROUP BY works correctly with matching data

Example

// Returns [] as expected
alasql('SELECT Name FROM ? WHERE 1=0 GROUP BY Name', [[{Name: 'test'}]]);

// Returns [{cnt: 0}] as expected (aggregate without GROUP BY)
alasql('SELECT COUNT(*) as cnt FROM ? WHERE 1=0', [[{Name: 'test'}]]);

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>GROUP BY returns 1 invalid result if no rows meet condition </issue_title>
<issue_description>If there is a query that has no results, but contains a GROUP BY, it returns one result regardless, with the data of "undefined"

console.log(alasql('SELECT Name FROM ? WHERE 1=0 GROUP BY Name',[[{Name:'test'}]]));
returns: [{Name: 'undefined'}] but should be []

</issue_description>

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

@agershun Thank you! For the second question you should: a) add Name to the GROUP BY clause b) Use FIRST() aggregator
var res = alasql('SELECT Type,Name FROM ? GROUP BY Type, Name',[...);

or

var res = alasql('SELECT Type,FIRST(Name) FROM ? GROUP BY Type',[...]);

For the first question - sorry, it still in progress...
</comment_new>
<comment_new>@agershun
Try this example in jsFiddle
</comment_new>
<comment_new>@mathiasrw
👍
</comment_new>
<comment_new>@agershun
Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY?

I think, that the standard SQL reaction is to throw a error like "column is not found". I think, Alasql can check this, but only for SELECT with GROUP BY clause. Plus, it is hard to recognize these "naked" columns in expressions, like:

    SELECT mycol+1 FROM mytable GROUP BY othercol

</comment_new>
<comment_new>@mathiasrw
Is this issue resolved?
</comment_new>
<comment_new>@mathiasrw
Well... its now returning [{}] instead of the original problem - but its still not correct.

Any help to resolve this would be awesome.

http://jsfiddle.net/t780pnz1/</comment_new>

Original prompt

This section details on the original issue you should resolve

<issue_title>GROUP BY returns 1 invalid result if no rows meet condition </issue_title>
<issue_description>If there is a query that has no results, but contains a GROUP BY, it returns one result regardless, with the data of "undefined"

console.log(alasql('SELECT Name FROM ? WHERE 1=0 GROUP BY Name',[[{Name:'test'}]]));
returns: [{Name: 'undefined'}] but should be []

</issue_description>

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

@agershun Thank you! For the second question you should: a) add Name to the GROUP BY clause b) Use FIRST() aggregator
var res = alasql('SELECT Type,Name FROM ? GROUP BY Type, Name',[...);

or

var res = alasql('SELECT Type,FIRST(Name) FROM ? GROUP BY Type',[...]);

For the first question - sorry, it still in progress...

@agershun Try this example [in jsFiddle](http://jsfiddle.net/trr2b7qv/2/) @mathiasrw 👍 @agershun **Is there a way you can test if the fields are requested naked, to automatically apply the FROM() function when used in a GROUP BY?**

I think, that the standard SQL reaction is to throw a error like "column is not found". I think, Alasql can check this, but only for SELECT with GROUP BY clause. Plus, it is hard to recognize these "naked" columns in expressions, like:

    SELECT mycol+1 FROM mytable GROUP BY othercol
@mathiasrw Is this issue resolved? @mathiasrw Well... its now returning `[{}]` instead of the original problem - but its still not correct.

Any help to resolve this would be awesome.

http://jsfiddle.net/t780pnz1/</comment_new>


💡 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.

Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix GROUP BY returning invalid result for empty query Add regression test for GROUP BY with no matching rows Dec 2, 2025
Copilot finished work on behalf of mathiasrw December 2, 2025 12:58
Copilot AI requested a review from mathiasrw December 2, 2025 12:58
@mathiasrw
Copy link
Member

@copilot Continue

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

Copilot AI commented Dec 2, 2025

@copilot Continue

Renamed test file from test/test926.js to test/test79-B.js and updated the test description. All tests pass. (6626dca)

Copilot finished work on behalf of mathiasrw December 2, 2025 19:57
@mathiasrw mathiasrw marked this pull request as ready for review December 2, 2025 22:42
@mathiasrw mathiasrw merged commit ace837b into develop Dec 2, 2025
10 checks passed
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 returns 1 invalid result if no rows meet condition

2 participants