Skip to content

Add grammar support for insert...returning... (most cases)#419

Merged
angelamayxie merged 6 commits intomainfrom
angela/returning
Jun 4, 2025
Merged

Add grammar support for insert...returning... (most cases)#419
angelamayxie merged 6 commits intomainfrom
angela/returning

Conversation

@angelamayxie
Copy link

@angelamayxie angelamayxie commented Jun 3, 2025

Part of dolthub/dolt#9248

Insert...returning... statements work with insert_data_alias and SET assignment_list cases. The following examples (from the MariaDB documentation) work.

INSERT INTO t2 (id) VALUES (2),(3) RETURNING id,t;
INSERT INTO t1 SET id1=1, animal1='Bear' RETURNING f(id1), UPPER(animal1);

I wasn't able to get insert...returning... to work with insert_data_select cases. The RETURNING clause is not supported for the following and would lead to a syntax error.

INSERT INTO archive (id, data) SELECT id, data FROM live_data WHERE flag = 'old' RETURNING id;

When I had added a RETURNING clause, there was a grammar conflict because the parser was not able to distinguish between a column alias and the 'RETURNING' keyword. This could be resolved by making RETURNING a reserved keyword, but we decided against that since RETURNING is a non-reserved keyword in MySQL (doc).

I ran into a similar conflict with insert_data_alias cases because the parser was not able to distinguish between a table alias and the RETURNING keyword. This was resolved by adding RETURNING to non_reserved_keyword2. This means RETURNING can't be used as a table alias and may cause some customer issues but the likelihood is very low.

Also removed TestSingle from parse_test.go (we already have TestSingleSQL)

@angelamayxie angelamayxie changed the title Add grammar support for returning clauses Add grammar support for insert...returning... Jun 3, 2025
@angelamayxie angelamayxie requested a review from fulghum June 3, 2025 18:13
@angelamayxie angelamayxie changed the title Add grammar support for insert...returning... Add grammar support for insert...returning... (most cases) Jun 4, 2025
@angelamayxie angelamayxie marked this pull request as ready for review June 4, 2025 17:49
@angelamayxie angelamayxie requested a review from zachmu as a code owner June 4, 2025 17:49
Copy link

@fulghum fulghum left a comment

Choose a reason for hiding this comment

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

Looks good! Nice work on this one.

@angelamayxie angelamayxie merged commit cad71bb into main Jun 4, 2025
4 checks passed
@angelamayxie angelamayxie deleted the angela/returning branch June 4, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants