From fcf4b43dae7062f4786536fb262a665ad0b05b26 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Sun, 28 Oct 2018 16:09:37 +0100
Subject: [PATCH 2/2] review

---
 doc/src/sgml/ref/copy.sgml     |  2 +-
 src/backend/commands/copy.c    | 22 +++++++++++-----------
 src/backend/parser/gram.y      |  2 +-
 src/include/nodes/parsenodes.h |  2 +-
 4 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/ref/copy.sgml b/doc/src/sgml/ref/copy.sgml
index 8088e779b6..17bedf95cc 100644
--- a/doc/src/sgml/ref/copy.sgml
+++ b/doc/src/sgml/ref/copy.sgml
@@ -366,7 +366,7 @@ COPY { <replaceable class="parameter">table_name</replaceable> [ ( <replaceable
    </varlistentry>
 
    <varlistentry>
-    <term><literal>WHEN Clause</literal></term>
+    <term><literal>WHEN</literal></term>
     <listitem>
    <para>
     The optional <literal>WHEN</literal> clause has the general form
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 192a1c576b..8e87605135 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -803,7 +803,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
 	Relation	rel;
 	Oid			relid;
 	RawStmt    *query = NULL;
-	Node    *when_cluase= NULL;
+	Node	   *whenClause = NULL;
 
 	/*
 	 * Disallow COPY to/from file or program except to users with the
@@ -857,19 +857,19 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
 											NULL, false, false);
 		rte->requiredPerms = (is_from ? ACL_INSERT : ACL_SELECT);
 
-		if(stmt->whenClause)
+		if (stmt->whenClause)
 		{
 			/* add rte to column namespace  */
 			addRTEtoQuery(pstate, rte, false, false, true);
 
 			/* Transform the raw expression tree */
-			when_cluase = transformExpr(pstate, stmt->whenClause, EXPR_KIND_OTHER);
+			whenClause = transformExpr(pstate, stmt->whenClause, EXPR_KIND_OTHER);
 
 			/*  Make sure it yields a boolean result. */
-			when_cluase = coerce_to_boolean(pstate, when_cluase, "WHEN");
+			whenClause = coerce_to_boolean(pstate, whenClause, "WHEN");
 
-			when_cluase = (Node *) canonicalize_qual((Expr *) when_cluase, false);
-			when_cluase = (Node *) make_ands_implicit((Expr *) when_cluase);
+			whenClause = (Node *) canonicalize_qual((Expr *) whenClause, false);
+			whenClause = (Node *) make_ands_implicit((Expr *) whenClause);
 		}
 
 		tupDesc = RelationGetDescr(rel);
@@ -1020,7 +1020,7 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
 
 		cstate = BeginCopyFrom(pstate, rel, stmt->filename, stmt->is_program,
 							   NULL, stmt->attlist, stmt->options);
-		cstate->whenClause=when_cluase;
+		cstate->whenClause = whenClause;
 		*processed = CopyFrom(cstate);	/* copy from file to database */
 		EndCopyFrom(cstate);
 	}
@@ -2559,9 +2559,9 @@ CopyFrom(CopyState cstate)
 			ExecSetupChildParentMapForLeaf(proute);
 	}
 
-	if(cstate->whenClause )
+	if (cstate->whenClause)
 		cstate->qualexpr = ExecInitQual(castNode(List, cstate->whenClause),
-												&mtstate->ps);
+												 &mtstate->ps);
 
 	/*
 	 * It's more efficient to prepare a bunch of tuples for insertion, and
@@ -2717,11 +2717,11 @@ next_record:
 		slot = myslot;
 		ExecStoreHeapTuple(tuple, slot, false);
 
-		if(cstate->whenClause)
+		if (cstate->whenClause)
 		{
 			econtext->ecxt_scantuple = myslot;
 			if (!ExecQual(cstate->qualexpr, econtext))
-			goto next_record;
+				goto next_record;
 		}
 
 		/* Determine the partition to heap_insert the tuple into */
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 0f316846b6..6063644850 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -3180,7 +3180,7 @@ copy_generic_opt_arg_list:
 		;
 
 opt_when_clause:
-			WHEN a_expr							{ $$ = $2; }
+			WHEN '(' a_expr ')'						{ $$ = $3; }
 			| /*EMPTY*/								{ $$ = NULL; }
 		;
 
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 4d3a97f9bc..d9d41b5119 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -1972,7 +1972,7 @@ typedef struct CopyStmt
 	bool		is_program;		/* is 'filename' a program to popen? */
 	char	   *filename;		/* filename, or NULL for STDIN/STDOUT */
 	List	   *options;		/* List of DefElem nodes */
-	Node	   *whenClause;	/* qualifications */
+	Node	   *whenClause;		/* WHEN condition (or NULL) */
 } CopyStmt;
 
 /* ----------------------
-- 
2.13.6

