Three different modulo operators seems like a lot for a language that
doesn't even have a real expression syntax, but I'll yield to whatever
the consensus is on this one.
Here is a third simpler patch which only implements the Knuth's modulo,
where the remainder has the same sign as the divisor.
I would prefer this version 3 (one simple modulo based on Knuth
definition) or if there is a problem version 2 (all 3 modulos). Version 1
which provides a modulo compatible with C & SQL is really useless to me.
--
Fabien.
diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index c0e5e24..3f53e2c 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -1574,6 +1574,22 @@ top:
}
snprintf(res, sizeof(res), INT64_FORMAT, ope1 / ope2);
}
+ else if (strcmp(argv[3], "%") == 0)
+ {
+ int64_t remainder;
+ if (ope2 == 0)
+ {
+ fprintf(stderr, "%s: division by zero in modulo\n", argv[0]);
+ st->ecnt++;
+ return true;
+ }
+ /* divisor signed remainder */
+ remainder = ope1 % ope2;
+ if ((ope2 < 0 && remainder > 0) ||
+ (ope2 > 0 && remainder < 0))
+ remainder += ope2;
+ snprintf(res, sizeof(res), INT64_FORMAT, remainder);
+ }
else
{
fprintf(stderr, "%s: unsupported operator %s\n", argv[0], argv[3]);
diff --git a/doc/src/sgml/pgbench.sgml b/doc/src/sgml/pgbench.sgml
index b7d88f3..d722f31 100644
--- a/doc/src/sgml/pgbench.sgml
+++ b/doc/src/sgml/pgbench.sgml
@@ -735,7 +735,7 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
Each <replaceable>operand</> is either an integer constant or a
<literal>:</><replaceable>variablename</> reference to a variable
having an integer value. The <replaceable>operator</> can be
- <literal>+</>, <literal>-</>, <literal>*</>, or <literal>/</>.
+ <literal>+</>, <literal>-</>, <literal>*</>, <literal>%</> (divisor-signed version) or <literal>/</>.
</para>
<para>
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers