Skip to content

[WIP] Fix BETWEEN operator issue in CASE statement - #2195

Merged
mathiasrw merged 4 commits into
developfrom
copilot/fix-between-case-statement
Nov 19, 2025
Merged

mathiasrw merged 4 commits into
developfrom
copilot/fix-between-case-statement

Conversation

Copilot AI commented Nov 19, 2025 •

Copy link
Copy Markdown
Contributor

Fix BETWEEN operator in CASE statement (Issue #735)

Issue: BETWEEN fails in CASE statement with "Cannot read property 'toString' of undefined" error when aggregator functions are used in BETWEEN clauses.

Root Cause: The findAggregator method in the Op class (src/50expression.js) only checked this.right for aggregator functions, but BETWEEN operators use this.right1 and this.right2 instead.

Fix: Updated the findAggregator method to handle BETWEEN and NOT BETWEEN operators by checking both right1 and right2 properties for aggregator functions.

Changes:

  • Added test file test735.js to verify BETWEEN works in CASE statements
  • Modified src/50expression.js to handle BETWEEN operators in findAggregator method
  • Build and all tests passing
  • Code formatting verified with prettier
  • Security scan completed with CodeQL - no vulnerabilities found

Testing:

  • All existing tests pass (test023.js, test041.js, test045.js, test052.js, test053.js)
  • New test735.js validates:
    1. CASE WHEN with > and < operators
    2. CASE WHEN with BETWEEN operator (inclusive bounds)
    3. CASE WHEN with BETWEEN operator (exclusive bounds)
  • Tested with subqueries in BETWEEN clauses

Security Summary: No vulnerabilities found by CodeQL security scan.

Original prompt

This section details on the original issue you should resolve

<issue_title>BETWEEN fails in CASE statement</issue_title>
<issue_description>I get Uncaught TypeError: Cannot read property 'toString' of undefined when I'm trying to use BETWEEN in a CASE. Other operators such as > and < still work but would be too verbose for range determination.

See fiddle: https://jsfiddle.net/h2tyvmuj/
</issue_description>

<agent_instructions>Please make a test file called test735.js verifying if this issue is still a problem.

If it is a problem try to solve it.

If you need to update the grammar of alasql, then please do small precice changes to alasqlparser.jison and then run yarn jison && yarn test
</agent_instructions>

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

@mathiasrw Thanks for a perfect bugreport @mathiasrw Context:
var cities = [
    {City: "Moscow", Population: 1},
    {City: "London", Population: 10},
    {City: "Paris", Population: 100}];

alasql.promise('SELECT * FROM ?',[cities]).then(function(city) {

	// This works
	print(alasql('SELECT City, CASE WHEN Population > 1 AND Population < 100 THEN "yes" ELSE "no" END AS isBetween1and100 FROM ?',[city]));
  
  // This doesn't work
	print(alasql('SELECT City, CASE WHEN Population BETWEEN 1 AND 100 THEN "yes" ELSE "no" END AS isBetween1and100 FROM ?',[city]));

}).catch(function(reason) {
	print(reason['message']);
}).then(function() {
	print('AlaSQL v'+alasql.version)
});





function print(x) {document.getElementById('output').textContent += JSON.stringify(x,null,'\t')+'\n';}

becomes

[
	{
		"City": "Moscow",
		"isBetween1and100": "no"
	},
	{
		"City": "London",
		"isBetween1and100": "yes"
	},
	{
		"City": "Paris",
		"isBetween1and100": "no"
	}
]
"Cannot read properties of undefined (reading 'toString')"
"AlaSQL v0.3.1"
```</body></comment_new>
</comments>


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

@mathiasrw
mathiasrw marked this pull request as ready for review November 19, 2025 12:53
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.

BETWEEN fails in CASE statement

2 participants