question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support WHERE clause in INSERT ... ON CONFLICT

See original GitHub issue

In PostgreSQL the full synopsis for the conflict_action in an INSERT statement is:

and conflict_action is one of:

    DO NOTHING
    DO UPDATE SET { column_name = { expression | DEFAULT } |
                    ( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) |
                    ( column_name [, ...] ) = ( sub-SELECT )
                  } [, ...]
              [ WHERE condition ]

CrateDB does not support WHERE condition.

Example from the PostgreSQL documentation:

-- Don't update existing distributors based in a certain ZIP code
INSERT INTO distributors AS d (did, dname) VALUES (8, 'Anvil Distribution')
    ON CONFLICT (did) DO UPDATE
    SET dname = EXCLUDED.dname || ' (formerly ' || d.dname || ')'
    WHERE d.zipcode <> '21201';

Users could avoid “no-op” updates like the following:

insert into test (a, b) 
values (1, ‘2020-01-01T00:00:00Z’::timestamp)
on conflict (a)
do update set b = case when excluded.b > b then excluded.b else b end;

(This is from https://github.com/crate/crate/issues/9723)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mfusseneggercommented, May 2, 2020

@daminichopra

Yes, this issue is quite involved and touches many parts.

  • The parser needs to be extended to support the WHERE clause in ON CONFLICT

    • The grammar is in sql-parser/src/main/antlr/SqlBase.g4, tests are in sql-parser/src/test/java/io/crate/sql/parser/TestStatementBuilder.java. To generate the parser from the grammar you can run ./gradlew :sql-parser:compileJava or ./gradlew :sql-parser:test.
    • visitInsert in sql-parser/src/main/java/io/crate/sql/parser/AstBuilder.java needs to be extended, so that the DuplicateKeyContext receives a where Expression
  • sql/src/main/java/io/crate/analyze/InsertAnalyzer.java needs to be extended to analyze the where Expression. The AnalyzedInsertStatement should receive a new property of type Symbol that contains the analyzed expression. Analyzer tests for inserts are in sql/src/test/java/io/crate/analyze/InsertAnalyzerTest.java

  • The execution layer needs to be extended in various components. I can give some more pointers if you’d like later. Maybe you could first open up a draft with the parser/analyzer extension and we can guide you further from there?

1reaction
daminichopracommented, Apr 20, 2020

Hi @mfussenegger , if no one is working on this issue then please assign this issue to me. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL ON CONFLICT with a WHERE clause
The WHERE clause is subordinate to the ON CONFLICT (constraint) DO UPDATE SET ... clause. It only looks at the single row that...
Read more >
How to use `INSERT ON CONFLICT` to upsert data in ... - Prisma
The actual implementation within PostgreSQL uses the INSERT command with a special ON CONFLICT clause to specify what to do if the record...
Read more >
Use INSERT ON CONFLICT to overwrite data - AnalyticDB for ...
This topic describes how to use INSERT ON CONFLICT to overwrite data in AnalyticDB for PostgreSQL. The INSERT ON CONFLICT statement allows ...
Read more >
Documentation: 15: INSERT - PostgreSQL
The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias),...
Read more >
How To Use ON CONFLICT CLAUSE in PostgreSQL
A simple way to upsert data in postgreSQL database using ON CONFLICT CLAUSE.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found