Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using UNION in a subquery leads to a syntax error in Sqlite #329

Closed
hakuya opened this issue Sep 11, 2021 · 1 comment
Closed

Using UNION in a subquery leads to a syntax error in Sqlite #329

hakuya opened this issue Sep 11, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@hakuya
Copy link

hakuya commented Sep 11, 2021

Hi, I'm attempting to use a UNION in a subquery, and that is leading to a syntax error when executing the query against a Sqlite backend. Based on attempting to understand the issue, I believe it is a bug in the ORM similar to this other project.

gocraft/dbr#167

Apparently MySQL and Postgres tolerate (SELECT ...) UNION (SELECT ...), but this is not correct syntax. There should be no parenthesis around the individual selects around a union. It should be SELECT ... UNION SELECT ....

The code I'm running is,

        val files = this.session
                .from( Objects )
                .select( Objects.object_id )
                .where { Objects.object_type eq TYPE_FILE }

        val albums = this.session
                .from( Objects )
                .select( Objects.object_id )
                .where { Objects.object_type eq TYPE_ALBUM }

        val all_children = this.session
                .from( Relations )
                .select( Relations.child_id )
                .where { Relations.parent_id inList albums }
    
        val free_files = files.where { Objects.object_id notInList all_children }

        val select_ids = free_files.union( albums )

        val query = this.session
                .from( Objects )
                .select()
                .where { Objects.object_id inList select_ids }
                .orderBy( randOrder() )

Which yeilds the following query:

SELECT *
FROM objects
WHERE objects.object_id IN (
  (
    SELECT objects.object_id AS objects_object_id
    FROM objects WHERE objects.object_id NOT IN (
      SELECT relations.child_id AS relations_child_id
      FROM relations
      WHERE relations.parent_id IN (
        SELECT objects.object_id AS objects_object_id
        FROM objects WHERE objects.object_type = ?
      )
    )
  )
  UNION (
    SELECT objects.object_id AS objects_object_id
    FROM objects
    WHERE objects.object_type = ?
  )
)
ORDER BY RANDOM();

Both when executed via the code, and directly into sqlite3 this query fails with,
Error: near "UNION": syntax error

Manually removing the extra parenthesis such that the query reads as follows, works.

SELECT *
FROM objects
WHERE objects.object_id IN (
    SELECT objects.object_id AS objects_object_id
    FROM objects WHERE objects.object_id NOT IN (
      SELECT relations.child_id AS relations_child_id
      FROM relations
      WHERE relations.parent_id IN (
        SELECT objects.object_id AS objects_object_id
        FROM objects WHERE objects.object_type = ?
      )
    )
  UNION
    SELECT objects.object_id AS objects_object_id
    FROM objects
    WHERE objects.object_type = ?
)
ORDER BY RANDOM();
@vincentlauvlwj
Copy link
Member

Thank you, will fix in next version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants