Skip to content

Add comprehensive test coverage for scalar subquery functionality - #2397

Merged
mathiasrw merged 5 commits into
developfrom
copilot/fix-scalar-subquery-issue
Dec 22, 2025
Merged

mathiasrw merged 5 commits into
developfrom
copilot/fix-scalar-subquery-issue

Conversation

Copilot AI commented Dec 22, 2025 •

Copy link
Copy Markdown
Contributor

Investigation: Scalar Subquery Support in AlaSQL - COMPLETE ✅

Summary

Scalar subqueries ARE fully functional and working correctly in AlaSQL.

Investigation Complete

  • Explored the repository structure and test framework
  • Built the project successfully
  • Verified scalar subqueries in SELECT clause work ✓
  • Verified scalar subqueries in WHERE clause work ✓
  • Verified correlated scalar subqueries work ✓
  • Created comprehensive test file with 12 test cases
  • All tests passing (12/12)
  • Full test suite passes (2307 passing)
  • Code review feedback addressed
  • Test file renamed to test1967.js

Test Coverage

Created test/test1967.js covering:

  • ✅ Basic scalar subqueries in SELECT clause
  • ✅ Multiple aggregate functions (MAX, MIN, AVG, SUM, COUNT)
  • ✅ Scalar subqueries in WHERE clause
  • ✅ Correlated scalar subqueries
  • ✅ Multiple scalar subqueries in one query
  • ✅ Arithmetic expressions with scalar subqueries
  • ✅ NULL/0 handling for empty results
  • ✅ Complex queries with GROUP BY

Real-World Verification

Tested complex scenarios similar to the issue description:

  • Calculating averages by groups with scalar subqueries ✓
  • Comparing periods using scalar subqueries ✓
  • Using scalar subqueries in arithmetic expressions ✓

Conclusion

The issue appears to be incorrect or based on outdated information. Scalar subqueries are fully implemented and functional in AlaSQL. All major use cases work correctly, including:

  • Non-correlated scalar subqueries
  • Correlated scalar subqueries
  • Scalar subqueries with all aggregate functions
  • Complex nested scenarios

The comprehensive test suite ensures this functionality is properly documented and protected against future regressions.

Original prompt

This section details on the original issue you should resolve

<issue_title>It seems like Scalar Subquery is not available in AlaSQL. </issue_title>
<issue_description>Hi, I thought sub query was working in AlaSQL(https://github.com/AlaSQL/alasql/wiki/Subqueries) however, this scalar subquery seems like it's not working.

  • Query Explanation: This query calculates the average profit per customer by education level and annual income, and compares it with the previous period. It assumes that "previous period" means the previous stock period.
<SQL-99>
SELECT 
    [educationlevel],
    [annualincome],
    AVG([totalprofit]) AS [average_profit_current_period],
    (SELECT AVG([totalprofit]) 
     FROM [temptable] AS previous
     WHERE previous.[educationlevel] = current.[educationlevel]
       AND previous.[annualincome] = current.[annualincome]
       AND previous.[stockperiod] = current.[stockperiod] - 1) AS [average_profit_previous_period]
FROM 
    [temptable] AS current
WHERE 
    [educationlevel] IS NOT NULL 
    AND [annualincome] IS NOT NULL 
    AND [totalprofit] IS NOT NULL
GROUP BY 
    [educationlevel], [annualincome]
ORDER BY 
    [educationlevel], [annualincome];

This is the Error Message I'm facing.

runQuery.js:25 Failed to execute query: 
TypeError: Cannot read properties of undefined (reading 'educationlevel')
    at pr.eval [as wherefn] (eval at T.Select.compileWhere (alasql@4:78:9531), <anonymous>:3:76)
    at Wt (alasql@4:60:6110)
    at Wt (alasql@4:60:7141)
    at Er (alasql@4:59:1015)
    at br (alasql@4:59:926)
    at l.options.autocommit.l.databases.<computed>.engineid.l.databases.<computed>.tables.<computed>.view.s.datafn (alasql@4:78:2021)
    at alasql@4:59:429
    at Array.forEach (<anonymous>)
    at Ts (alasql@4:59:389)
    at Array.u (alasql@4:77:648)

Just to make sure that the query itself has no issue, I've tried to run it in PostgreSQL, indeed I got a decent result.

SELECT 
    educationlevel,
    annualincome,
    AVG(totalprofit) AS average_profit_current_period,
    (SELECT AVG(previous.totalprofit) 
     FROM adventureworks."adventureworks-total" AS previous
     WHERE previous.educationlevel = current.educationlevel
    ) AS average_profit_previous_period
FROM 
    adventureworks."adventureworks-total" AS current
WHERE 
    educationlevel IS NOT NULL 
    AND annualincome IS NOT NULL 
    AND totalprofit IS NOT NULL
GROUP BY 
    educationlevel, annualincome
ORDER BY 
    educationlevel, annualincome;

Following is the screenshot of 5 head rows from the result table.

result

Please help me resolve this error.
Thank you so much :)

</issue_description>

<agent_instructions>This might have been solved already. Can you please help assert if its the cases? and if not please try to fix. </agent_instructions>

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


💡 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 scalar subquery functionality in AlaSQL Add comprehensive test coverage for scalar subquery functionality Dec 22, 2025
Copilot AI requested a review from mathiasrw December 22, 2025 02:29

@mathiasrw mathiasrw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P

Comment thread test/test1967.js Resolved
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.

It seems like Scalar Subquery is not available in AlaSQL.

2 participants