Skip to content

Commit 9231c2c

Browse files
committed
Fix COPY WHERE into a hypertable with dropped columns
COPY with a WHERE clause could fail with an attribute number error when the target hypertable had dropped columns. Evaluate the WHERE clause before converting to chunk layout, since the chunk layout may differ from hypertable layout. Fixes #9977
1 parent 217fe4c commit 9231c2c

4 files changed

Lines changed: 58 additions & 9 deletions

File tree

.unreleased/pr_9983

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #9977 Fix COPY WHERE into a hypertable with dropped columns

src/copy.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,19 @@ copyfrom(CopyChunkState *ccstate, ParseState *pstate, Hypertable *ht, MemoryCont
10981098

10991099
ExecStoreVirtualTuple(myslot);
11001100

1101+
/*
1102+
* Apply the WHERE clause before the tuple is converted to the chunk
1103+
* layout. The chunk can have different physical layout.
1104+
*/
1105+
if (qualexpr != NULL)
1106+
{
1107+
econtext->ecxt_scantuple = myslot;
1108+
if (!ExecQual(qualexpr, econtext))
1109+
{
1110+
continue;
1111+
}
1112+
}
1113+
11011114
/* Calculate the tuple's point in the N-dimensional hyperspace */
11021115
point = ts_hyperspace_calculate_point(ht->space, myslot);
11031116

@@ -1190,15 +1203,6 @@ copyfrom(CopyChunkState *ccstate, ParseState *pstate, Hypertable *ht, MemoryCont
11901203
}
11911204
}
11921205

1193-
if (qualexpr != NULL)
1194-
{
1195-
econtext->ecxt_scantuple = myslot;
1196-
if (!ExecQual(qualexpr, econtext))
1197-
{
1198-
continue;
1199-
}
1200-
}
1201-
12021206
/*
12031207
* Set the result relation in the executor state to the target chunk.
12041208
* This makes sure that the tuple gets inserted into the correct

test/expected/copy.out

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,41 @@ SELECT * FROM table_with_layout_change ORDER BY time, value7;
678678
5 | 724
679679
6 | 725
680680

681+
-- COPY WHERE into a hypertable whose chunks have dropped columns
682+
COPY table_with_layout_change (value7, time) FROM STDIN DELIMITER ',' NULL AS 'null' WHERE value7 > 800;
683+
SELECT * FROM table_with_layout_change ORDER BY time, value7;
684+
time | value7
685+
------+--------
686+
1 | 700
687+
1 | 700
688+
1 | 700
689+
1 | 700
690+
1 | 700
691+
1 | 700
692+
1 | 722
693+
2 | 700
694+
2 | 700
695+
2 | 700
696+
2 | 700
697+
2 | 700
698+
2 | 700
699+
2 | 700
700+
2 | 700
701+
2 | 721
702+
2 | 850
703+
3 | 700
704+
3 | 700
705+
3 | 700
706+
3 | 700
707+
3 | 723
708+
3 | 860
709+
4 | 700
710+
4 | 700
711+
4 | 726
712+
5 | 700
713+
5 | 724
714+
6 | 725
715+
681716
-- verify check constraints work
682717
CREATE TABLE test_check(a INT, b TIMESTAMPTZ);
683718
ALTER TABLE test_check ADD CONSTRAINT c1 CHECK (a > 7);

test/sql/copy.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,15 @@ COPY table_with_layout_change (value7, time) FROM STDIN DELIMITER ',' NULL AS 'n
545545

546546
SELECT * FROM table_with_layout_change ORDER BY time, value7;
547547

548+
-- COPY WHERE into a hypertable whose chunks have dropped columns
549+
COPY table_with_layout_change (value7, time) FROM STDIN DELIMITER ',' NULL AS 'null' WHERE value7 > 800;
550+
850,2
551+
727,1
552+
860,3
553+
\.
554+
555+
SELECT * FROM table_with_layout_change ORDER BY time, value7;
556+
548557
-- verify check constraints work
549558
CREATE TABLE test_check(a INT, b TIMESTAMPTZ);
550559
ALTER TABLE test_check ADD CONSTRAINT c1 CHECK (a > 7);

0 commit comments

Comments
 (0)