{
cstate->json_buf = makeStringInfo();
- if (rel && list_length(cstate->attnumlist) < tupDesc->natts)
+ /*
+ * Build a projected TupleDesc describing only the selected columns
+ * so that composite_to_json() emits the right column names and
+ * types; needed when an explicit column list was given (possibly
+ * with a different column order) or when generated columns are
+ * excluded from the output.
+ */
+ if (rel && (attnamelist != NIL ||
+ list_length(cstate->attnumlist) < tupDesc->natts))
{
int natts = list_length(cstate->attnumlist);
TupleDesc resultDesc;
- /*
- * Build a TupleDesc describing only the selected columns so that
- * composite_to_json() emits the right column names and types.
- */
resultDesc = CreateTemplateTupleDesc(natts);
foreach_int(attnum, cstate->attnumlist)
c1,"col with , comma","col with "" quote"
1,a,1
2,b,2
+-- testing explicit column order
+create temp table copytest_order (a int, b int, c int);
+copy copytest_order from stdin;
+copy copytest_order (c, b, a) to stdout;
+3 2 1
+copy copytest_order (c, b, a) to stdout (format csv);
+3,2,1
+copy copytest_order (c, b, a) to stdout (format json);
+{"c":3,"b":2,"a":1}
--- test copying in JSON mode with various styles
copy (select 1 union all select 2) to stdout with (format json);
{"?column?":1}
copy copytest3 to stdout csv header;
+-- testing explicit column order
+create temp table copytest_order (a int, b int, c int);
+copy copytest_order from stdin;
+1 2 3
+\.
+copy copytest_order (c, b, a) to stdout;
+copy copytest_order (c, b, a) to stdout (format csv);
+copy copytest_order (c, b, a) to stdout (format json);
+
--- test copying in JSON mode with various styles
copy (select 1 union all select 2) to stdout with (format json);
copy (select 1 as foo union all select 2) to stdout with (format json);