LCOV - code coverage report
Current view: top level - src/test/modules/test_predtest - test_predtest.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17beta1 Lines: 77 90 85.6 %
Date: 2024-06-18 00:11:41 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 :     if (SPI_connect() != SPI_OK_CONNECT)
      58           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             :      */
      66         148 :     spiplan = SPI_prepare(query_string, 0, NULL);
      67         148 :     if (spiplan == NULL)
      68           0 :         elog(ERROR, "SPI_prepare failed for \"%s\"", query_string);
      69             : 
      70         148 :     spirc = SPI_execute_plan(spiplan, NULL, NULL, true, 0);
      71         148 :     if (spirc != SPI_OK_SELECT)
      72           0 :         elog(ERROR, "failed to execute \"%s\"", query_string);
      73         148 :     tupdesc = SPI_tuptable->tupdesc;
      74         148 :     if (tupdesc->natts != 2 ||
      75         148 :         TupleDescAttr(tupdesc, 0)->atttypid != BOOLOID ||
      76         148 :         TupleDescAttr(tupdesc, 1)->atttypid != BOOLOID)
      77           0 :         elog(ERROR, "test_predtest query must yield two boolean columns");
      78             : 
      79         148 :     s_i_holds = w_i_holds = s_r_holds = w_r_holds = true;
      80       15100 :     for (i = 0; i < SPI_processed; i++)
      81             :     {
      82       14952 :         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       14952 :         dat = SPI_getbinval(tup, tupdesc, 1, &isnull);
      90       14952 :         if (isnull)
      91        2308 :             c1 = 'n';
      92       12644 :         else if (DatumGetBool(dat))
      93        6252 :             c1 = 't';
      94             :         else
      95        6392 :             c1 = 'f';
      96             : 
      97       14952 :         dat = SPI_getbinval(tup, tupdesc, 2, &isnull);
      98       14952 :         if (isnull)
      99        2384 :             c2 = 'n';
     100       12568 :         else if (DatumGetBool(dat))
     101        5240 :             c2 = 't';
     102             :         else
     103        7328 :             c2 = 'f';
     104             : 
     105             :         /* Check for violations of various proof conditions */
     106             : 
     107             :         /* strong implication: truth of c2 implies truth of c1 */
     108       14952 :         if (c2 == 't' && c1 != 't')
     109        2732 :             s_i_holds = false;
     110             :         /* weak implication: non-falsity of c2 implies non-falsity of c1 */
     111       14952 :         if (c2 != 'f' && c1 == 'f')
     112        2840 :             w_i_holds = false;
     113             :         /* strong refutation: truth of c2 implies falsity of c1 */
     114       14952 :         if (c2 == 't' && c1 != 'f')
     115        2904 :             s_r_holds = false;
     116             :         /* weak refutation: truth of c2 implies non-truth of c1 */
     117       14952 :         if (c2 == 't' && c1 == 't')
     118        2508 :             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             :      */
     134         148 :     if (s_r_holds && !w_r_holds)
     135           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             :      */
     141         148 :     cplan = SPI_plan_get_cached_plan(spiplan);
     142             : 
     143         148 :     if (cplan == NULL || list_length(cplan->stmt_list) != 1)
     144           0 :         elog(ERROR, "test_predtest query string must contain exactly one query");
     145         148 :     stmt = linitial_node(PlannedStmt, cplan->stmt_list);
     146         148 :     if (stmt->commandType != CMD_SELECT)
     147           0 :         elog(ERROR, "test_predtest query must be a SELECT");
     148         148 :     plan = stmt->planTree;
     149             :     Assert(list_length(plan->targetlist) >= 2);
     150         148 :     clause1 = linitial_node(TargetEntry, plan->targetlist)->expr;
     151         148 :     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             :      */
     167         148 :     clause1 = (Expr *) make_ands_implicit(clause1);
     168         148 :     clause2 = (Expr *) make_ands_implicit(clause2);
     169             : 
     170         148 :     strong_implied_by = predicate_implied_by((List *) clause1,
     171             :                                              (List *) clause2,
     172             :                                              false);
     173             : 
     174         148 :     weak_implied_by = predicate_implied_by((List *) clause1,
     175             :                                            (List *) clause2,
     176             :                                            true);
     177             : 
     178         148 :     strong_refuted_by = predicate_refuted_by((List *) clause1,
     179             :                                              (List *) clause2,
     180             :                                              false);
     181             : 
     182         148 :     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         148 :     if (strong_implied_by && !s_i_holds)
     190           0 :         elog(WARNING, "strong_implied_by result is incorrect");
     191         148 :     if (weak_implied_by && !w_i_holds)
     192           0 :         elog(WARNING, "weak_implied_by result is incorrect");
     193         148 :     if (strong_refuted_by && !s_r_holds)
     194           0 :         elog(WARNING, "strong_refuted_by result is incorrect");
     195         148 :     if (weak_refuted_by && !w_r_holds)
     196           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             :      */
     208         148 :     if (strong_refuted_by && !weak_refuted_by)
     209           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             :      */
     214         148 :     if (SPI_finish() != SPI_OK_FINISH)
     215           0 :         elog(ERROR, "SPI_finish failed");
     216             : 
     217         148 :     tupdesc = CreateTemplateTupleDesc(8);
     218         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 1,
     219             :                        "strong_implied_by", BOOLOID, -1, 0);
     220         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 2,
     221             :                        "weak_implied_by", BOOLOID, -1, 0);
     222         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 3,
     223             :                        "strong_refuted_by", BOOLOID, -1, 0);
     224         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 4,
     225             :                        "weak_refuted_by", BOOLOID, -1, 0);
     226         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 5,
     227             :                        "s_i_holds", BOOLOID, -1, 0);
     228         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 6,
     229             :                        "w_i_holds", BOOLOID, -1, 0);
     230         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 7,
     231             :                        "s_r_holds", BOOLOID, -1, 0);
     232         148 :     TupleDescInitEntry(tupdesc, (AttrNumber) 8,
     233             :                        "w_r_holds", BOOLOID, -1, 0);
     234         148 :     tupdesc = BlessTupleDesc(tupdesc);
     235             : 
     236         148 :     values[0] = BoolGetDatum(strong_implied_by);
     237         148 :     values[1] = BoolGetDatum(weak_implied_by);
     238         148 :     values[2] = BoolGetDatum(strong_refuted_by);
     239         148 :     values[3] = BoolGetDatum(weak_refuted_by);
     240         148 :     values[4] = BoolGetDatum(s_i_holds);
     241         148 :     values[5] = BoolGetDatum(w_i_holds);
     242         148 :     values[6] = BoolGetDatum(s_r_holds);
     243         148 :     values[7] = BoolGetDatum(w_r_holds);
     244             : 
     245         148 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
     246             : }

Generated by: LCOV version 1.14