Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7421589

Browse files
committed
fix COPY (columns) TO statement (issue #50)
1 parentf603e6c commit7421589

File tree

3 files changed

+165
-80
lines changed

3 files changed

+165
-80
lines changed

‎expected/pathman_copy_stmt_hooking.out

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
\set VERBOSITY terse
22
CREATE EXTENSION pg_pathman;
33
CREATE SCHEMA copy_stmt_hooking;
4-
CREATE TABLE copy_stmt_hooking.test(val int not null, comment text);
4+
CREATE TABLE copy_stmt_hooking.test(
5+
val int not null,
6+
comment text,
7+
c3 int,
8+
c4 int);
59
INSERT INTO copy_stmt_hooking.test SELECT generate_series(1, 20), 'comment';
610
CREATE INDEX ON copy_stmt_hooking.test(val);
711
/* test for RANGE partitioning */
@@ -20,47 +24,47 @@ VACUUM FULL copy_stmt_hooking.test_3;
2024
VACUUM FULL copy_stmt_hooking.test_4;
2125
/* COPY TO */
2226
COPY copy_stmt_hooking.test TO stdout;
23-
1comment
24-
2comment
25-
3comment
26-
4comment
27-
5comment
28-
6comment
29-
7comment
30-
8comment
31-
9comment
32-
10comment
33-
11comment
34-
12comment
35-
13comment
36-
14comment
37-
15comment
38-
16comment
39-
17comment
40-
18comment
41-
19comment
42-
20comment
27+
1comment\N\N
28+
2comment\N\N
29+
3comment\N\N
30+
4comment\N\N
31+
5comment\N\N
32+
6comment\N\N
33+
7comment\N\N
34+
8comment\N\N
35+
9comment\N\N
36+
10comment\N\N
37+
11comment\N\N
38+
12comment\N\N
39+
13comment\N\N
40+
14comment\N\N
41+
15comment\N\N
42+
16comment\N\N
43+
17comment\N\N
44+
18comment\N\N
45+
19comment\N\N
46+
20comment\N\N
4347
\copy copy_stmt_hooking.test to stdout (format csv)
44-
1,comment
45-
2,comment
46-
3,comment
47-
4,comment
48-
5,comment
49-
6,comment
50-
7,comment
51-
8,comment
52-
9,comment
53-
10,comment
54-
11,comment
55-
12,comment
56-
13,comment
57-
14,comment
58-
15,comment
59-
16,comment
60-
17,comment
61-
18,comment
62-
19,comment
63-
20,comment
48+
1,comment,,
49+
2,comment,,
50+
3,comment,,
51+
4,comment,,
52+
5,comment,,
53+
6,comment,,
54+
7,comment,,
55+
8,comment,,
56+
9,comment,,
57+
10,comment,,
58+
11,comment,,
59+
12,comment,,
60+
13,comment,,
61+
14,comment,,
62+
15,comment,,
63+
16,comment,,
64+
17,comment,,
65+
18,comment,,
66+
19,comment,,
67+
20,comment,,
6468
\copy copy_stmt_hooking.test(comment) to stdout
6569
comment
6670
comment
@@ -92,31 +96,62 @@ SELECT count(*) FROM ONLY copy_stmt_hooking.test;
9296
(1 row)
9397

9498
SELECT *, tableoid::REGCLASS FROM copy_stmt_hooking.test ORDER BY val;
95-
val | comment | tableoid
96-
-----+---------+--------------------------
97-
1 | test_1 | copy_stmt_hooking.test_1
98-
6 | test_2 | copy_stmt_hooking.test_2
99-
7 | test_2 | copy_stmt_hooking.test_2
100-
11 | test_3 | copy_stmt_hooking.test_3
101-
16 | test_4 | copy_stmt_hooking.test_4
99+
val | comment |c3 | c4 | tableoid
100+
-----+---------+----+----+--------------------------
101+
1 | test_1 | 0 | 0 |copy_stmt_hooking.test_1
102+
6 | test_2 | 0 | 0 |copy_stmt_hooking.test_2
103+
7 | test_2 | 0 | 0 |copy_stmt_hooking.test_2
104+
11 | test_3 | 0 | 0 |copy_stmt_hooking.test_3
105+
16 | test_4 | 0 | 0 |copy_stmt_hooking.test_4
102106
(5 rows)
103107

108+
/* perform VACUUM */
109+
VACUUM FULL copy_stmt_hooking.test;
110+
VACUUM FULL copy_stmt_hooking.test_1;
111+
VACUUM FULL copy_stmt_hooking.test_2;
112+
VACUUM FULL copy_stmt_hooking.test_3;
113+
VACUUM FULL copy_stmt_hooking.test_4;
114+
/* COPY FROM (specified columns) */
115+
COPY copy_stmt_hooking.test (val) TO stdout;
116+
1
117+
6
118+
7
119+
11
120+
16
121+
COPY copy_stmt_hooking.test (val, comment) TO stdout;
122+
1test_1
123+
6test_2
124+
7test_2
125+
11test_3
126+
16test_4
127+
COPY copy_stmt_hooking.test (c3, val, comment) TO stdout;
128+
01test_1
129+
06test_2
130+
07test_2
131+
011test_3
132+
016test_4
133+
COPY copy_stmt_hooking.test (val, comment, c3, c4) TO stdout;
134+
1test_100
135+
6test_200
136+
7test_200
137+
11test_300
138+
16test_400
104139
/* COPY TO (partition does not exist, NOT allowed to create partitions) */
105140
SET pg_pathman.enable_auto_partition = OFF;
106141
COPY copy_stmt_hooking.test FROM stdin;
107142
ERROR: no suitable partition for key '21'
108143
SELECT * FROM copy_stmt_hooking.test WHERE val > 20;
109-
val | comment
110-
-----+---------
144+
val | comment| c3 | c4
145+
-----+---------+----+----
111146
(0 rows)
112147

113148
/* COPY TO (partition does not exist, allowed to create partitions) */
114149
SET pg_pathman.enable_auto_partition = ON;
115150
COPY copy_stmt_hooking.test FROM stdin;
116151
SELECT * FROM copy_stmt_hooking.test WHERE val > 20;
117-
val | comment
118-
-----+--------------
119-
21 | test_no_part
152+
val | comment| c3 | c4
153+
-----+--------------+----+----
154+
21 | test_no_part | 0 | 0
120155
(1 row)
121156

122157
/* COPY TO (partitioned column is not specified) */
@@ -147,10 +182,10 @@ SELECT count(*) FROM ONLY copy_stmt_hooking.test;
147182
(1 row)
148183

149184
SELECT * FROM copy_stmt_hooking.test ORDER BY val;
150-
val | comment
151-
-----+---------
152-
1 | hash_1
153-
6 | hash_2
185+
val | comment| c3 | c4
186+
-----+---------+----+----
187+
1 | hash_1 | 0 | 0
188+
6 | hash_2 | 0 | 0
154189
(2 rows)
155190

156191
DROP SCHEMA copy_stmt_hooking CASCADE;

‎sql/pathman_copy_stmt_hooking.sql

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ CREATE EXTENSION pg_pathman;
44
CREATESCHEMAcopy_stmt_hooking;
55

66

7-
CREATETABLEcopy_stmt_hooking.test(valintnot null, commenttext);
7+
CREATETABLEcopy_stmt_hooking.test(
8+
valintnot null,
9+
commenttext,
10+
c3int,
11+
c4int);
812
INSERT INTOcopy_stmt_hooking.testSELECT generate_series(1,20),'comment';
913
CREATEINDEXONcopy_stmt_hooking.test(val);
1014

@@ -27,26 +31,39 @@ COPY copy_stmt_hooking.test TO stdout;
2731
/* DELETE ROWS, COPY FROM*/
2832
DELETEFROMcopy_stmt_hooking.test;
2933
COPYcopy_stmt_hooking.testFROM stdin;
30-
1test_1
31-
6test_2
32-
7test_2
33-
11test_3
34-
16test_4
34+
1test_100
35+
6test_200
36+
7test_200
37+
11test_300
38+
16test_400
3539
\.
3640
SELECTcount(*)FROM ONLYcopy_stmt_hooking.test;
3741
SELECT*, tableoid::REGCLASSFROMcopy_stmt_hooking.testORDER BY val;
3842

43+
/* perform VACUUM*/
44+
VACUUM FULLcopy_stmt_hooking.test;
45+
VACUUM FULLcopy_stmt_hooking.test_1;
46+
VACUUM FULLcopy_stmt_hooking.test_2;
47+
VACUUM FULLcopy_stmt_hooking.test_3;
48+
VACUUM FULLcopy_stmt_hooking.test_4;
49+
50+
/* COPY FROM (specified columns)*/
51+
COPYcopy_stmt_hooking.test (val) TO stdout;
52+
COPYcopy_stmt_hooking.test (val, comment) TO stdout;
53+
COPYcopy_stmt_hooking.test (c3, val, comment) TO stdout;
54+
COPYcopy_stmt_hooking.test (val, comment, c3, c4) TO stdout;
55+
3956
/* COPY TO (partition does not exist, NOT allowed to create partitions)*/
4057
SETpg_pathman.enable_auto_partition= OFF;
4158
COPYcopy_stmt_hooking.testFROM stdin;
42-
21test_no_part
59+
21test_no_part00
4360
\.
4461
SELECT*FROMcopy_stmt_hooking.testWHERE val>20;
4562

4663
/* COPY TO (partition does not exist, allowed to create partitions)*/
4764
SETpg_pathman.enable_auto_partition=ON;
4865
COPYcopy_stmt_hooking.testFROM stdin;
49-
21test_no_part
66+
21test_no_part00
5067
\.
5168
SELECT*FROMcopy_stmt_hooking.testWHERE val>20;
5269

@@ -66,8 +83,8 @@ SELECT create_hash_partitions('copy_stmt_hooking.test', 'val', 5);
6683
/* DELETE ROWS, COPY FROM*/
6784
DELETEFROMcopy_stmt_hooking.test;
6885
COPYcopy_stmt_hooking.testFROM stdin;
69-
1hash_1
70-
6hash_2
86+
1hash_100
87+
6hash_200
7188
\.
7289
SELECTcount(*)FROM ONLYcopy_stmt_hooking.test;
7390
SELECT*FROMcopy_stmt_hooking.testORDER BY val;

‎src/copy_stmt_hooking.c

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,8 @@ PathmanDoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
269269
is_from== false)/* rewrite COPY table TO statements */
270270
{
271271
SelectStmt*select;
272-
ColumnRef*cr;
273-
ResTarget*target;
274272
RangeVar*from;
273+
List*target_list=NIL;
275274

276275
if (is_from)
277276
ereport(ERROR,
@@ -280,20 +279,54 @@ PathmanDoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
280279
errhint("Use INSERT statements instead.")));
281280

282281
/* Build target list */
283-
cr=makeNode(ColumnRef);
284-
285282
if (!stmt->attlist)
283+
{
284+
ColumnRef*cr;
285+
ResTarget*target;
286+
287+
cr=makeNode(ColumnRef);
286288
cr->fields=list_make1(makeNode(A_Star));
287-
else
288-
cr->fields=stmt->attlist;
289+
cr->location=-1;
289290

290-
cr->location=1;
291+
/* Build the ResTarget and add the ColumnRef to it. */
292+
target=makeNode(ResTarget);
293+
target->name=NULL;
294+
target->indirection=NIL;
295+
target->val= (Node*)cr;
296+
target->location=-1;
291297

292-
target=makeNode(ResTarget);
293-
target->name=NULL;
294-
target->indirection=NIL;
295-
target->val= (Node*)cr;
296-
target->location=1;
298+
target_list=list_make1(target);
299+
}
300+
else
301+
{
302+
ListCell*lc;
303+
304+
foreach(lc,stmt->attlist)
305+
{
306+
ColumnRef*cr;
307+
ResTarget*target;
308+
309+
/*
310+
* Build the ColumnRef for each column. The ColumnRef
311+
* 'fields' property is a String 'Value' node (see
312+
* nodes/value.h) that corresponds to the column name
313+
* respectively.
314+
*/
315+
cr=makeNode(ColumnRef);
316+
cr->fields=list_make1(lfirst(lc));
317+
cr->location=-1;
318+
319+
/* Build the ResTarget and add the ColumnRef to it. */
320+
target=makeNode(ResTarget);
321+
target->name=NULL;
322+
target->indirection=NIL;
323+
target->val= (Node*)cr;
324+
target->location=-1;
325+
326+
/* Add each column to the SELECT statements target list */
327+
target_list=lappend(target_list,target);
328+
}
329+
}
297330

298331
/*
299332
* Build RangeVar for from clause, fully qualified based on the
@@ -304,7 +337,7 @@ PathmanDoCopy(const CopyStmt *stmt, const char *queryString, uint64 *processed)
304337

305338
/* Build query */
306339
select=makeNode(SelectStmt);
307-
select->targetList=list_make1(target);
340+
select->targetList=target_list;
308341
select->fromClause=list_make1(from);
309342

310343
query= (Node*)select;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp