Commit f67b113a authored by Teodor Sigaev's avatar Teodor Sigaev

Add \if support to pgbench

Patch adds \if to pgbench as it done for psql. Implementation shares condition
stack code with psql, so, this code is moved to fe_utils directory.

Author: Fabien COELHO with minor editorization by me
Review by: Vik Fearing, Fedor Sigaev
Discussion: https://www.postgresql.org/message-id/flat/alpine.DEB.2.20.1711252200190.28523@lancre
parent b5db1d93
......@@ -900,6 +900,21 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
</para>
<variablelist>
<varlistentry>
<term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
<term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
<term><literal>\else</literal></term>
<term><literal>\endif</literal></term>
<listitem>
<para>
This group of commands implements nestable conditional blocks,
similarly to <literal>psql</literal>'s <xref linkend="psql-metacommand-if"/>.
Conditional expressions are identical to those with <literal>\set</literal>,
with non-zero values interpreted as true.
</para>
</listitem>
</varlistentry>
<varlistentry id='pgbench-metacommand-set'>
<term>
<literal>\set <replaceable>varname</replaceable> <replaceable>expression</replaceable></literal>
......
......@@ -2169,7 +2169,7 @@ hello 10
</varlistentry>
<varlistentry>
<varlistentry id="psql-metacommand-if">
<term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
<term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>
<term><literal>\else</literal></term>
......
This diff is collapsed.
......@@ -264,6 +264,12 @@ pgbench(
qr{command=51.: int -7793829335365542153\b},
qr{command=52.: int -?\d+\b},
qr{command=53.: boolean true\b},
qr{command=65.: int 65\b},
qr{command=74.: int 74\b},
qr{command=83.: int 83\b},
qr{command=86.: int 86\b},
qr{command=93.: int 93\b},
qr{command=95.: int 0\b},
],
'pgbench expressions',
{ '001_pgbench_expressions' => q{-- integer functions
......@@ -349,6 +355,41 @@ pgbench(
\set v2 5432
\set v3 -54.21E-2
SELECT :v0, :v1, :v2, :v3;
-- if tests
\set nope 0
\if 1 > 0
\set id debug(65)
\elif 0
\set nope 1
\else
\set nope 1
\endif
\if 1 < 0
\set nope 1
\elif 1 > 0
\set ie debug(74)
\else
\set nope 1
\endif
\if 1 < 0
\set nope 1
\elif 1 < 0
\set nope 1
\else
\set if debug(83)
\endif
\if 1 = 1
\set ig debug(86)
\elif 0
\set nope 1
\endif
\if 1 = 0
\set nope 1
\elif 1 <> 0
\set ih debug(93)
\endif
-- must be zero if false branches where skipped
\set nope debug(:nope)
} });
# backslash commands
......@@ -396,7 +437,7 @@ SELECT LEAST(:i, :i, :i, :i, :i, :i, :i, :i, :i, :i, :i);
# SHELL
[ 'shell bad command', 0,
[qr{meta-command 'shell' failed}], q{\shell no-such-command} ],
[qr{\(shell\) .* meta-command failed}], q{\shell no-such-command} ],
[ 'shell undefined variable', 0,
[qr{undefined variable ":nosuchvariable"}],
q{-- undefined variable in shell
......
......@@ -8,6 +8,16 @@ use warnings;
use TestLib;
use Test::More;
# create a directory for scripts
my $testname = $0;
$testname =~ s,.*/,,;
$testname =~ s/\.pl$//;
my $testdir = "$TestLib::tmp_check/t_${testname}_stuff";
mkdir $testdir
or
BAIL_OUT("could not create test directory \"${testdir}\": $!");
# invoke pgbench
sub pgbench
{
......@@ -17,6 +27,28 @@ sub pgbench
$stat, $out, $err, $name);
}
# invoke pgbench with scripts
sub pgbench_scripts
{
my ($opts, $stat, $out, $err, $name, $files) = @_;
my @cmd = ('pgbench', split /\s+/, $opts);
my @filenames = ();
if (defined $files)
{
for my $fn (sort keys %$files)
{
my $filename = $testdir . '/' . $fn;
# cleanup file weight if any
$filename =~ s/\@\d+$//;
# cleanup from prior runs
unlink $filename;
append_to_file($filename, $$files{$fn});
push @cmd, '-f', $filename;
}
}
command_checks_all(\@cmd, $stat, $out, $err, $name);
}
#
# Option various errors
#
......@@ -125,4 +157,24 @@ pgbench(
qr{simple-update}, qr{select-only} ],
'pgbench builtin list');
my @script_tests = (
# name, err, { file => contents }
[ 'missing endif', [qr{\\if without matching \\endif}], {'if-noendif.sql' => '\if 1'} ],
[ 'missing if on elif', [qr{\\elif without matching \\if}], {'elif-noif.sql' => '\elif 1'} ],
[ 'missing if on else', [qr{\\else without matching \\if}], {'else-noif.sql' => '\else'} ],
[ 'missing if on endif', [qr{\\endif without matching \\if}], {'endif-noif.sql' => '\endif'} ],
[ 'elif after else', [qr{\\elif after \\else}], {'else-elif.sql' => "\\if 1\n\\else\n\\elif 0\n\\endif"} ],
[ 'else after else', [qr{\\else after \\else}], {'else-else.sql' => "\\if 1\n\\else\n\\else\n\\endif"} ],
[ 'if syntax error', [qr{syntax error in command "if"}], {'if-bad.sql' => "\\if\n\\endif\n"} ],
[ 'elif syntax error', [qr{syntax error in command "elif"}], {'elif-bad.sql' => "\\if 0\n\\elif +\n\\endif\n"} ],
[ 'else syntax error', [qr{unexpected argument in command "else"}], {'else-bad.sql' => "\\if 0\n\\else BAD\n\\endif\n"} ],
[ 'endif syntax error', [qr{unexpected argument in command "endif"}], {'endif-bad.sql' => "\\if 0\n\\endif BAD\n"} ],
);
for my $t (@script_tests)
{
my ($name, $err, $files) = @$t;
pgbench_scripts('', 1, [qr{^$}], $err, 'pgbench option error: ' . $name, $files);
}
done_testing();
......@@ -21,7 +21,7 @@ REFDOCDIR= $(top_srcdir)/doc/src/sgml/ref
override CPPFLAGS := -I. -I$(srcdir) -I$(libpq_srcdir) $(CPPFLAGS)
override LDFLAGS := -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $(LDFLAGS)
OBJS= command.o common.o conditional.o copy.o crosstabview.o \
OBJS= command.o common.o copy.o crosstabview.o \
describe.o help.o input.o large_obj.o mainloop.o \
prompt.o psqlscanslash.o sql_help.o startup.o stringutils.o \
tab-complete.o variables.o \
......
......@@ -10,7 +10,7 @@
#include "fe_utils/print.h"
#include "fe_utils/psqlscan.h"
#include "conditional.h"
#include "fe_utils/conditional.h"
typedef enum _backslashResult
......
......@@ -10,7 +10,7 @@
/* enum promptStatus_t is now defined by psqlscan.h */
#include "fe_utils/psqlscan.h"
#include "conditional.h"
#include "fe_utils/conditional.h"
char *get_prompt(promptStatus_t status, ConditionalStack cstack);
......
......@@ -19,7 +19,7 @@
#include "postgres_fe.h"
#include "psqlscanslash.h"
#include "conditional.h"
#include "fe_utils/conditional.h"
#include "libpq-fe.h"
}
......
......@@ -19,7 +19,7 @@ include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) $(CPPFLAGS)
OBJS = mbprint.o print.o psqlscan.o simple_list.o string_utils.o
OBJS = mbprint.o print.o psqlscan.o simple_list.o string_utils.o conditional.o
all: libpgfeutils.a
......
/*
* psql - the PostgreSQL interactive terminal
/*-------------------------------------------------------------------------
* A stack of automaton states to handle nested conditionals.
*
* Copyright (c) 2000-2018, PostgreSQL Global Development Group
*
* src/bin/psql/conditional.c
* src/fe_utils/conditional.c
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "conditional.h"
#include "fe_utils/conditional.h"
/*
* create stack
......@@ -63,6 +65,27 @@ conditional_stack_pop(ConditionalStack cstack)
return true;
}
/*
* Returns current stack depth, for debugging purposes.
*/
int
conditional_stack_depth(ConditionalStack cstack)
{
if (cstack == NULL)
return -1;
else
{
IfStackElem *p = cstack->head;
int depth = 0;
while (p != NULL)
{
depth++;
p = p->next;
}
return depth;
}
}
/*
* Fetch the current state of the top of the stack.
*/
......
/*
* psql - the PostgreSQL interactive terminal
/*-------------------------------------------------------------------------
* A stack of automaton states to handle nested conditionals.
*
* This file describes a stack of automaton states which
* allow a manage nested conditionals.
*
* It is used by:
* - "psql" interpretor for handling \if ... \endif
* - "pgbench" interpretor for handling \if ... \endif
* - "pgbench" syntax checker to test for proper nesting
*
* The stack holds the state of enclosing conditionals (are we in
* a true branch? in a false branch? have we already encountered
* a true branch?) so that the interpreter knows whether to execute
* code and whether to evaluate conditions.
*
* Copyright (c) 2000-2018, PostgreSQL Global Development Group
*
* src/bin/psql/conditional.h
* src/include/fe_utils/conditional.h
*
*-------------------------------------------------------------------------
*/
#ifndef CONDITIONAL_H
#define CONDITIONAL_H
......@@ -60,6 +75,8 @@ extern ConditionalStack conditional_stack_create(void);
extern void conditional_stack_destroy(ConditionalStack cstack);
extern int conditional_stack_depth(ConditionalStack cstack);
extern void conditional_stack_push(ConditionalStack cstack, ifState new_state);
extern bool conditional_stack_pop(ConditionalStack cstack);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment