pgbench_pow_v6.patch
text/x-patch
Filename: pgbench_pow_v6.patch
Type: text/x-patch
Part: 0
Patch
Format: format-patch
Series: patch v6
Subject: Add pow() support to pgbench
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/pgbench.sgml | 7 | 0 |
| src/bin/pgbench/exprparse.y | 3 | 0 |
| src/bin/pgbench/pgbench.c | 62 | 0 |
| src/bin/pgbench/pgbench.h | 2 | 1 |
| src/bin/pgbench/t/001_pgbench_with_server.pl | 15 | 1 |
From b12a7a37b1af1ede1aa9d5d88d0918808c54e19f Mon Sep 17 00:00:00 2001
From: Raul Marin <rmrodriguez@cartodb.com>
Date: Fri, 1 Dec 2017 10:49:17 +0100
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 | 62 ++++++++++++++++++++++++++++
src/bin/pgbench/pgbench.h | 3 +-
src/bin/pgbench/t/001_pgbench_with_server.pl | 16 ++++++-
5 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml
index 94b495e606..769e604dd6 100644
--- a/doc/src/sgml/ref/pgbench.sgml
+++ b/doc/src/sgml/ref/pgbench.sgml
@@ -1099,6 +1099,13 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
<entry><literal>sqrt(2.0)</literal></entry>
<entry><literal>1.414213562</literal></entry>
</row>
+ <row>
+ <entry><literal><function>pow(<replaceable>x</replaceable>, <replaceable>y</replaceable>)</function></literal></entry>
+ <entry>integer if <replaceable>x</replaceable> and <replaceable>y</replaceable> are integers and <literal><replaceable>y</replaceable> >= 0</literal>, else double</entry>
+ <entry>Numeric exponentiation</entry>
+ <entry><literal>pow(2.0, 10)</literal></entry>
+ <entry><literal>1024.0</literal></entry>
+ </row>
</tbody>
</tgroup>
</table>
diff --git a/src/bin/pgbench/exprparse.y b/src/bin/pgbench/exprparse.y
index b3a2d9bfd3..4db3b215ab 100644
--- a/src/bin/pgbench/exprparse.y
+++ b/src/bin/pgbench/exprparse.y
@@ -191,6 +191,9 @@ static const struct
{
"random_exponential", 3, PGBENCH_RANDOM_EXPONENTIAL
},
+ {
+ "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 bd96eae5e6..5a5197cf63 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -746,6 +746,27 @@ getPoissonRand(TState *thread, int64 center)
return (int64) (-log(uniform) * ((double) center) + 0.5);
}
+ /*
+ * pow() for integer values with exp >= 0. Matches SQL pow() behaviour
+ */
+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
*/
@@ -1673,6 +1694,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 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));
+ 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 fd428af274..e0132b5fcf 100644
--- a/src/bin/pgbench/pgbench.h
+++ b/src/bin/pgbench/pgbench.h
@@ -75,7 +75,8 @@ typedef enum PgBenchFunction
PGBENCH_SQRT,
PGBENCH_RANDOM,
PGBENCH_RANDOM_GAUSSIAN,
- PGBENCH_RANDOM_EXPONENTIAL
+ PGBENCH_RANDOM_EXPONENTIAL,
+ 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 c095881312..fcb30cdde5 100644
--- a/src/bin/pgbench/t/001_pgbench_with_server.pl
+++ b/src/bin/pgbench/t/001_pgbench_with_server.pl
@@ -231,7 +231,14 @@ pgbench(
qr{command=18.: double 18\b},
qr{command=19.: double 19\b},
qr{command=20.: double 20\b},
- qr{command=21.: int 9223372036854775807\b}, ],
+ qr{command=21.: int 9223372036854775807\b},
+ qr{command=23.: int -27\b},
+ qr{command=24.: double 1024\b},
+ qr{command=25.: int 1\b},
+ qr{command=26.: double 1\b},
+ qr{command=27.: double -0.125\b},
+ qr{command=28.: double -0.125\b},
+ ],
'pgbench expressions',
{ '001_pgbench_expressions' => q{-- integer functions
\set i1 debug(random(1, 100))
@@ -261,6 +268,13 @@ pgbench(
\set maxint debug(:minint - 1)
-- reset a variable
\set i1 0
+--- 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))
} });
# backslash commands
--
2.15.1