LCOV - code coverage report
Current view: top level - src/bin/pgbench - exprparse.y (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 133 134 99.3 %
Date: 2019-11-13 22:07:24 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : %{
       2             : /*-------------------------------------------------------------------------
       3             :  *
       4             :  * exprparse.y
       5             :  *    bison grammar for a simple expression syntax
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  * src/bin/pgbench/exprparse.y
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres_fe.h"
      16             : 
      17             : #include "pgbench.h"
      18             : 
      19             : #define PGBENCH_NARGS_VARIABLE  (-1)
      20             : #define PGBENCH_NARGS_CASE      (-2)
      21             : #define PGBENCH_NARGS_HASH      (-3)
      22             : 
      23             : PgBenchExpr *expr_parse_result;
      24             : 
      25             : static PgBenchExprList *make_elist(PgBenchExpr *exp, PgBenchExprList *list);
      26             : static PgBenchExpr *make_null_constant(void);
      27             : static PgBenchExpr *make_boolean_constant(bool bval);
      28             : static PgBenchExpr *make_integer_constant(int64 ival);
      29             : static PgBenchExpr *make_double_constant(double dval);
      30             : static PgBenchExpr *make_variable(char *varname);
      31             : static PgBenchExpr *make_op(yyscan_t yyscanner, const char *operator,
      32             :         PgBenchExpr *lexpr, PgBenchExpr *rexpr);
      33             : static PgBenchExpr *make_uop(yyscan_t yyscanner, const char *operator, PgBenchExpr *expr);
      34             : static int  find_func(yyscan_t yyscanner, const char *fname);
      35             : static PgBenchExpr *make_func(yyscan_t yyscanner, int fnumber, PgBenchExprList *args);
      36             : static PgBenchExpr *make_case(yyscan_t yyscanner, PgBenchExprList *when_then_list, PgBenchExpr *else_part);
      37             : 
      38             : %}
      39             : 
      40             : %pure-parser
      41             : %expect 0
      42             : %name-prefix="expr_yy"
      43             : 
      44             : %parse-param {yyscan_t yyscanner}
      45             : %lex-param   {yyscan_t yyscanner}
      46             : 
      47             : %union
      48             : {
      49             :     int64       ival;
      50             :     double      dval;
      51             :     bool        bval;
      52             :     char       *str;
      53             :     PgBenchExpr *expr;
      54             :     PgBenchExprList *elist;
      55             : }
      56             : 
      57             : %type <elist> elist when_then_list
      58             : %type <expr> expr case_control
      59             : %type <ival> INTEGER_CONST function
      60             : %type <dval> DOUBLE_CONST
      61             : %type <bval> BOOLEAN_CONST
      62             : %type <str> VARIABLE FUNCTION
      63             : 
      64             : %token NULL_CONST INTEGER_CONST MAXINT_PLUS_ONE_CONST DOUBLE_CONST
      65             : %token BOOLEAN_CONST VARIABLE FUNCTION
      66             : %token AND_OP OR_OP NOT_OP NE_OP LE_OP GE_OP LS_OP RS_OP IS_OP
      67             : %token CASE_KW WHEN_KW THEN_KW ELSE_KW END_KW
      68             : 
      69             : /* Precedence: lowest to highest, taken from postgres SQL parser */
      70             : %left   OR_OP
      71             : %left   AND_OP
      72             : %right  NOT_OP
      73             : %nonassoc IS_OP ISNULL_OP NOTNULL_OP
      74             : %nonassoc '<' '>' '=' LE_OP GE_OP NE_OP
      75             : %left   '|' '#' '&' LS_OP RS_OP '~'
      76             : %left   '+' '-'
      77             : %left   '*' '/' '%'
      78             : %right  UNARY
      79             : 
      80             : %%
      81             : 
      82         680 : result: expr                { expr_parse_result = $1; }
      83             : 
      84          10 : elist:                      { $$ = NULL; }
      85         650 :     | expr                  { $$ = make_elist($1, NULL); }
      86         552 :     | elist ',' expr        { $$ = make_elist($3, $1); }
      87             :     ;
      88             : 
      89         104 : expr: '(' expr ')'          { $$ = $2; }
      90           4 :     | '+' expr %prec UNARY  { $$ = $2; }
      91             :     /* unary minus "-x" implemented as "0 - x" */
      92          88 :     | '-' expr %prec UNARY  { $$ = make_op(yyscanner, "-",
      93             :                                            make_integer_constant(0), $2); }
      94             :     /* special PG_INT64_MIN handling, only after a unary minus */
      95             :     | '-' MAXINT_PLUS_ONE_CONST %prec UNARY
      96           2 :                             { $$ = make_integer_constant(PG_INT64_MIN); }
      97             :     /* binary ones complement "~x" implemented as 0xffff... xor x" */
      98           4 :     | '~' expr              { $$ = make_op(yyscanner, "#",
      99             :                                            make_integer_constant(~INT64CONST(0)), $2); }
     100          24 :     | NOT_OP expr           { $$ = make_uop(yyscanner, "!not", $2); }
     101          42 :     | expr '+' expr         { $$ = make_op(yyscanner, "+", $1, $3); }
     102          18 :     | expr '-' expr         { $$ = make_op(yyscanner, "-", $1, $3); }
     103         402 :     | expr '*' expr         { $$ = make_op(yyscanner, "*", $1, $3); }
     104          24 :     | expr '/' expr         { $$ = make_op(yyscanner, "/", $1, $3); }
     105           4 :     | expr '%' expr         { $$ = make_op(yyscanner, "mod", $1, $3); }
     106          18 :     | expr '<' expr          { $$ = make_op(yyscanner, "<", $1, $3); }
     107           6 :     | expr LE_OP expr       { $$ = make_op(yyscanner, "<=", $1, $3); }
     108          12 :     | expr '>' expr          { $$ = make_op(yyscanner, "<", $3, $1); }
     109           6 :     | expr GE_OP expr       { $$ = make_op(yyscanner, "<=", $3, $1); }
     110          26 :     | expr '=' expr         { $$ = make_op(yyscanner, "=", $1, $3); }
     111          14 :     | expr NE_OP expr       { $$ = make_op(yyscanner, "<>", $1, $3); }
     112           2 :     | expr '&' expr         { $$ = make_op(yyscanner, "&", $1, $3); }
     113           4 :     | expr '|' expr         { $$ = make_op(yyscanner, "|", $1, $3); }
     114           2 :     | expr '#' expr         { $$ = make_op(yyscanner, "#", $1, $3); }
     115          14 :     | expr LS_OP expr       { $$ = make_op(yyscanner, "<<", $1, $3); }
     116           2 :     | expr RS_OP expr       { $$ = make_op(yyscanner, ">>", $1, $3); }
     117          64 :     | expr AND_OP expr      { $$ = make_op(yyscanner, "!and", $1, $3); }
     118          10 :     | expr OR_OP expr       { $$ = make_op(yyscanner, "!or", $1, $3); }
     119             :     /* IS variants */
     120           2 :     | expr ISNULL_OP        { $$ = make_op(yyscanner, "!is", $1, make_null_constant()); }
     121             :     | expr NOTNULL_OP       {
     122           4 :                                 $$ = make_uop(yyscanner, "!not",
     123           2 :                                               make_op(yyscanner, "!is", $1, make_null_constant()));
     124             :                             }
     125           8 :     | expr IS_OP NULL_CONST { $$ = make_op(yyscanner, "!is", $1, make_null_constant()); }
     126             :     | expr IS_OP NOT_OP NULL_CONST
     127             :                             {
     128           8 :                                 $$ = make_uop(yyscanner, "!not",
     129           4 :                                               make_op(yyscanner, "!is", $1, make_null_constant()));
     130             :                             }
     131             :     | expr IS_OP BOOLEAN_CONST
     132             :                             {
     133           2 :                                 $$ = make_op(yyscanner, "!is", $1, make_boolean_constant($3));
     134             :                             }
     135             :     | expr IS_OP NOT_OP BOOLEAN_CONST
     136             :                             {
     137           4 :                                 $$ = make_uop(yyscanner, "!not",
     138           4 :                                               make_op(yyscanner, "!is", $1, make_boolean_constant($4)));
     139             :                             }
     140             :     /* constants */
     141          12 :     | NULL_CONST            { $$ = make_null_constant(); }
     142          40 :     | BOOLEAN_CONST         { $$ = make_boolean_constant($1); }
     143        1324 :     | INTEGER_CONST         { $$ = make_integer_constant($1); }
     144         122 :     | DOUBLE_CONST          { $$ = make_double_constant($1); }
     145             :     /* misc */
     146         462 :     | VARIABLE              { $$ = make_variable($1); }
     147         660 :     | function '(' elist ')' { $$ = make_func(yyscanner, $1, $3); }
     148          28 :     | case_control          { $$ = $1; }
     149             :     ;
     150             : 
     151             : when_then_list:
     152           4 :       when_then_list WHEN_KW expr THEN_KW expr { $$ = make_elist($5, make_elist($3, $1)); }
     153          28 :     | WHEN_KW expr THEN_KW expr { $$ = make_elist($4, make_elist($2, NULL)); }
     154             : 
     155             : case_control:
     156           8 :       CASE_KW when_then_list END_KW { $$ = make_case(yyscanner, $2, make_null_constant()); }
     157          20 :     | CASE_KW when_then_list ELSE_KW expr END_KW { $$ = make_case(yyscanner, $2, $4); }
     158             : 
     159         662 : function: FUNCTION          { $$ = find_func(yyscanner, $1); pg_free($1); }
     160             :     ;
     161             : 
     162             : %%
     163             : 
     164             : static PgBenchExpr *
     165          36 : make_null_constant(void)
     166             : {
     167          36 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     168             : 
     169          36 :     expr->etype = ENODE_CONSTANT;
     170          36 :     expr->u.constant.type = PGBT_NULL;
     171          36 :     expr->u.constant.u.ival = 0;
     172          36 :     return expr;
     173             : }
     174             : 
     175             : static PgBenchExpr *
     176        1418 : make_integer_constant(int64 ival)
     177             : {
     178        1418 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     179             : 
     180        1418 :     expr->etype = ENODE_CONSTANT;
     181        1418 :     expr->u.constant.type = PGBT_INT;
     182        1418 :     expr->u.constant.u.ival = ival;
     183        1418 :     return expr;
     184             : }
     185             : 
     186             : static PgBenchExpr *
     187         122 : make_double_constant(double dval)
     188             : {
     189         122 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     190             : 
     191         122 :     expr->etype = ENODE_CONSTANT;
     192         122 :     expr->u.constant.type = PGBT_DOUBLE;
     193         122 :     expr->u.constant.u.dval = dval;
     194         122 :     return expr;
     195             : }
     196             : 
     197             : static PgBenchExpr *
     198          44 : make_boolean_constant(bool bval)
     199             : {
     200          44 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     201             : 
     202          44 :     expr->etype = ENODE_CONSTANT;
     203          44 :     expr->u.constant.type = PGBT_BOOLEAN;
     204          44 :     expr->u.constant.u.bval = bval;
     205          44 :     return expr;
     206             : }
     207             : 
     208             : static PgBenchExpr *
     209         466 : make_variable(char *varname)
     210             : {
     211         466 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     212             : 
     213         466 :     expr->etype = ENODE_VARIABLE;
     214         466 :     expr->u.variable.varname = varname;
     215         466 :     return expr;
     216             : }
     217             : 
     218             : /* binary operators */
     219             : static PgBenchExpr *
     220         782 : make_op(yyscan_t yyscanner, const char *operator,
     221             :         PgBenchExpr *lexpr, PgBenchExpr *rexpr)
     222             : {
     223         782 :     return make_func(yyscanner, find_func(yyscanner, operator),
     224             :                      make_elist(rexpr, make_elist(lexpr, NULL)));
     225             : }
     226             : 
     227             : /* unary operator */
     228             : static PgBenchExpr *
     229          32 : make_uop(yyscan_t yyscanner, const char *operator, PgBenchExpr *expr)
     230             : {
     231          32 :     return make_func(yyscanner, find_func(yyscanner, operator), make_elist(expr, NULL));
     232             : }
     233             : 
     234             : /*
     235             :  * List of available functions:
     236             :  * - fname: function name, "!..." for special internal functions
     237             :  * - nargs: number of arguments. Special cases:
     238             :  *          - PGBENCH_NARGS_VARIABLE is a special value for least & greatest
     239             :  *            meaning #args >= 1;
     240             :  *          - PGBENCH_NARGS_CASE is for the "CASE WHEN ..." function, which
     241             :  *            has #args >= 3 and odd;
     242             :  *          - PGBENCH_NARGS_HASH is for hash functions, which have one required
     243             :  *            and one optional argument;
     244             :  * - tag: function identifier from PgBenchFunction enum
     245             :  */
     246             : static const struct
     247             : {
     248             :     const char *fname;
     249             :     int         nargs;
     250             :     PgBenchFunction tag;
     251             : }   PGBENCH_FUNCTIONS[] =
     252             : {
     253             :     /* parsed as operators, executed as functions */
     254             :     {
     255             :         "+", 2, PGBENCH_ADD
     256             :     },
     257             :     {
     258             :         "-", 2, PGBENCH_SUB
     259             :     },
     260             :     {
     261             :         "*", 2, PGBENCH_MUL
     262             :     },
     263             :     {
     264             :         "/", 2, PGBENCH_DIV
     265             :     },
     266             :     {
     267             :         "mod", 2, PGBENCH_MOD
     268             :     },
     269             :     /* actual functions */
     270             :     {
     271             :         "abs", 1, PGBENCH_ABS
     272             :     },
     273             :     {
     274             :         "least", PGBENCH_NARGS_VARIABLE, PGBENCH_LEAST
     275             :     },
     276             :     {
     277             :         "greatest", PGBENCH_NARGS_VARIABLE, PGBENCH_GREATEST
     278             :     },
     279             :     {
     280             :         "debug", 1, PGBENCH_DEBUG
     281             :     },
     282             :     {
     283             :         "pi", 0, PGBENCH_PI
     284             :     },
     285             :     {
     286             :         "sqrt", 1, PGBENCH_SQRT
     287             :     },
     288             :     {
     289             :         "ln", 1, PGBENCH_LN
     290             :     },
     291             :     {
     292             :         "exp", 1, PGBENCH_EXP
     293             :     },
     294             :     {
     295             :         "int", 1, PGBENCH_INT
     296             :     },
     297             :     {
     298             :         "double", 1, PGBENCH_DOUBLE
     299             :     },
     300             :     {
     301             :         "random", 2, PGBENCH_RANDOM
     302             :     },
     303             :     {
     304             :         "random_gaussian", 3, PGBENCH_RANDOM_GAUSSIAN
     305             :     },
     306             :     {
     307             :         "random_exponential", 3, PGBENCH_RANDOM_EXPONENTIAL
     308             :     },
     309             :     {
     310             :         "random_zipfian", 3, PGBENCH_RANDOM_ZIPFIAN
     311             :     },
     312             :     {
     313             :         "pow", 2, PGBENCH_POW
     314             :     },
     315             :     {
     316             :         "power", 2, PGBENCH_POW
     317             :     },
     318             :     /* logical operators */
     319             :     {
     320             :         "!and", 2, PGBENCH_AND
     321             :     },
     322             :     {
     323             :         "!or", 2, PGBENCH_OR
     324             :     },
     325             :     {
     326             :         "!not", 1, PGBENCH_NOT
     327             :     },
     328             :     /* bitwise integer operators */
     329             :     {
     330             :         "&", 2, PGBENCH_BITAND
     331             :     },
     332             :     {
     333             :         "|", 2, PGBENCH_BITOR
     334             :     },
     335             :     {
     336             :         "#", 2, PGBENCH_BITXOR
     337             :     },
     338             :     {
     339             :         "<<", 2, PGBENCH_LSHIFT
     340             :     },
     341             :     {
     342             :         ">>", 2, PGBENCH_RSHIFT
     343             :     },
     344             :     /* comparison operators */
     345             :     {
     346             :         "=", 2, PGBENCH_EQ
     347             :     },
     348             :     {
     349             :         "<>", 2, PGBENCH_NE
     350             :     },
     351             :     {
     352             :         "<=", 2, PGBENCH_LE
     353             :     },
     354             :     {
     355             :         "<", 2, PGBENCH_LT
     356             :     },
     357             :     {
     358             :         "!is", 2, PGBENCH_IS
     359             :     },
     360             :     /* "case when ... then ... else ... end" construction */
     361             :     {
     362             :         "!case_end", PGBENCH_NARGS_CASE, PGBENCH_CASE
     363             :     },
     364             :     {
     365             :         "hash", PGBENCH_NARGS_HASH, PGBENCH_HASH_MURMUR2
     366             :     },
     367             :     {
     368             :         "hash_murmur2", PGBENCH_NARGS_HASH, PGBENCH_HASH_MURMUR2
     369             :     },
     370             :     {
     371             :         "hash_fnv1a", PGBENCH_NARGS_HASH, PGBENCH_HASH_FNV1A
     372             :     },
     373             :     /* keep as last array element */
     374             :     {
     375             :         NULL, 0, 0
     376             :     }
     377             : };
     378             : 
     379             : /*
     380             :  * Find a function from its name
     381             :  *
     382             :  * return the index of the function from the PGBENCH_FUNCTIONS array
     383             :  * or fail if the function is unknown.
     384             :  */
     385             : static int
     386        1504 : find_func(yyscan_t yyscanner, const char *fname)
     387             : {
     388        1504 :     int         i = 0;
     389             : 
     390       20296 :     while (PGBENCH_FUNCTIONS[i].fname)
     391             :     {
     392       18790 :         if (pg_strcasecmp(fname, PGBENCH_FUNCTIONS[i].fname) == 0)
     393        1502 :             return i;
     394       17288 :         i++;
     395             :     }
     396             : 
     397           2 :     expr_yyerror_more(yyscanner, "unexpected function name", fname);
     398             : 
     399             :     /* not reached */
     400             :     return -1;
     401             : }
     402             : 
     403             : /* Expression linked list builder */
     404             : static PgBenchExprList *
     405        2894 : make_elist(PgBenchExpr *expr, PgBenchExprList *list)
     406             : {
     407             :     PgBenchExprLink *cons;
     408             : 
     409        2894 :     if (list == NULL)
     410             :     {
     411        1492 :         list = pg_malloc(sizeof(PgBenchExprList));
     412        1492 :         list->head = NULL;
     413        1492 :         list->tail = NULL;
     414             :     }
     415             : 
     416        2894 :     cons = pg_malloc(sizeof(PgBenchExprLink));
     417        2894 :     cons->expr = expr;
     418        2894 :     cons->next = NULL;
     419             : 
     420        2894 :     if (list->head == NULL)
     421        1492 :         list->head = cons;
     422             :     else
     423        1402 :         list->tail->next = cons;
     424             : 
     425        2894 :     list->tail = cons;
     426             : 
     427        2894 :     return list;
     428             : }
     429             : 
     430             : /* Return the length of an expression list */
     431             : static int
     432        1502 : elist_length(PgBenchExprList *list)
     433             : {
     434        1502 :     PgBenchExprLink *link = list != NULL ? list->head : NULL;
     435        1502 :     int         len = 0;
     436             : 
     437        4392 :     for (; link != NULL; link = link->next)
     438        2890 :         len++;
     439             : 
     440        1502 :     return len;
     441             : }
     442             : 
     443             : /* Build function call expression */
     444             : static PgBenchExpr *
     445        1502 : make_func(yyscan_t yyscanner, int fnumber, PgBenchExprList *args)
     446             : {
     447        1502 :     int len = elist_length(args);
     448             : 
     449        1502 :     PgBenchExpr *expr = pg_malloc(sizeof(PgBenchExpr));
     450             : 
     451             :     Assert(fnumber >= 0);
     452             : 
     453             :     /* validate arguments number including few special cases */
     454        1502 :     switch (PGBENCH_FUNCTIONS[fnumber].nargs)
     455             :     {
     456             :         /* check at least one arg for least & greatest */
     457             :         case PGBENCH_NARGS_VARIABLE:
     458          16 :             if (len == 0)
     459           6 :                 expr_yyerror_more(yyscanner, "at least one argument expected",
     460             :                                   PGBENCH_FUNCTIONS[fnumber].fname);
     461          10 :             break;
     462             : 
     463             :         /* case (when ... then ...)+ (else ...)? end */
     464             :         case PGBENCH_NARGS_CASE:
     465             :             /* 'else' branch is always present, but could be a NULL-constant */
     466          28 :             if (len < 3 || len % 2 != 1)
     467           0 :                 expr_yyerror_more(yyscanner,
     468             :                                   "odd and >= 3 number of arguments expected",
     469             :                                   "case control structure");
     470          28 :             break;
     471             : 
     472             :         /* hash functions with optional seed argument */
     473             :         case PGBENCH_NARGS_HASH:
     474          16 :             if (len < 1 || len > 2)
     475           4 :                 expr_yyerror_more(yyscanner, "unexpected number of arguments",
     476             :                                   PGBENCH_FUNCTIONS[fnumber].fname);
     477             : 
     478          12 :             if (len == 1)
     479             :             {
     480           4 :                 PgBenchExpr *var = make_variable("default_seed");
     481           4 :                 args = make_elist(var, args);
     482             :             }
     483          12 :             break;
     484             : 
     485             :         /* common case: positive arguments number */
     486             :         default:
     487             :             Assert(PGBENCH_FUNCTIONS[fnumber].nargs >= 0);
     488             : 
     489        1442 :             if (PGBENCH_FUNCTIONS[fnumber].nargs != len)
     490           2 :                 expr_yyerror_more(yyscanner, "unexpected number of arguments",
     491             :                                   PGBENCH_FUNCTIONS[fnumber].fname);
     492             :     }
     493             : 
     494        1490 :     expr->etype = ENODE_FUNCTION;
     495        1490 :     expr->u.function.function = PGBENCH_FUNCTIONS[fnumber].tag;
     496             : 
     497             :     /* only the link is used, the head/tail is not useful anymore */
     498        1490 :     expr->u.function.args = args != NULL ? args->head : NULL;
     499        1490 :     if (args)
     500        1488 :         pg_free(args);
     501             : 
     502        1490 :     return expr;
     503             : }
     504             : 
     505             : static PgBenchExpr *
     506          28 : make_case(yyscan_t yyscanner, PgBenchExprList *when_then_list, PgBenchExpr *else_part)
     507             : {
     508          28 :     return make_func(yyscanner,
     509             :                      find_func(yyscanner, "!case_end"),
     510             :                      make_elist(else_part, when_then_list));
     511             : }
     512             : 
     513             : /*
     514             :  * exprscan.l is compiled as part of exprparse.y.  Currently, this is
     515             :  * unavoidable because exprparse does not create a .h file to export
     516             :  * its token symbols.  If these files ever grow large enough to be
     517             :  * worth compiling separately, that could be fixed; but for now it
     518             :  * seems like useless complication.
     519             :  */
     520             : 
     521             : /* First, get rid of "#define yyscan_t" from pgbench.h */
     522             : #undef yyscan_t
     523             : /* ... and the yylval macro, which flex will have its own definition for */
     524             : #undef yylval
     525             : 
     526             : #include "exprscan.c"

Generated by: LCOV version 1.13