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

Fix on conflict error and add support for other on conflict queries in Postgres #34227

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

omkar-shitole
Copy link
Contributor

@omkar-shitole omkar-shitole commented Jan 2, 2025

Fixes #32280

Changes proposed in this pull request:

  • Added flow for whereSegment in onConflictKeyColumnSegment which was missing and required. The current flow is more intent on the on_duplicate of MySQL than on_conflict in Postgres which has been added in Postgres after Postgres 9.
  • While using ON CONFLICT with INSERT INTO statement in Postgres it was throwing an exception (no value specified for parameter) as it was not able to set the flow for where statement present in the optOnConflict rule which was eventually failing to read parameter markers expressions and assignments present in whereSegment
  • This fix resolves the error and ensures the successful execution of the INSERT INTO statement. The flow for ON DUPLICATE has not been removed or altered for now, as It may be useful for certain conditions of older versions of Postgres (and can be deprecated in future if no use case is observed for long)

Before committing this PR, I'm sure that I have checked the following options:

  • My code follows the code of conduct of this project.
  • I have self-reviewed the commit code.
  • I have (or in comment I request) added corresponding labels for the pull request.
  • I have passed maven check locally : ./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e.
  • I have made corresponding changes to the documentation.
  • I have added corresponding unit tests for my changes.
  • I have updated the Release Notes of the current development version. For more details, see Update Release Note

@omkar-shitole omkar-shitole changed the title Issue#32280 bugfix on conflict for postgres Fix on conflict error and add support for other on conflict queries in Postgres Jan 7, 2025
@omkar-shitole
Copy link
Contributor Author

Hi @terrymanu,

Will you please review and proceed?

(#32280 (comment))

@strongduanmu
Copy link
Member

Hi @terrymanu,

Will you please review and proceed?

(#32280 (comment))

@omkar-shitole Thank you for your contribution, I will review this pr later.

@@ -75,6 +75,7 @@
1. Encrypt: Use sql bind info in EncryptInsertPredicateColumnTokenGenerator to avoid wrong column table mapping - [#34110](https://github.com/apache/shardingsphere/pull/34110)
1. Mode: Fixes `JDBCRepository` improper handling of H2-database in memory mode - [#33281](https://github.com/apache/shardingsphere/issues/33281)
1. Mode: Fixes duplicate column names added when index changed in DDL - [#33982](https://github.com/apache/shardingsphere/issues/33281)
1. SQL Binder: Fixes bug: throwing exception while using WHERE statement in ON CONFLICT with INSERT INTO in Postgres [#32280](https://github.com/apache/shardingsphere/issues/32280)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you modify this change log to Fix on conflict error and add support for other on conflict queries in Postgres #34227?

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add javadoc for this class.

import java.util.stream.Collectors;

@Getter
@ToString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add @ToString annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out. I added the annotation because, while it has not been explicitly used anywhere currently, I observed a similar annotation unused applied to the OnDuplicateUpdateContext class. I included it to maintain consistency and to ensure that if needed later.
However, if you feel this annotation is unnecessary for now, let me know will cover this in the correction commit.


@Getter
@ToString
public final class OnConflictUpdateContext implements WhereAvailable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove WhereAvailable here, it's only for SQLStatement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing and pointing this out too. Adding it here was redundant and an oversight on my part as I missed removing it during cleanup. I will address it in the correction commit.
Thank you again!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strongduanmu
All other suggested changes/modifications are ready and added to the branch.
Will you please confirm this once?


private final Collection<ColumnSegment> columnSegments;

private final Collection<BinaryOperationExpression> joinConditions = new LinkedList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a join condition in On Conflict segment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, It's not part of a particular segment. join can be handled outside the conflict segment in Insert statement . I'll address this in a correction commit.
https://www.postgresql.org/docs/current/sql-insert.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants