Skip to content

Commit 79a1b89

Browse files
committed
handle wholerow references as well
1 parent 2ce250c commit 79a1b89

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

Diff for: expected/pathman_rebuild_updates.out

+19
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ RETURNING t1.*, t1.tableoid::REGCLASS;
8383
Filter: (val = 101)
8484
(6 rows)
8585

86+
EXPLAIN (COSTS OFF) UPDATE test_updates.test SET b = 0
87+
WHERE val = 101 AND test >= (100, 8)
88+
RETURNING *, tableoid::REGCLASS;
89+
QUERY PLAN
90+
-----------------------------------------------------------------------------------
91+
Update on test_11
92+
-> Seq Scan on test_11
93+
Filter: (((test_11.*)::test_updates.test >= ROW(100, 8)) AND (val = 101))
94+
(3 rows)
95+
96+
/* execute this one */
97+
UPDATE test_updates.test SET b = 0
98+
WHERE val = 101 AND test >= (100, -1)
99+
RETURNING test;
100+
test
101+
---------
102+
(101,0)
103+
(1 row)
104+
86105
DROP TABLE test_updates.test_dummy;
87106
DROP SCHEMA test_updates CASCADE;
88107
NOTICE: drop cascades to 13 other objects

Diff for: sql/pathman_rebuild_updates.sql

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ FROM test_updates.test_dummy t2
4545
WHERE t1.val = 101 AND t1.val = t2.val
4646
RETURNING t1.*, t1.tableoid::REGCLASS;
4747

48+
EXPLAIN (COSTS OFF) UPDATE test_updates.test SET b = 0
49+
WHERE val = 101 AND test >= (100, 8)
50+
RETURNING *, tableoid::REGCLASS;
51+
52+
/* execute this one */
53+
UPDATE test_updates.test SET b = 0
54+
WHERE val = 101 AND test >= (100, -1)
55+
RETURNING test;
56+
4857
DROP TABLE test_updates.test_dummy;
4958

5059

Diff for: src/planner_tree_modification.c

+40-17
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ typedef struct
9696
typedef struct
9797
{
9898
Index child_varno;
99-
Oid parent_relid;
99+
Oid parent_relid,
100+
parent_reltype,
101+
child_reltype;
100102
List *translated_vars;
101103
} adjust_appendrel_varnos_cxt;
102104

@@ -483,9 +485,11 @@ handle_modification_query(Query *parse, transform_query_cxt *context)
483485
if (!inh_translation_list_is_trivial(translated_vars))
484486
{
485487
/* Translate varnos for this child */
486-
aav_cxt.child_varno = result_rti;
487-
aav_cxt.parent_relid = parent;
488-
aav_cxt.translated_vars = translated_vars;
488+
aav_cxt.child_varno = result_rti;
489+
aav_cxt.parent_relid = parent;
490+
aav_cxt.parent_reltype = RelationGetDescr(parent_rel)->tdtypeid;
491+
aav_cxt.child_reltype = RelationGetDescr(child_rel)->tdtypeid;
492+
aav_cxt.translated_vars = translated_vars;
489493
adjust_appendrel_varnos((Node *) parse, &aav_cxt);
490494

491495
/* Translate column privileges for this child */
@@ -612,24 +616,43 @@ adjust_appendrel_varnos(Node *node, adjust_appendrel_varnos_cxt *context)
612616
{
613617
Var *var = (Var *) node;
614618

615-
/* Don't transform system columns & other relations' Vars */
616-
if (var->varattno > 0 && var->varno == context->child_varno)
619+
/* See adjust_appendrel_attrs_mutator() */
620+
if (var->varno == context->child_varno)
617621
{
618-
Var *child_var;
622+
if (var->varattno > 0)
623+
{
624+
Var *child_var;
619625

620-
var = copyObject(var);
626+
var = copyObject(var);
621627

622-
if (var->varattno > list_length(context->translated_vars))
623-
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
624-
var->varattno, get_rel_name(context->parent_relid));
628+
if (var->varattno > list_length(context->translated_vars))
629+
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
630+
var->varattno, get_rel_name(context->parent_relid));
625631

626-
child_var = list_nth(context->translated_vars, var->varattno - 1);
627-
if (!child_var)
628-
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
629-
var->varattno, get_rel_name(context->parent_relid));
632+
child_var = list_nth(context->translated_vars, var->varattno - 1);
633+
if (!child_var)
634+
elog(ERROR, "attribute %d of relation \"%s\" does not exist",
635+
var->varattno, get_rel_name(context->parent_relid));
630636

631-
/* Transform attribute number */
632-
var->varattno = child_var->varattno;
637+
/* Transform attribute number */
638+
var->varattno = child_var->varattno;
639+
}
640+
else if (var->varattno == 0)
641+
{
642+
ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
643+
644+
Assert(var->vartype = context->parent_reltype);
645+
646+
r->arg = (Expr *) var;
647+
r->resulttype = context->parent_reltype;
648+
r->convertformat = COERCE_IMPLICIT_CAST;
649+
r->location = -1;
650+
651+
/* Make sure the Var node has the right type ID, too */
652+
var->vartype = context->child_reltype;
653+
654+
return (Node *) r;
655+
}
633656
}
634657

635658
return (Node *) var;

0 commit comments

Comments
 (0)