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

MERGE statement is deleting rows if the column is set as NOT NULL even though it should not #7136

Closed
v-from-meiro opened this issue May 31, 2021 · 7 comments
Labels
bug
Milestone

Comments

@v-from-meiro
Copy link

@v-from-meiro v-from-meiro commented May 31, 2021

Describe the bug
MERGE statement is deleting rows if the column is set as NOT NULL even though it should not.

To Reproduce

drop table sys.v;
CREATE TABLE "sys"."v" (
	"id"      INTEGER NOT NULL,
	"version" INTEGER NOT NULL,
	"value"   DOUBLE
);

insert into sys."v" values (1,1622470128,1);
insert into sys."v" values (2,1622470128,2);
insert into sys."v" values (3,1622470128,3);
insert into sys."v" values (4,1622470128,4);
insert into sys."v" values (5,1622470128,5);

MERGE INTO sys."v" dst 
USING (SELECT id, version FROM (SELECT id, version, DENSE_RANK() OVER (PARTITION BY id ORDER BY id, version DESC) AS rn FROM sys."v")t WHERE rn > 1 ) src
ON src.id = dst.id AND src.version = dst.version
WHEN MATCHED THEN DELETE;

Above merge statement deletes all rows in the table, even if it should not.

Expected behavior

drop table sys.v;
CREATE TABLE "sys"."v" (
	"id"      INTEGER,
	"version" INTEGER,
	"value"   DOUBLE
);

insert into sys."v" values (1,1622470128,1);
insert into sys."v" values (2,1622470128,2);
insert into sys."v" values (3,1622470128,3);
insert into sys."v" values (4,1622470128,4);
insert into sys."v" values (5,1622470128,5);

MERGE INTO sys."v" dst 
USING (SELECT id, version FROM (SELECT id, version, DENSE_RANK() OVER (PARTITION BY id ORDER BY id, version DESC) AS rn FROM sys."v")t WHERE rn > 1 ) src
ON src.id = dst.id AND src.version = dst.version
WHEN MATCHED THEN DELETE;

If the column is NOT set as NOT NULL, the statement works as expected.

Software versions
Linux, Docker, latest version od MDB (MonetDB v11.39.17 (Oct2020-SP5))

@PedroTadim
Copy link
Contributor

@PedroTadim PedroTadim commented May 31, 2021

Hello! I noticed this issue a few months ago too, and it is fixed in the Jul2021 branch, so the fix will be available in the next feature release.

@v-from-meiro
Copy link
Author

@v-from-meiro v-from-meiro commented May 31, 2021

Oh, good to know. It is a pity the fix was not released earlier, still thanks a lot for the reply.

@PedroTadim PedroTadim added the bug label May 31, 2021
@PedroTadim PedroTadim added this to the NEXTFEATURERELEASE milestone May 31, 2021
@PedroTadim
Copy link
Contributor

@PedroTadim PedroTadim commented May 31, 2021

As a side note, I had to disable subqueries on the joining clause too. This is because the current way they are handled in the query parser makes it difficult to integrate with merge statements.

@v-from-meiro
Copy link
Author

@v-from-meiro v-from-meiro commented May 31, 2021

@PedroTadim Could you, please, elaborate more? Does it mean that something like:

MERGE INTO table_name dst 
USING (subquery) src
ON condition
WHEN MATCHED THEN DELETE;

would not be possible to do?

@PedroTadim
Copy link
Contributor

@PedroTadim PedroTadim commented May 31, 2021

No, your example is fine. I disabled subqueries in the join clause, ie the ON condition

@v-from-meiro
Copy link
Author

@v-from-meiro v-from-meiro commented May 31, 2021

Oh, yeah, I read your message again and it makes sense, I need more coffe. Thank you very much for all the info and have a nice day!

@PedroTadim
Copy link
Contributor

@PedroTadim PedroTadim commented May 31, 2021

And we should have tested this feature more back then too :)

@sjoerdmullender sjoerdmullender removed this from the NEXTFEATURERELEASE milestone Aug 9, 2021
@sjoerdmullender sjoerdmullender added this to the Jul2021 milestone Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug
Projects
None yet
Development

No branches or pull requests

3 participants