LCOV - differential code coverage report
Current view: top level - src/test/modules/test_predtest - test_predtest.c (source / functions) Coverage Total Hit UNC UBC GNC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 85.7 % 91 78 2 11 2 76
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 3 3 1 2
Baseline: 16@8cea358b128 Branches: 56.2 % 96 54 6 36 6 48
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 50.0 % 4 2 2 2
(240..) days: 87.4 % 87 76 11 76
Function coverage date bins:
(240..) days: 100.0 % 3 3 1 2
Branch coverage date bins:
[..60] days: 50.0 % 12 6 6 6
(240..) days: 57.1 % 84 48 36 48

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*--------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * test_predtest.c
                                  4                 :                :  *      Test correctness of optimizer's predicate proof logic.
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2018-2024, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *      src/test/modules/test_predtest/test_predtest.c
                                 10                 :                :  *
                                 11                 :                :  * -------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : 
                                 14                 :                : #include "postgres.h"
                                 15                 :                : 
                                 16                 :                : #include "access/htup_details.h"
                                 17                 :                : #include "catalog/pg_type.h"
                                 18                 :                : #include "executor/spi.h"
                                 19                 :                : #include "funcapi.h"
                                 20                 :                : #include "nodes/makefuncs.h"
                                 21                 :                : #include "optimizer/optimizer.h"
                                 22                 :                : #include "utils/builtins.h"
                                 23                 :                : 
 2229 tgl@sss.pgh.pa.us          24                 :CBC           1 : PG_MODULE_MAGIC;
                                 25                 :                : 
                                 26                 :                : /*
                                 27                 :                :  * test_predtest(query text) returns record
                                 28                 :                :  */
                                 29                 :              2 : PG_FUNCTION_INFO_V1(test_predtest);
                                 30                 :                : 
                                 31                 :                : Datum
                                 32                 :             74 : test_predtest(PG_FUNCTION_ARGS)
                                 33                 :                : {
                                 34                 :             74 :     text       *txt = PG_GETARG_TEXT_PP(0);
                                 35                 :             74 :     char       *query_string = text_to_cstring(txt);
                                 36                 :                :     SPIPlanPtr  spiplan;
                                 37                 :                :     int         spirc;
                                 38                 :                :     TupleDesc   tupdesc;
                                 39                 :                :     bool        s_i_holds,
                                 40                 :                :                 w_i_holds,
                                 41                 :                :                 s_r_holds,
                                 42                 :                :                 w_r_holds;
                                 43                 :                :     CachedPlan *cplan;
                                 44                 :                :     PlannedStmt *stmt;
                                 45                 :                :     Plan       *plan;
                                 46                 :                :     Expr       *clause1;
                                 47                 :                :     Expr       *clause2;
                                 48                 :                :     bool        strong_implied_by,
                                 49                 :                :                 weak_implied_by,
                                 50                 :                :                 strong_refuted_by,
                                 51                 :                :                 weak_refuted_by;
                                 52                 :                :     Datum       values[8];
  638 peter@eisentraut.org       53                 :             74 :     bool        nulls[8] = {0};
                                 54                 :                :     int         i;
                                 55                 :                : 
                                 56                 :                :     /* We use SPI to parse, plan, and execute the test query */
 2229 tgl@sss.pgh.pa.us          57         [ -  + ]:             74 :     if (SPI_connect() != SPI_OK_CONNECT)
 2229 tgl@sss.pgh.pa.us          58         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_connect failed");
                                 59                 :                : 
                                 60                 :                :     /*
                                 61                 :                :      * First, plan and execute the query, and inspect the results.  To the
                                 62                 :                :      * extent that the query fully exercises the two expressions, this
                                 63                 :                :      * provides an experimental indication of whether implication or
                                 64                 :                :      * refutation holds.
                                 65                 :                :      */
 2229 tgl@sss.pgh.pa.us          66                 :CBC          74 :     spiplan = SPI_prepare(query_string, 0, NULL);
                                 67         [ -  + ]:             74 :     if (spiplan == NULL)
 2229 tgl@sss.pgh.pa.us          68         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_prepare failed for \"%s\"", query_string);
                                 69                 :                : 
 2229 tgl@sss.pgh.pa.us          70                 :CBC          74 :     spirc = SPI_execute_plan(spiplan, NULL, NULL, true, 0);
                                 71         [ -  + ]:             74 :     if (spirc != SPI_OK_SELECT)
 2229 tgl@sss.pgh.pa.us          72         [ #  # ]:UBC           0 :         elog(ERROR, "failed to execute \"%s\"", query_string);
 2229 tgl@sss.pgh.pa.us          73                 :CBC          74 :     tupdesc = SPI_tuptable->tupdesc;
                                 74         [ +  - ]:             74 :     if (tupdesc->natts != 2 ||
                                 75         [ +  - ]:             74 :         TupleDescAttr(tupdesc, 0)->atttypid != BOOLOID ||
                                 76         [ -  + ]:             74 :         TupleDescAttr(tupdesc, 1)->atttypid != BOOLOID)
 2229 tgl@sss.pgh.pa.us          77         [ #  # ]:UBC           0 :         elog(ERROR, "query must yield two boolean columns");
                                 78                 :                : 
 2229 tgl@sss.pgh.pa.us          79                 :CBC          74 :     s_i_holds = w_i_holds = s_r_holds = w_r_holds = true;
                                 80         [ +  + ]:           7550 :     for (i = 0; i < SPI_processed; i++)
                                 81                 :                :     {
                                 82                 :           7476 :         HeapTuple   tup = SPI_tuptable->vals[i];
                                 83                 :                :         Datum       dat;
                                 84                 :                :         bool        isnull;
                                 85                 :                :         char        c1,
                                 86                 :                :                     c2;
                                 87                 :                : 
                                 88                 :                :         /* Extract column values in a 3-way representation */
                                 89                 :           7476 :         dat = SPI_getbinval(tup, tupdesc, 1, &isnull);
                                 90         [ +  + ]:           7476 :         if (isnull)
                                 91                 :           1154 :             c1 = 'n';
                                 92         [ +  + ]:           6322 :         else if (DatumGetBool(dat))
                                 93                 :           3126 :             c1 = 't';
                                 94                 :                :         else
                                 95                 :           3196 :             c1 = 'f';
                                 96                 :                : 
                                 97                 :           7476 :         dat = SPI_getbinval(tup, tupdesc, 2, &isnull);
                                 98         [ +  + ]:           7476 :         if (isnull)
                                 99                 :           1192 :             c2 = 'n';
                                100         [ +  + ]:           6284 :         else if (DatumGetBool(dat))
                                101                 :           2620 :             c2 = 't';
                                102                 :                :         else
                                103                 :           3664 :             c2 = 'f';
                                104                 :                : 
                                105                 :                :         /* Check for violations of various proof conditions */
                                106                 :                : 
                                107                 :                :         /* strong implication: truth of c2 implies truth of c1 */
                                108   [ +  +  +  + ]:           7476 :         if (c2 == 't' && c1 != 't')
                                109                 :           1366 :             s_i_holds = false;
                                110                 :                :         /* weak implication: non-falsity of c2 implies non-falsity of c1 */
                                111   [ +  +  +  + ]:           7476 :         if (c2 != 'f' && c1 == 'f')
                                112                 :           1420 :             w_i_holds = false;
                                113                 :                :         /* strong refutation: truth of c2 implies falsity of c1 */
                                114   [ +  +  +  + ]:           7476 :         if (c2 == 't' && c1 != 'f')
                                115                 :           1452 :             s_r_holds = false;
                                116                 :                :         /* weak refutation: truth of c2 implies non-truth of c1 */
                                117   [ +  +  +  + ]:           7476 :         if (c2 == 't' && c1 == 't')
                                118                 :           1254 :             w_r_holds = false;
                                119                 :                :     }
                                120                 :                : 
                                121                 :                :     /*
                                122                 :                :      * Strong refutation implies weak refutation, so we should never observe
                                123                 :                :      * s_r_holds = true with w_r_holds = false.
                                124                 :                :      *
                                125                 :                :      * We can't make a comparable assertion for implication since moving from
                                126                 :                :      * strong to weak implication expands the allowed values of "A" from true
                                127                 :                :      * to either true or NULL.
                                128                 :                :      *
                                129                 :                :      * If this fails it constitutes a bug not with the proofs but with either
                                130                 :                :      * this test module or a more core part of expression evaluation since we
                                131                 :                :      * are validating the logical correctness of the observed result rather
                                132                 :                :      * than the proof.
                                133                 :                :      */
   20 tgl@sss.pgh.pa.us         134   [ +  +  -  + ]:GNC          74 :     if (s_r_holds && !w_r_holds)
   20 tgl@sss.pgh.pa.us         135         [ #  # ]:UNC           0 :         elog(WARNING, "s_r_holds was true; w_r_holds must not be false");
                                136                 :                : 
                                137                 :                :     /*
                                138                 :                :      * Now, dig the clause querytrees out of the plan, and see what predtest.c
                                139                 :                :      * does with them.
                                140                 :                :      */
 2229 tgl@sss.pgh.pa.us         141                 :CBC          74 :     cplan = SPI_plan_get_cached_plan(spiplan);
                                142                 :                : 
                                143         [ -  + ]:             74 :     if (list_length(cplan->stmt_list) != 1)
 2229 tgl@sss.pgh.pa.us         144         [ #  # ]:UBC           0 :         elog(ERROR, "failed to decipher query plan");
 2229 tgl@sss.pgh.pa.us         145                 :CBC          74 :     stmt = linitial_node(PlannedStmt, cplan->stmt_list);
                                146         [ -  + ]:             74 :     if (stmt->commandType != CMD_SELECT)
 2229 tgl@sss.pgh.pa.us         147         [ #  # ]:UBC           0 :         elog(ERROR, "failed to decipher query plan");
 2229 tgl@sss.pgh.pa.us         148                 :CBC          74 :     plan = stmt->planTree;
                                149         [ -  + ]:             74 :     Assert(list_length(plan->targetlist) >= 2);
 1000 peter@eisentraut.org      150                 :             74 :     clause1 = linitial_node(TargetEntry, plan->targetlist)->expr;
                                151                 :             74 :     clause2 = lsecond_node(TargetEntry, plan->targetlist)->expr;
                                152                 :                : 
                                153                 :                :     /*
                                154                 :                :      * Because the clauses are in the SELECT list, preprocess_expression did
                                155                 :                :      * not pass them through canonicalize_qual nor make_ands_implicit.
                                156                 :                :      *
                                157                 :                :      * We can't do canonicalize_qual here, since it's unclear whether the
                                158                 :                :      * expressions ought to be treated as WHERE or CHECK clauses. Fortunately,
                                159                 :                :      * useful test expressions wouldn't be affected by those transformations
                                160                 :                :      * anyway.  We should do make_ands_implicit, though.
                                161                 :                :      *
                                162                 :                :      * Another way in which this does not exactly duplicate the normal usage
                                163                 :                :      * of the proof functions is that they are often given qual clauses
                                164                 :                :      * containing RestrictInfo nodes.  But since predtest.c just looks through
                                165                 :                :      * those anyway, it seems OK to not worry about that point.
                                166                 :                :      */
 2229 tgl@sss.pgh.pa.us         167                 :             74 :     clause1 = (Expr *) make_ands_implicit(clause1);
                                168                 :             74 :     clause2 = (Expr *) make_ands_implicit(clause2);
                                169                 :                : 
                                170                 :             74 :     strong_implied_by = predicate_implied_by((List *) clause1,
                                171                 :                :                                              (List *) clause2,
                                172                 :                :                                              false);
                                173                 :                : 
                                174                 :             74 :     weak_implied_by = predicate_implied_by((List *) clause1,
                                175                 :                :                                            (List *) clause2,
                                176                 :                :                                            true);
                                177                 :                : 
                                178                 :             74 :     strong_refuted_by = predicate_refuted_by((List *) clause1,
                                179                 :                :                                              (List *) clause2,
                                180                 :                :                                              false);
                                181                 :                : 
                                182                 :             74 :     weak_refuted_by = predicate_refuted_by((List *) clause1,
                                183                 :                :                                            (List *) clause2,
                                184                 :                :                                            true);
                                185                 :                : 
                                186                 :                :     /*
                                187                 :                :      * Issue warning if any proof is demonstrably incorrect.
                                188                 :                :      */
                                189   [ +  +  -  + ]:             74 :     if (strong_implied_by && !s_i_holds)
 2229 tgl@sss.pgh.pa.us         190         [ #  # ]:UBC           0 :         elog(WARNING, "strong_implied_by result is incorrect");
 2229 tgl@sss.pgh.pa.us         191   [ +  +  -  + ]:CBC          74 :     if (weak_implied_by && !w_i_holds)
 2229 tgl@sss.pgh.pa.us         192         [ #  # ]:UBC           0 :         elog(WARNING, "weak_implied_by result is incorrect");
 2229 tgl@sss.pgh.pa.us         193   [ +  +  -  + ]:CBC          74 :     if (strong_refuted_by && !s_r_holds)
 2229 tgl@sss.pgh.pa.us         194         [ #  # ]:UBC           0 :         elog(WARNING, "strong_refuted_by result is incorrect");
 2229 tgl@sss.pgh.pa.us         195   [ +  +  -  + ]:CBC          74 :     if (weak_refuted_by && !w_r_holds)
 2229 tgl@sss.pgh.pa.us         196         [ #  # ]:UBC           0 :         elog(WARNING, "weak_refuted_by result is incorrect");
                                197                 :                : 
                                198                 :                :     /*
                                199                 :                :      * As with our earlier check of the logical consistency of whether strong
                                200                 :                :      * and weak refutation hold, we ought never prove strong refutation
                                201                 :                :      * without also proving weak refutation.
                                202                 :                :      *
                                203                 :                :      * Also as earlier we cannot make the same guarantee about implication
                                204                 :                :      * proofs.
                                205                 :                :      *
                                206                 :                :      * A warning here suggests a bug in the proof code.
                                207                 :                :      */
   20 tgl@sss.pgh.pa.us         208   [ +  +  -  + ]:GNC          74 :     if (strong_refuted_by && !weak_refuted_by)
   20 tgl@sss.pgh.pa.us         209         [ #  # ]:UNC           0 :         elog(WARNING, "strong_refuted_by was proven; weak_refuted_by should also be proven");
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * Clean up and return a record of the results.
                                213                 :                :      */
 2229 tgl@sss.pgh.pa.us         214         [ -  + ]:CBC          74 :     if (SPI_finish() != SPI_OK_FINISH)
 2229 tgl@sss.pgh.pa.us         215         [ #  # ]:UBC           0 :         elog(ERROR, "SPI_finish failed");
                                216                 :                : 
 1972 andres@anarazel.de        217                 :CBC          74 :     tupdesc = CreateTemplateTupleDesc(8);
 2229 tgl@sss.pgh.pa.us         218                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
                                219                 :                :                        "strong_implied_by", BOOLOID, -1, 0);
                                220                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
                                221                 :                :                        "weak_implied_by", BOOLOID, -1, 0);
                                222                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
                                223                 :                :                        "strong_refuted_by", BOOLOID, -1, 0);
                                224                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4,
                                225                 :                :                        "weak_refuted_by", BOOLOID, -1, 0);
                                226                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 5,
                                227                 :                :                        "s_i_holds", BOOLOID, -1, 0);
                                228                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 6,
                                229                 :                :                        "w_i_holds", BOOLOID, -1, 0);
                                230                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 7,
                                231                 :                :                        "s_r_holds", BOOLOID, -1, 0);
                                232                 :             74 :     TupleDescInitEntry(tupdesc, (AttrNumber) 8,
                                233                 :                :                        "w_r_holds", BOOLOID, -1, 0);
                                234                 :             74 :     tupdesc = BlessTupleDesc(tupdesc);
                                235                 :                : 
                                236                 :             74 :     values[0] = BoolGetDatum(strong_implied_by);
                                237                 :             74 :     values[1] = BoolGetDatum(weak_implied_by);
                                238                 :             74 :     values[2] = BoolGetDatum(strong_refuted_by);
                                239                 :             74 :     values[3] = BoolGetDatum(weak_refuted_by);
                                240                 :             74 :     values[4] = BoolGetDatum(s_i_holds);
                                241                 :             74 :     values[5] = BoolGetDatum(w_i_holds);
                                242                 :             74 :     values[6] = BoolGetDatum(s_r_holds);
                                243                 :             74 :     values[7] = BoolGetDatum(w_r_holds);
                                244                 :                : 
                                245                 :             74 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
                                246                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622