Re: New function normal_rand_array function to contrib/tablefunc. - Mailing list pgsql-hackers

From Japin Li
Subject Re: New function normal_rand_array function to contrib/tablefunc.
Date
Msg-id ME0P300MB044589098B6FB20AA771C97DB6952@ME0P300MB0445.AUSP300.PROD.OUTLOOK.COM
Whole thread Raw
In response to New function normal_rand_array function to contrib/tablefunc.  (Andy Fan <zhihuifan1213@163.com>)
List pgsql-hackers
On Wed, 28 Aug 2024 at 12:27, Andy Fan <zhihuifan1213@163.com> wrote:
> Japin Li <japinli@hotmail.com> writes:
>
>
>> Thanks for updating the patch. Here are some comments.
>>
>> +    if (minlen >= maxlen)
>> +        ereport(ERROR,
>> +                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>> +                 errmsg("minlen must be greater than maxlen.")));
>>
>> There error message should be "minlen must be smaller than maxlen", right?
>>
>> +    if (minlen < 0)
>> +        ereport(ERROR,
>> +                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
>> +                 errmsg("minlen and maxlen must be greater than zero.")));
>>
>> Here the minlen might be zero, so the error message is incorrect.
>> How about use "minlen must be greater than or equal to zero"?
>
> Yes, you are right. A new version is attached, thanks for checking! 
>

Nitpick, the minlen is smaller than maxlen, so the maxlen cannot be zero.

After giving it some more thought, it would also be helpful if maxlen is
equal to minlen.

For example, I want have each row has four items, I can use the following

SELECT rand_array(10, 4, 4, 50::int, 80::int);

OTOH, I find the range bound uses "less than or equal to", how about
replacing "smaller" with "less"?

-- 
Regrads,
Japin Li

diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index b24c70d538..69b276b285 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -306,10 +306,10 @@ rand_array_internal(FunctionCallInfo fcinfo, Oid datatype)
                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                  errmsg("number of rows cannot be negative")));
 
-    if (minlen >= maxlen)
+    if (minlen > maxlen)
         ereport(ERROR,
                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                 errmsg("minlen must be smaller than maxlen.")));
+                 errmsg("minlen must be less than or equal to maxlen.")));
 
     if (minlen < 0)
         ereport(ERROR,

pgsql-hackers by date:

Previous
From: Ajin Cherian
Date:
Subject: Re: Conflict Detection and Resolution
Next
From: shveta malik
Date:
Subject: Re: Conflict Detection and Resolution