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 "nodes/makefuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/predtest.h"
23 #include "utils/builtins.h"
28 * test_predtest(query text) returns record
30 PG_FUNCTION_INFO_V1(test_predtest);
33 test_predtest(PG_FUNCTION_ARGS)
35 text *txt = PG_GETARG_TEXT_PP(0);
36 char *query_string = text_to_cstring(txt);
49 bool strong_implied_by,
57 /* We use SPI to parse, plan, and execute the test query */
58 if (SPI_connect() != SPI_OK_CONNECT)
59 elog(ERROR, "SPI_connect failed");
62 * First, plan and execute the query, and inspect the results. To the
63 * extent that the query fully exercises the two expressions, this
64 * provides an experimental indication of whether implication or
67 spiplan = SPI_prepare(query_string, 0, NULL);
69 elog(ERROR, "SPI_prepare failed for \"%s\"", query_string);
71 spirc = SPI_execute_plan(spiplan, NULL, NULL, true, 0);
72 if (spirc != SPI_OK_SELECT)
73 elog(ERROR, "failed to execute \"%s\"", query_string);
74 tupdesc = SPI_tuptable->tupdesc;
75 if (tupdesc->natts != 2 ||
76 TupleDescAttr(tupdesc, 0)->atttypid != BOOLOID ||
77 TupleDescAttr(tupdesc, 1)->atttypid != BOOLOID)
78 elog(ERROR, "query must yield two boolean columns");
80 s_i_holds = w_i_holds = s_r_holds = w_r_holds = true;
81 for (i = 0; i < SPI_processed; i++)
83 HeapTuple tup = SPI_tuptable->vals[i];
89 /* Extract column values in a 3-way representation */
90 dat = SPI_getbinval(tup, tupdesc, 1, &isnull);
93 else if (DatumGetBool(dat))
98 dat = SPI_getbinval(tup, tupdesc, 2, &isnull);
101 else if (DatumGetBool(dat))
106 /* Check for violations of various proof conditions */
108 /* strong implication: truth of c2 implies truth of c1 */
109 if (c2 == 't' && c1 != 't')
111 /* weak implication: non-falsity of c2 implies non-falsity of c1 */
112 if (c2 != 'f' && c1 == 'f')
114 /* strong refutation: truth of c2 implies falsity of c1 */
115 if (c2 == 't' && c1 != 'f')
117 /* weak refutation: truth of c2 implies non-truth of c1 */
118 if (c2 == 't' && c1 == 't')
123 * Now, dig the clause querytrees out of the plan, and see what predtest.c
126 cplan = SPI_plan_get_cached_plan(spiplan);
128 if (list_length(cplan->stmt_list) != 1)
129 elog(ERROR, "failed to decipher query plan");
130 stmt = linitial_node(PlannedStmt, cplan->stmt_list);
131 if (stmt->commandType != CMD_SELECT)
132 elog(ERROR, "failed to decipher query plan");
133 plan = stmt->planTree;
134 Assert(list_length(plan->targetlist) >= 2);
135 clause1 = castNode(TargetEntry, linitial(plan->targetlist))->expr;
136 clause2 = castNode(TargetEntry, lsecond(plan->targetlist))->expr;
139 * Because the clauses are in the SELECT list, preprocess_expression did
140 * not pass them through canonicalize_qual nor make_ands_implicit.
142 * We can't do canonicalize_qual here, since it's unclear whether the
143 * expressions ought to be treated as WHERE or CHECK clauses. Fortunately,
144 * useful test expressions wouldn't be affected by those transformations
145 * anyway. We should do make_ands_implicit, though.
147 * Another way in which this does not exactly duplicate the normal usage
148 * of the proof functions is that they are often given qual clauses
149 * containing RestrictInfo nodes. But since predtest.c just looks through
150 * those anyway, it seems OK to not worry about that point.
152 clause1 = (Expr *) make_ands_implicit(clause1);
153 clause2 = (Expr *) make_ands_implicit(clause2);
155 strong_implied_by = predicate_implied_by((List *) clause1,
159 weak_implied_by = predicate_implied_by((List *) clause1,
163 strong_refuted_by = predicate_refuted_by((List *) clause1,
167 weak_refuted_by = predicate_refuted_by((List *) clause1,
172 * Issue warning if any proof is demonstrably incorrect.
174 if (strong_implied_by && !s_i_holds)
175 elog(WARNING, "strong_implied_by result is incorrect");
176 if (weak_implied_by && !w_i_holds)
177 elog(WARNING, "weak_implied_by result is incorrect");
178 if (strong_refuted_by && !s_r_holds)
179 elog(WARNING, "strong_refuted_by result is incorrect");
180 if (weak_refuted_by && !w_r_holds)
181 elog(WARNING, "weak_refuted_by result is incorrect");
184 * Clean up and return a record of the results.
186 if (SPI_finish() != SPI_OK_FINISH)
187 elog(ERROR, "SPI_finish failed");
189 tupdesc = CreateTemplateTupleDesc(8);
190 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
191 "strong_implied_by", BOOLOID, -1, 0);
192 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
193 "weak_implied_by", BOOLOID, -1, 0);
194 TupleDescInitEntry(tupdesc, (AttrNumber) 3,
195 "strong_refuted_by", BOOLOID, -1, 0);
196 TupleDescInitEntry(tupdesc, (AttrNumber) 4,
197 "weak_refuted_by", BOOLOID, -1, 0);
198 TupleDescInitEntry(tupdesc, (AttrNumber) 5,
199 "s_i_holds", BOOLOID, -1, 0);
200 TupleDescInitEntry(tupdesc, (AttrNumber) 6,
201 "w_i_holds", BOOLOID, -1, 0);
202 TupleDescInitEntry(tupdesc, (AttrNumber) 7,
203 "s_r_holds", BOOLOID, -1, 0);
204 TupleDescInitEntry(tupdesc, (AttrNumber) 8,
205 "w_r_holds", BOOLOID, -1, 0);
206 tupdesc = BlessTupleDesc(tupdesc);
208 MemSet(nulls, 0, sizeof(nulls));
209 values[0] = BoolGetDatum(strong_implied_by);
210 values[1] = BoolGetDatum(weak_implied_by);
211 values[2] = BoolGetDatum(strong_refuted_by);
212 values[3] = BoolGetDatum(weak_refuted_by);
213 values[4] = BoolGetDatum(s_i_holds);
214 values[5] = BoolGetDatum(w_i_holds);
215 values[6] = BoolGetDatum(s_r_holds);
216 values[7] = BoolGetDatum(w_r_holds);
218 PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));