Fix merge into.

This commit is contained in:
totaj
2023-08-23 19:10:53 +08:00
parent 0e0b85c794
commit f6d6d6dfbe
3 changed files with 82 additions and 2 deletions

View File

@ -128,14 +128,18 @@ static void transformMergeJoinClause(ParseState* pstate, Node* merge)
foreach (cell1, relnamespace) {
ParseNamespaceItem* nitem1 = (ParseNamespaceItem*)lfirst(cell1);
RangeTblEntry* entry1 = nitem1->p_rte;
bool unique = true;
foreach (cell2, pstate->p_relnamespace) {
ParseNamespaceItem* nitem2 = (ParseNamespaceItem*)lfirst(cell2);
RangeTblEntry* entry2 = nitem2->p_rte;
if (entry1->relid == entry2->relid) {
continue;
if (entry1->relid == entry2->relid && strcmp(entry1->eref->aliasname, entry2->eref->aliasname) == 0) {
unique = false;
break;
}
}
if (unique) {
pstate->p_relnamespace = lappend(pstate->p_relnamespace, lfirst(cell1));
}
}

View File

@ -625,6 +625,52 @@ SET
WHERE
alias1.h_c_w_id >= alias1.h_amount
OR alias3 != 30002;
CREATE TABLE products
(
product_id INTEGER,
product_name VARCHAR2(60),
category VARCHAR2(60)
);
INSERT INTO products VALUES (1501, 'vivitar 35mm', 'electrncs');
INSERT INTO products VALUES (1502, 'olympus is50', 'electrncs');
INSERT INTO products VALUES (1600, 'play gym', 'toys');
INSERT INTO products VALUES (1601, 'lamaze', 'toys');
INSERT INTO products VALUES (1666, 'harry potter', 'dvd');
MERGE INTO products vp
USING products np
ON (vp.product_id = np.product_id)
WHEN MATCHED THEN
UPDATE SET vp.product_name = np.product_name, vp.category = np.category WHERE vp.product_name != 'play gym'
WHEN NOT MATCHED THEN
INSERT VALUES (np.product_id, np.product_name, np.category) WHERE np.category = 'books';
select * from products order by 1;
product_id | product_name | category
------------+--------------+-----------
1501 | vivitar 35mm | electrncs
1502 | olympus is50 | electrncs
1600 | play gym | toys
1601 | lamaze | toys
1666 | harry potter | dvd
(5 rows)
MERGE INTO products vp
USING products np
ON (vp.product_id = np.product_id)
WHEN MATCHED THEN
UPDATE SET vp.product_name = np.category, vp.category = np.product_name WHERE vp.product_name != 'play gym'
WHEN NOT MATCHED THEN
INSERT VALUES (np.product_id, np.product_name, np.category) WHERE np.category = 'books';
select * from products order by 1;
product_id | product_name | category
------------+--------------+--------------
1501 | electrncs | vivitar 35mm
1502 | electrncs | olympus is50
1600 | play gym | toys
1601 | toys | lamaze
1666 | dvd | harry potter
(5 rows)
drop table products;
reset current_schema;
------------------------------------------------
-- clean up

View File

@ -540,6 +540,36 @@ SET
WHERE
alias1.h_c_w_id >= alias1.h_amount
OR alias3 != 30002;
CREATE TABLE products
(
product_id INTEGER,
product_name VARCHAR2(60),
category VARCHAR2(60)
);
INSERT INTO products VALUES (1501, 'vivitar 35mm', 'electrncs');
INSERT INTO products VALUES (1502, 'olympus is50', 'electrncs');
INSERT INTO products VALUES (1600, 'play gym', 'toys');
INSERT INTO products VALUES (1601, 'lamaze', 'toys');
INSERT INTO products VALUES (1666, 'harry potter', 'dvd');
MERGE INTO products vp
USING products np
ON (vp.product_id = np.product_id)
WHEN MATCHED THEN
UPDATE SET vp.product_name = np.product_name, vp.category = np.category WHERE vp.product_name != 'play gym'
WHEN NOT MATCHED THEN
INSERT VALUES (np.product_id, np.product_name, np.category) WHERE np.category = 'books';
select * from products order by 1;
MERGE INTO products vp
USING products np
ON (vp.product_id = np.product_id)
WHEN MATCHED THEN
UPDATE SET vp.product_name = np.category, vp.category = np.product_name WHERE vp.product_name != 'play gym'
WHEN NOT MATCHED THEN
INSERT VALUES (np.product_id, np.product_name, np.category) WHERE np.category = 'books';
select * from products order by 1;
drop table products;
reset current_schema;
------------------------------------------------
-- clean up