Skip to content

JOINs only return the second subquery #1789

Closed

Description

@gygabyte017

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

Activity

  1. mathiasrw commented on Sep 7, 2023

    @mathiasrw
    Member

    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.

  2. adarshjhaa100 commented on Sep 8, 2023

    @adarshjhaa100
    Contributor

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

  3. mathiasrw commented on Sep 8, 2023

    @mathiasrw
    Member

    Thank you!

  4. adarshjhaa100 commented on Sep 16, 2023

    @adarshjhaa100
    Contributor

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

  5. added a commit that references this issue on Sep 16, 2023
    2ef9e3a
  6. adarshjhaa100 commented on Sep 16, 2023

    @adarshjhaa100
    Contributor

    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.

  7. mathiasrw commented on Sep 17, 2023

    @mathiasrw
    Member

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

  8. added a commit that references this issue on Apr 25, 2024
    a600ce7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions