so 23. 2. 2019 v 20:23 odesílatel Chapman Flack <c...@anastigmatix.net>
napsal:

> On 02/23/19 13:35, Pavel Stehule wrote:
> > please, see, attached patch
>
> It is getting close, for my purposes. There is still this:
>
> >> Can the sentence added to the doc be changed to "These functions support
> >> passing parameters as an array after <literal>VARIADIC</literal>
> >> keyword."? That is, s/supports/support/ and s/a/an/. I've done that
> >> after a couple of recent patches, but it seems to be on springs.
>

fixed


>
>
> > I know so is important to understand to things, but nobody can understand
> > to all. And then it is nice, so the things just works
> >>
> >> The approach with more parsimony indoors would be to just create a few
> >> new ordinary functions, and add to the doc explaining why they are
> >> different, and that would be a patch only needing to touch a couple
> files.
> >>
> >> I have a feeling that the final decision on whether the indoor or
> outdoor
> >> parsimony matters more will be made by Tom, or another committer; I find
> >> myself seeing both sides, and not feeling insider-y enough myself to
> >> pick one.
> >
> > sure, every time it is commiter's decision.
>
>
> A part of me waits to see if another voice chimes in on the high-level
> want/don't-want question ... I think enough of the patch is ready for
> that question to be ripe, and if the answer is going to be "don't want"
> it would be ideal to know that before additional iterations of work on it.
>

sure

Regards

Pavel


> -Chap
>
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 86ff4e5c9e..5e10124de9 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -12679,6 +12679,12 @@ SELECT NULLIF(value, '(none)') ...
 </synopsis>
 <synopsis>
 <function>LEAST</function>(<replaceable>value</replaceable> <optional>, ...</optional>)
+</synopsis>
+<synopsis>
+<function>GREATEST</function>(VARIADIC <replaceable>anyarray</replaceable>)
+</synopsis>
+<synopsis>
+<function>LEAST</function>(VARIADIC <replaceable>anyarray</replaceable>)
 </synopsis>
 
    <para>
@@ -12697,6 +12703,11 @@ SELECT NULLIF(value, '(none)') ...
     make them return NULL if any argument is NULL, rather than only when
     all are NULL.
    </para>
+
+   <para>
+    These functions support passing parameters as an array after
+    <literal>VARIADIC</literal> keyword.
+   </para>
   </sect2>
  </sect1>
 
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index a018925d4e..b1f3bc86de 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -2811,6 +2811,10 @@ ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
 	FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data;
 	MinMaxOp	operator = op->d.minmax.op;
 	int			off;
+	ArrayIterator array_iterator = NULL;
+	int			nelems;
+	bool		is_greatest;
+	bool		is_least;
 
 	/* set at initialization */
 	Assert(fcinfo->args[0].isnull == false);
@@ -2819,16 +2823,52 @@ ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
 	/* default to null result */
 	*op->resnull = true;
 
-	for (off = 0; off < op->d.minmax.nelems; off++)
+	is_least = operator == IS_LEAST || operator == IS_LEAST_VARIADIC;
+	is_greatest = operator == IS_GREATEST || operator == IS_GREATEST_VARIADIC;
+
+	if (IsVariadicMinMax(operator))
+	{
+		ArrayType  *arr;
+
+		/* result is null, when variadic argument is NULL */
+		if (nulls[0])
+			return;
+
+		/* prepare iterarator */
+		arr = DatumGetArrayTypeP(values[0]);
+		array_iterator = array_create_iterator(arr, 0, NULL);
+
+		nelems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
+	}
+	else
+		nelems = op->d.minmax.nelems;
+
+	for (off = 0; off < nelems; off++)
 	{
+		Datum	value;
+		bool	isnull;
+
+		if (array_iterator)
+		{
+			bool has_data PG_USED_FOR_ASSERTS_ONLY;
+
+			has_data = array_iterate(array_iterator, &value, &isnull);
+			Assert(has_data);
+		}
+		else
+		{
+			value = values[off];
+			isnull = nulls[off];
+		}
+
 		/* ignore NULL inputs */
-		if (nulls[off])
+		if (isnull)
 			continue;
 
 		if (*op->resnull)
 		{
 			/* first nonnull input, adopt value */
-			*op->resvalue = values[off];
+			*op->resvalue = value;
 			*op->resnull = false;
 		}
 		else
@@ -2837,19 +2877,23 @@ ExecEvalMinMax(ExprState *state, ExprEvalStep *op)
 
 			/* apply comparison function */
 			fcinfo->args[0].value = *op->resvalue;
-			fcinfo->args[1].value = values[off];
+			fcinfo->args[1].value = value;
 
 			fcinfo->isnull = false;
 			cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo));
 			if (fcinfo->isnull) /* probably should not happen */
 				continue;
 
