pgbench - very minor bug fix on hash() missing argument

Started by Fabien COELHOover 7 years ago3 messages
#1Fabien COELHO
coelho@cri.ensmp.fr
1 attachment(s)

While doing something else, I noticed that pgbench's hash() does not fail
gracefully:

sh> cat hash.sql
\set i hash()

sh> pgbench -f hash.sql -t 1
...
cannot coerce (null) to int
client 0 aborted in command 0 (set) of script 0; evaluation of meta-command failed

The message is not very helful. With the attached one-line patch plus
test, it is clearer:

sh> pgbench -f hash.sql -t 1
hash.sql:1: unexpected number of arguments (hash) in command "set"
\set i hash()
^ error found here

Could be backpatched to 11 where hash was introduced.

--
Fabien.

Attachments:

pgbench-hash-nargs-1.patchtext/plain; name=pgbench-hash-nargs-1.patchDownload
diff --git a/src/bin/pgbench/exprparse.y b/src/bin/pgbench/exprparse.y
index 66288632d1..f7c56cc6a3 100644
--- a/src/bin/pgbench/exprparse.y
+++ b/src/bin/pgbench/exprparse.y
@@ -467,7 +467,7 @@ make_func(yyscan_t yyscanner, int fnumber, PgBenchExprList *args)
 
 		/* hash functions with optional seed argument */
 		case PGBENCH_NARGS_HASH:
-			if (len > 2)
+			if (len < 1 || len > 2)
 				expr_yyerror_more(yyscanner, "unexpected number of arguments",
 								  PGBENCH_FUNCTIONS[fnumber].fname);
 
diff --git a/src/bin/pgbench/t/002_pgbench_no_server.pl b/src/bin/pgbench/t/002_pgbench_no_server.pl
index a2845a583b..51875f99d1 100644
--- a/src/bin/pgbench/t/002_pgbench_no_server.pl
+++ b/src/bin/pgbench/t/002_pgbench_no_server.pl
@@ -270,6 +270,11 @@ my @script_tests = (
 		'endif syntax error',
 		[qr{unexpected argument in command "endif"}],
 		{ 'endif-bad.sql' => "\\if 0\n\\endif BAD\n" }
+	],
+	[
+		'hash unexpected #args',
+		[qr{unexpected number of arguments \(hash\)}],
+		{ 'bad-hash.sql' => "\\set i hash()\n" }
 	],);
 
 for my $t (@script_tests)
#2Michael Paquier
michael@paquier.xyz
In reply to: Fabien COELHO (#1)
Re: pgbench - very minor bug fix on hash() missing argument

On Thu, Jul 26, 2018 at 11:16:06PM -0400, Fabien COELHO wrote:

Could be backpatched to 11 where hash was introduced.

Thanks, committed and back-patched. I have added some tests for least()
and greatest() on the way.
--
Michael

#3Fabien COELHO
coelho@cri.ensmp.fr
In reply to: Michael Paquier (#2)
Re: pgbench - very minor bug fix on hash() missing argument

Hello Michaᅵl,

Thanks, committed and back-patched.

Ok.

I have added some tests for least() and greatest() on the way.

Good!

Thanks,

--
Fabien.