LCOV - code coverage report
Current view: top level - src/test/modules/test_predtest - test_predtest.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 77 89 86.5 %
Date: 2024-11-21 08:14:44 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          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             : 
      24           2 : PG_MODULE_MAGIC;
      25             : 
      26             : /*
      27             :  * test_predtest(query text) returns record
      28             :  */
      29           4 : PG_FUNCTION_INFO_V1(test_predtest);
      30             : 
      31             : Datum
      32         148 : test_predtest(PG_FUNCTION_ARGS)
      33             : {
      34         148 :     text       *txt = PG_GETARG_TEXT_PP(0);
      35         148 :     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];
      53         148 :     bool        nulls[8] = {0};
      54             :     int         i;
      55             : 
      56             :     /* We use SPI to parse, plan, and execute the test query */
      57         148 :     SPI_connect();
      58             : 
      59             :     /*
      60             :      * First, plan and execute the query, and inspect the results.  To the
      61             :      * extent that the query fully exercises the two expressions, this
      62             :      * provides an experimental indication of whether implication or
      63             :      * refutation holds.
      64             :      */
      65         148 :     spiplan = SPI_prepare(query_string, 0, NULL);
      66         148 :     if (spiplan == NULL)
      67           0 :         elog(ERROR, "SPI_prepare failed for \"%s\"", query_string);
      68             : 
      69         148 :     spirc = SPI_execute_plan(spiplan, NULL, NULL, true, 0);
      70         148 :     if (spirc != SPI_OK_SELECT)
      71           0 :         elog(ERROR, "failed to execute \"%s\"", query_string);
      72         148 :     tupdesc = SPI_tuptable->tupdesc;
      73         148 :     if (tupdesc->natts != 2 ||
      74         148 :         TupleDescAttr(tupdesc, 0)->atttypid != BOOLOID ||
      75         148 :         TupleDescAttr(tupdesc, 1)->atttypid != BOOLOID)
      76           0 :         elog(ERROR, "test_predtest query must yield two boolean columns");
      77             : 
      78         148 :     s_i_holds = w_i_holds = s_r_holds = w_r_holds = true;
      79       15100 :     for (i = 0; i < SPI_processed; i++)
      80             :     {
      81       14952 :         HeapTuple   tup = SPI_tuptable->vals[i];
      82             :         Datum       dat;
      83             :         bool        isnull;
      84             :         char        c1,
      85             :                     c2;
      86             : 
      87             :         /* Extract column values in a 3-way representation */
      88       14952 :         dat = SPI_getbinval(tup, tupdesc, 1, &isnull);
      89       14952 :         if (isnull)
      90        2308 :             c1 = 'n';
      91       12644 :         else if (DatumGetBool(dat))
      92        6252 :             c1 = 't';
      93             :         else
      94        6392 :             c1 = 'f';
      95             : 
      96       14952 :         dat = SPI_getbinval(tup, tupdesc, 2, &isnull);
      97       14952 :         if (isnull)
      98        2384 :             c2 = 'n';
      99       12568 :         else if (DatumGetBool(dat))
     100        5240 :             c2 = 't';
     101             :         else
     102        7328 :             c2 = 'f';
     103             : 
     104             :         /* Check for violations of various proof conditions */
     105             : 
     106             :         /* strong implication: truth of c2 implies truth of c1 */
     107       14952 :         if (c2 == 't' && c1 != 't')
     108        2732 :             s_i_holds = false;
     109             :         /* weak implication: non-falsity of c2 implies non-falsity of c1 */
     110       14952 :         if (c2 != 'f' && c1 == 'f')
     111        2840 :             w_i_holds = false;
     112             :         /* strong refutation: truth of c2 implies falsity of c1 */
     113       14952 :         if (c2 == 't' && c1 != 'f')
     114        2904 :             s_r_holds = false;
     115             :         /* weak refutation: truth of c2 implies non-truth of c1 */
     116       14952 :         if (c2 == 't' && c1 == 't')
     117        2508 :             w_r_holds = false;
     118             :     }
     119             : 
     120             :     /*
     121             :      * Strong refutation implies weak refutation, so we should never observe
     122             :      * s_r_holds = true with w_r_holds = false.
     123             :      *
     124             :      * We can't make a comparable assertion for implication since moving from
     125             :      * strong to weak implication expands the allowed values of "A" from true
     126             :      * to either true or NULL.
     127             :      *
     128             :      * If this fails it constitutes a bug not with the proofs but with either
     129             :      * this test module or a more core part of expression evaluation since we
     130             :      * are validating the logical correctness of the observed result rather
     131             :      * than the proof.
     132             :      */
     133         148 :     if (s_r_holds && !w_r_holds)
     134           0 :         elog(WARNING, "s_r_holds was true; w_r_holds must not be false");
     135             : 
     136             :     /*
     137             :      * Now, dig the clause querytrees out of the plan, and see what predtest.c
     138             :      * does with them.
     139             :      */
     140         148 :     cplan = SPI_plan_get_cached_plan(spiplan);
     141             : 
     142         148 :     if (cplan == NULL || list_length(cplan->stmt_list) != 1)
     143           0 :         elog(ERROR, "test_predtest query string must contain exactly one query");
     144         148 :     stmt = linitial_node(PlannedStmt, cplan->stmt_list);
     145         148 :     if (stmt->commandType != CMD_SELECT)
     146           0 :         elog(ERROR, "test_predtest query must be a SELECT");
     147         148 :     plan = stmt->planTree;
     148             :     Assert(list_length(plan->targetlist) >= 2);
     149         148 :     clause1 = linitial_node(TargetEntry, plan->targetlist)->expr;
     150         148 :     clause2 = lsecond_node(TargetEntry, plan->targetlist)->expr;
     151             : 
     152             :     /*
     153             :      * Because the clauses are in the SELECT list, preprocess_expression did
     154             :      * not pass them through canonicalize_qual nor make_ands_implicit.
     155             :      *
     156             :      * We can't do canonicalize_qual here, since it's unclear whether the
     157             :      * expressions ought to be treated as WHERE or CHECK clauses. Fortunately,
     158             :      * useful test expressions wouldn't be affected by those transformations
     159             :      * anyway.  We should do make_ands_implicit, though.
     160             :      *
     161             :      * Another way in which this does not exactly duplicate the normal usage
     162             :      * of the proof functions is that they are often given qual clauses
     163             :      * containing RestrictInfo nodes.  But since predtest.c just looks through
     164             :      * those anyway, it seems OK to not worry about that point.
     165             :      */
     166         148 :     clause1 = (Expr *) make_ands_implicit(clause1);
     167         148 :     clause2 = (Expr *) make_ands_implicit(clause2);
     168             : 
     169         148 :     strong_implied_by = predicate_implied_by((List *) clause1,
     170             :                                              (List *) clause2,
     171             :                                              false);
     172             : 
     173         148 :     weak_implied_by = predicate_implied_by((List *) clause1,
     174             :                                            (List *) clause2,
     175             :                                            true);
     176             : 
     177         148 :     strong_refuted_by = predicate_refuted_by((List *) clause1,
     178             :                                              (List *) clause2,
     179             :                                              false);
     180             : 
     181         148 :     weak_refuted_by = predicate_refuted_by((List *) clause1,
     182             :                                            (List *) clause2,
     183             :                                            true);
     184             : 
     185             :     /*
     186             :      * Issue warning if any proof is demonstrably incorrect.
     187             :      */
     188         148 :     if (strong_implied_by && !s_i_holds)
     189           0 :         elog(WARNING, "strong_implied_by result is incorrect");
     190         148 :     if (weak_implied_by && !w_i_holds)
     191           0 :         elog(WARNING, "weak_implied_by result is incorrect");
     192         148 :     if (strong_refuted_by && !s_r_holds)
     193           0 :         elog(WARNING, "strong_refuted_by result is incorrect");
     194         148 :     if (weak_refuted_by && !w_r_holds)
     195           0 :         elog(WARNING, "weak_refuted_by result is incorrect");
     196             : 
     197             :     /*
     198             :      * As with our earlier check of the logical consistency of whether strong
     199             :      * and weak refutation hold, we ought never prove strong refutation
     200             :      * without also proving weak refutation.
     201             :      *
     202             :      * Also as earlier we cannot make the same guarantee about implication
     203             :      * proofs.
     204             :      *
     205             :      * A warning here suggests a bug in the proof code.
     206             :      */
     207         148 :     if (strong_refuted_by && !weak_refuted_by)
     208           0 :         elog(WARNING, "strong_refuted_by was proven; weak_refuted_by should also be proven");
     209             : 
     210             :     /*
     211             :      * Clean up and return a record of the results.
     212             :      */
     213         148 :     if (SPI_finish() != SPI_OK_FINISH)
     214           0 :         elog(ERROR, "SPI_finish failed");
     215             : 
     216         148 :     tupdesc = CreateTemplateTupleDesc(8);
     217         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
     218             :                        "strong_implied_by", BOOLOID, -1, 0);
     219         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
     220             :                        "weak_implied_by", BOOLOID, -1, 0);
     221         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
     222             :                        "strong_refuted_by", BOOLOID, -1, 0);
     223         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4,
     224             :                        "weak_refuted_by", BOOLOID, -1, 0);
     225         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 5,
     226             :                        "s_i_holds", BOOLOID, -1, 0);
     227         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 6,
     228             :                        "w_i_holds", BOOLOID, -1, 0);
     229         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 7,
     230             :                        "s_r_holds", BOOLOID, -1, 0);
     231         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 8,
     232             :                        "w_r_holds", BOOLOID, -1, 0);
     233         148 :     tupdesc = BlessTupleDesc(tupdesc);
     234             : 
     235         148 :     values[0] = BoolGetDatum(strong_implied_by);
     236         148 :     values[1] = BoolGetDatum(weak_implied_by);
     237         148 :     values[2] = BoolGetDatum(strong_refuted_by);
     238         148 :     values[3] = BoolGetDatum(weak_refuted_by);
     239         148 :     values[4] = BoolGetDatum(s_i_holds);
     240         148 :     values[5] = BoolGetDatum(w_i_holds);
     241         148 :     values[6] = BoolGetDatum(s_r_holds);
     242         148 :     values[7] = BoolGetDatum(w_r_holds);
     243             : 
     244         148 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
     245             : }

Generated by: LCOV version 1.14