|
| 1 | +#include "postgres.h" |
| 2 | + |
| 3 | +#include "access/htup_details.h" |
| 4 | +#include "access/nbtree.h" |
| 5 | +#include "access/tupconvert.h" |
| 6 | +#include "catalog/objectaccess.h" |
| 7 | +#include "catalog/pg_type.h" |
| 8 | +#include "executor/execdebug.h" |
| 9 | +#include "executor/nodeSubplan.h" |
| 10 | +#include "funcapi.h" |
| 11 | +#include "miscadmin.h" |
| 12 | +#include "nodes/makefuncs.h" |
| 13 | +#include "nodes/nodeFuncs.h" |
| 14 | +#include "optimizer/planner.h" |
| 15 | +#include "parser/parse_coerce.h" |
| 16 | +#include "parser/parsetree.h" |
| 17 | +#include "pgstat.h" |
| 18 | +#include "utils/acl.h" |
| 19 | +#include "utils/builtins.h" |
| 20 | +#include "utils/lsyscache.h" |
| 21 | +#include "utils/memutils.h" |
| 22 | +#include "utils/typcache.h" |
| 23 | +#include "utils/xml.h" |
| 24 | + |
| 25 | +#include "executor.h" |
| 26 | +#include "execTuples.h" |
| 27 | +#include "vectorTupleSlot.h" |
| 28 | + |
| 29 | +/* ---------------------------------------------------------------- |
| 30 | + * ExecQual |
| 31 | + * |
| 32 | + * Evaluates a conjunctive boolean expression (qual list) and |
| 33 | + * returns true iff none of the subexpressions are false. |
| 34 | + * (We also return true if the list is empty.) |
| 35 | + * |
| 36 | + * If some of the subexpressions yield NULL but none yield FALSE, |
| 37 | + * then the result of the conjunction is NULL (ie, unknown) |
| 38 | + * according to three-valued boolean logic. In this case, |
| 39 | + * we return the value specified by the "resultForNull" parameter. |
| 40 | + * |
| 41 | + * Callers evaluating WHERE clauses should pass resultForNull=FALSE, |
| 42 | + * since SQL specifies that tuples with null WHERE results do not |
| 43 | + * get selected. On the other hand, callers evaluating constraint |
| 44 | + * conditions should pass resultForNull=TRUE, since SQL also specifies |
| 45 | + * that NULL constraint conditions are not failures. |
| 46 | + * |
| 47 | + * NOTE: it would not be correct to use this routine to evaluate an |
| 48 | + * AND subclause of a boolean expression; for that purpose, a NULL |
| 49 | + * result must be returned as NULL so that it can be properly treated |
| 50 | + * in the next higher operator (cf. ExecEvalAnd and ExecEvalOr). |
| 51 | + * This routine is only used in contexts where a complete expression |
| 52 | + * is being evaluated and we know that NULL can be treated the same |
| 53 | + * as one boolean result or the other. |
| 54 | + * |
| 55 | + * ---------------------------------------------------------------- |
| 56 | + */ |
| 57 | +bool |
| 58 | +VExecScanQual(List *qual, ExprContext *econtext, bool resultForNull) |
| 59 | +{ |
| 60 | + MemoryContext oldContext; |
| 61 | + TupleTableSlot *slot; |
| 62 | + VectorTupleSlot *vslot; |
| 63 | + ListCell *l; |
| 64 | + int row; |
| 65 | + |
| 66 | + /* |
| 67 | + * debugging stuff |
| 68 | + */ |
| 69 | + EV_printf("ExecQual: qual is "); |
| 70 | + EV_nodeDisplay(qual); |
| 71 | + EV_printf("\n"); |
| 72 | + |
| 73 | + /* |
| 74 | + * Run in short-lived per-tuple context while computing expressions. |
| 75 | + */ |
| 76 | + oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory); |
| 77 | + |
| 78 | + /* |
| 79 | + * Evaluate the qual conditions one at a time. If we find a FALSE result, |
| 80 | + * we can stop evaluating and return FALSE --- the AND result must be |
| 81 | + * FALSE. Also, if we find a NULL result when resultForNull is FALSE, we |
| 82 | + * can stop and return FALSE --- the AND result must be FALSE or NULL in |
| 83 | + * that case, and the caller doesn't care which. |
| 84 | + * |
| 85 | + * If we get to the end of the list, we can return TRUE. This will happen |
| 86 | + * when the AND result is indeed TRUE, or when the AND result is NULL (one |
| 87 | + * or more NULL subresult, with all the rest TRUE) and the caller has |
| 88 | + * specified resultForNull = TRUE. |
| 89 | + */ |
| 90 | + |
| 91 | + slot = econtext->ecxt_scantuple; |
| 92 | + vslot = (VectorTupleSlot *)slot; |
| 93 | + foreach(l, qual) |
| 94 | + { |
| 95 | + ExprState *clause = (ExprState *) lfirst(l); |
| 96 | + Datum expr_value; |
| 97 | + bool isNull; |
| 98 | + vbool *expr_val_bools; |
| 99 | + |
| 100 | + /* take a batch as input to evaluate quals */ |
| 101 | + expr_value = ExecEvalExpr(clause, econtext, &isNull, NULL); |
| 102 | + |
| 103 | + expr_val_bools = (vbool *)DatumGetPointer(expr_value); |
| 104 | + |
| 105 | + /* using skip array to indicated row which didn't pass the qual */ |
| 106 | + for(row = 0; row < BATCHSIZE; row++) |
| 107 | + if((!expr_val_bools->isnull[row] || !resultForNull) && |
| 108 | + !DatumGetBool(expr_val_bools->values[row]) && |
| 109 | + !vslot->skip[row]) |
| 110 | + vslot->skip[row] = true; |
| 111 | + /* TODO: opt: add skipped count for vslot to support skipping the whole batch?*/ |
| 112 | + } |
| 113 | + |
| 114 | + MemoryContextSwitchTo(oldContext); |
| 115 | + |
| 116 | + /* return true if any tuple in batch pass the qual. */ |
| 117 | + for(row = 0; row < BATCHSIZE; row++) |
| 118 | + if (!vslot->skip[row]) |
| 119 | + return true; |
| 120 | + |
| 121 | + return false; |
| 122 | +} |
| 123 | + |
0 commit comments