Skip to content

Commit 7a727c1

Browse files
committed
Add pow(), aka power(), function to pgbench.
Raúl Marín Rodríguez, reviewed by Fabien Coelho and Michael Paquier, with a minor fix by me. Discussion: http://postgr.es/m/CAM6_UM4XiA14y9HnDqu9kAAOtwMhHZxW--q_ZACZW9Hsrsf-tg@mail.gmail.com
1 parent ad337c7 commit 7a727c1

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

doc/src/sgml/ref/pgbench.sgml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,13 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
10691069
<entry><literal>pi()</literal></entry>
10701070
<entry><literal>3.14159265358979323846</literal></entry>
10711071
</row>
1072+
<row>
1073+
<entry><literal><function>pow(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</function>, <function>power(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</function></literal></entry>
1074+
<entry>double</entry>
1075+
<entry>exponentiation</entry>
1076+
<entry><literal>pow(2.0, 10)</literal>, <literal>power(2.0, 10)</literal></entry>
1077+
<entry><literal>1024.0</literal></entry>
1078+
</row>
10721079
<row>
10731080
<entry><literal><function>random(<replaceable>lb</replaceable>, <replaceable>ub</replaceable>)</function></literal></entry>
10741081
<entry>integer</entry>

src/bin/pgbench/exprparse.y

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ static const struct
194194
{
195195
"random_zipfian", 3, PGBENCH_RANDOM_ZIPFIAN
196196
},
197+
{
198+
"pow", 2, PGBENCH_POW
199+
},
200+
{
201+
"power", 2, PGBENCH_POW
202+
},
197203
/* keep as last array element */
198204
{
199205
NULL, 0, 0

src/bin/pgbench/pgbench.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,24 @@ evalFunc(TState *thread, CState *st,
18501850
return true;
18511851
}
18521852

1853+
case PGBENCH_POW:
1854+
{
1855+
PgBenchValue *lval = &vargs[0];
1856+
PgBenchValue *rval = &vargs[1];
1857+
double ld,
1858+
rd;
1859+
1860+
Assert(nargs == 2);
1861+
1862+
if (!coerceToDouble(lval, &ld) ||
1863+
!coerceToDouble(rval, &rd))
1864+
return false;
1865+
1866+
setDoubleValue(retval, pow(ld, rd));
1867+
1868+
return true;
1869+
}
1870+
18531871
default:
18541872
/* cannot get here */
18551873
Assert(0);

src/bin/pgbench/pgbench.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ typedef enum PgBenchFunction
7676
PGBENCH_RANDOM,
7777
PGBENCH_RANDOM_GAUSSIAN,
7878
PGBENCH_RANDOM_EXPONENTIAL,
79-
PGBENCH_RANDOM_ZIPFIAN
79+
PGBENCH_RANDOM_ZIPFIAN,
80+
PGBENCH_POW
8081
} PgBenchFunction;
8182

8283
typedef struct PgBenchExpr PgBenchExpr;

src/bin/pgbench/t/001_pgbench_with_server.pl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,17 @@ sub pgbench
232232
qr{command=19.: double 19\b},
233233
qr{command=20.: double 20\b},
234234
qr{command=21.: int 9223372036854775807\b},
235-
qr{command=23.: int [1-9]\b}, ],
235+
qr{command=23.: int [1-9]\b},
236+
qr{command=24.: double -27\b},
237+
qr{command=25.: double 1024\b},
238+
qr{command=26.: double 1\b},
239+
qr{command=27.: double 1\b},
240+
qr{command=28.: double -0.125\b},
241+
qr{command=29.: double -0.125\b},
242+
qr{command=30.: double -0.00032\b},
243+
qr{command=31.: double 8.50705917302346e\+37\b},
244+
qr{command=32.: double 1e\+30\b},
245+
],
236246
'pgbench expressions',
237247
{ '001_pgbench_expressions' => q{-- integer functions
238248
\set i1 debug(random(1, 100))
@@ -264,6 +274,16 @@ sub pgbench
264274
\set i1 0
265275
-- yet another integer function
266276
\set id debug(random_zipfian(1, 9, 1.3))
277+
--- pow and power
278+
\set poweri debug(pow(-3,3))
279+
\set powerd debug(pow(2.0,10))
280+
\set poweriz debug(pow(0,0))
281+
\set powerdz debug(pow(0.0,0.0))
282+
\set powernegi debug(pow(-2,-3))
283+
\set powernegd debug(pow(-2.0,-3.0))
284+
\set powernegd2 debug(power(-5.0,-5.0))
285+
\set powerov debug(pow(9223372036854775807, 2))
286+
\set powerov2 debug(pow(10,30))
267287
} });
268288

269289
# backslash commands

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy