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

Problem compiling upsert statement #2791

Closed
Zlash92 opened this issue Jan 19, 2022 · 3 comments
Closed

Problem compiling upsert statement #2791

Zlash92 opened this issue Jan 19, 2022 · 3 comments
Labels
bug component: sql-psi Should be implemented in https://github.com/AlecStrong/sql-psi

Comments

@Zlash92
Copy link

Zlash92 commented Jan 19, 2022

SQLDelight Version

1.5.3

Operating System

macOS Big Sur 11.6.2

Gradle Version

7.3.1

Kotlin Version

1.6.0

AGP Version

No response

Describe the Bug

I'm trying to implement the upsert query using postgres. Here's a simple example (I also tried using a grouped statement, but in postgres this operation can be done in one statement):

upsert:
INSERT INTO pld1.Person (id, name, age)
VALUES (:id, :name, :age)
ON CONFLICT(id) DO UPDATE SET id = :id, name = :name, age = :age;

However, compilation fails unless exactly two fields are updated in the query. The stacktrace shows the error I get trying to update all three fields in the table. I get the same error when only the id is updated in the query. When the query contains two fields, it compiles and works as expected.

Stacktrace

line 15:52 - <binary like operator real>, <column name real>, BETWEEN, IN or RETURNING expected, got ','
13    INSERT INTO pld1.Person (id, name, age)
14    VALUES (:id, :name, :age)
15    ON CONFLICT(id) DO UPDATE SET id = :id, name = :name, age = :age
                                                          ^


Execution failed for task ':generateMainTestDatabaseInterface'.
> A failure occurred while executing com.squareup.sqldelight.gradle.SqlDelightTask$GenerateInterfaces
   > Generation failed; see the generator error output for details.

Gradle Build Script

No response

@Zlash92 Zlash92 added the bug label Jan 19, 2022
@AlecKazakova AlecKazakova added the component: sql-psi Should be implemented in https://github.com/AlecStrong/sql-psi label Jan 19, 2022
@Satook
Copy link
Contributor

Satook commented Mar 16, 2022

Happy to make a PR but I'm unsure if the cause is the grouping of the statements or the priority pinning of the trailing (column = expr) pairs.

I wasn't able to reproduce with tests in sql-psi alone. It seems to be something about the way the grammar/parser is constructed within SQLDelight.

@AlecKazakova
Copy link
Collaborator

are you able to create a failing test in the sqldelight-compiler tests? That would be a good place to start

@Satook
Copy link
Contributor

Satook commented Mar 21, 2022

I'll give it a go. Thanks for the hint 😄 !

Satook added a commit to SuppApp/sqldelight that referenced this issue Mar 21, 2022
Satook added a commit to SuppApp/sqldelight that referenced this issue Mar 21, 2022
AlecKazakova pushed a commit that referenced this issue Apr 6, 2022
AlecKazakova pushed a commit that referenced this issue Apr 6, 2022
AlecKazakova pushed a commit that referenced this issue Apr 6, 2022
* Added failing tests for Postgres INSERT DO UPDATE

To help with diagnosis of issue #2791.

* Added passing test for Postgres INSERT DO UPDATE

To help with diagnosis of issue #2791.

* Fix the bug

Co-authored-by: andrew <me@satook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component: sql-psi Should be implemented in https://github.com/AlecStrong/sql-psi
Projects
None yet
Development

No branches or pull requests

3 participants