-			if (cmpresult > 0 && operator == IS_LEAST)
-				*op->resvalue = values[off];
-			else if (cmpresult < 0 && operator == IS_GREATEST)
-				*op->resvalue = values[off];
+			if (cmpresult > 0 && is_least)
+				*op->resvalue = value;
+			else if (cmpresult < 0 && is_greatest)
+				*op->resvalue = value;
 		}
 	}
+
+	/* release iterator */
+	if (array_iterator)
+		array_free_iterator(array_iterator);
 }
 
 /*
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index 8ed30c011a..efabaa0046 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -465,7 +465,8 @@ exprTypmod(const Node *expr)
 				int32		typmod;
 				ListCell   *arg;
 
-				if (exprType((Node *) linitial(mexpr->args)) != minmaxtype)
+				if (!IsVariadicMinMax(mexpr->op) &&
+						exprType((Node *) linitial(mexpr->args)) != minmaxtype)
 					return -1;
 				typmod = exprTypmod((Node *) linitial(mexpr->args));
 				if (typmod < 0)
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index a68f78e0e0..65f44a8df6 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -13803,6 +13803,22 @@ func_expr_common_subexpr:
 					v->location = @1;
 					$$ = (Node *)v;
 				}
+			| GREATEST '(' VARIADIC a_expr ')'
+				{
+					MinMaxExpr *v = makeNode(MinMaxExpr);
+					v->args = list_make1($4);
+					v->op = IS_GREATEST_VARIADIC;
+					v->location = @1;
+					$$ = (Node *)v;
+				}
+			| LEAST '(' VARIADIC a_expr ')'
+				{
+					MinMaxExpr *v = makeNode(MinMaxExpr);
+					v->args = list_make1($4);
+					v->op = IS_LEAST_VARIADIC;
+					v->location = @1;
+					$$ = (Node *)v;
+				}
 			| XMLCONCAT '(' expr_list ')'
 				{
 					$$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index e559353529..7025b5c3f5 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -2263,9 +2263,12 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
 	MinMaxExpr *newm = makeNode(MinMaxExpr);
 	List	   *newargs = NIL;
 	List	   *newcoercedargs = NIL;
-	const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
+	const char *funcname;
 	ListCell   *args;
 
+	funcname = (m->op == IS_GREATEST || m->op == IS_GREATEST_VARIADIC)
+					 ? "GREATEST" : "LEAST";
+
 	newm->op = m->op;
 	foreach(args, m->args)
 	{
@@ -2276,22 +2279,47 @@ transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
 		newargs = lappend(newargs, newe);
 	}
 
-	newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
-	/* minmaxcollid and inputcollid will be set by parse_collate.c */
+	if (IsVariadicMinMax(newm->op))
+	{
+		Oid		array_typid;
+		Oid		element_typid;
 
-	/* Convert arguments if necessary */
-	foreach(args, newargs)
+		array_typid = exprType(linitial(newargs));
+
+		if (array_typid == InvalidOid)
+			elog(ERROR, "cannot determine result type");
+
+		element_typid = get_base_element_type(array_typid);
+		if (!OidIsValid(element_typid))
+			ereport(ERROR,
+					(errcode(ERRCODE_DATATYPE_MISMATCH),
+					 errmsg("VARIADIC argument must be an array"),
+					 parser_errposition(pstate,
+								exprLocation((Node *) linitial(newargs)))));
+
+		newm->minmaxtype = element_typid;
+		newm->args = newargs;
+	}
+	else
 	{
-		Node	   *e = (Node *) lfirst(args);
-		Node	   *newe;
+		newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
+		/* minmaxcollid and inputcollid will be set by parse_collate.c */
 
-		newe = coerce_to_common_type(pstate, e,
-									 newm->minmaxtype,
-									 funcname);
-		newcoercedargs = lappend(newcoercedargs, newe);
+		/* Convert arguments if necessary */
+		foreach(args, newargs)
+		{
+			Node	   *e = (Node *) lfirst(args);
+			Node	   *newe;
+
+			newe = coerce_to_common_type(pstate, e,
+										 newm->minmaxtype,
+										 funcname);
+			newcoercedargs = lappend(newcoercedargs, newe);
+		}
+
+		newm->args = newcoercedargs;
 	}
 
-	newm->args = newcoercedargs;
 	newm->location = m->location;
 	return (Node *) newm;
 }
diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c
index 0e9598ebfe..013596756d 100644
--- a/src/backend/parser/parse_target.c
+++ b/src/backend/parser/parse_target.c
@@ -1843,9 +1843,11 @@ FigureColnameInternal(Node *node, char **name)
 			switch (((MinMaxExpr *) node)->op)
 			{
 				case IS_GREATEST:
+				case IS_GREATEST_VARIADIC:
 					*name = "greatest";
 					return 2;
 				case IS_LEAST:
+				case IS_LEAST_VARIADIC:
 					*name = "least";
 					return 2;
 			}
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 1258092dc8..6e7ebb1861 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -8468,13 +8468,23 @@ get_rule_expr(Node *node, deparse_context *context,
 				switch (minmaxexpr->op)
 				{
 					case IS_GREATEST:
+					case IS_GREATEST_VARIADIC:
 						appendStringInfoString(buf, "GREATEST(");
 						break;
 					case IS_LEAST:
+					case IS_LEAST_VARIADIC:
 						appendStringInfoString(buf, "LEAST(");
 						break;
 				}
-				get_rule_expr((Node *) minmaxexpr->args, context, true);
+
+				if (IsVariadicMinMax(minmaxexpr->op))
+				{
+					appendStringInfoString(buf, "VARIADIC ");
+					get_rule_expr(linitial(minmaxexpr->args), context, true);
+				}
+				else
+					get_rule_expr((Node *) minmaxexpr->args, context, true);
+
 				appendStringInfoChar(buf, ')');
 			}
 			break;
diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h
index a7efae7038..827fcf6d42 100644
--- a/src/include/nodes/primnodes.h
+++ b/src/include/nodes/primnodes.h
@@ -1080,9 +1080,14 @@ typedef struct CoalesceExpr
 typedef enum MinMaxOp
 {
 	IS_GREATEST,
-	IS_LEAST
+	IS_LEAST,
+	IS_GREATEST_VARIADIC,
+	IS_LEAST_VARIADIC
 } MinMaxOp;
 
+#define IsVariadicMinMax(op)		(op == IS_GREATEST_VARIADIC || \
+									 op == IS_LEAST_VARIADIC)
+
 typedef struct MinMaxExpr
 {
 	Expr		xpr;
diff --git a/src/test/regress/expected/case.out b/src/test/regress/expected/case.out
index c0c8acf035..eca127ed2d 100644
--- a/src/test/regress/expected/case.out
+++ b/src/test/regress/expected/case.out
@@ -392,3 +392,54 @@ ROLLBACK;
 --
 DROP TABLE CASE_TBL;
 DROP TABLE CASE2_TBL;
+--
+-- greatest and least tests
+--
+SELECT least(10,20,30,40);
+ least 
+-------
+    10
+(1 row)
+
+SELECT greatest(10,20,30,40);
+ greatest 
+----------
+       40
+(1 row)
+
+SELECT least(VARIADIC ARRAY[10,20,30,40]);
+ least 
+-------
+    10
+(1 row)
+
+SELECT greatest(VARIADIC ARRAY[10,20,30,40]);
+ greatest 
+----------
+       40
+(1 row)
+
+SELECT least(10,20,30,40, NULL);
+ least 
+-------
+    10
+(1 row)
+
+SELECT greatest(10,20,30,40, NULL);
+ greatest 
+----------
+       40
+(1 row)
+
+SELECT least(VARIADIC ARRAY[10,20,30,40, NULL]);
+ least 
+-------
+    10
+(1 row)
+
+SELECT greatest(VARIADIC ARRAY[10,20,30,40, NULL]);
+ greatest 
+----------
+       40
+(1 row)
+
diff --git a/src/test/regress/sql/case.sql b/src/test/regress/sql/case.sql
index 17436c524a..c482676e17 100644
--- a/src/test/regress/sql/case.sql
+++ b/src/test/regress/sql/case.sql
@@ -252,3 +252,16 @@ ROLLBACK;
 
 DROP TABLE CASE_TBL;
 DROP TABLE CASE2_TBL;
+
+--
+-- greatest and least tests
+--
+SELECT least(10,20,30,40);
+SELECT greatest(10,20,30,40);
+SELECT least(VARIADIC ARRAY[10,20,30,40]);
+SELECT greatest(VARIADIC ARRAY[10,20,30,40]);
+
+SELECT least(10,20,30,40, NULL);
+SELECT greatest(10,20,30,40, NULL);
+SELECT least(VARIADIC ARRAY[10,20,30,40, NULL]);
+SELECT greatest(VARIADIC ARRAY[10,20,30,40, NULL]);

Reply via email to