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

Generated by: LCOV version 1.13