Support WHERE clause in INSERT ... ON CONFLICT
See original GitHub issueIn 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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@daminichopra
Yes, this issue is quite involved and touches many parts.
The parser needs to be extended to support the
WHERE
clause inON CONFLICT
sql-parser/src/main/antlr/SqlBase.g4
, tests are insql-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
insql-parser/src/main/java/io/crate/sql/parser/AstBuilder.java
needs to be extended, so that theDuplicateKeyContext
receives awhere
Expression
sql/src/main/java/io/crate/analyze/InsertAnalyzer.java
needs to be extended to analyze thewhere
Expression
. TheAnalyzedInsertStatement
should receive a new property of typeSymbol
that contains the analyzed expression. Analyzer tests for inserts are insql/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?
Hi @mfussenegger , if no one is working on this issue then please assign this issue to me. Thank you