Class InsertConflictStrategy

  • All Implemented Interfaces:
    Serializable

    @PublicEvolving
    public final class InsertConflictStrategy
    extends Object
    implements Serializable
    Defines the conflict resolution strategies for INSERT INTO statements when the query's upsert key differs from the target table's primary key.

    The upsert key is derived from the query's semantics (e.g., GROUP BY columns). When it differs from the target table's primary key, multiple records from the query may map to the same primary key, causing a conflict. For example:

    
     CREATE TABLE user_totals (
       user_id BIGINT,
       category STRING,
       total DECIMAL(10, 2),
       PRIMARY KEY (user_id) NOT ENFORCED
     );
    
     -- Upsert key is (user_id, category), but primary key is just (user_id).
     -- Updates for (user_id=1, category='A') and (user_id=1, category='B')
     -- both target the same primary key row, causing a conflict.
     INSERT INTO user_totals
     SELECT user_id, category, SUM(amount) as total
     FROM orders
     GROUP BY user_id, category
     ON CONFLICT DO NOTHING;
     

    These strategies are used with the ON CONFLICT clause:

    • ON CONFLICT DO ERROR - Throw an exception on primary key constraint violation
    • ON CONFLICT DO NOTHING - Keep the first record, ignore subsequent conflicts
    • ON CONFLICT DO DEDUPLICATE - Maintain history for rollback (current behavior)

    Use the static factory methods to create instances:

    
     InsertConflictStrategy.error()
     InsertConflictStrategy.nothing()
     InsertConflictStrategy.deduplicate()
     

    Or use the builder for more complex configurations:

    
     InsertConflictStrategy.newBuilder()
         .withBehavior(ConflictBehavior.DEDUPLICATE)
         .build()
     
    See Also:
    Serialized Form
    • Method Detail

      • error

        public static InsertConflictStrategy error()
        Creates a strategy that throws an exception when multiple distinct records arrive for the same primary key after watermark compaction.
        Returns:
        the ERROR conflict strategy
      • nothing

        public static InsertConflictStrategy nothing()
        Creates a strategy that keeps the first record that arrives for a given primary key and discards subsequent conflicts.
        Returns:
        the NOTHING conflict strategy
      • deduplicate

        public static InsertConflictStrategy deduplicate()
        Creates a strategy that maintains the full history of changes for each primary key to support rollback on retraction. This is the current default behavior.
        Returns:
        the DEDUPLICATE conflict strategy
      • equals

        public boolean equals​(@Nullable
                              Object o)
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object