Hi Fabien, Thanks for the review.
If a double is always returned, I'm wondering whether keeping the ipow > version makes much sense: In case of double loss of precision, the > precision is lost, too bad, and casting back to int won't bring it back. I've kept it because knowing that both are ints enables not making a lot of checks (done in math.h pow) so it's way faster. In my system it's 2-3ns vs ~40ns. I'm willing to settle for using just pow() if that makes it clearer. In the doc, I'm not sure that "Numeric" brings anything. "Exponentiation" > would be enough. Done. Also, in pg I just noticed that POW is a shorthand for POWER. Maybe both > should be supported? Or not. I've never used power instead of pow, but I've added for compatibility shake. Attached the updated patch. On Thu, Dec 21, 2017 at 10:48 PM, Fabien COELHO <coe...@cri.ensmp.fr> wrote: > > Hello Raúl, > > v7 needs a rebase. >>> >>> Also, you might try to produce a version which is compatible with >>> Robert's >>> constraints. >>> >> > My 0.02€ on this new version: Applies cleanly, compiles and works. > > I cannot say that I like it more than the previous version. > > If a double is always returned, I'm wondering whether keeping the ipow > version makes much sense: In case of double loss of precision, the > precision is lost, too bad, and casting back to int won't bring it back. > > In the doc, I'm not sure that "Numeric" brings anything. "Exponentiation" > would be enough. > > Also, in pg I just noticed that POW is a shorthand for POWER. Maybe both > should be supported? Or not. > > -- > Fabien. -- *Raúl Marín Rodríguez *carto.com
From 58463d2751c016a68410d5357625bf3d7f519ca2 Mon Sep 17 00:00:00 2001 From: Raul Marin <rmrodrig...@cartodb.com> Date: Thu, 21 Dec 2017 20:28:02 +0100 Subject: [PATCH] Add pow() support to pgbench --- doc/src/sgml/ref/pgbench.sgml | 7 ++++ src/bin/pgbench/exprparse.y | 6 +++ src/bin/pgbench/pgbench.c | 60 ++++++++++++++++++++++++++++ src/bin/pgbench/pgbench.h | 3 +- src/bin/pgbench/t/001_pgbench_with_server.pl | 18 ++++++++- 5 files changed, 92 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml index 4431fc3eb7..1519fe78ef 100644 --- a/doc/src/sgml/ref/pgbench.sgml +++ b/doc/src/sgml/ref/pgbench.sgml @@ -1069,6 +1069,13 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d <entry><literal>pi()</literal></entry> <entry><literal>3.14159265358979323846</literal></entry> </row> + <row> + <entry><literal><function>pow(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</function>, <function>power(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</function></literal></entry> + <entry>double</entry> + <entry>exponentiation</entry> + <entry><literal>pow(2.0, 10)</literal>, <literal>power(2.0, 10)</literal></entry> + <entry><literal>1024.0</literal></entry> + </row> <row> <entry><literal><function>random(<replaceable>lb</replaceable>, <replaceable>ub</replaceable>)</function></literal></entry> <entry>integer</entry> diff --git a/src/bin/pgbench/exprparse.y b/src/bin/pgbench/exprparse.y index 25d5ad48e5..74ffe5e7a7 100644 --- a/src/bin/pgbench/exprparse.y +++ b/src/bin/pgbench/exprparse.y @@ -194,6 +194,12 @@ static const struct { "random_zipfian", 3, PGBENCH_RANDOM_ZIPFIAN }, + { + "pow", 2, PGBENCH_POW + }, + { + "power", 2, PGBENCH_POW + }, /* keep as last array element */ { NULL, 0, 0 diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 7ce6f607f5..f1c1e568e1 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -911,6 +911,25 @@ getZipfianRand(TState *thread, int64 min, int64 max, double s) : computeHarmonicZipfian(thread, n, s)); } +/* Faster power function for integer values and exp >= 0 */ +static int64 +ipow(int64 base, int64 exp) +{ + int64 result = 1; + + Assert(exp >= 0); + + while (exp) + { + if (exp & 1) + result *= base; + exp >>= 1; + base *= base; + } + + return result; +} + /* * Initialize the given SimpleStats struct to all zeroes */ @@ -1850,6 +1869,47 @@ evalFunc(TState *thread, CState *st, return true; } + case PGBENCH_POW: + { + PgBenchValue *lval = &vargs[0]; + PgBenchValue *rval = &vargs[1]; + + Assert(nargs == 2); + + /* + * If both operands are int and exp >= 0 use + * the faster ipow() function, else use pow() + */ + if (lval->type == PGBT_INT && + rval->type == PGBT_INT) + { + + int64 li, + ri; + + if (!coerceToInt(lval, &li) || + !coerceToInt(rval, &ri)) + return false; + + if (ri >= 0) + setDoubleValue(retval, ipow(li, ri)); + else + setDoubleValue(retval, pow(li, ri)); + } + else + { + double ld, + rd; + + if (!coerceToDouble(lval, &ld) || + !coerceToDouble(rval, &rd)) + return false; + + setDoubleValue(retval, pow(ld, rd)); + } + return true; + } + default: /* cannot get here */ Assert(0); diff --git a/src/bin/pgbench/pgbench.h b/src/bin/pgbench/pgbench.h index 83fee1ae74..0e92882a4c 100644 --- a/src/bin/pgbench/pgbench.h +++ b/src/bin/pgbench/pgbench.h @@ -76,7 +76,8 @@ typedef enum PgBenchFunction PGBENCH_RANDOM, PGBENCH_RANDOM_GAUSSIAN, PGBENCH_RANDOM_EXPONENTIAL, - PGBENCH_RANDOM_ZIPFIAN + PGBENCH_RANDOM_ZIPFIAN, + PGBENCH_POW } PgBenchFunction; typedef struct PgBenchExpr PgBenchExpr; diff --git a/src/bin/pgbench/t/001_pgbench_with_server.pl b/src/bin/pgbench/t/001_pgbench_with_server.pl index e3cdf28628..b0aeae66f0 100644 --- a/src/bin/pgbench/t/001_pgbench_with_server.pl +++ b/src/bin/pgbench/t/001_pgbench_with_server.pl @@ -232,7 +232,15 @@ pgbench( qr{command=19.: double 19\b}, qr{command=20.: double 20\b}, qr{command=21.: int 9223372036854775807\b}, - qr{command=23.: int [1-9]\b}, ], + qr{command=23.: int [1-9]\b}, + qr{command=24.: double -27\b}, + qr{command=25.: double 1024\b}, + qr{command=26.: double 1\b}, + qr{command=27.: double 1\b}, + qr{command=28.: double -0.125\b}, + qr{command=29.: double -0.125\b}, + qr{command=30.: double -0.00032\b}, + ], 'pgbench expressions', { '001_pgbench_expressions' => q{-- integer functions \set i1 debug(random(1, 100)) @@ -264,6 +272,14 @@ pgbench( \set i1 0 -- yet another integer function \set id debug(random_zipfian(1, 9, 1.3)) +--- pow and power +\set poweri debug(pow(-3,3)) +\set powerd debug(pow(2.0,10)) +\set poweriz debug(pow(0,0)) +\set powerdz debug(pow(0.0,0.0)) +\set powernegi debug(pow(-2,-3)) +\set powernegd debug(pow(-2.0,-3.0)) +\set powernegd2 debug(power(-5.0,-5.0)) } }); # backslash commands -- 2.15.1