From be2fedcae277f7eede621dcda66b15b08372ce63 Mon Sep 17 00:00:00 2001 From: Raul Marin Date: Fri, 13 Oct 2017 17:42:23 +0200 Subject: [PATCH] Add pow() support to pgbench --- doc/src/sgml/ref/pgbench.sgml | 7 +++ src/bin/pgbench/exprparse.y | 3 + src/bin/pgbench/pgbench.c | 84 ++++++++++++++++++++++++++++ src/bin/pgbench/pgbench.h | 3 +- src/bin/pgbench/t/001_pgbench_with_server.pl | 13 +++++ 5 files changed, 109 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml index 1f55967e40a..32c94ba0dc1 100644 --- a/doc/src/sgml/ref/pgbench.sgml +++ b/doc/src/sgml/ref/pgbench.sgml @@ -1233,6 +1233,13 @@ pgbench options d sqrt(2.0) 1.414213562 + + pow(x, y) + integer if x and y are integers and y >= 0, else double + Numeric exponentiation + pow(2.0, 10) + 1024.0 + diff --git a/src/bin/pgbench/exprparse.y b/src/bin/pgbench/exprparse.y index 770be981f06..290bca99d12 100644 --- a/src/bin/pgbench/exprparse.y +++ b/src/bin/pgbench/exprparse.y @@ -334,6 +334,9 @@ static const struct { "!case_end", -2, PGBENCH_CASE }, + { + "pow", 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 add653bf90c..d565880b6e2 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -739,6 +739,51 @@ getPoissonRand(TState *thread, int64 center) } /* + * pow() for integer values + */ +static int64 +ipow(int64 base, int64 exp) +{ + int64 result; + + if (base == 0) + { + if (exp > 0) + return 0; + else if (exp == 0) + return 1; + return PG_INT64_MAX; + } + + /* + * For exp > 0 calculate normally + * For exp == 0 return 1 * sign of base + * For exp < 0 return 0 except when the base is 1 or -1 + */ + if (exp > 0) + { + result = 1; + while (exp) + { + if (exp & 1) + result *= base; + exp >>= 1; + base *= base; + } + } + else if (exp == 0) + result = (base > 0) - (base < 0); + else + { + result = 1 / base; + if (exp % 2 == 0) + result *= result; + } + + return result; +} + +/* * Initialize the given SimpleStats struct to all zeroes */ static void @@ -1918,6 +1963,45 @@ evalFunc(TState *thread, CState *st, return true; } + case PGBENCH_POW: + { + PgBenchValue *lval = &vargs[0]; + PgBenchValue *rval = &vargs[1]; + double ld, + rd; + + Assert(nargs == 2); + + /* + * If both operands are int and exp >= 0 use + * the 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) + { + setIntValue(retval, ipow(li, ri)); + return true; + } + } + + 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 e1277a1dde6..9f26af92bf6 100644 --- a/src/bin/pgbench/pgbench.h +++ b/src/bin/pgbench/pgbench.h @@ -94,7 +94,8 @@ typedef enum PgBenchFunction PGBENCH_LE, PGBENCH_LT, PGBENCH_IS, - PGBENCH_CASE + PGBENCH_CASE, + 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 8e19bbd3f45..2a1bb1216d7 100644 --- a/src/bin/pgbench/t/001_pgbench_with_server.pl +++ b/src/bin/pgbench/t/001_pgbench_with_server.pl @@ -237,6 +237,12 @@ sub pgbench qr{command=36.: int 36\b}, qr{command=37.: boolean true\b}, qr{command=38.: boolean true\b}, + qr{command=44.: int -27\b}, + qr{command=45.: double 1024\b}, + qr{command=46.: int 1\b}, + qr{command=47.: double 1\b}, + qr{command=48.: double -0.125\b}, + qr{command=49.: double -0.125\b}, ], 'pgbench expressions', { '001_pgbench_expressions' => q{-- integer functions @@ -299,6 +305,13 @@ sub pgbench \set v2 5432 \set v3 -54.21E-2 SELECT :v0, :v1, :v2, :v3; +--- pow() operator +\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)) } }); =head