Skip to content

Conversation

@MooSayed1
Copy link

MySQL 8.0.20 introduced a new syntax for INSERT ... ON DUPLICATE KEY UPDATE that allows referencing the inserted row using an alias instead of the VALUES() function:

-- Old syntax (still supported)
INSERT INTO t1 (a, b) VALUES (1, 2)
  ON DUPLICATE KEY UPDATE b = VALUES(b);

-- New syntax (this patch adds support)
INSERT INTO t1 (a, b) VALUES (1, 2) AS new
  ON DUPLICATE KEY UPDATE b = new.b;

The alias syntax is cleaner and more readable, especially when referencing multiple columns or using expressions like new.a + new.b.


Implementation

  • Parser: Added opt_values_row_alias rule to accept AS alias after VALUES
  • LEX: Added insert_values_alias field to store the alias
  • Name resolution: In Item_field::fix_fields(), when we're in ON DUPLICATE KEY UPDATE and the table qualifier matches the alias, we convert the reference to an Item_insert_value (equivalent to VALUES())

The traditional VALUES() function continues to work unchanged.

Tests

Added mysql-test/main/insert_update_alias.test covering:

  • Basic INSERT ... AS alias ON DUPLICATE KEY UPDATE
  • Multiple rows with alias
  • Expressions using alias columns (new.a + new.b)
  • Backward compatibility with VALUES() function
  • Mix of alias and table column references
  • INSERT without ON DUPLICATE KEY (alias ignored)
  • Different alias names

@CLAassistant
Copy link

CLAassistant commented Jan 15, 2026

CLA assistant check
All committers have signed the CLA.

@MooSayed1 MooSayed1 force-pushed the MDEV-29919-insert-as-alias branch from b3b3fa7 to d724e69 Compare January 15, 2026 20:20
@MooSayed1 MooSayed1 force-pushed the MDEV-29919-insert-as-alias branch from d724e69 to ff235ab Compare January 15, 2026 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants