AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.

Home Page:http://alasql.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JOINs only return the second subquery

gygabyte017 opened this issue · comments

Consider this simple query:

alasql(`	
	SELECT 
		a.outcome, 
		a.n, a.n/b.n as p 
	FROM (
		SELECT 	
			result as outcome, 
			COUNT(*) AS n 
		FROM ? 
		GROUP BY result
	) a 
	CROSS JOIN (
		SELECT COUNT(*) as n FROM ?
	) b
`, [x, x])

It returns this kind of object: [{n: 123}], so it seems to be returing just the result of the subquery SELECT COUNT(*) as n FROM ? instead of the whole query, how is that possible?

It consistently happens as well with other types of JOINs

this simple query

Welll, lots of things going on there in my opinion :)

It returns this kind of object: [{n: 123}]

Clearly a bug. Thank you for taking the time to let the world know.

Hi @mathiasrw I could diagnose this issue and if I find something will raise a PR if nobody's working on this already.

Thank you!

@mathiasrw I've identified the issue. Whenever we have a subquery inside the join, the source.datafn is equal to the following function:
image

The problem with just using the subquery function here is that even though it returns the correct subquery result,
image

there's no procedure following this that would join this specific result back with the exisiting result set.

One possible solution could be to update the source.data with the result of subquery and add the following procedures that's used in queries of type "queryparam"
image

Have had success in joining with results of non aggregate subqueries.
With aggregates like count, there seems to be some problem, im looking for a solution for the same.

Found the issue, was accidentally passed the param data rather than query set:
image

Getting correct join results for the subquery pointed out in the bug report for the following value of x:

image

image

I've added the fix and test cases (test/test1789.js) with the PR #1800

You've got a great project here! Pretty useful especially for embedding with Frontend projects.

Really nice work getting to the bottom of this. Really nice.