1 /*--------------------------------------------------------------------------
4 * Test correctness of optimizer's predicate proof logic.
6 * Copyright (c) 2018-2019, PostgreSQL Global Development Group
9 * src/test/modules/test_predtest/test_predtest.c
11 * -------------------------------------------------------------------------
16 #include "access/htup_details.h"
17 #include "catalog/pg_type.h"
18 #include "executor/spi.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/predtest.h"
22 #include "utils/builtins.h"
27 * test_predtest(query text) returns record
29 PG_FUNCTION_INFO_V1(test_predtest);
32 test_predtest(PG_FUNCTION_ARGS)
34 text *txt = PG_GETARG_TEXT_PP(0);
35 char *query_string = text_to_cstring(txt);
48 bool strong_implied_by,
56 /* We use SPI to parse, plan, and execute the test query */
57 if (SPI_connect() != SPI_OK_CONNECT)
58 elog(ERROR, "SPI_connect failed");
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
66 spiplan = SPI_prepare(query_string, 0, NULL);
68 elog(ERROR, "SPI_prepare failed for \"%s\"", query_string);
70 spirc = SPI_execute_plan(spiplan, NULL, NULL, true, 0);
71 if (spirc != SPI_OK_SELECT)
72 elog(ERROR, "failed to execute \"%s\"", query_string);
73 tupdesc = SPI_tuptable->tupdesc;
74 if (tupdesc->natts != 2 ||
75 TupleDescAttr(tupdesc, 0)->atttypid != BOOLOID ||
76 TupleDescAttr(tupdesc, 1)->atttypid != BOOLOID)
77 elog(ERROR, "query must yield two boolean columns");
79 s_i_holds = w_i_holds = s_r_holds = w_r_holds = true;
80 for (i = 0; i < SPI_processed; i++)
82 HeapTuple tup = SPI_tuptable->vals[i];
88 /* Extract column values in a 3-way representation */
89 dat = SPI_getbinval(tup, tupdesc, 1, &isnull);
92 else if (DatumGetBool(dat))
97 dat = SPI_getbinval(tup, tupdesc, 2, &isnull);
100 else if (DatumGetBool(dat))
105 /* Check for violations of various proof conditions */
107 /* strong implication: truth of c2 implies truth of c1 */
108 if (c2 == 't' && c1 != 't')
110 /* weak implication: non-falsity of c2 implies non-falsity of c1 */
111 if (c2 != 'f' && c1 == 'f')
113 /* strong refutation: truth of c2 implies falsity of c1 */
114 if (c2 == 't' && c1 != 'f')
116 /* weak refutation: truth of c2 implies non-truth of c1 */
117 if (c2 == 't' && c1 == 't')
122 * Now, dig the clause querytrees out of the plan, and see what predtest.c
125 cplan = SPI_plan_get_cached_plan(spiplan);
127 if (list_length(cplan->stmt_list) != 1)
128 elog(ERROR, "failed to decipher query plan");
129 stmt = linitial_node(PlannedStmt, cplan->stmt_list);
130 if (stmt->commandType != CMD_SELECT)
131 elog(ERROR, "failed to decipher query plan");
132 plan = stmt->planTree;
133 Assert(list_length(plan->targetlist) >= 2);
134 clause1 = castNode(TargetEntry, linitial(plan->targetlist))->expr;
135 clause2 = castNode(TargetEntry, lsecond(plan->targetlist))->expr;
138 * Because the clauses are in the SELECT list, preprocess_expression did
139 * not pass them through canonicalize_qual nor make_ands_implicit.
141 * We can't do canonicalize_qual here, since it's unclear whether the
142 * expressions ought to be treated as WHERE or CHECK clauses. Fortunately,
143 * useful test expressions wouldn't be affected by those transformations
144 * anyway. We should do make_ands_implicit, though.
146 * Another way in which this does not exactly duplicate the normal usage
147 * of the proof functions is that they are often given qual clauses
148 * containing RestrictInfo nodes. But since predtest.c just looks through
149 * those anyway, it seems OK to not worry about that point.
151 clause1 = (Expr *) make_ands_implicit(clause1);
152 clause2 = (Expr *) make_ands_implicit(clause2);
154 strong_implied_by = predicate_implied_by((List *) clause1,
158 weak_implied_by = predicate_implied_by((List *) clause1,
162 strong_refuted_by = predicate_refuted_by((List *) clause1,
166 weak_refuted_by = predicate_refuted_by((List *) clause1,
171 * Issue warning if any proof is demonstrably incorrect.
173 if (strong_implied_by && !s_i_holds)
174 elog(WARNING, "strong_implied_by result is incorrect");
175 if (weak_implied_by && !w_i_holds)
176 elog(WARNING, "weak_implied_by result is incorrect");
177 if (strong_refuted_by && !s_r_holds)
178 elog(WARNING, "strong_refuted_by result is incorrect");
179 if (weak_refuted_by && !w_r_holds)
180 elog(WARNING, "weak_refuted_by result is incorrect");
183 * Clean up and return a record of the results.
185 if (SPI_finish() != SPI_OK_FINISH)
186 elog(ERROR, "SPI_finish failed");
188 tupdesc = CreateTemplateTupleDesc(8);
189 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
190 "strong_implied_by", BOOLOID, -1, 0);
191 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
192 "weak_implied_by", BOOLOID, -1, 0);
193 TupleDescInitEntry(tupdesc, (AttrNumber) 3,
194 "strong_refuted_by", BOOLOID, -1, 0);
195 TupleDescInitEntry(tupdesc, (AttrNumber) 4,
196 "weak_refuted_by", BOOLOID, -1, 0);
197 TupleDescInitEntry(tupdesc, (AttrNumber) 5,
198 "s_i_holds", BOOLOID, -1, 0);
199 TupleDescInitEntry(tupdesc, (AttrNumber) 6,
200 "w_i_holds", BOOLOID, -1, 0);
201 TupleDescInitEntry(tupdesc, (AttrNumber) 7,
202 "s_r_holds", BOOLOID, -1, 0);
203 TupleDescInitEntry(tupdesc, (AttrNumber) 8,
204 "w_r_holds", BOOLOID, -1, 0);
205 tupdesc = BlessTupleDesc(tupdesc);
207 MemSet(nulls, 0, sizeof(nulls));
208 values[0] = BoolGetDatum(strong_implied_by);
209 values[1] = BoolGetDatum(weak_implied_by);
210 values[2] = BoolGetDatum(strong_refuted_by);
211 values[3] = BoolGetDatum(weak_refuted_by);
212 values[4] = BoolGetDatum(s_i_holds);
213 values[5] = BoolGetDatum(w_i_holds);
214 values[6] = BoolGetDatum(s_r_holds);
215 values[7] = BoolGetDatum(w_r_holds);
217 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));