| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* execExprInterp.c |
| 4 |
* Interpreted evaluation of an expression step list. |
| 5 |
* |
| 6 |
* This file provides either a "direct threaded" (for gcc, clang and |
| 7 |
* compatible) or a "switch threaded" (for all compilers) implementation of |
| 8 |
* expression evaluation. The former is amongst the fastest known methods |
| 9 |
* of interpreting programs without resorting to assembly level work, or |
| 10 |
* just-in-time compilation, but it requires support for computed gotos. |
| 11 |
* The latter is amongst the fastest approaches doable in standard C. |
| 12 |
* |
| 13 |
* In either case we use ExprEvalStep->opcode to dispatch to the code block |
| 14 |
* within ExecInterpExpr() that implements the specific opcode type. |
| 15 |
* |
| 16 |
* Switch-threading uses a plain switch() statement to perform the |
| 17 |
* dispatch. This has the advantages of being plain C and allowing the |
| 18 |
* compiler to warn if implementation of a specific opcode has been forgotten. |
| 19 |
* The disadvantage is that dispatches will, as commonly implemented by |
| 20 |
* compilers, happen from a single location, requiring more jumps and causing |
| 21 |
* bad branch prediction. |
| 22 |
* |
| 23 |
* In direct threading, we use gcc's label-as-values extension - also adopted |
| 24 |
* by some other compilers - to replace ExprEvalStep->opcode with the address |
| 25 |
* of the block implementing the instruction. Dispatch to the next instruction |
| 26 |
* is done by a "computed goto". This allows for better branch prediction |
| 27 |
* (as the jumps are happening from different locations) and fewer jumps |
| 28 |
* (as no preparatory jump to a common dispatch location is needed). |
| 29 |
* |
| 30 |
* When using direct threading, ExecReadyInterpretedExpr will replace |
| 31 |
* each step's opcode field with the address of the relevant code block and |
| 32 |
* ExprState->flags will contain EEO_FLAG_DIRECT_THREADED to remember that |
| 33 |
* that's been done. |
| 34 |
* |
| 35 |
* For very simple instructions the overhead of the full interpreter |
| 36 |
* "startup", as minimal as it is, is noticeable. Therefore |
| 37 |
* ExecReadyInterpretedExpr will choose to implement certain simple |
| 38 |
* opcode patterns using special fast-path routines (ExecJust*). |
| 39 |
* |
| 40 |
* Complex or uncommon instructions are not implemented in-line in |
| 41 |
* ExecInterpExpr(), rather we call out to a helper function appearing later |
| 42 |
* in this file. For one reason, there'd not be a noticeable performance |
| 43 |
* benefit, but more importantly those complex routines are intended to be |
| 44 |
* shared between different expression evaluation approaches. For instance |
| 45 |
* a JIT compiler would generate calls to them. (This is why they are |
| 46 |
* exported rather than being "static" in this file.) |
| 47 |
* |
| 48 |
* |
| 49 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 50 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 51 |
* |
| 52 |
* IDENTIFICATION |
| 53 |
* src/backend/executor/execExprInterp.c |
| 54 |
* |
| 55 |
*------------------------------------------------------------------------- |
| 56 |
*/ |
| 57 |
#include "postgres.h" |
| 58 |
|
| 59 |
#include "access/heaptoast.h" |
| 60 |
#include "catalog/pg_type.h" |
| 61 |
#include "commands/sequence.h" |
| 62 |
#include "executor/execExpr.h" |
| 63 |
#include "executor/nodeSubplan.h" |
| 64 |
#include "funcapi.h" |
| 65 |
#include "miscadmin.h" |
| 66 |
#include "nodes/miscnodes.h" |
| 67 |
#include "nodes/nodeFuncs.h" |
| 68 |
#include "pgstat.h" |
| 69 |
#include "utils/array.h" |
| 70 |
#include "utils/builtins.h" |
| 71 |
#include "utils/date.h" |
| 72 |
#include "utils/datum.h" |
| 73 |
#include "utils/expandedrecord.h" |
| 74 |
#include "utils/json.h" |
| 75 |
#include "utils/jsonfuncs.h" |
| 76 |
#include "utils/jsonpath.h" |
| 77 |
#include "utils/lsyscache.h" |
| 78 |
#include "utils/memutils.h" |
| 79 |
#include "utils/timestamp.h" |
| 80 |
#include "utils/typcache.h" |
| 81 |
#include "utils/xml.h" |
| 82 |
|
| 83 |
/* |
| 84 |
* Use computed-goto-based opcode dispatch when computed gotos are available. |
| 85 |
* But use a separate symbol so that it's easy to adjust locally in this file |
| 86 |
* for development and testing. |
| 87 |
*/ |
| 88 |
#ifdef HAVE_COMPUTED_GOTO |
| 89 |
#define EEO_USE_COMPUTED_GOTO |
| 90 |
#endif /* HAVE_COMPUTED_GOTO */ |
| 91 |
|
| 92 |
/* |
| 93 |
* Macros for opcode dispatch. |
| 94 |
* |
| 95 |
* EEO_SWITCH - just hides the switch if not in use. |
| 96 |
* EEO_CASE - labels the implementation of named expression step type. |
| 97 |
* EEO_DISPATCH - jump to the implementation of the step type for 'op'. |
| 98 |
* EEO_OPCODE - compute opcode required by used expression evaluation method. |
| 99 |
* EEO_NEXT - increment 'op' and jump to correct next step type. |
| 100 |
* EEO_JUMP - jump to the specified step number within the current expression. |
| 101 |
*/ |
| 102 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 103 |
|
| 104 |
/* struct for jump target -> opcode lookup table */ |
| 105 |
typedef struct ExprEvalOpLookup |
| 106 |
{ |
| 107 |
const void *opcode; |
| 108 |
ExprEvalOp op; |
| 109 |
} ExprEvalOpLookup; |
| 110 |
|
| 111 |
/* to make dispatch_table accessible outside ExecInterpExpr() */ |
| 112 |
static const void **dispatch_table = NULL; |
| 113 |
|
| 114 |
/* jump target -> opcode lookup table */ |
| 115 |
static ExprEvalOpLookup reverse_dispatch_table[EEOP_LAST]; |
| 116 |
|
| 117 |
#define EEO_SWITCH() |
| 118 |
#define EEO_CASE(name) CASE_##name: |
| 119 |
#define EEO_DISPATCH() goto *((void *) op->opcode) |
| 120 |
#define EEO_OPCODE(opcode) ((intptr_t) dispatch_table[opcode]) |
| 121 |
|
| 122 |
#else /* !EEO_USE_COMPUTED_GOTO */ |
| 123 |
|
| 124 |
#define EEO_SWITCH() starteval: switch ((ExprEvalOp) op->opcode) |
| 125 |
#define EEO_CASE(name) case name: |
| 126 |
#define EEO_DISPATCH() goto starteval |
| 127 |
#define EEO_OPCODE(opcode) (opcode) |
| 128 |
|
| 129 |
#endif /* EEO_USE_COMPUTED_GOTO */ |
| 130 |
|
| 131 |
#define EEO_NEXT() \ |
| 132 |
do { \ |
| 133 |
op++; \ |
| 134 |
EEO_DISPATCH(); \ |
| 135 |
} while (0) |
| 136 |
|
| 137 |
#define EEO_JUMP(stepno) \ |
| 138 |
do { \ |
| 139 |
op = &state->steps[stepno]; \ |
| 140 |
EEO_DISPATCH(); \ |
| 141 |
} while (0) |
| 142 |
|
| 143 |
|
| 144 |
static Datum ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull); |
| 145 |
static void ExecInitInterpreter(void); |
| 146 |
|
| 147 |
/* support functions */ |
| 148 |
static void CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype); |
| 149 |
static void CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot); |
| 150 |
static TupleDesc get_cached_rowtype(Oid type_id, int32 typmod, |
| 151 |
ExprEvalRowtypeCache *rowcache, |
| 152 |
bool *changed); |
| 153 |
static void ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, |
| 154 |
ExprContext *econtext, bool checkisnull); |
| 155 |
|
| 156 |
/* fast-path evaluation functions */ |
| 157 |
static Datum ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 158 |
static Datum ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 159 |
static Datum ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 160 |
static Datum ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 161 |
static Datum ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 162 |
static Datum ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 163 |
static Datum ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull); |
| 164 |
static Datum ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull); |
| 165 |
static Datum ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 166 |
static Datum ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 167 |
static Datum ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 168 |
static Datum ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 169 |
static Datum ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 170 |
static Datum ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 171 |
static Datum ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext, bool *isnull); |
| 172 |
static Datum ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 173 |
static Datum ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull); |
| 174 |
static Datum ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 175 |
static Datum ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull); |
| 176 |
static Datum ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext, bool *isnull); |
| 177 |
|
| 178 |
/* execution helper functions */ |
| 179 |
static pg_always_inline void ExecEvalArrayCompareInternal(FunctionCallInfo fcinfo, |
| 180 |
ArrayType *arr, |
| 181 |
int16 typlen, |
| 182 |
bool typbyval, |
| 183 |
char typalign, |
| 184 |
bool useOr, |
| 185 |
Datum *result, |
| 186 |
bool *resultnull); |
| 187 |
static pg_always_inline void ExecAggPlainTransByVal(AggState *aggstate, |
| 188 |
AggStatePerTrans pertrans, |
| 189 |
AggStatePerGroup pergroup, |
| 190 |
ExprContext *aggcontext, |
| 191 |
int setno); |
| 192 |
static pg_always_inline void ExecAggPlainTransByRef(AggState *aggstate, |
| 193 |
AggStatePerTrans pertrans, |
| 194 |
AggStatePerGroup pergroup, |
| 195 |
ExprContext *aggcontext, |
| 196 |
int setno); |
| 197 |
static char *ExecGetJsonValueItemString(JsonbValue *item, bool *resnull); |
| 198 |
|
| 199 |
/* |
| 200 |
* ScalarArrayOpExprHashEntry |
| 201 |
* Hash table entry type used during EEOP_HASHED_SCALARARRAYOP |
| 202 |
*/ |
| 203 |
typedef struct ScalarArrayOpExprHashEntry |
| 204 |
{ |
| 205 |
Datum key; |
| 206 |
uint32 status; /* hash status */ |
| 207 |
uint32 hash; /* hash value (cached) */ |
| 208 |
} ScalarArrayOpExprHashEntry; |
| 209 |
|
| 210 |
#define SH_PREFIX saophash |
| 211 |
#define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry |
| 212 |
#define SH_KEY_TYPE Datum |
| 213 |
#define SH_SCOPE static inline |
| 214 |
#define SH_DECLARE |
| 215 |
#include "lib/simplehash.h" |
| 216 |
|
| 217 |
static bool saop_hash_element_match(struct saophash_hash *tb, Datum key1, |
| 218 |
Datum key2); |
| 219 |
static uint32 saop_element_hash(struct saophash_hash *tb, Datum key); |
| 220 |
|
| 221 |
/* |
| 222 |
* ScalarArrayOpExprHashTable |
| 223 |
* Hash table for EEOP_HASHED_SCALARARRAYOP |
| 224 |
*/ |
| 225 |
typedef struct ScalarArrayOpExprHashTable |
| 226 |
{ |
| 227 |
saophash_hash *hashtab; /* underlying hash table */ |
| 228 |
struct ExprEvalStep *op; |
| 229 |
FmgrInfo hash_finfo; /* function's lookup data */ |
| 230 |
FunctionCallInfoBaseData hash_fcinfo_data; /* arguments etc */ |
| 231 |
} ScalarArrayOpExprHashTable; |
| 232 |
|
| 233 |
/* Define parameters for ScalarArrayOpExpr hash table code generation. */ |
| 234 |
#define SH_PREFIX saophash |
| 235 |
#define SH_ELEMENT_TYPE ScalarArrayOpExprHashEntry |
| 236 |
#define SH_KEY_TYPE Datum |
| 237 |
#define SH_KEY key |
| 238 |
#define SH_HASH_KEY(tb, key) saop_element_hash(tb, key) |
| 239 |
#define SH_EQUAL(tb, a, b) saop_hash_element_match(tb, a, b) |
| 240 |
#define SH_SCOPE static inline |
| 241 |
#define SH_STORE_HASH |
| 242 |
#define SH_GET_HASH(tb, a) a->hash |
| 243 |
#define SH_DEFINE |
| 244 |
#include "lib/simplehash.h" |
| 245 |
|
| 246 |
/* |
| 247 |
* Prepare ExprState for interpreted execution. |
| 248 |
*/ |
| 249 |
void |
| 250 |
ExecReadyInterpretedExpr(ExprState *state) |
| 251 |
{ |
| 252 |
/* Ensure one-time interpreter setup has been done */ |
| 253 |
ExecInitInterpreter(); |
| 254 |
|
| 255 |
/* Simple validity checks on expression */ |
| 256 |
Assert(state->steps_len >= 1); |
| 257 |
Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN || |
| 258 |
state->steps[state->steps_len - 1].opcode == EEOP_DONE_NO_RETURN); |
| 259 |
|
| 260 |
/* |
| 261 |
* Don't perform redundant initialization. This is unreachable in current |
| 262 |
* cases, but might be hit if there's additional expression evaluation |
| 263 |
* methods that rely on interpreted execution to work. |
| 264 |
*/ |
| 265 |
if (state->flags & EEO_FLAG_INTERPRETER_INITIALIZED) |
| 266 |
return; |
| 267 |
|
| 268 |
/* |
| 269 |
* First time through, check whether attribute matches Var. Might not be |
| 270 |
* ok anymore, due to schema changes. We do that by setting up a callback |
| 271 |
* that does checking on the first call, which then sets the evalfunc |
| 272 |
* callback to the actual method of execution. |
| 273 |
*/ |
| 274 |
state->evalfunc = ExecInterpExprStillValid; |
| 275 |
|
| 276 |
/* DIRECT_THREADED should not already be set */ |
| 277 |
Assert((state->flags & EEO_FLAG_DIRECT_THREADED) == 0); |
| 278 |
|
| 279 |
/* |
| 280 |
* There shouldn't be any errors before the expression is fully |
| 281 |
* initialized, and even if so, it'd lead to the expression being |
| 282 |
* abandoned. So we can set the flag now and save some code. |
| 283 |
*/ |
| 284 |
state->flags |= EEO_FLAG_INTERPRETER_INITIALIZED; |
| 285 |
|
| 286 |
/* |
| 287 |
* Select fast-path evalfuncs for very simple expressions. "Starting up" |
| 288 |
* the full interpreter is a measurable overhead for these, and these |
| 289 |
* patterns occur often enough to be worth optimizing. |
| 290 |
*/ |
| 291 |
if (state->steps_len == 5) |
| 292 |
{ |
| 293 |
ExprEvalOp step0 = state->steps[0].opcode; |
| 294 |
ExprEvalOp step1 = state->steps[1].opcode; |
| 295 |
ExprEvalOp step2 = state->steps[2].opcode; |
| 296 |
ExprEvalOp step3 = state->steps[3].opcode; |
| 297 |
|
| 298 |
if (step0 == EEOP_INNER_FETCHSOME && |
| 299 |
step1 == EEOP_HASHDATUM_SET_INITVAL && |
| 300 |
step2 == EEOP_INNER_VAR && |
| 301 |
step3 == EEOP_HASHDATUM_NEXT32) |
| 302 |
{ |
| 303 |
state->evalfunc_private = (void *) ExecJustHashInnerVarWithIV; |
| 304 |
return; |
| 305 |
} |
| 306 |
} |
| 307 |
else if (state->steps_len == 4) |
| 308 |
{ |
| 309 |
ExprEvalOp step0 = state->steps[0].opcode; |
| 310 |
ExprEvalOp step1 = state->steps[1].opcode; |
| 311 |
ExprEvalOp step2 = state->steps[2].opcode; |
| 312 |
|
| 313 |
if (step0 == EEOP_OUTER_FETCHSOME && |
| 314 |
step1 == EEOP_OUTER_VAR && |
| 315 |
step2 == EEOP_HASHDATUM_FIRST) |
| 316 |
{ |
| 317 |
state->evalfunc_private = (void *) ExecJustHashOuterVar; |
| 318 |
return; |
| 319 |
} |
| 320 |
else if (step0 == EEOP_INNER_FETCHSOME && |
| 321 |
step1 == EEOP_INNER_VAR && |
| 322 |
step2 == EEOP_HASHDATUM_FIRST) |
| 323 |
{ |
| 324 |
state->evalfunc_private = (void *) ExecJustHashInnerVar; |
| 325 |
return; |
| 326 |
} |
| 327 |
else if (step0 == EEOP_OUTER_FETCHSOME && |
| 328 |
step1 == EEOP_OUTER_VAR && |
| 329 |
step2 == EEOP_HASHDATUM_FIRST_STRICT) |
| 330 |
{ |
| 331 |
state->evalfunc_private = (void *) ExecJustHashOuterVarStrict; |
| 332 |
return; |
| 333 |
} |
| 334 |
} |
| 335 |
else if (state->steps_len == 3) |
| 336 |
{ |
| 337 |
ExprEvalOp step0 = state->steps[0].opcode; |
| 338 |
ExprEvalOp step1 = state->steps[1].opcode; |
| 339 |
|
| 340 |
if (step0 == EEOP_INNER_FETCHSOME && |
| 341 |
step1 == EEOP_INNER_VAR) |
| 342 |
{ |
| 343 |
state->evalfunc_private = ExecJustInnerVar; |
| 344 |
return; |
| 345 |
} |
| 346 |
else if (step0 == EEOP_OUTER_FETCHSOME && |
| 347 |
step1 == EEOP_OUTER_VAR) |
| 348 |
{ |
| 349 |
state->evalfunc_private = ExecJustOuterVar; |
| 350 |
return; |
| 351 |
} |
| 352 |
else if (step0 == EEOP_SCAN_FETCHSOME && |
| 353 |
step1 == EEOP_SCAN_VAR) |
| 354 |
{ |
| 355 |
state->evalfunc_private = ExecJustScanVar; |
| 356 |
return; |
| 357 |
} |
| 358 |
else if (step0 == EEOP_INNER_FETCHSOME && |
| 359 |
step1 == EEOP_ASSIGN_INNER_VAR) |
| 360 |
{ |
| 361 |
state->evalfunc_private = ExecJustAssignInnerVar; |
| 362 |
return; |
| 363 |
} |
| 364 |
else if (step0 == EEOP_OUTER_FETCHSOME && |
| 365 |
step1 == EEOP_ASSIGN_OUTER_VAR) |
| 366 |
{ |
| 367 |
state->evalfunc_private = ExecJustAssignOuterVar; |
| 368 |
return; |
| 369 |
} |
| 370 |
else if (step0 == EEOP_SCAN_FETCHSOME && |
| 371 |
step1 == EEOP_ASSIGN_SCAN_VAR) |
| 372 |
{ |
| 373 |
state->evalfunc_private = ExecJustAssignScanVar; |
| 374 |
return; |
| 375 |
} |
| 376 |
else if (step0 == EEOP_CASE_TESTVAL && |
| 377 |
(step1 == EEOP_FUNCEXPR_STRICT || |
| 378 |
step1 == EEOP_FUNCEXPR_STRICT_1 || |
| 379 |
step1 == EEOP_FUNCEXPR_STRICT_2)) |
| 380 |
{ |
| 381 |
state->evalfunc_private = ExecJustApplyFuncToCase; |
| 382 |
return; |
| 383 |
} |
| 384 |
else if (step0 == EEOP_INNER_VAR && |
| 385 |
step1 == EEOP_HASHDATUM_FIRST) |
| 386 |
{ |
| 387 |
state->evalfunc_private = (void *) ExecJustHashInnerVarVirt; |
| 388 |
return; |
| 389 |
} |
| 390 |
else if (step0 == EEOP_OUTER_VAR && |
| 391 |
step1 == EEOP_HASHDATUM_FIRST) |
| 392 |
{ |
| 393 |
state->evalfunc_private = (void *) ExecJustHashOuterVarVirt; |
| 394 |
return; |
| 395 |
} |
| 396 |
} |
| 397 |
else if (state->steps_len == 2) |
| 398 |
{ |
| 399 |
ExprEvalOp step0 = state->steps[0].opcode; |
| 400 |
|
| 401 |
if (step0 == EEOP_CONST) |
| 402 |
{ |
| 403 |
state->evalfunc_private = ExecJustConst; |
| 404 |
return; |
| 405 |
} |
| 406 |
else if (step0 == EEOP_INNER_VAR) |
| 407 |
{ |
| 408 |
state->evalfunc_private = ExecJustInnerVarVirt; |
| 409 |
return; |
| 410 |
} |
| 411 |
else if (step0 == EEOP_OUTER_VAR) |
| 412 |
{ |
| 413 |
state->evalfunc_private = ExecJustOuterVarVirt; |
| 414 |
return; |
| 415 |
} |
| 416 |
else if (step0 == EEOP_SCAN_VAR) |
| 417 |
{ |
| 418 |
state->evalfunc_private = ExecJustScanVarVirt; |
| 419 |
return; |
| 420 |
} |
| 421 |
else if (step0 == EEOP_ASSIGN_INNER_VAR) |
| 422 |
{ |
| 423 |
state->evalfunc_private = ExecJustAssignInnerVarVirt; |
| 424 |
return; |
| 425 |
} |
| 426 |
else if (step0 == EEOP_ASSIGN_OUTER_VAR) |
| 427 |
{ |
| 428 |
state->evalfunc_private = ExecJustAssignOuterVarVirt; |
| 429 |
return; |
| 430 |
} |
| 431 |
else if (step0 == EEOP_ASSIGN_SCAN_VAR) |
| 432 |
{ |
| 433 |
state->evalfunc_private = ExecJustAssignScanVarVirt; |
| 434 |
return; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 439 |
|
| 440 |
/* |
| 441 |
* In the direct-threaded implementation, replace each opcode with the |
| 442 |
* address to jump to. (Use ExecEvalStepOp() to get back the opcode.) |
| 443 |
*/ |
| 444 |
for (int off = 0; off < state->steps_len; off++) |
| 445 |
{ |
| 446 |
ExprEvalStep *op = &state->steps[off]; |
| 447 |
|
| 448 |
op->opcode = EEO_OPCODE(op->opcode); |
| 449 |
} |
| 450 |
|
| 451 |
state->flags |= EEO_FLAG_DIRECT_THREADED; |
| 452 |
#endif /* EEO_USE_COMPUTED_GOTO */ |
| 453 |
|
| 454 |
state->evalfunc_private = ExecInterpExpr; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
/* |
| 459 |
* Evaluate expression identified by "state" in the execution context |
| 460 |
* given by "econtext". *isnull is set to the is-null flag for the result, |
| 461 |
* and the Datum value is the function result. |
| 462 |
* |
| 463 |
* As a special case, return the dispatch table's address if state is NULL. |
| 464 |
* This is used by ExecInitInterpreter to set up the dispatch_table global. |
| 465 |
* (Only applies when EEO_USE_COMPUTED_GOTO is defined.) |
| 466 |
*/ |
| 467 |
static Datum |
| 468 |
ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) |
| 469 |
{ |
| 470 |
ExprEvalStep *op; |
| 471 |
TupleTableSlot *resultslot; |
| 472 |
TupleTableSlot *innerslot; |
| 473 |
TupleTableSlot *outerslot; |
| 474 |
TupleTableSlot *scanslot; |
| 475 |
TupleTableSlot *oldslot; |
| 476 |
TupleTableSlot *newslot; |
| 477 |
|
| 478 |
/* |
| 479 |
* This array has to be in the same order as enum ExprEvalOp. |
| 480 |
*/ |
| 481 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 482 |
static const void *const dispatch_table[] = { |
| 483 |
&&CASE_EEOP_DONE_RETURN, |
| 484 |
&&CASE_EEOP_DONE_NO_RETURN, |
| 485 |
&&CASE_EEOP_INNER_FETCHSOME, |
| 486 |
&&CASE_EEOP_OUTER_FETCHSOME, |
| 487 |
&&CASE_EEOP_SCAN_FETCHSOME, |
| 488 |
&&CASE_EEOP_OLD_FETCHSOME, |
| 489 |
&&CASE_EEOP_NEW_FETCHSOME, |
| 490 |
&&CASE_EEOP_INNER_VAR, |
| 491 |
&&CASE_EEOP_OUTER_VAR, |
| 492 |
&&CASE_EEOP_SCAN_VAR, |
| 493 |
&&CASE_EEOP_OLD_VAR, |
| 494 |
&&CASE_EEOP_NEW_VAR, |
| 495 |
&&CASE_EEOP_INNER_SYSVAR, |
| 496 |
&&CASE_EEOP_OUTER_SYSVAR, |
| 497 |
&&CASE_EEOP_SCAN_SYSVAR, |
| 498 |
&&CASE_EEOP_OLD_SYSVAR, |
| 499 |
&&CASE_EEOP_NEW_SYSVAR, |
| 500 |
&&CASE_EEOP_WHOLEROW, |
| 501 |
&&CASE_EEOP_ASSIGN_INNER_VAR, |
| 502 |
&&CASE_EEOP_ASSIGN_OUTER_VAR, |
| 503 |
&&CASE_EEOP_ASSIGN_SCAN_VAR, |
| 504 |
&&CASE_EEOP_ASSIGN_OLD_VAR, |
| 505 |
&&CASE_EEOP_ASSIGN_NEW_VAR, |
| 506 |
&&CASE_EEOP_ASSIGN_TMP, |
| 507 |
&&CASE_EEOP_ASSIGN_TMP_MAKE_RO, |
| 508 |
&&CASE_EEOP_CONST, |
| 509 |
&&CASE_EEOP_FUNCEXPR, |
| 510 |
&&CASE_EEOP_FUNCEXPR_STRICT, |
| 511 |
&&CASE_EEOP_FUNCEXPR_STRICT_1, |
| 512 |
&&CASE_EEOP_FUNCEXPR_STRICT_2, |
| 513 |
&&CASE_EEOP_FUNCEXPR_FUSAGE, |
| 514 |
&&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE, |
| 515 |
&&CASE_EEOP_BOOL_AND_STEP_FIRST, |
| 516 |
&&CASE_EEOP_BOOL_AND_STEP, |
| 517 |
&&CASE_EEOP_BOOL_AND_STEP_LAST, |
| 518 |
&&CASE_EEOP_BOOL_OR_STEP_FIRST, |
| 519 |
&&CASE_EEOP_BOOL_OR_STEP, |
| 520 |
&&CASE_EEOP_BOOL_OR_STEP_LAST, |
| 521 |
&&CASE_EEOP_BOOL_NOT_STEP, |
| 522 |
&&CASE_EEOP_QUAL, |
| 523 |
&&CASE_EEOP_JUMP, |
| 524 |
&&CASE_EEOP_JUMP_IF_NULL, |
| 525 |
&&CASE_EEOP_JUMP_IF_NOT_NULL, |
| 526 |
&&CASE_EEOP_JUMP_IF_NOT_TRUE, |
| 527 |
&&CASE_EEOP_NULLTEST_ISNULL, |
| 528 |
&&CASE_EEOP_NULLTEST_ISNOTNULL, |
| 529 |
&&CASE_EEOP_NULLTEST_ROWISNULL, |
| 530 |
&&CASE_EEOP_NULLTEST_ROWISNOTNULL, |
| 531 |
&&CASE_EEOP_BOOLTEST_IS_TRUE, |
| 532 |
&&CASE_EEOP_BOOLTEST_IS_NOT_TRUE, |
| 533 |
&&CASE_EEOP_BOOLTEST_IS_FALSE, |
| 534 |
&&CASE_EEOP_BOOLTEST_IS_NOT_FALSE, |
| 535 |
&&CASE_EEOP_PARAM_EXEC, |
| 536 |
&&CASE_EEOP_PARAM_EXTERN, |
| 537 |
&&CASE_EEOP_PARAM_CALLBACK, |
| 538 |
&&CASE_EEOP_PARAM_SET, |
| 539 |
&&CASE_EEOP_CASE_TESTVAL, |
| 540 |
&&CASE_EEOP_CASE_TESTVAL_EXT, |
| 541 |
&&CASE_EEOP_MAKE_READONLY, |
| 542 |
&&CASE_EEOP_IOCOERCE, |
| 543 |
&&CASE_EEOP_IOCOERCE_SAFE, |
| 544 |
&&CASE_EEOP_DISTINCT, |
| 545 |
&&CASE_EEOP_NOT_DISTINCT, |
| 546 |
&&CASE_EEOP_NULLIF, |
| 547 |
&&CASE_EEOP_SQLVALUEFUNCTION, |
| 548 |
&&CASE_EEOP_CURRENTOFEXPR, |
| 549 |
&&CASE_EEOP_NEXTVALUEEXPR, |
| 550 |
&&CASE_EEOP_RETURNINGEXPR, |
| 551 |
&&CASE_EEOP_ARRAYEXPR, |
| 552 |
&&CASE_EEOP_ARRAYCOERCE, |
| 553 |
&&CASE_EEOP_ROW, |
| 554 |
&&CASE_EEOP_ROWCOMPARE_STEP, |
| 555 |
&&CASE_EEOP_ROWCOMPARE_FINAL, |
| 556 |
&&CASE_EEOP_MINMAX, |
| 557 |
&&CASE_EEOP_FIELDSELECT, |
| 558 |
&&CASE_EEOP_FIELDSTORE_DEFORM, |
| 559 |
&&CASE_EEOP_FIELDSTORE_FORM, |
| 560 |
&&CASE_EEOP_SBSREF_SUBSCRIPTS, |
| 561 |
&&CASE_EEOP_SBSREF_OLD, |
| 562 |
&&CASE_EEOP_SBSREF_ASSIGN, |
| 563 |
&&CASE_EEOP_SBSREF_FETCH, |
| 564 |
&&CASE_EEOP_DOMAIN_TESTVAL, |
| 565 |
&&CASE_EEOP_DOMAIN_TESTVAL_EXT, |
| 566 |
&&CASE_EEOP_DOMAIN_NOTNULL, |
| 567 |
&&CASE_EEOP_DOMAIN_CHECK, |
| 568 |
&&CASE_EEOP_HASHDATUM_SET_INITVAL, |
| 569 |
&&CASE_EEOP_HASHDATUM_FIRST, |
| 570 |
&&CASE_EEOP_HASHDATUM_FIRST_STRICT, |
| 571 |
&&CASE_EEOP_HASHDATUM_NEXT32, |
| 572 |
&&CASE_EEOP_HASHDATUM_NEXT32_STRICT, |
| 573 |
&&CASE_EEOP_CONVERT_ROWTYPE, |
| 574 |
&&CASE_EEOP_SCALARARRAYOP, |
| 575 |
&&CASE_EEOP_HASHED_SCALARARRAYOP, |
| 576 |
&&CASE_EEOP_XMLEXPR, |
| 577 |
&&CASE_EEOP_JSON_CONSTRUCTOR, |
| 578 |
&&CASE_EEOP_IS_JSON, |
| 579 |
&&CASE_EEOP_JSONEXPR_PATH, |
| 580 |
&&CASE_EEOP_JSONEXPR_COERCION, |
| 581 |
&&CASE_EEOP_JSONEXPR_COERCION_FINISH, |
| 582 |
&&CASE_EEOP_AGGREF, |
| 583 |
&&CASE_EEOP_GROUPING_FUNC, |
| 584 |
&&CASE_EEOP_WINDOW_FUNC, |
| 585 |
&&CASE_EEOP_MERGE_SUPPORT_FUNC, |
| 586 |
&&CASE_EEOP_SUBPLAN, |
| 587 |
&&CASE_EEOP_AGG_STRICT_DESERIALIZE, |
| 588 |
&&CASE_EEOP_AGG_DESERIALIZE, |
| 589 |
&&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS, |
| 590 |
&&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1, |
| 591 |
&&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS, |
| 592 |
&&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK, |
| 593 |
&&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL, |
| 594 |
&&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL, |
| 595 |
&&CASE_EEOP_AGG_PLAIN_TRANS_BYVAL, |
| 596 |
&&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF, |
| 597 |
&&CASE_EEOP_AGG_PLAIN_TRANS_STRICT_BYREF, |
| 598 |
&&CASE_EEOP_AGG_PLAIN_TRANS_BYREF, |
| 599 |
&&CASE_EEOP_AGG_PRESORTED_DISTINCT_SINGLE, |
| 600 |
&&CASE_EEOP_AGG_PRESORTED_DISTINCT_MULTI, |
| 601 |
&&CASE_EEOP_AGG_ORDERED_TRANS_DATUM, |
| 602 |
&&CASE_EEOP_AGG_ORDERED_TRANS_TUPLE, |
| 603 |
&&CASE_EEOP_LAST |
| 604 |
}; |
| 605 |
|
| 606 |
StaticAssertDecl(lengthof(dispatch_table) == EEOP_LAST + 1, |
| 607 |
"dispatch_table out of whack with ExprEvalOp"); |
| 608 |
|
| 609 |
if (unlikely(state == NULL)) |
| 610 |
return PointerGetDatum(dispatch_table); |
| 611 |
#else |
| 612 |
Assert(state != NULL); |
| 613 |
#endif /* EEO_USE_COMPUTED_GOTO */ |
| 614 |
|
| 615 |
/* setup state */ |
| 616 |
op = state->steps; |
| 617 |
resultslot = state->resultslot; |
| 618 |
innerslot = econtext->ecxt_innertuple; |
| 619 |
outerslot = econtext->ecxt_outertuple; |
| 620 |
scanslot = econtext->ecxt_scantuple; |
| 621 |
oldslot = econtext->ecxt_oldtuple; |
| 622 |
newslot = econtext->ecxt_newtuple; |
| 623 |
|
| 624 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 625 |
EEO_DISPATCH(); |
| 626 |
#endif |
| 627 |
|
| 628 |
EEO_SWITCH() |
| 629 |
{ |
| 630 |
EEO_CASE(EEOP_DONE_RETURN) |
| 631 |
{ |
| 632 |
*isnull = state->resnull; |
| 633 |
return state->resvalue; |
| 634 |
} |
| 635 |
|
| 636 |
EEO_CASE(EEOP_DONE_NO_RETURN) |
| 637 |
{ |
| 638 |
Assert(isnull == NULL); |
| 639 |
return (Datum) 0; |
| 640 |
} |
| 641 |
|
| 642 |
EEO_CASE(EEOP_INNER_FETCHSOME) |
| 643 |
{ |
| 644 |
CheckOpSlotCompatibility(op, innerslot); |
| 645 |
|
| 646 |
slot_getsomeattrs(innerslot, op->d.fetch.last_var); |
| 647 |
|
| 648 |
EEO_NEXT(); |
| 649 |
} |
| 650 |
|
| 651 |
EEO_CASE(EEOP_OUTER_FETCHSOME) |
| 652 |
{ |
| 653 |
CheckOpSlotCompatibility(op, outerslot); |
| 654 |
|
| 655 |
slot_getsomeattrs(outerslot, op->d.fetch.last_var); |
| 656 |
|
| 657 |
EEO_NEXT(); |
| 658 |
} |
| 659 |
|
| 660 |
EEO_CASE(EEOP_SCAN_FETCHSOME) |
| 661 |
{ |
| 662 |
CheckOpSlotCompatibility(op, scanslot); |
| 663 |
|
| 664 |
slot_getsomeattrs(scanslot, op->d.fetch.last_var); |
| 665 |
|
| 666 |
EEO_NEXT(); |
| 667 |
} |
| 668 |
|
| 669 |
EEO_CASE(EEOP_OLD_FETCHSOME) |
| 670 |
{ |
| 671 |
CheckOpSlotCompatibility(op, oldslot); |
| 672 |
|
| 673 |
slot_getsomeattrs(oldslot, op->d.fetch.last_var); |
| 674 |
|
| 675 |
EEO_NEXT(); |
| 676 |
} |
| 677 |
|
| 678 |
EEO_CASE(EEOP_NEW_FETCHSOME) |
| 679 |
{ |
| 680 |
CheckOpSlotCompatibility(op, newslot); |
| 681 |
|
| 682 |
slot_getsomeattrs(newslot, op->d.fetch.last_var); |
| 683 |
|
| 684 |
EEO_NEXT(); |
| 685 |
} |
| 686 |
|
| 687 |
EEO_CASE(EEOP_INNER_VAR) |
| 688 |
{ |
| 689 |
int attnum = op->d.var.attnum; |
| 690 |
|
| 691 |
/* |
| 692 |
* Since we already extracted all referenced columns from the |
| 693 |
* tuple with a FETCHSOME step, we can just grab the value |
| 694 |
* directly out of the slot's decomposed-data arrays. But let's |
| 695 |
* have an Assert to check that that did happen. |
| 696 |
*/ |
| 697 |
Assert(attnum >= 0 && attnum < innerslot->tts_nvalid); |
| 698 |
*op->resvalue = innerslot->tts_values[attnum]; |
| 699 |
*op->resnull = innerslot->tts_isnull[attnum]; |
| 700 |
|
| 701 |
EEO_NEXT(); |
| 702 |
} |
| 703 |
|
| 704 |
EEO_CASE(EEOP_OUTER_VAR) |
| 705 |
{ |
| 706 |
int attnum = op->d.var.attnum; |
| 707 |
|
| 708 |
/* See EEOP_INNER_VAR comments */ |
| 709 |
|
| 710 |
Assert(attnum >= 0 && attnum < outerslot->tts_nvalid); |
| 711 |
*op->resvalue = outerslot->tts_values[attnum]; |
| 712 |
*op->resnull = outerslot->tts_isnull[attnum]; |
| 713 |
|
| 714 |
EEO_NEXT(); |
| 715 |
} |
| 716 |
|
| 717 |
EEO_CASE(EEOP_SCAN_VAR) |
| 718 |
{ |
| 719 |
int attnum = op->d.var.attnum; |
| 720 |
|
| 721 |
/* See EEOP_INNER_VAR comments */ |
| 722 |
|
| 723 |
Assert(attnum >= 0 && attnum < scanslot->tts_nvalid); |
| 724 |
*op->resvalue = scanslot->tts_values[attnum]; |
| 725 |
*op->resnull = scanslot->tts_isnull[attnum]; |
| 726 |
|
| 727 |
EEO_NEXT(); |
| 728 |
} |
| 729 |
|
| 730 |
EEO_CASE(EEOP_OLD_VAR) |
| 731 |
{ |
| 732 |
int attnum = op->d.var.attnum; |
| 733 |
|
| 734 |
/* See EEOP_INNER_VAR comments */ |
| 735 |
|
| 736 |
Assert(attnum >= 0 && attnum < oldslot->tts_nvalid); |
| 737 |
*op->resvalue = oldslot->tts_values[attnum]; |
| 738 |
*op->resnull = oldslot->tts_isnull[attnum]; |
| 739 |
|
| 740 |
EEO_NEXT(); |
| 741 |
} |
| 742 |
|
| 743 |
EEO_CASE(EEOP_NEW_VAR) |
| 744 |
{ |
| 745 |
int attnum = op->d.var.attnum; |
| 746 |
|
| 747 |
/* See EEOP_INNER_VAR comments */ |
| 748 |
|
| 749 |
Assert(attnum >= 0 && attnum < newslot->tts_nvalid); |
| 750 |
*op->resvalue = newslot->tts_values[attnum]; |
| 751 |
*op->resnull = newslot->tts_isnull[attnum]; |
| 752 |
|
| 753 |
EEO_NEXT(); |
| 754 |
} |
| 755 |
|
| 756 |
EEO_CASE(EEOP_INNER_SYSVAR) |
| 757 |
{ |
| 758 |
ExecEvalSysVar(state, op, econtext, innerslot); |
| 759 |
EEO_NEXT(); |
| 760 |
} |
| 761 |
|
| 762 |
EEO_CASE(EEOP_OUTER_SYSVAR) |
| 763 |
{ |
| 764 |
ExecEvalSysVar(state, op, econtext, outerslot); |
| 765 |
EEO_NEXT(); |
| 766 |
} |
| 767 |
|
| 768 |
EEO_CASE(EEOP_SCAN_SYSVAR) |
| 769 |
{ |
| 770 |
ExecEvalSysVar(state, op, econtext, scanslot); |
| 771 |
EEO_NEXT(); |
| 772 |
} |
| 773 |
|
| 774 |
EEO_CASE(EEOP_OLD_SYSVAR) |
| 775 |
{ |
| 776 |
ExecEvalSysVar(state, op, econtext, oldslot); |
| 777 |
EEO_NEXT(); |
| 778 |
} |
| 779 |
|
| 780 |
EEO_CASE(EEOP_NEW_SYSVAR) |
| 781 |
{ |
| 782 |
ExecEvalSysVar(state, op, econtext, newslot); |
| 783 |
EEO_NEXT(); |
| 784 |
} |
| 785 |
|
| 786 |
EEO_CASE(EEOP_WHOLEROW) |
| 787 |
{ |
| 788 |
/* too complex for an inline implementation */ |
| 789 |
ExecEvalWholeRowVar(state, op, econtext); |
| 790 |
|
| 791 |
EEO_NEXT(); |
| 792 |
} |
| 793 |
|
| 794 |
EEO_CASE(EEOP_ASSIGN_INNER_VAR) |
| 795 |
{ |
| 796 |
int resultnum = op->d.assign_var.resultnum; |
| 797 |
int attnum = op->d.assign_var.attnum; |
| 798 |
|
| 799 |
/* |
| 800 |
* We do not need CheckVarSlotCompatibility here; that was taken |
| 801 |
* care of at compilation time. But see EEOP_INNER_VAR comments. |
| 802 |
*/ |
| 803 |
Assert(attnum >= 0 && attnum < innerslot->tts_nvalid); |
| 804 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 805 |
resultslot->tts_values[resultnum] = innerslot->tts_values[attnum]; |
| 806 |
resultslot->tts_isnull[resultnum] = innerslot->tts_isnull[attnum]; |
| 807 |
|
| 808 |
EEO_NEXT(); |
| 809 |
} |
| 810 |
|
| 811 |
EEO_CASE(EEOP_ASSIGN_OUTER_VAR) |
| 812 |
{ |
| 813 |
int resultnum = op->d.assign_var.resultnum; |
| 814 |
int attnum = op->d.assign_var.attnum; |
| 815 |
|
| 816 |
/* |
| 817 |
* We do not need CheckVarSlotCompatibility here; that was taken |
| 818 |
* care of at compilation time. But see EEOP_INNER_VAR comments. |
| 819 |
*/ |
| 820 |
Assert(attnum >= 0 && attnum < outerslot->tts_nvalid); |
| 821 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 822 |
resultslot->tts_values[resultnum] = outerslot->tts_values[attnum]; |
| 823 |
resultslot->tts_isnull[resultnum] = outerslot->tts_isnull[attnum]; |
| 824 |
|
| 825 |
EEO_NEXT(); |
| 826 |
} |
| 827 |
|
| 828 |
EEO_CASE(EEOP_ASSIGN_SCAN_VAR) |
| 829 |
{ |
| 830 |
int resultnum = op->d.assign_var.resultnum; |
| 831 |
int attnum = op->d.assign_var.attnum; |
| 832 |
|
| 833 |
/* |
| 834 |
* We do not need CheckVarSlotCompatibility here; that was taken |
| 835 |
* care of at compilation time. But see EEOP_INNER_VAR comments. |
| 836 |
*/ |
| 837 |
Assert(attnum >= 0 && attnum < scanslot->tts_nvalid); |
| 838 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 839 |
resultslot->tts_values[resultnum] = scanslot->tts_values[attnum]; |
| 840 |
resultslot->tts_isnull[resultnum] = scanslot->tts_isnull[attnum]; |
| 841 |
|
| 842 |
EEO_NEXT(); |
| 843 |
} |
| 844 |
|
| 845 |
EEO_CASE(EEOP_ASSIGN_OLD_VAR) |
| 846 |
{ |
| 847 |
int resultnum = op->d.assign_var.resultnum; |
| 848 |
int attnum = op->d.assign_var.attnum; |
| 849 |
|
| 850 |
/* |
| 851 |
* We do not need CheckVarSlotCompatibility here; that was taken |
| 852 |
* care of at compilation time. But see EEOP_INNER_VAR comments. |
| 853 |
*/ |
| 854 |
Assert(attnum >= 0 && attnum < oldslot->tts_nvalid); |
| 855 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 856 |
resultslot->tts_values[resultnum] = oldslot->tts_values[attnum]; |
| 857 |
resultslot->tts_isnull[resultnum] = oldslot->tts_isnull[attnum]; |
| 858 |
|
| 859 |
EEO_NEXT(); |
| 860 |
} |
| 861 |
|
| 862 |
EEO_CASE(EEOP_ASSIGN_NEW_VAR) |
| 863 |
{ |
| 864 |
int resultnum = op->d.assign_var.resultnum; |
| 865 |
int attnum = op->d.assign_var.attnum; |
| 866 |
|
| 867 |
/* |
| 868 |
* We do not need CheckVarSlotCompatibility here; that was taken |
| 869 |
* care of at compilation time. But see EEOP_INNER_VAR comments. |
| 870 |
*/ |
| 871 |
Assert(attnum >= 0 && attnum < newslot->tts_nvalid); |
| 872 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 873 |
resultslot->tts_values[resultnum] = newslot->tts_values[attnum]; |
| 874 |
resultslot->tts_isnull[resultnum] = newslot->tts_isnull[attnum]; |
| 875 |
|
| 876 |
EEO_NEXT(); |
| 877 |
} |
| 878 |
|
| 879 |
EEO_CASE(EEOP_ASSIGN_TMP) |
| 880 |
{ |
| 881 |
int resultnum = op->d.assign_tmp.resultnum; |
| 882 |
|
| 883 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 884 |
resultslot->tts_values[resultnum] = state->resvalue; |
| 885 |
resultslot->tts_isnull[resultnum] = state->resnull; |
| 886 |
|
| 887 |
EEO_NEXT(); |
| 888 |
} |
| 889 |
|
| 890 |
EEO_CASE(EEOP_ASSIGN_TMP_MAKE_RO) |
| 891 |
{ |
| 892 |
int resultnum = op->d.assign_tmp.resultnum; |
| 893 |
|
| 894 |
Assert(resultnum >= 0 && resultnum < resultslot->tts_tupleDescriptor->natts); |
| 895 |
resultslot->tts_isnull[resultnum] = state->resnull; |
| 896 |
if (!resultslot->tts_isnull[resultnum]) |
| 897 |
resultslot->tts_values[resultnum] = |
| 898 |
MakeExpandedObjectReadOnlyInternal(state->resvalue); |
| 899 |
else |
| 900 |
resultslot->tts_values[resultnum] = state->resvalue; |
| 901 |
|
| 902 |
EEO_NEXT(); |
| 903 |
} |
| 904 |
|
| 905 |
EEO_CASE(EEOP_CONST) |
| 906 |
{ |
| 907 |
*op->resnull = op->d.constval.isnull; |
| 908 |
*op->resvalue = op->d.constval.value; |
| 909 |
|
| 910 |
EEO_NEXT(); |
| 911 |
} |
| 912 |
|
| 913 |
/* |
| 914 |
* Function-call implementations. Arguments have previously been |
| 915 |
* evaluated directly into fcinfo->args. |
| 916 |
* |
| 917 |
* As both STRICT checks and function-usage are noticeable performance |
| 918 |
* wise, and function calls are a very hot-path (they also back |
| 919 |
* operators!), it's worth having so many separate opcodes. |
| 920 |
* |
| 921 |
* Note: the reason for using a temporary variable "d", here and in |
| 922 |
* other places, is that some compilers think "*op->resvalue = f();" |
| 923 |
* requires them to evaluate op->resvalue into a register before |
| 924 |
* calling f(), just in case f() is able to modify op->resvalue |
| 925 |
* somehow. The extra line of code can save a useless register spill |
| 926 |
* and reload across the function call. |
| 927 |
*/ |
| 928 |
EEO_CASE(EEOP_FUNCEXPR) |
| 929 |
{ |
| 930 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 931 |
Datum d; |
| 932 |
|
| 933 |
fcinfo->isnull = false; |
| 934 |
d = op->d.func.fn_addr(fcinfo); |
| 935 |
*op->resvalue = d; |
| 936 |
*op->resnull = fcinfo->isnull; |
| 937 |
|
| 938 |
EEO_NEXT(); |
| 939 |
} |
| 940 |
|
| 941 |
/* strict function call with more than two arguments */ |
| 942 |
EEO_CASE(EEOP_FUNCEXPR_STRICT) |
| 943 |
{ |
| 944 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 945 |
NullableDatum *args = fcinfo->args; |
| 946 |
int nargs = op->d.func.nargs; |
| 947 |
Datum d; |
| 948 |
|
| 949 |
Assert(nargs > 2); |
| 950 |
|
| 951 |
/* strict function, so check for NULL args */ |
| 952 |
for (int argno = 0; argno < nargs; argno++) |
| 953 |
{ |
| 954 |
if (args[argno].isnull) |
| 955 |
{ |
| 956 |
*op->resnull = true; |
| 957 |
goto strictfail; |
| 958 |
} |
| 959 |
} |
| 960 |
fcinfo->isnull = false; |
| 961 |
d = op->d.func.fn_addr(fcinfo); |
| 962 |
*op->resvalue = d; |
| 963 |
*op->resnull = fcinfo->isnull; |
| 964 |
|
| 965 |
strictfail: |
| 966 |
EEO_NEXT(); |
| 967 |
} |
| 968 |
|
| 969 |
/* strict function call with one argument */ |
| 970 |
EEO_CASE(EEOP_FUNCEXPR_STRICT_1) |
| 971 |
{ |
| 972 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 973 |
NullableDatum *args = fcinfo->args; |
| 974 |
|
| 975 |
Assert(op->d.func.nargs == 1); |
| 976 |
|
| 977 |
/* strict function, so check for NULL args */ |
| 978 |
if (args[0].isnull) |
| 979 |
*op->resnull = true; |
| 980 |
else |
| 981 |
{ |
| 982 |
Datum d; |
| 983 |
|
| 984 |
fcinfo->isnull = false; |
| 985 |
d = op->d.func.fn_addr(fcinfo); |
| 986 |
*op->resvalue = d; |
| 987 |
*op->resnull = fcinfo->isnull; |
| 988 |
} |
| 989 |
|
| 990 |
EEO_NEXT(); |
| 991 |
} |
| 992 |
|
| 993 |
/* strict function call with two arguments */ |
| 994 |
EEO_CASE(EEOP_FUNCEXPR_STRICT_2) |
| 995 |
{ |
| 996 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 997 |
NullableDatum *args = fcinfo->args; |
| 998 |
|
| 999 |
Assert(op->d.func.nargs == 2); |
| 1000 |
|
| 1001 |
/* strict function, so check for NULL args */ |
| 1002 |
if (args[0].isnull || args[1].isnull) |
| 1003 |
*op->resnull = true; |
| 1004 |
else |
| 1005 |
{ |
| 1006 |
Datum d; |
| 1007 |
|
| 1008 |
fcinfo->isnull = false; |
| 1009 |
d = op->d.func.fn_addr(fcinfo); |
| 1010 |
*op->resvalue = d; |
| 1011 |
*op->resnull = fcinfo->isnull; |
| 1012 |
} |
| 1013 |
|
| 1014 |
EEO_NEXT(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
EEO_CASE(EEOP_FUNCEXPR_FUSAGE) |
| 1018 |
{ |
| 1019 |
/* not common enough to inline */ |
| 1020 |
ExecEvalFuncExprFusage(state, op, econtext); |
| 1021 |
|
| 1022 |
EEO_NEXT(); |
| 1023 |
} |
| 1024 |
|
| 1025 |
EEO_CASE(EEOP_FUNCEXPR_STRICT_FUSAGE) |
| 1026 |
{ |
| 1027 |
/* not common enough to inline */ |
| 1028 |
ExecEvalFuncExprStrictFusage(state, op, econtext); |
| 1029 |
|
| 1030 |
EEO_NEXT(); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* If any of its clauses is FALSE, an AND's result is FALSE regardless |
| 1035 |
* of the states of the rest of the clauses, so we can stop evaluating |
| 1036 |
* and return FALSE immediately. If none are FALSE and one or more is |
| 1037 |
* NULL, we return NULL; otherwise we return TRUE. This makes sense |
| 1038 |
* when you interpret NULL as "don't know": perhaps one of the "don't |
| 1039 |
* knows" would have been FALSE if we'd known its value. Only when |
| 1040 |
* all the inputs are known to be TRUE can we state confidently that |
| 1041 |
* the AND's result is TRUE. |
| 1042 |
*/ |
| 1043 |
EEO_CASE(EEOP_BOOL_AND_STEP_FIRST) |
| 1044 |
{ |
| 1045 |
*op->d.boolexpr.anynull = false; |
| 1046 |
|
| 1047 |
/* |
| 1048 |
* EEOP_BOOL_AND_STEP_FIRST resets anynull, otherwise it's the |
| 1049 |
* same as EEOP_BOOL_AND_STEP - so fall through to that. |
| 1050 |
*/ |
| 1051 |
|
| 1052 |
/* FALL THROUGH */ |
| 1053 |
} |
| 1054 |
|
| 1055 |
EEO_CASE(EEOP_BOOL_AND_STEP) |
| 1056 |
{ |
| 1057 |
if (*op->resnull) |
| 1058 |
{ |
| 1059 |
*op->d.boolexpr.anynull = true; |
| 1060 |
} |
| 1061 |
else if (!DatumGetBool(*op->resvalue)) |
| 1062 |
{ |
| 1063 |
/* result is already set to FALSE, need not change it */ |
| 1064 |
/* bail out early */ |
| 1065 |
EEO_JUMP(op->d.boolexpr.jumpdone); |
| 1066 |
} |
| 1067 |
|
| 1068 |
EEO_NEXT(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
EEO_CASE(EEOP_BOOL_AND_STEP_LAST) |
| 1072 |
{ |
| 1073 |
if (*op->resnull) |
| 1074 |
{ |
| 1075 |
/* result is already set to NULL, need not change it */ |
| 1076 |
} |
| 1077 |
else if (!DatumGetBool(*op->resvalue)) |
| 1078 |
{ |
| 1079 |
/* result is already set to FALSE, need not change it */ |
| 1080 |
|
| 1081 |
/* |
| 1082 |
* No point jumping early to jumpdone - would be same target |
| 1083 |
* (as this is the last argument to the AND expression), |
| 1084 |
* except more expensive. |
| 1085 |
*/ |
| 1086 |
} |
| 1087 |
else if (*op->d.boolexpr.anynull) |
| 1088 |
{ |
| 1089 |
*op->resvalue = (Datum) 0; |
| 1090 |
*op->resnull = true; |
| 1091 |
} |
| 1092 |
else |
| 1093 |
{ |
| 1094 |
/* result is already set to TRUE, need not change it */ |
| 1095 |
} |
| 1096 |
|
| 1097 |
EEO_NEXT(); |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* |
| 1101 |
* If any of its clauses is TRUE, an OR's result is TRUE regardless of |
| 1102 |
* the states of the rest of the clauses, so we can stop evaluating |
| 1103 |
* and return TRUE immediately. If none are TRUE and one or more is |
| 1104 |
* NULL, we return NULL; otherwise we return FALSE. This makes sense |
| 1105 |
* when you interpret NULL as "don't know": perhaps one of the "don't |
| 1106 |
* knows" would have been TRUE if we'd known its value. Only when all |
| 1107 |
* the inputs are known to be FALSE can we state confidently that the |
| 1108 |
* OR's result is FALSE. |
| 1109 |
*/ |
| 1110 |
EEO_CASE(EEOP_BOOL_OR_STEP_FIRST) |
| 1111 |
{ |
| 1112 |
*op->d.boolexpr.anynull = false; |
| 1113 |
|
| 1114 |
/* |
| 1115 |
* EEOP_BOOL_OR_STEP_FIRST resets anynull, otherwise it's the same |
| 1116 |
* as EEOP_BOOL_OR_STEP - so fall through to that. |
| 1117 |
*/ |
| 1118 |
|
| 1119 |
/* FALL THROUGH */ |
| 1120 |
} |
| 1121 |
|
| 1122 |
EEO_CASE(EEOP_BOOL_OR_STEP) |
| 1123 |
{ |
| 1124 |
if (*op->resnull) |
| 1125 |
{ |
| 1126 |
*op->d.boolexpr.anynull = true; |
| 1127 |
} |
| 1128 |
else if (DatumGetBool(*op->resvalue)) |
| 1129 |
{ |
| 1130 |
/* result is already set to TRUE, need not change it */ |
| 1131 |
/* bail out early */ |
| 1132 |
EEO_JUMP(op->d.boolexpr.jumpdone); |
| 1133 |
} |
| 1134 |
|
| 1135 |
EEO_NEXT(); |
| 1136 |
} |
| 1137 |
|
| 1138 |
EEO_CASE(EEOP_BOOL_OR_STEP_LAST) |
| 1139 |
{ |
| 1140 |
if (*op->resnull) |
| 1141 |
{ |
| 1142 |
/* result is already set to NULL, need not change it */ |
| 1143 |
} |
| 1144 |
else if (DatumGetBool(*op->resvalue)) |
| 1145 |
{ |
| 1146 |
/* result is already set to TRUE, need not change it */ |
| 1147 |
|
| 1148 |
/* |
| 1149 |
* No point jumping to jumpdone - would be same target (as |
| 1150 |
* this is the last argument to the AND expression), except |
| 1151 |
* more expensive. |
| 1152 |
*/ |
| 1153 |
} |
| 1154 |
else if (*op->d.boolexpr.anynull) |
| 1155 |
{ |
| 1156 |
*op->resvalue = (Datum) 0; |
| 1157 |
*op->resnull = true; |
| 1158 |
} |
| 1159 |
else |
| 1160 |
{ |
| 1161 |
/* result is already set to FALSE, need not change it */ |
| 1162 |
} |
| 1163 |
|
| 1164 |
EEO_NEXT(); |
| 1165 |
} |
| 1166 |
|
| 1167 |
EEO_CASE(EEOP_BOOL_NOT_STEP) |
| 1168 |
{ |
| 1169 |
/* |
| 1170 |
* Evaluation of 'not' is simple... if expr is false, then return |
| 1171 |
* 'true' and vice versa. It's safe to do this even on a |
| 1172 |
* nominally null value, so we ignore resnull; that means that |
| 1173 |
* NULL in produces NULL out, which is what we want. |
| 1174 |
*/ |
| 1175 |
*op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
| 1176 |
|
| 1177 |
EEO_NEXT(); |
| 1178 |
} |
| 1179 |
|
| 1180 |
EEO_CASE(EEOP_QUAL) |
| 1181 |
{ |
| 1182 |
/* simplified version of BOOL_AND_STEP for use by ExecQual() */ |
| 1183 |
|
| 1184 |
/* If argument (also result) is false or null ... */ |
| 1185 |
if (*op->resnull || |
| 1186 |
!DatumGetBool(*op->resvalue)) |
| 1187 |
{ |
| 1188 |
/* ... bail out early, returning FALSE */ |
| 1189 |
*op->resnull = false; |
| 1190 |
*op->resvalue = BoolGetDatum(false); |
| 1191 |
EEO_JUMP(op->d.qualexpr.jumpdone); |
| 1192 |
} |
| 1193 |
|
| 1194 |
/* |
| 1195 |
* Otherwise, leave the TRUE value in place, in case this is the |
| 1196 |
* last qual. Then, TRUE is the correct answer. |
| 1197 |
*/ |
| 1198 |
|
| 1199 |
EEO_NEXT(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
EEO_CASE(EEOP_JUMP) |
| 1203 |
{ |
| 1204 |
/* Unconditionally jump to target step */ |
| 1205 |
EEO_JUMP(op->d.jump.jumpdone); |
| 1206 |
} |
| 1207 |
|
| 1208 |
EEO_CASE(EEOP_JUMP_IF_NULL) |
| 1209 |
{ |
| 1210 |
/* Transfer control if current result is null */ |
| 1211 |
if (*op->resnull) |
| 1212 |
EEO_JUMP(op->d.jump.jumpdone); |
| 1213 |
|
| 1214 |
EEO_NEXT(); |
| 1215 |
} |
| 1216 |
|
| 1217 |
EEO_CASE(EEOP_JUMP_IF_NOT_NULL) |
| 1218 |
{ |
| 1219 |
/* Transfer control if current result is non-null */ |
| 1220 |
if (!*op->resnull) |
| 1221 |
EEO_JUMP(op->d.jump.jumpdone); |
| 1222 |
|
| 1223 |
EEO_NEXT(); |
| 1224 |
} |
| 1225 |
|
| 1226 |
EEO_CASE(EEOP_JUMP_IF_NOT_TRUE) |
| 1227 |
{ |
| 1228 |
/* Transfer control if current result is null or false */ |
| 1229 |
if (*op->resnull || !DatumGetBool(*op->resvalue)) |
| 1230 |
EEO_JUMP(op->d.jump.jumpdone); |
| 1231 |
|
| 1232 |
EEO_NEXT(); |
| 1233 |
} |
| 1234 |
|
| 1235 |
EEO_CASE(EEOP_NULLTEST_ISNULL) |
| 1236 |
{ |
| 1237 |
*op->resvalue = BoolGetDatum(*op->resnull); |
| 1238 |
*op->resnull = false; |
| 1239 |
|
| 1240 |
EEO_NEXT(); |
| 1241 |
} |
| 1242 |
|
| 1243 |
EEO_CASE(EEOP_NULLTEST_ISNOTNULL) |
| 1244 |
{ |
| 1245 |
*op->resvalue = BoolGetDatum(!*op->resnull); |
| 1246 |
*op->resnull = false; |
| 1247 |
|
| 1248 |
EEO_NEXT(); |
| 1249 |
} |
| 1250 |
|
| 1251 |
EEO_CASE(EEOP_NULLTEST_ROWISNULL) |
| 1252 |
{ |
| 1253 |
/* out of line implementation: too large */ |
| 1254 |
ExecEvalRowNull(state, op, econtext); |
| 1255 |
|
| 1256 |
EEO_NEXT(); |
| 1257 |
} |
| 1258 |
|
| 1259 |
EEO_CASE(EEOP_NULLTEST_ROWISNOTNULL) |
| 1260 |
{ |
| 1261 |
/* out of line implementation: too large */ |
| 1262 |
ExecEvalRowNotNull(state, op, econtext); |
| 1263 |
|
| 1264 |
EEO_NEXT(); |
| 1265 |
} |
| 1266 |
|
| 1267 |
/* BooleanTest implementations for all booltesttypes */ |
| 1268 |
|
| 1269 |
EEO_CASE(EEOP_BOOLTEST_IS_TRUE) |
| 1270 |
{ |
| 1271 |
if (*op->resnull) |
| 1272 |
{ |
| 1273 |
*op->resvalue = BoolGetDatum(false); |
| 1274 |
*op->resnull = false; |
| 1275 |
} |
| 1276 |
/* else, input value is the correct output as well */ |
| 1277 |
|
| 1278 |
EEO_NEXT(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
EEO_CASE(EEOP_BOOLTEST_IS_NOT_TRUE) |
| 1282 |
{ |
| 1283 |
if (*op->resnull) |
| 1284 |
{ |
| 1285 |
*op->resvalue = BoolGetDatum(true); |
| 1286 |
*op->resnull = false; |
| 1287 |
} |
| 1288 |
else |
| 1289 |
*op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
| 1290 |
|
| 1291 |
EEO_NEXT(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
EEO_CASE(EEOP_BOOLTEST_IS_FALSE) |
| 1295 |
{ |
| 1296 |
if (*op->resnull) |
| 1297 |
{ |
| 1298 |
*op->resvalue = BoolGetDatum(false); |
| 1299 |
*op->resnull = false; |
| 1300 |
} |
| 1301 |
else |
| 1302 |
*op->resvalue = BoolGetDatum(!DatumGetBool(*op->resvalue)); |
| 1303 |
|
| 1304 |
EEO_NEXT(); |
| 1305 |
} |
| 1306 |
|
| 1307 |
EEO_CASE(EEOP_BOOLTEST_IS_NOT_FALSE) |
| 1308 |
{ |
| 1309 |
if (*op->resnull) |
| 1310 |
{ |
| 1311 |
*op->resvalue = BoolGetDatum(true); |
| 1312 |
*op->resnull = false; |
| 1313 |
} |
| 1314 |
/* else, input value is the correct output as well */ |
| 1315 |
|
| 1316 |
EEO_NEXT(); |
| 1317 |
} |
| 1318 |
|
| 1319 |
EEO_CASE(EEOP_PARAM_EXEC) |
| 1320 |
{ |
| 1321 |
/* out of line implementation: too large */ |
| 1322 |
ExecEvalParamExec(state, op, econtext); |
| 1323 |
|
| 1324 |
EEO_NEXT(); |
| 1325 |
} |
| 1326 |
|
| 1327 |
EEO_CASE(EEOP_PARAM_EXTERN) |
| 1328 |
{ |
| 1329 |
/* out of line implementation: too large */ |
| 1330 |
ExecEvalParamExtern(state, op, econtext); |
| 1331 |
EEO_NEXT(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
EEO_CASE(EEOP_PARAM_CALLBACK) |
| 1335 |
{ |
| 1336 |
/* allow an extension module to supply a PARAM_EXTERN value */ |
| 1337 |
op->d.cparam.paramfunc(state, op, econtext); |
| 1338 |
EEO_NEXT(); |
| 1339 |
} |
| 1340 |
|
| 1341 |
EEO_CASE(EEOP_PARAM_SET) |
| 1342 |
{ |
| 1343 |
/* out of line, unlikely to matter performance-wise */ |
| 1344 |
ExecEvalParamSet(state, op, econtext); |
| 1345 |
EEO_NEXT(); |
| 1346 |
} |
| 1347 |
|
| 1348 |
EEO_CASE(EEOP_CASE_TESTVAL) |
| 1349 |
{ |
| 1350 |
*op->resvalue = *op->d.casetest.value; |
| 1351 |
*op->resnull = *op->d.casetest.isnull; |
| 1352 |
|
| 1353 |
EEO_NEXT(); |
| 1354 |
} |
| 1355 |
|
| 1356 |
EEO_CASE(EEOP_CASE_TESTVAL_EXT) |
| 1357 |
{ |
| 1358 |
*op->resvalue = econtext->caseValue_datum; |
| 1359 |
*op->resnull = econtext->caseValue_isNull; |
| 1360 |
|
| 1361 |
EEO_NEXT(); |
| 1362 |
} |
| 1363 |
|
| 1364 |
EEO_CASE(EEOP_MAKE_READONLY) |
| 1365 |
{ |
| 1366 |
/* |
| 1367 |
* Force a varlena value that might be read multiple times to R/O |
| 1368 |
*/ |
| 1369 |
if (!*op->d.make_readonly.isnull) |
| 1370 |
*op->resvalue = |
| 1371 |
MakeExpandedObjectReadOnlyInternal(*op->d.make_readonly.value); |
| 1372 |
*op->resnull = *op->d.make_readonly.isnull; |
| 1373 |
|
| 1374 |
EEO_NEXT(); |
| 1375 |
} |
| 1376 |
|
| 1377 |
EEO_CASE(EEOP_IOCOERCE) |
| 1378 |
{ |
| 1379 |
/* |
| 1380 |
* Evaluate a CoerceViaIO node. This can be quite a hot path, so |
| 1381 |
* inline as much work as possible. The source value is in our |
| 1382 |
* result variable. |
| 1383 |
* |
| 1384 |
* Also look at ExecEvalCoerceViaIOSafe() if you change anything |
| 1385 |
* here. |
| 1386 |
*/ |
| 1387 |
char *str; |
| 1388 |
|
| 1389 |
/* call output function (similar to OutputFunctionCall) */ |
| 1390 |
if (*op->resnull) |
| 1391 |
{ |
| 1392 |
/* output functions are not called on nulls */ |
| 1393 |
str = NULL; |
| 1394 |
} |
| 1395 |
else |
| 1396 |
{ |
| 1397 |
FunctionCallInfo fcinfo_out; |
| 1398 |
|
| 1399 |
fcinfo_out = op->d.iocoerce.fcinfo_data_out; |
| 1400 |
fcinfo_out->args[0].value = *op->resvalue; |
| 1401 |
fcinfo_out->args[0].isnull = false; |
| 1402 |
|
| 1403 |
fcinfo_out->isnull = false; |
| 1404 |
str = DatumGetCString(FunctionCallInvoke(fcinfo_out)); |
| 1405 |
|
| 1406 |
/* OutputFunctionCall assumes result isn't null */ |
| 1407 |
Assert(!fcinfo_out->isnull); |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* call input function (similar to InputFunctionCall) */ |
| 1411 |
if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL) |
| 1412 |
{ |
| 1413 |
FunctionCallInfo fcinfo_in; |
| 1414 |
Datum d; |
| 1415 |
|
| 1416 |
fcinfo_in = op->d.iocoerce.fcinfo_data_in; |
| 1417 |
fcinfo_in->args[0].value = PointerGetDatum(str); |
| 1418 |
fcinfo_in->args[0].isnull = *op->resnull; |
| 1419 |
/* second and third arguments are already set up */ |
| 1420 |
|
| 1421 |
fcinfo_in->isnull = false; |
| 1422 |
d = FunctionCallInvoke(fcinfo_in); |
| 1423 |
*op->resvalue = d; |
| 1424 |
|
| 1425 |
/* Should get null result if and only if str is NULL */ |
| 1426 |
if (str == NULL) |
| 1427 |
{ |
| 1428 |
Assert(*op->resnull); |
| 1429 |
Assert(fcinfo_in->isnull); |
| 1430 |
} |
| 1431 |
else |
| 1432 |
{ |
| 1433 |
Assert(!*op->resnull); |
| 1434 |
Assert(!fcinfo_in->isnull); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
EEO_NEXT(); |
| 1439 |
} |
| 1440 |
|
| 1441 |
EEO_CASE(EEOP_IOCOERCE_SAFE) |
| 1442 |
{ |
| 1443 |
ExecEvalCoerceViaIOSafe(state, op); |
| 1444 |
EEO_NEXT(); |
| 1445 |
} |
| 1446 |
|
| 1447 |
EEO_CASE(EEOP_DISTINCT) |
| 1448 |
{ |
| 1449 |
/* |
| 1450 |
* IS DISTINCT FROM must evaluate arguments (already done into |
| 1451 |
* fcinfo->args) to determine whether they are NULL; if either is |
| 1452 |
* NULL then the result is determined. If neither is NULL, then |
| 1453 |
* proceed to evaluate the comparison function, which is just the |
| 1454 |
* type's standard equality operator. We need not care whether |
| 1455 |
* that function is strict. Because the handling of nulls is |
| 1456 |
* different, we can't just reuse EEOP_FUNCEXPR. |
| 1457 |
*/ |
| 1458 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 1459 |
|
| 1460 |
/* check function arguments for NULLness */ |
| 1461 |
if (fcinfo->args[0].isnull && fcinfo->args[1].isnull) |
| 1462 |
{ |
| 1463 |
/* Both NULL? Then is not distinct... */ |
| 1464 |
*op->resvalue = BoolGetDatum(false); |
| 1465 |
*op->resnull = false; |
| 1466 |
} |
| 1467 |
else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull) |
| 1468 |
{ |
| 1469 |
/* Only one is NULL? Then is distinct... */ |
| 1470 |
*op->resvalue = BoolGetDatum(true); |
| 1471 |
*op->resnull = false; |
| 1472 |
} |
| 1473 |
else |
| 1474 |
{ |
| 1475 |
/* Neither null, so apply the equality function */ |
| 1476 |
Datum eqresult; |
| 1477 |
|
| 1478 |
fcinfo->isnull = false; |
| 1479 |
eqresult = op->d.func.fn_addr(fcinfo); |
| 1480 |
/* Must invert result of "="; safe to do even if null */ |
| 1481 |
*op->resvalue = BoolGetDatum(!DatumGetBool(eqresult)); |
| 1482 |
*op->resnull = fcinfo->isnull; |
| 1483 |
} |
| 1484 |
|
| 1485 |
EEO_NEXT(); |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* see EEOP_DISTINCT for comments, this is just inverted */ |
| 1489 |
EEO_CASE(EEOP_NOT_DISTINCT) |
| 1490 |
{ |
| 1491 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 1492 |
|
| 1493 |
if (fcinfo->args[0].isnull && fcinfo->args[1].isnull) |
| 1494 |
{ |
| 1495 |
*op->resvalue = BoolGetDatum(true); |
| 1496 |
*op->resnull = false; |
| 1497 |
} |
| 1498 |
else if (fcinfo->args[0].isnull || fcinfo->args[1].isnull) |
| 1499 |
{ |
| 1500 |
*op->resvalue = BoolGetDatum(false); |
| 1501 |
*op->resnull = false; |
| 1502 |
} |
| 1503 |
else |
| 1504 |
{ |
| 1505 |
Datum eqresult; |
| 1506 |
|
| 1507 |
fcinfo->isnull = false; |
| 1508 |
eqresult = op->d.func.fn_addr(fcinfo); |
| 1509 |
*op->resvalue = eqresult; |
| 1510 |
*op->resnull = fcinfo->isnull; |
| 1511 |
} |
| 1512 |
|
| 1513 |
EEO_NEXT(); |
| 1514 |
} |
| 1515 |
|
| 1516 |
EEO_CASE(EEOP_NULLIF) |
| 1517 |
{ |
| 1518 |
/* |
| 1519 |
* The arguments are already evaluated into fcinfo->args. |
| 1520 |
*/ |
| 1521 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 1522 |
Datum save_arg0 = fcinfo->args[0].value; |
| 1523 |
|
| 1524 |
/* if either argument is NULL they can't be equal */ |
| 1525 |
if (!fcinfo->args[0].isnull && !fcinfo->args[1].isnull) |
| 1526 |
{ |
| 1527 |
Datum result; |
| 1528 |
|
| 1529 |
/* |
| 1530 |
* If first argument is of varlena type, it might be an |
| 1531 |
* expanded datum. We need to ensure that the value passed to |
| 1532 |
* the comparison function is a read-only pointer. However, |
| 1533 |
* if we end by returning the first argument, that will be the |
| 1534 |
* original read-write pointer if it was read-write. |
| 1535 |
*/ |
| 1536 |
if (op->d.func.make_ro) |
| 1537 |
fcinfo->args[0].value = |
| 1538 |
MakeExpandedObjectReadOnlyInternal(save_arg0); |
| 1539 |
|
| 1540 |
fcinfo->isnull = false; |
| 1541 |
result = op->d.func.fn_addr(fcinfo); |
| 1542 |
|
| 1543 |
/* if the arguments are equal return null */ |
| 1544 |
if (!fcinfo->isnull && DatumGetBool(result)) |
| 1545 |
{ |
| 1546 |
*op->resvalue = (Datum) 0; |
| 1547 |
*op->resnull = true; |
| 1548 |
|
| 1549 |
EEO_NEXT(); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
/* Arguments aren't equal, so return the first one */ |
| 1554 |
*op->resvalue = save_arg0; |
| 1555 |
*op->resnull = fcinfo->args[0].isnull; |
| 1556 |
|
| 1557 |
EEO_NEXT(); |
| 1558 |
} |
| 1559 |
|
| 1560 |
EEO_CASE(EEOP_SQLVALUEFUNCTION) |
| 1561 |
{ |
| 1562 |
/* |
| 1563 |
* Doesn't seem worthwhile to have an inline implementation |
| 1564 |
* efficiency-wise. |
| 1565 |
*/ |
| 1566 |
ExecEvalSQLValueFunction(state, op); |
| 1567 |
|
| 1568 |
EEO_NEXT(); |
| 1569 |
} |
| 1570 |
|
| 1571 |
EEO_CASE(EEOP_CURRENTOFEXPR) |
| 1572 |
{ |
| 1573 |
/* error invocation uses space, and shouldn't ever occur */ |
| 1574 |
ExecEvalCurrentOfExpr(state, op); |
| 1575 |
|
| 1576 |
EEO_NEXT(); |
| 1577 |
} |
| 1578 |
|
| 1579 |
EEO_CASE(EEOP_NEXTVALUEEXPR) |
| 1580 |
{ |
| 1581 |
/* |
| 1582 |
* Doesn't seem worthwhile to have an inline implementation |
| 1583 |
* efficiency-wise. |
| 1584 |
*/ |
| 1585 |
ExecEvalNextValueExpr(state, op); |
| 1586 |
|
| 1587 |
EEO_NEXT(); |
| 1588 |
} |
| 1589 |
|
| 1590 |
EEO_CASE(EEOP_RETURNINGEXPR) |
| 1591 |
{ |
| 1592 |
/* |
| 1593 |
* The next op actually evaluates the expression. If the OLD/NEW |
| 1594 |
* row doesn't exist, skip that and return NULL. |
| 1595 |
*/ |
| 1596 |
if (state->flags & op->d.returningexpr.nullflag) |
| 1597 |
{ |
| 1598 |
*op->resvalue = (Datum) 0; |
| 1599 |
*op->resnull = true; |
| 1600 |
|
| 1601 |
EEO_JUMP(op->d.returningexpr.jumpdone); |
| 1602 |
} |
| 1603 |
|
| 1604 |
EEO_NEXT(); |
| 1605 |
} |
| 1606 |
|
| 1607 |
EEO_CASE(EEOP_ARRAYEXPR) |
| 1608 |
{ |
| 1609 |
/* too complex for an inline implementation */ |
| 1610 |
ExecEvalArrayExpr(state, op); |
| 1611 |
|
| 1612 |
EEO_NEXT(); |
| 1613 |
} |
| 1614 |
|
| 1615 |
EEO_CASE(EEOP_ARRAYCOERCE) |
| 1616 |
{ |
| 1617 |
/* too complex for an inline implementation */ |
| 1618 |
ExecEvalArrayCoerce(state, op, econtext); |
| 1619 |
|
| 1620 |
EEO_NEXT(); |
| 1621 |
} |
| 1622 |
|
| 1623 |
EEO_CASE(EEOP_ROW) |
| 1624 |
{ |
| 1625 |
/* too complex for an inline implementation */ |
| 1626 |
ExecEvalRow(state, op); |
| 1627 |
|
| 1628 |
EEO_NEXT(); |
| 1629 |
} |
| 1630 |
|
| 1631 |
EEO_CASE(EEOP_ROWCOMPARE_STEP) |
| 1632 |
{ |
| 1633 |
FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data; |
| 1634 |
Datum d; |
| 1635 |
|
| 1636 |
/* force NULL result if strict fn and NULL input */ |
| 1637 |
if (op->d.rowcompare_step.finfo->fn_strict && |
| 1638 |
(fcinfo->args[0].isnull || fcinfo->args[1].isnull)) |
| 1639 |
{ |
| 1640 |
*op->resnull = true; |
| 1641 |
EEO_JUMP(op->d.rowcompare_step.jumpnull); |
| 1642 |
} |
| 1643 |
|
| 1644 |
/* Apply comparison function */ |
| 1645 |
fcinfo->isnull = false; |
| 1646 |
d = op->d.rowcompare_step.fn_addr(fcinfo); |
| 1647 |
*op->resvalue = d; |
| 1648 |
|
| 1649 |
/* force NULL result if NULL function result */ |
| 1650 |
if (fcinfo->isnull) |
| 1651 |
{ |
| 1652 |
*op->resnull = true; |
| 1653 |
EEO_JUMP(op->d.rowcompare_step.jumpnull); |
| 1654 |
} |
| 1655 |
*op->resnull = false; |
| 1656 |
|
| 1657 |
/* If unequal, no need to compare remaining columns */ |
| 1658 |
if (DatumGetInt32(*op->resvalue) != 0) |
| 1659 |
{ |
| 1660 |
EEO_JUMP(op->d.rowcompare_step.jumpdone); |
| 1661 |
} |
| 1662 |
|
| 1663 |
EEO_NEXT(); |
| 1664 |
} |
| 1665 |
|
| 1666 |
EEO_CASE(EEOP_ROWCOMPARE_FINAL) |
| 1667 |
{ |
| 1668 |
int32 cmpresult = DatumGetInt32(*op->resvalue); |
| 1669 |
CompareType cmptype = op->d.rowcompare_final.cmptype; |
| 1670 |
|
| 1671 |
*op->resnull = false; |
| 1672 |
switch (cmptype) |
| 1673 |
{ |
| 1674 |
/* EQ and NE cases aren't allowed here */ |
| 1675 |
case COMPARE_LT: |
| 1676 |
*op->resvalue = BoolGetDatum(cmpresult < 0); |
| 1677 |
break; |
| 1678 |
case COMPARE_LE: |
| 1679 |
*op->resvalue = BoolGetDatum(cmpresult <= 0); |
| 1680 |
break; |
| 1681 |
case COMPARE_GE: |
| 1682 |
*op->resvalue = BoolGetDatum(cmpresult >= 0); |
| 1683 |
break; |
| 1684 |
case COMPARE_GT: |
| 1685 |
*op->resvalue = BoolGetDatum(cmpresult > 0); |
| 1686 |
break; |
| 1687 |
default: |
| 1688 |
Assert(false); |
| 1689 |
break; |
| 1690 |
} |
| 1691 |
|
| 1692 |
EEO_NEXT(); |
| 1693 |
} |
| 1694 |
|
| 1695 |
EEO_CASE(EEOP_MINMAX) |
| 1696 |
{ |
| 1697 |
/* too complex for an inline implementation */ |
| 1698 |
ExecEvalMinMax(state, op); |
| 1699 |
|
| 1700 |
EEO_NEXT(); |
| 1701 |
} |
| 1702 |
|
| 1703 |
EEO_CASE(EEOP_FIELDSELECT) |
| 1704 |
{ |
| 1705 |
/* too complex for an inline implementation */ |
| 1706 |
ExecEvalFieldSelect(state, op, econtext); |
| 1707 |
|
| 1708 |
EEO_NEXT(); |
| 1709 |
} |
| 1710 |
|
| 1711 |
EEO_CASE(EEOP_FIELDSTORE_DEFORM) |
| 1712 |
{ |
| 1713 |
/* too complex for an inline implementation */ |
| 1714 |
ExecEvalFieldStoreDeForm(state, op, econtext); |
| 1715 |
|
| 1716 |
EEO_NEXT(); |
| 1717 |
} |
| 1718 |
|
| 1719 |
EEO_CASE(EEOP_FIELDSTORE_FORM) |
| 1720 |
{ |
| 1721 |
/* too complex for an inline implementation */ |
| 1722 |
ExecEvalFieldStoreForm(state, op, econtext); |
| 1723 |
|
| 1724 |
EEO_NEXT(); |
| 1725 |
} |
| 1726 |
|
| 1727 |
EEO_CASE(EEOP_SBSREF_SUBSCRIPTS) |
| 1728 |
{ |
| 1729 |
/* Precheck SubscriptingRef subscript(s) */ |
| 1730 |
if (op->d.sbsref_subscript.subscriptfunc(state, op, econtext)) |
| 1731 |
{ |
| 1732 |
EEO_NEXT(); |
| 1733 |
} |
| 1734 |
else |
| 1735 |
{ |
| 1736 |
/* Subscript is null, short-circuit SubscriptingRef to NULL */ |
| 1737 |
EEO_JUMP(op->d.sbsref_subscript.jumpdone); |
| 1738 |
} |
| 1739 |
} |
| 1740 |
|
| 1741 |
EEO_CASE(EEOP_SBSREF_OLD) |
| 1742 |
EEO_CASE(EEOP_SBSREF_ASSIGN) |
| 1743 |
EEO_CASE(EEOP_SBSREF_FETCH) |
| 1744 |
{ |
| 1745 |
/* Perform a SubscriptingRef fetch or assignment */ |
| 1746 |
op->d.sbsref.subscriptfunc(state, op, econtext); |
| 1747 |
|
| 1748 |
EEO_NEXT(); |
| 1749 |
} |
| 1750 |
|
| 1751 |
EEO_CASE(EEOP_CONVERT_ROWTYPE) |
| 1752 |
{ |
| 1753 |
/* too complex for an inline implementation */ |
| 1754 |
ExecEvalConvertRowtype(state, op, econtext); |
| 1755 |
|
| 1756 |
EEO_NEXT(); |
| 1757 |
} |
| 1758 |
|
| 1759 |
EEO_CASE(EEOP_SCALARARRAYOP) |
| 1760 |
{ |
| 1761 |
/* too complex for an inline implementation */ |
| 1762 |
ExecEvalScalarArrayOp(state, op); |
| 1763 |
|
| 1764 |
EEO_NEXT(); |
| 1765 |
} |
| 1766 |
|
| 1767 |
EEO_CASE(EEOP_HASHED_SCALARARRAYOP) |
| 1768 |
{ |
| 1769 |
/* too complex for an inline implementation */ |
| 1770 |
ExecEvalHashedScalarArrayOp(state, op, econtext); |
| 1771 |
|
| 1772 |
EEO_NEXT(); |
| 1773 |
} |
| 1774 |
|
| 1775 |
EEO_CASE(EEOP_DOMAIN_TESTVAL) |
| 1776 |
{ |
| 1777 |
*op->resvalue = *op->d.casetest.value; |
| 1778 |
*op->resnull = *op->d.casetest.isnull; |
| 1779 |
|
| 1780 |
EEO_NEXT(); |
| 1781 |
} |
| 1782 |
|
| 1783 |
EEO_CASE(EEOP_DOMAIN_TESTVAL_EXT) |
| 1784 |
{ |
| 1785 |
*op->resvalue = econtext->domainValue_datum; |
| 1786 |
*op->resnull = econtext->domainValue_isNull; |
| 1787 |
|
| 1788 |
EEO_NEXT(); |
| 1789 |
} |
| 1790 |
|
| 1791 |
EEO_CASE(EEOP_DOMAIN_NOTNULL) |
| 1792 |
{ |
| 1793 |
/* too complex for an inline implementation */ |
| 1794 |
ExecEvalConstraintNotNull(state, op); |
| 1795 |
|
| 1796 |
EEO_NEXT(); |
| 1797 |
} |
| 1798 |
|
| 1799 |
EEO_CASE(EEOP_DOMAIN_CHECK) |
| 1800 |
{ |
| 1801 |
/* too complex for an inline implementation */ |
| 1802 |
ExecEvalConstraintCheck(state, op); |
| 1803 |
|
| 1804 |
EEO_NEXT(); |
| 1805 |
} |
| 1806 |
|
| 1807 |
EEO_CASE(EEOP_HASHDATUM_SET_INITVAL) |
| 1808 |
{ |
| 1809 |
*op->resvalue = op->d.hashdatum_initvalue.init_value; |
| 1810 |
*op->resnull = false; |
| 1811 |
|
| 1812 |
EEO_NEXT(); |
| 1813 |
} |
| 1814 |
|
| 1815 |
EEO_CASE(EEOP_HASHDATUM_FIRST) |
| 1816 |
{ |
| 1817 |
FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
| 1818 |
|
| 1819 |
/* |
| 1820 |
* Save the Datum on non-null inputs, otherwise store 0 so that |
| 1821 |
* subsequent NEXT32 operations combine with an initialized value. |
| 1822 |
*/ |
| 1823 |
if (!fcinfo->args[0].isnull) |
| 1824 |
*op->resvalue = op->d.hashdatum.fn_addr(fcinfo); |
| 1825 |
else |
| 1826 |
*op->resvalue = (Datum) 0; |
| 1827 |
|
| 1828 |
*op->resnull = false; |
| 1829 |
|
| 1830 |
EEO_NEXT(); |
| 1831 |
} |
| 1832 |
|
| 1833 |
EEO_CASE(EEOP_HASHDATUM_FIRST_STRICT) |
| 1834 |
{ |
| 1835 |
FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
| 1836 |
|
| 1837 |
if (fcinfo->args[0].isnull) |
| 1838 |
{ |
| 1839 |
/* |
| 1840 |
* With strict we have the expression return NULL instead of |
| 1841 |
* ignoring NULL input values. We've nothing more to do after |
| 1842 |
* finding a NULL. |
| 1843 |
*/ |
| 1844 |
state->resnull = true; |
| 1845 |
state->resvalue = (Datum) 0; |
| 1846 |
EEO_JUMP(op->d.hashdatum.jumpdone); |
| 1847 |
} |
| 1848 |
|
| 1849 |
/* execute the hash function and save the resulting value */ |
| 1850 |
*op->resvalue = op->d.hashdatum.fn_addr(fcinfo); |
| 1851 |
*op->resnull = false; |
| 1852 |
|
| 1853 |
EEO_NEXT(); |
| 1854 |
} |
| 1855 |
|
| 1856 |
EEO_CASE(EEOP_HASHDATUM_NEXT32) |
| 1857 |
{ |
| 1858 |
FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
| 1859 |
uint32 existinghash; |
| 1860 |
|
| 1861 |
existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value); |
| 1862 |
/* combine successive hash values by rotating */ |
| 1863 |
existinghash = pg_rotate_left32(existinghash, 1); |
| 1864 |
|
| 1865 |
/* leave the hash value alone on NULL inputs */ |
| 1866 |
if (!fcinfo->args[0].isnull) |
| 1867 |
{ |
| 1868 |
uint32 hashvalue; |
| 1869 |
|
| 1870 |
/* execute hash func and combine with previous hash value */ |
| 1871 |
hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo)); |
| 1872 |
existinghash = existinghash ^ hashvalue; |
| 1873 |
} |
| 1874 |
|
| 1875 |
*op->resvalue = UInt32GetDatum(existinghash); |
| 1876 |
*op->resnull = false; |
| 1877 |
|
| 1878 |
EEO_NEXT(); |
| 1879 |
} |
| 1880 |
|
| 1881 |
EEO_CASE(EEOP_HASHDATUM_NEXT32_STRICT) |
| 1882 |
{ |
| 1883 |
FunctionCallInfo fcinfo = op->d.hashdatum.fcinfo_data; |
| 1884 |
|
| 1885 |
if (fcinfo->args[0].isnull) |
| 1886 |
{ |
| 1887 |
/* |
| 1888 |
* With strict we have the expression return NULL instead of |
| 1889 |
* ignoring NULL input values. We've nothing more to do after |
| 1890 |
* finding a NULL. |
| 1891 |
*/ |
| 1892 |
state->resnull = true; |
| 1893 |
state->resvalue = (Datum) 0; |
| 1894 |
EEO_JUMP(op->d.hashdatum.jumpdone); |
| 1895 |
} |
| 1896 |
else |
| 1897 |
{ |
| 1898 |
uint32 existinghash; |
| 1899 |
uint32 hashvalue; |
| 1900 |
|
| 1901 |
existinghash = DatumGetUInt32(op->d.hashdatum.iresult->value); |
| 1902 |
/* combine successive hash values by rotating */ |
| 1903 |
existinghash = pg_rotate_left32(existinghash, 1); |
| 1904 |
|
| 1905 |
/* execute hash func and combine with previous hash value */ |
| 1906 |
hashvalue = DatumGetUInt32(op->d.hashdatum.fn_addr(fcinfo)); |
| 1907 |
*op->resvalue = UInt32GetDatum(existinghash ^ hashvalue); |
| 1908 |
*op->resnull = false; |
| 1909 |
} |
| 1910 |
|
| 1911 |
EEO_NEXT(); |
| 1912 |
} |
| 1913 |
|
| 1914 |
EEO_CASE(EEOP_XMLEXPR) |
| 1915 |
{ |
| 1916 |
/* too complex for an inline implementation */ |
| 1917 |
ExecEvalXmlExpr(state, op); |
| 1918 |
|
| 1919 |
EEO_NEXT(); |
| 1920 |
} |
| 1921 |
|
| 1922 |
EEO_CASE(EEOP_JSON_CONSTRUCTOR) |
| 1923 |
{ |
| 1924 |
/* too complex for an inline implementation */ |
| 1925 |
ExecEvalJsonConstructor(state, op, econtext); |
| 1926 |
EEO_NEXT(); |
| 1927 |
} |
| 1928 |
|
| 1929 |
EEO_CASE(EEOP_IS_JSON) |
| 1930 |
{ |
| 1931 |
/* too complex for an inline implementation */ |
| 1932 |
ExecEvalJsonIsPredicate(state, op); |
| 1933 |
|
| 1934 |
EEO_NEXT(); |
| 1935 |
} |
| 1936 |
|
| 1937 |
EEO_CASE(EEOP_JSONEXPR_PATH) |
| 1938 |
{ |
| 1939 |
/* too complex for an inline implementation */ |
| 1940 |
EEO_JUMP(ExecEvalJsonExprPath(state, op, econtext)); |
| 1941 |
} |
| 1942 |
|
| 1943 |
EEO_CASE(EEOP_JSONEXPR_COERCION) |
| 1944 |
{ |
| 1945 |
/* too complex for an inline implementation */ |
| 1946 |
ExecEvalJsonCoercion(state, op, econtext); |
| 1947 |
|
| 1948 |
EEO_NEXT(); |
| 1949 |
} |
| 1950 |
|
| 1951 |
EEO_CASE(EEOP_JSONEXPR_COERCION_FINISH) |
| 1952 |
{ |
| 1953 |
/* too complex for an inline implementation */ |
| 1954 |
ExecEvalJsonCoercionFinish(state, op); |
| 1955 |
|
| 1956 |
EEO_NEXT(); |
| 1957 |
} |
| 1958 |
|
| 1959 |
EEO_CASE(EEOP_AGGREF) |
| 1960 |
{ |
| 1961 |
/* |
| 1962 |
* Returns a Datum whose value is the precomputed aggregate value |
| 1963 |
* found in the given expression context. |
| 1964 |
*/ |
| 1965 |
int aggno = op->d.aggref.aggno; |
| 1966 |
|
| 1967 |
Assert(econtext->ecxt_aggvalues != NULL); |
| 1968 |
|
| 1969 |
*op->resvalue = econtext->ecxt_aggvalues[aggno]; |
| 1970 |
*op->resnull = econtext->ecxt_aggnulls[aggno]; |
| 1971 |
|
| 1972 |
EEO_NEXT(); |
| 1973 |
} |
| 1974 |
|
| 1975 |
EEO_CASE(EEOP_GROUPING_FUNC) |
| 1976 |
{ |
| 1977 |
/* too complex/uncommon for an inline implementation */ |
| 1978 |
ExecEvalGroupingFunc(state, op); |
| 1979 |
|
| 1980 |
EEO_NEXT(); |
| 1981 |
} |
| 1982 |
|
| 1983 |
EEO_CASE(EEOP_WINDOW_FUNC) |
| 1984 |
{ |
| 1985 |
/* |
| 1986 |
* Like Aggref, just return a precomputed value from the econtext. |
| 1987 |
*/ |
| 1988 |
WindowFuncExprState *wfunc = op->d.window_func.wfstate; |
| 1989 |
|
| 1990 |
Assert(econtext->ecxt_aggvalues != NULL); |
| 1991 |
|
| 1992 |
*op->resvalue = econtext->ecxt_aggvalues[wfunc->wfuncno]; |
| 1993 |
*op->resnull = econtext->ecxt_aggnulls[wfunc->wfuncno]; |
| 1994 |
|
| 1995 |
EEO_NEXT(); |
| 1996 |
} |
| 1997 |
|
| 1998 |
EEO_CASE(EEOP_MERGE_SUPPORT_FUNC) |
| 1999 |
{ |
| 2000 |
/* too complex/uncommon for an inline implementation */ |
| 2001 |
ExecEvalMergeSupportFunc(state, op, econtext); |
| 2002 |
|
| 2003 |
EEO_NEXT(); |
| 2004 |
} |
| 2005 |
|
| 2006 |
EEO_CASE(EEOP_SUBPLAN) |
| 2007 |
{ |
| 2008 |
/* too complex for an inline implementation */ |
| 2009 |
ExecEvalSubPlan(state, op, econtext); |
| 2010 |
|
| 2011 |
EEO_NEXT(); |
| 2012 |
} |
| 2013 |
|
| 2014 |
/* evaluate a strict aggregate deserialization function */ |
| 2015 |
EEO_CASE(EEOP_AGG_STRICT_DESERIALIZE) |
| 2016 |
{ |
| 2017 |
/* Don't call a strict deserialization function with NULL input */ |
| 2018 |
if (op->d.agg_deserialize.fcinfo_data->args[0].isnull) |
| 2019 |
EEO_JUMP(op->d.agg_deserialize.jumpnull); |
| 2020 |
|
| 2021 |
/* fallthrough */ |
| 2022 |
} |
| 2023 |
|
| 2024 |
/* evaluate aggregate deserialization function (non-strict portion) */ |
| 2025 |
EEO_CASE(EEOP_AGG_DESERIALIZE) |
| 2026 |
{ |
| 2027 |
FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data; |
| 2028 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2029 |
MemoryContext oldContext; |
| 2030 |
|
| 2031 |
/* |
| 2032 |
* We run the deserialization functions in per-input-tuple memory |
| 2033 |
* context. |
| 2034 |
*/ |
| 2035 |
oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
| 2036 |
fcinfo->isnull = false; |
| 2037 |
*op->resvalue = FunctionCallInvoke(fcinfo); |
| 2038 |
*op->resnull = fcinfo->isnull; |
| 2039 |
MemoryContextSwitchTo(oldContext); |
| 2040 |
|
| 2041 |
EEO_NEXT(); |
| 2042 |
} |
| 2043 |
|
| 2044 |
/* |
| 2045 |
* Check that a strict aggregate transition / combination function's |
| 2046 |
* input is not NULL. |
| 2047 |
*/ |
| 2048 |
|
| 2049 |
/* when checking more than one argument */ |
| 2050 |
EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS) |
| 2051 |
{ |
| 2052 |
NullableDatum *args = op->d.agg_strict_input_check.args; |
| 2053 |
int nargs = op->d.agg_strict_input_check.nargs; |
| 2054 |
|
| 2055 |
Assert(nargs > 1); |
| 2056 |
|
| 2057 |
for (int argno = 0; argno < nargs; argno++) |
| 2058 |
{ |
| 2059 |
if (args[argno].isnull) |
| 2060 |
EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
| 2061 |
} |
| 2062 |
EEO_NEXT(); |
| 2063 |
} |
| 2064 |
|
| 2065 |
/* special case for just one argument */ |
| 2066 |
EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1) |
| 2067 |
{ |
| 2068 |
NullableDatum *args = op->d.agg_strict_input_check.args; |
| 2069 |
PG_USED_FOR_ASSERTS_ONLY int nargs = op->d.agg_strict_input_check.nargs; |
| 2070 |
|
| 2071 |
Assert(nargs == 1); |
| 2072 |
|
| 2073 |
if (args[0].isnull) |
| 2074 |
EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
| 2075 |
EEO_NEXT(); |
| 2076 |
} |
| 2077 |
|
| 2078 |
EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_NULLS) |
| 2079 |
{ |
| 2080 |
bool *nulls = op->d.agg_strict_input_check.nulls; |
| 2081 |
int nargs = op->d.agg_strict_input_check.nargs; |
| 2082 |
|
| 2083 |
for (int argno = 0; argno < nargs; argno++) |
| 2084 |
{ |
| 2085 |
if (nulls[argno]) |
| 2086 |
EEO_JUMP(op->d.agg_strict_input_check.jumpnull); |
| 2087 |
} |
| 2088 |
EEO_NEXT(); |
| 2089 |
} |
| 2090 |
|
| 2091 |
/* |
| 2092 |
* Check for a NULL pointer to the per-group states. |
| 2093 |
*/ |
| 2094 |
|
| 2095 |
EEO_CASE(EEOP_AGG_PLAIN_PERGROUP_NULLCHECK) |
| 2096 |
{ |
| 2097 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2098 |
AggStatePerGroup pergroup_allaggs = |
| 2099 |
aggstate->all_pergroups[op->d.agg_plain_pergroup_nullcheck.setoff]; |
| 2100 |
|
| 2101 |
if (pergroup_allaggs == NULL) |
| 2102 |
EEO_JUMP(op->d.agg_plain_pergroup_nullcheck.jumpnull); |
| 2103 |
|
| 2104 |
EEO_NEXT(); |
| 2105 |
} |
| 2106 |
|
| 2107 |
/* |
| 2108 |
* Different types of aggregate transition functions are implemented |
| 2109 |
* as different types of steps, to avoid incurring unnecessary |
| 2110 |
* overhead. There's a step type for each valid combination of having |
| 2111 |
* a by value / by reference transition type, [not] needing to the |
| 2112 |
* initialize the transition value for the first row in a group from |
| 2113 |
* input, and [not] strict transition function. |
| 2114 |
* |
| 2115 |
* Could optimize further by splitting off by-reference for |
| 2116 |
* fixed-length types, but currently that doesn't seem worth it. |
| 2117 |
*/ |
| 2118 |
|
| 2119 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL) |
| 2120 |
{ |
| 2121 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2122 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2123 |
AggStatePerGroup pergroup = |
| 2124 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2125 |
|
| 2126 |
Assert(pertrans->transtypeByVal); |
| 2127 |
|
| 2128 |
if (pergroup->noTransValue) |
| 2129 |
{ |
| 2130 |
/* If transValue has not yet been initialized, do so now. */ |
| 2131 |
ExecAggInitGroup(aggstate, pertrans, pergroup, |
| 2132 |
op->d.agg_trans.aggcontext); |
| 2133 |
/* copied trans value from input, done this round */ |
| 2134 |
} |
| 2135 |
else if (likely(!pergroup->transValueIsNull)) |
| 2136 |
{ |
| 2137 |
/* invoke transition function, unless prevented by strictness */ |
| 2138 |
ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
| 2139 |
op->d.agg_trans.aggcontext, |
| 2140 |
op->d.agg_trans.setno); |
| 2141 |
} |
| 2142 |
|
| 2143 |
EEO_NEXT(); |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
| 2147 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL) |
| 2148 |
{ |
| 2149 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2150 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2151 |
AggStatePerGroup pergroup = |
| 2152 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2153 |
|
| 2154 |
Assert(pertrans->transtypeByVal); |
| 2155 |
|
| 2156 |
if (likely(!pergroup->transValueIsNull)) |
| 2157 |
ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
| 2158 |
op->d.agg_trans.aggcontext, |
| 2159 |
op->d.agg_trans.setno); |
| 2160 |
|
| 2161 |
EEO_NEXT(); |
| 2162 |
} |
| 2163 |
|
| 2164 |
/* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
| 2165 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYVAL) |
| 2166 |
{ |
| 2167 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2168 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2169 |
AggStatePerGroup pergroup = |
| 2170 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2171 |
|
| 2172 |
Assert(pertrans->transtypeByVal); |
| 2173 |
|
| 2174 |
ExecAggPlainTransByVal(aggstate, pertrans, pergroup, |
| 2175 |
op->d.agg_trans.aggcontext, |
| 2176 |
op->d.agg_trans.setno); |
| 2177 |
|
| 2178 |
EEO_NEXT(); |
| 2179 |
} |
| 2180 |
|
| 2181 |
/* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
| 2182 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF) |
| 2183 |
{ |
| 2184 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2185 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2186 |
AggStatePerGroup pergroup = |
| 2187 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2188 |
|
| 2189 |
Assert(!pertrans->transtypeByVal); |
| 2190 |
|
| 2191 |
if (pergroup->noTransValue) |
| 2192 |
ExecAggInitGroup(aggstate, pertrans, pergroup, |
| 2193 |
op->d.agg_trans.aggcontext); |
| 2194 |
else if (likely(!pergroup->transValueIsNull)) |
| 2195 |
ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
| 2196 |
op->d.agg_trans.aggcontext, |
| 2197 |
op->d.agg_trans.setno); |
| 2198 |
|
| 2199 |
EEO_NEXT(); |
| 2200 |
} |
| 2201 |
|
| 2202 |
/* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
| 2203 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_STRICT_BYREF) |
| 2204 |
{ |
| 2205 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2206 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2207 |
AggStatePerGroup pergroup = |
| 2208 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2209 |
|
| 2210 |
Assert(!pertrans->transtypeByVal); |
| 2211 |
|
| 2212 |
if (likely(!pergroup->transValueIsNull)) |
| 2213 |
ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
| 2214 |
op->d.agg_trans.aggcontext, |
| 2215 |
op->d.agg_trans.setno); |
| 2216 |
EEO_NEXT(); |
| 2217 |
} |
| 2218 |
|
| 2219 |
/* see comments above EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL */ |
| 2220 |
EEO_CASE(EEOP_AGG_PLAIN_TRANS_BYREF) |
| 2221 |
{ |
| 2222 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2223 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 2224 |
AggStatePerGroup pergroup = |
| 2225 |
&aggstate->all_pergroups[op->d.agg_trans.setoff][op->d.agg_trans.transno]; |
| 2226 |
|
| 2227 |
Assert(!pertrans->transtypeByVal); |
| 2228 |
|
| 2229 |
ExecAggPlainTransByRef(aggstate, pertrans, pergroup, |
| 2230 |
op->d.agg_trans.aggcontext, |
| 2231 |
op->d.agg_trans.setno); |
| 2232 |
|
| 2233 |
EEO_NEXT(); |
| 2234 |
} |
| 2235 |
|
| 2236 |
EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_SINGLE) |
| 2237 |
{ |
| 2238 |
AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans; |
| 2239 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2240 |
|
| 2241 |
if (ExecEvalPreOrderedDistinctSingle(aggstate, pertrans)) |
| 2242 |
EEO_NEXT(); |
| 2243 |
else |
| 2244 |
EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct); |
| 2245 |
} |
| 2246 |
|
| 2247 |
EEO_CASE(EEOP_AGG_PRESORTED_DISTINCT_MULTI) |
| 2248 |
{ |
| 2249 |
AggState *aggstate = castNode(AggState, state->parent); |
| 2250 |
AggStatePerTrans pertrans = op->d.agg_presorted_distinctcheck.pertrans; |
| 2251 |
|
| 2252 |
if (ExecEvalPreOrderedDistinctMulti(aggstate, pertrans)) |
| 2253 |
EEO_NEXT(); |
| 2254 |
else |
| 2255 |
EEO_JUMP(op->d.agg_presorted_distinctcheck.jumpdistinct); |
| 2256 |
} |
| 2257 |
|
| 2258 |
/* process single-column ordered aggregate datum */ |
| 2259 |
EEO_CASE(EEOP_AGG_ORDERED_TRANS_DATUM) |
| 2260 |
{ |
| 2261 |
/* too complex for an inline implementation */ |
| 2262 |
ExecEvalAggOrderedTransDatum(state, op, econtext); |
| 2263 |
|
| 2264 |
EEO_NEXT(); |
| 2265 |
} |
| 2266 |
|
| 2267 |
/* process multi-column ordered aggregate tuple */ |
| 2268 |
EEO_CASE(EEOP_AGG_ORDERED_TRANS_TUPLE) |
| 2269 |
{ |
| 2270 |
/* too complex for an inline implementation */ |
| 2271 |
ExecEvalAggOrderedTransTuple(state, op, econtext); |
| 2272 |
|
| 2273 |
EEO_NEXT(); |
| 2274 |
} |
| 2275 |
|
| 2276 |
EEO_CASE(EEOP_LAST) |
| 2277 |
{ |
| 2278 |
/* unreachable */ |
| 2279 |
Assert(false); |
| 2280 |
goto out_error; |
| 2281 |
} |
| 2282 |
} |
| 2283 |
|
| 2284 |
out_error: |
| 2285 |
pg_unreachable(); |
| 2286 |
return (Datum) 0; |
| 2287 |
} |
| 2288 |
|
| 2289 |
/* |
| 2290 |
* Expression evaluation callback that performs extra checks before executing |
| 2291 |
* the expression. Declared extern so other methods of execution can use it |
| 2292 |
* too. |
| 2293 |
*/ |
| 2294 |
Datum |
| 2295 |
ExecInterpExprStillValid(ExprState *state, ExprContext *econtext, bool *isNull) |
| 2296 |
{ |
| 2297 |
/* |
| 2298 |
* First time through, check whether attribute matches Var. Might not be |
| 2299 |
* ok anymore, due to schema changes. |
| 2300 |
*/ |
| 2301 |
CheckExprStillValid(state, econtext); |
| 2302 |
|
| 2303 |
/* skip the check during further executions */ |
| 2304 |
state->evalfunc = (ExprStateEvalFunc) state->evalfunc_private; |
| 2305 |
|
| 2306 |
/* and actually execute */ |
| 2307 |
return state->evalfunc(state, econtext, isNull); |
| 2308 |
} |
| 2309 |
|
| 2310 |
/* |
| 2311 |
* Check that an expression is still valid in the face of potential schema |
| 2312 |
* changes since the plan has been created. |
| 2313 |
*/ |
| 2314 |
void |
| 2315 |
CheckExprStillValid(ExprState *state, ExprContext *econtext) |
| 2316 |
{ |
| 2317 |
TupleTableSlot *innerslot; |
| 2318 |
TupleTableSlot *outerslot; |
| 2319 |
TupleTableSlot *scanslot; |
| 2320 |
TupleTableSlot *oldslot; |
| 2321 |
TupleTableSlot *newslot; |
| 2322 |
|
| 2323 |
innerslot = econtext->ecxt_innertuple; |
| 2324 |
outerslot = econtext->ecxt_outertuple; |
| 2325 |
scanslot = econtext->ecxt_scantuple; |
| 2326 |
oldslot = econtext->ecxt_oldtuple; |
| 2327 |
newslot = econtext->ecxt_newtuple; |
| 2328 |
|
| 2329 |
for (int i = 0; i < state->steps_len; i++) |
| 2330 |
{ |
| 2331 |
ExprEvalStep *op = &state->steps[i]; |
| 2332 |
|
| 2333 |
switch (ExecEvalStepOp(state, op)) |
| 2334 |
{ |
| 2335 |
case EEOP_INNER_VAR: |
| 2336 |
{ |
| 2337 |
int attnum = op->d.var.attnum; |
| 2338 |
|
| 2339 |
CheckVarSlotCompatibility(innerslot, attnum + 1, op->d.var.vartype); |
| 2340 |
break; |
| 2341 |
} |
| 2342 |
|
| 2343 |
case EEOP_OUTER_VAR: |
| 2344 |
{ |
| 2345 |
int attnum = op->d.var.attnum; |
| 2346 |
|
| 2347 |
CheckVarSlotCompatibility(outerslot, attnum + 1, op->d.var.vartype); |
| 2348 |
break; |
| 2349 |
} |
| 2350 |
|
| 2351 |
case EEOP_SCAN_VAR: |
| 2352 |
{ |
| 2353 |
int attnum = op->d.var.attnum; |
| 2354 |
|
| 2355 |
CheckVarSlotCompatibility(scanslot, attnum + 1, op->d.var.vartype); |
| 2356 |
break; |
| 2357 |
} |
| 2358 |
|
| 2359 |
case EEOP_OLD_VAR: |
| 2360 |
{ |
| 2361 |
int attnum = op->d.var.attnum; |
| 2362 |
|
| 2363 |
CheckVarSlotCompatibility(oldslot, attnum + 1, op->d.var.vartype); |
| 2364 |
break; |
| 2365 |
} |
| 2366 |
|
| 2367 |
case EEOP_NEW_VAR: |
| 2368 |
{ |
| 2369 |
int attnum = op->d.var.attnum; |
| 2370 |
|
| 2371 |
CheckVarSlotCompatibility(newslot, attnum + 1, op->d.var.vartype); |
| 2372 |
break; |
| 2373 |
} |
| 2374 |
default: |
| 2375 |
break; |
| 2376 |
} |
| 2377 |
} |
| 2378 |
} |
| 2379 |
|
| 2380 |
/* |
| 2381 |
* Check whether a user attribute in a slot can be referenced by a Var |
| 2382 |
* expression. This should succeed unless there have been schema changes |
| 2383 |
* since the expression tree has been created. |
| 2384 |
*/ |
| 2385 |
static void |
| 2386 |
CheckVarSlotCompatibility(TupleTableSlot *slot, int attnum, Oid vartype) |
| 2387 |
{ |
| 2388 |
/* |
| 2389 |
* What we have to check for here is the possibility of an attribute |
| 2390 |
* having been dropped or changed in type since the plan tree was created. |
| 2391 |
* Ideally the plan will get invalidated and not re-used, but just in |
| 2392 |
* case, we keep these defenses. Fortunately it's sufficient to check |
| 2393 |
* once on the first time through. |
| 2394 |
* |
| 2395 |
* Note: ideally we'd check typmod as well as typid, but that seems |
| 2396 |
* impractical at the moment: in many cases the tupdesc will have been |
| 2397 |
* generated by ExecTypeFromTL(), and that can't guarantee to generate an |
| 2398 |
* accurate typmod in all cases, because some expression node types don't |
| 2399 |
* carry typmod. Fortunately, for precisely that reason, there should be |
| 2400 |
* no places with a critical dependency on the typmod of a value. |
| 2401 |
* |
| 2402 |
* System attributes don't require checking since their types never |
| 2403 |
* change. |
| 2404 |
*/ |
| 2405 |
if (attnum > 0) |
| 2406 |
{ |
| 2407 |
TupleDesc slot_tupdesc = slot->tts_tupleDescriptor; |
| 2408 |
Form_pg_attribute attr; |
| 2409 |
|
| 2410 |
if (attnum > slot_tupdesc->natts) /* should never happen */ |
| 2411 |
elog(ERROR, "attribute number %d exceeds number of columns %d", |
| 2412 |
attnum, slot_tupdesc->natts); |
| 2413 |
|
| 2414 |
attr = TupleDescAttr(slot_tupdesc, attnum - 1); |
| 2415 |
|
| 2416 |
/* Internal error: somebody forgot to expand it. */ |
| 2417 |
if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) |
| 2418 |
elog(ERROR, "unexpected virtual generated column reference"); |
| 2419 |
|
| 2420 |
if (attr->attisdropped) |
| 2421 |
ereport(ERROR, |
| 2422 |
(errcode(ERRCODE_UNDEFINED_COLUMN), |
| 2423 |
errmsg("attribute %d of type %s has been dropped", |
| 2424 |
attnum, format_type_be(slot_tupdesc->tdtypeid)))); |
| 2425 |
|
| 2426 |
if (vartype != attr->atttypid) |
| 2427 |
ereport(ERROR, |
| 2428 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 2429 |
errmsg("attribute %d of type %s has wrong type", |
| 2430 |
attnum, format_type_be(slot_tupdesc->tdtypeid)), |
| 2431 |
errdetail("Table has type %s, but query expects %s.", |
| 2432 |
format_type_be(attr->atttypid), |
| 2433 |
format_type_be(vartype)))); |
| 2434 |
} |
| 2435 |
} |
| 2436 |
|
| 2437 |
/* |
| 2438 |
* Verify that the slot is compatible with a EEOP_*_FETCHSOME operation. |
| 2439 |
*/ |
| 2440 |
static void |
| 2441 |
CheckOpSlotCompatibility(ExprEvalStep *op, TupleTableSlot *slot) |
| 2442 |
{ |
| 2443 |
#ifdef USE_ASSERT_CHECKING |
| 2444 |
/* there's nothing to check */ |
| 2445 |
if (!op->d.fetch.fixed) |
| 2446 |
return; |
| 2447 |
|
| 2448 |
/* |
| 2449 |
* Should probably fixed at some point, but for now it's easier to allow |
| 2450 |
* buffer and heap tuples to be used interchangeably. |
| 2451 |
*/ |
| 2452 |
if (slot->tts_ops == &TTSOpsBufferHeapTuple && |
| 2453 |
op->d.fetch.kind == &TTSOpsHeapTuple) |
| 2454 |
return; |
| 2455 |
if (slot->tts_ops == &TTSOpsHeapTuple && |
| 2456 |
op->d.fetch.kind == &TTSOpsBufferHeapTuple) |
| 2457 |
return; |
| 2458 |
|
| 2459 |
/* |
| 2460 |
* At the moment we consider it OK if a virtual slot is used instead of a |
| 2461 |
* specific type of slot, as a virtual slot never needs to be deformed. |
| 2462 |
*/ |
| 2463 |
if (slot->tts_ops == &TTSOpsVirtual) |
| 2464 |
return; |
| 2465 |
|
| 2466 |
Assert(op->d.fetch.kind == slot->tts_ops); |
| 2467 |
#endif |
| 2468 |
} |
| 2469 |
|
| 2470 |
/* |
| 2471 |
* get_cached_rowtype: utility function to lookup a rowtype tupdesc |
| 2472 |
* |
| 2473 |
* type_id, typmod: identity of the rowtype |
| 2474 |
* rowcache: space for caching identity info |
| 2475 |
* (rowcache->cacheptr must be initialized to NULL) |
| 2476 |
* changed: if not NULL, *changed is set to true on any update |
| 2477 |
* |
| 2478 |
* The returned TupleDesc is not guaranteed pinned; caller must pin it |
| 2479 |
* to use it across any operation that might incur cache invalidation, |
| 2480 |
* including for example detoasting of input tuples. |
| 2481 |
* (The TupleDesc is always refcounted, so just use IncrTupleDescRefCount.) |
| 2482 |
* |
| 2483 |
* NOTE: because composite types can change contents, we must be prepared |
| 2484 |
* to re-do this during any node execution; cannot call just once during |
| 2485 |
* expression initialization. |
| 2486 |
*/ |
| 2487 |
static TupleDesc |
| 2488 |
get_cached_rowtype(Oid type_id, int32 typmod, |
| 2489 |
ExprEvalRowtypeCache *rowcache, |
| 2490 |
bool *changed) |
| 2491 |
{ |
| 2492 |
if (type_id != RECORDOID) |
| 2493 |
{ |
| 2494 |
/* |
| 2495 |
* It's a named composite type, so use the regular typcache. Do a |
| 2496 |
* lookup first time through, or if the composite type changed. Note: |
| 2497 |
* "tupdesc_id == 0" may look redundant, but it protects against the |
| 2498 |
* admittedly-theoretical possibility that type_id was RECORDOID the |
| 2499 |
* last time through, so that the cacheptr isn't TypeCacheEntry *. |
| 2500 |
*/ |
| 2501 |
TypeCacheEntry *typentry = (TypeCacheEntry *) rowcache->cacheptr; |
| 2502 |
|
| 2503 |
if (unlikely(typentry == NULL || |
| 2504 |
rowcache->tupdesc_id == 0 || |
| 2505 |
typentry->tupDesc_identifier != rowcache->tupdesc_id)) |
| 2506 |
{ |
| 2507 |
typentry = lookup_type_cache(type_id, TYPECACHE_TUPDESC); |
| 2508 |
if (typentry->tupDesc == NULL) |
| 2509 |
ereport(ERROR, |
| 2510 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 2511 |
errmsg("type %s is not composite", |
| 2512 |
format_type_be(type_id)))); |
| 2513 |
rowcache->cacheptr = typentry; |
| 2514 |
rowcache->tupdesc_id = typentry->tupDesc_identifier; |
| 2515 |
if (changed) |
| 2516 |
*changed = true; |
| 2517 |
} |
| 2518 |
return typentry->tupDesc; |
| 2519 |
} |
| 2520 |
else |
| 2521 |
{ |
| 2522 |
/* |
| 2523 |
* A RECORD type, once registered, doesn't change for the life of the |
| 2524 |
* backend. So we don't need a typcache entry as such, which is good |
| 2525 |
* because there isn't one. It's possible that the caller is asking |
| 2526 |
* about a different type than before, though. |
| 2527 |
*/ |
| 2528 |
TupleDesc tupDesc = (TupleDesc) rowcache->cacheptr; |
| 2529 |
|
| 2530 |
if (unlikely(tupDesc == NULL || |
| 2531 |
rowcache->tupdesc_id != 0 || |
| 2532 |
type_id != tupDesc->tdtypeid || |
| 2533 |
typmod != tupDesc->tdtypmod)) |
| 2534 |
{ |
| 2535 |
tupDesc = lookup_rowtype_tupdesc(type_id, typmod); |
| 2536 |
/* Drop pin acquired by lookup_rowtype_tupdesc */ |
| 2537 |
ReleaseTupleDesc(tupDesc); |
| 2538 |
rowcache->cacheptr = tupDesc; |
| 2539 |
rowcache->tupdesc_id = 0; /* not a valid value for non-RECORD */ |
| 2540 |
if (changed) |
| 2541 |
*changed = true; |
| 2542 |
} |
| 2543 |
return tupDesc; |
| 2544 |
} |
| 2545 |
} |
| 2546 |
|
| 2547 |
|
| 2548 |
/* |
| 2549 |
* Fast-path functions, for very simple expressions |
| 2550 |
*/ |
| 2551 |
|
| 2552 |
/* implementation of ExecJust(Inner|Outer|Scan)Var */ |
| 2553 |
static pg_always_inline Datum |
| 2554 |
ExecJustVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
| 2555 |
{ |
| 2556 |
ExprEvalStep *op = &state->steps[1]; |
| 2557 |
int attnum = op->d.var.attnum + 1; |
| 2558 |
|
| 2559 |
CheckOpSlotCompatibility(&state->steps[0], slot); |
| 2560 |
|
| 2561 |
/* |
| 2562 |
* Since we use slot_getattr(), we don't need to implement the FETCHSOME |
| 2563 |
* step explicitly, and we also needn't Assert that the attnum is in range |
| 2564 |
* --- slot_getattr() will take care of any problems. |
| 2565 |
*/ |
| 2566 |
return slot_getattr(slot, attnum, isnull); |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* Simple reference to inner Var */ |
| 2570 |
static Datum |
| 2571 |
ExecJustInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2572 |
{ |
| 2573 |
return ExecJustVarImpl(state, econtext->ecxt_innertuple, isnull); |
| 2574 |
} |
| 2575 |
|
| 2576 |
/* Simple reference to outer Var */ |
| 2577 |
static Datum |
| 2578 |
ExecJustOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2579 |
{ |
| 2580 |
return ExecJustVarImpl(state, econtext->ecxt_outertuple, isnull); |
| 2581 |
} |
| 2582 |
|
| 2583 |
/* Simple reference to scan Var */ |
| 2584 |
static Datum |
| 2585 |
ExecJustScanVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2586 |
{ |
| 2587 |
return ExecJustVarImpl(state, econtext->ecxt_scantuple, isnull); |
| 2588 |
} |
| 2589 |
|
| 2590 |
/* implementation of ExecJustAssign(Inner|Outer|Scan)Var */ |
| 2591 |
static pg_always_inline Datum |
| 2592 |
ExecJustAssignVarImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull) |
| 2593 |
{ |
| 2594 |
ExprEvalStep *op = &state->steps[1]; |
| 2595 |
int attnum = op->d.assign_var.attnum + 1; |
| 2596 |
int resultnum = op->d.assign_var.resultnum; |
| 2597 |
TupleTableSlot *outslot = state->resultslot; |
| 2598 |
|
| 2599 |
CheckOpSlotCompatibility(&state->steps[0], inslot); |
| 2600 |
|
| 2601 |
/* |
| 2602 |
* We do not need CheckVarSlotCompatibility here; that was taken care of |
| 2603 |
* at compilation time. |
| 2604 |
* |
| 2605 |
* Since we use slot_getattr(), we don't need to implement the FETCHSOME |
| 2606 |
* step explicitly, and we also needn't Assert that the attnum is in range |
| 2607 |
* --- slot_getattr() will take care of any problems. Nonetheless, check |
| 2608 |
* that resultnum is in range. |
| 2609 |
*/ |
| 2610 |
Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts); |
| 2611 |
outslot->tts_values[resultnum] = |
| 2612 |
slot_getattr(inslot, attnum, &outslot->tts_isnull[resultnum]); |
| 2613 |
return 0; |
| 2614 |
} |
| 2615 |
|
| 2616 |
/* Evaluate inner Var and assign to appropriate column of result tuple */ |
| 2617 |
static Datum |
| 2618 |
ExecJustAssignInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2619 |
{ |
| 2620 |
return ExecJustAssignVarImpl(state, econtext->ecxt_innertuple, isnull); |
| 2621 |
} |
| 2622 |
|
| 2623 |
/* Evaluate outer Var and assign to appropriate column of result tuple */ |
| 2624 |
static Datum |
| 2625 |
ExecJustAssignOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2626 |
{ |
| 2627 |
return ExecJustAssignVarImpl(state, econtext->ecxt_outertuple, isnull); |
| 2628 |
} |
| 2629 |
|
| 2630 |
/* Evaluate scan Var and assign to appropriate column of result tuple */ |
| 2631 |
static Datum |
| 2632 |
ExecJustAssignScanVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2633 |
{ |
| 2634 |
return ExecJustAssignVarImpl(state, econtext->ecxt_scantuple, isnull); |
| 2635 |
} |
| 2636 |
|
| 2637 |
/* Evaluate CASE_TESTVAL and apply a strict function to it */ |
| 2638 |
static Datum |
| 2639 |
ExecJustApplyFuncToCase(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2640 |
{ |
| 2641 |
ExprEvalStep *op = &state->steps[0]; |
| 2642 |
FunctionCallInfo fcinfo; |
| 2643 |
NullableDatum *args; |
| 2644 |
int nargs; |
| 2645 |
Datum d; |
| 2646 |
|
| 2647 |
/* |
| 2648 |
* XXX with some redesign of the CaseTestExpr mechanism, maybe we could |
| 2649 |
* get rid of this data shuffling? |
| 2650 |
*/ |
| 2651 |
*op->resvalue = *op->d.casetest.value; |
| 2652 |
*op->resnull = *op->d.casetest.isnull; |
| 2653 |
|
| 2654 |
op++; |
| 2655 |
|
| 2656 |
nargs = op->d.func.nargs; |
| 2657 |
fcinfo = op->d.func.fcinfo_data; |
| 2658 |
args = fcinfo->args; |
| 2659 |
|
| 2660 |
/* strict function, so check for NULL args */ |
| 2661 |
for (int argno = 0; argno < nargs; argno++) |
| 2662 |
{ |
| 2663 |
if (args[argno].isnull) |
| 2664 |
{ |
| 2665 |
*isnull = true; |
| 2666 |
return (Datum) 0; |
| 2667 |
} |
| 2668 |
} |
| 2669 |
fcinfo->isnull = false; |
| 2670 |
d = op->d.func.fn_addr(fcinfo); |
| 2671 |
*isnull = fcinfo->isnull; |
| 2672 |
return d; |
| 2673 |
} |
| 2674 |
|
| 2675 |
/* Simple Const expression */ |
| 2676 |
static Datum |
| 2677 |
ExecJustConst(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2678 |
{ |
| 2679 |
ExprEvalStep *op = &state->steps[0]; |
| 2680 |
|
| 2681 |
*isnull = op->d.constval.isnull; |
| 2682 |
return op->d.constval.value; |
| 2683 |
} |
| 2684 |
|
| 2685 |
/* implementation of ExecJust(Inner|Outer|Scan)VarVirt */ |
| 2686 |
static pg_always_inline Datum |
| 2687 |
ExecJustVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
| 2688 |
{ |
| 2689 |
ExprEvalStep *op = &state->steps[0]; |
| 2690 |
int attnum = op->d.var.attnum; |
| 2691 |
|
| 2692 |
/* |
| 2693 |
* As it is guaranteed that a virtual slot is used, there never is a need |
| 2694 |
* to perform tuple deforming (nor would it be possible). Therefore |
| 2695 |
* execExpr.c has not emitted an EEOP_*_FETCHSOME step. Verify, as much as |
| 2696 |
* possible, that that determination was accurate. |
| 2697 |
*/ |
| 2698 |
Assert(TTS_IS_VIRTUAL(slot)); |
| 2699 |
Assert(TTS_FIXED(slot)); |
| 2700 |
Assert(attnum >= 0 && attnum < slot->tts_nvalid); |
| 2701 |
|
| 2702 |
*isnull = slot->tts_isnull[attnum]; |
| 2703 |
|
| 2704 |
return slot->tts_values[attnum]; |
| 2705 |
} |
| 2706 |
|
| 2707 |
/* Like ExecJustInnerVar, optimized for virtual slots */ |
| 2708 |
static Datum |
| 2709 |
ExecJustInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2710 |
{ |
| 2711 |
return ExecJustVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
| 2712 |
} |
| 2713 |
|
| 2714 |
/* Like ExecJustOuterVar, optimized for virtual slots */ |
| 2715 |
static Datum |
| 2716 |
ExecJustOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2717 |
{ |
| 2718 |
return ExecJustVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
| 2719 |
} |
| 2720 |
|
| 2721 |
/* Like ExecJustScanVar, optimized for virtual slots */ |
| 2722 |
static Datum |
| 2723 |
ExecJustScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2724 |
{ |
| 2725 |
return ExecJustVarVirtImpl(state, econtext->ecxt_scantuple, isnull); |
| 2726 |
} |
| 2727 |
|
| 2728 |
/* implementation of ExecJustAssign(Inner|Outer|Scan)VarVirt */ |
| 2729 |
static pg_always_inline Datum |
| 2730 |
ExecJustAssignVarVirtImpl(ExprState *state, TupleTableSlot *inslot, bool *isnull) |
| 2731 |
{ |
| 2732 |
ExprEvalStep *op = &state->steps[0]; |
| 2733 |
int attnum = op->d.assign_var.attnum; |
| 2734 |
int resultnum = op->d.assign_var.resultnum; |
| 2735 |
TupleTableSlot *outslot = state->resultslot; |
| 2736 |
|
| 2737 |
/* see ExecJustVarVirtImpl for comments */ |
| 2738 |
|
| 2739 |
Assert(TTS_IS_VIRTUAL(inslot)); |
| 2740 |
Assert(TTS_FIXED(inslot)); |
| 2741 |
Assert(attnum >= 0 && attnum < inslot->tts_nvalid); |
| 2742 |
Assert(resultnum >= 0 && resultnum < outslot->tts_tupleDescriptor->natts); |
| 2743 |
|
| 2744 |
outslot->tts_values[resultnum] = inslot->tts_values[attnum]; |
| 2745 |
outslot->tts_isnull[resultnum] = inslot->tts_isnull[attnum]; |
| 2746 |
|
| 2747 |
return 0; |
| 2748 |
} |
| 2749 |
|
| 2750 |
/* Like ExecJustAssignInnerVar, optimized for virtual slots */ |
| 2751 |
static Datum |
| 2752 |
ExecJustAssignInnerVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2753 |
{ |
| 2754 |
return ExecJustAssignVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
| 2755 |
} |
| 2756 |
|
| 2757 |
/* Like ExecJustAssignOuterVar, optimized for virtual slots */ |
| 2758 |
static Datum |
| 2759 |
ExecJustAssignOuterVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2760 |
{ |
| 2761 |
return ExecJustAssignVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
| 2762 |
} |
| 2763 |
|
| 2764 |
/* Like ExecJustAssignScanVar, optimized for virtual slots */ |
| 2765 |
static Datum |
| 2766 |
ExecJustAssignScanVarVirt(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2767 |
{ |
| 2768 |
return ExecJustAssignVarVirtImpl(state, econtext->ecxt_scantuple, isnull); |
| 2769 |
} |
| 2770 |
|
| 2771 |
/* |
| 2772 |
* implementation for hashing an inner Var, seeding with an initial value. |
| 2773 |
*/ |
| 2774 |
static Datum |
| 2775 |
ExecJustHashInnerVarWithIV(ExprState *state, ExprContext *econtext, |
| 2776 |
bool *isnull) |
| 2777 |
{ |
| 2778 |
ExprEvalStep *fetchop = &state->steps[0]; |
| 2779 |
ExprEvalStep *setivop = &state->steps[1]; |
| 2780 |
ExprEvalStep *innervar = &state->steps[2]; |
| 2781 |
ExprEvalStep *hashop = &state->steps[3]; |
| 2782 |
FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
| 2783 |
int attnum = innervar->d.var.attnum; |
| 2784 |
uint32 hashkey; |
| 2785 |
|
| 2786 |
CheckOpSlotCompatibility(fetchop, econtext->ecxt_innertuple); |
| 2787 |
slot_getsomeattrs(econtext->ecxt_innertuple, fetchop->d.fetch.last_var); |
| 2788 |
|
| 2789 |
fcinfo->args[0].value = econtext->ecxt_innertuple->tts_values[attnum]; |
| 2790 |
fcinfo->args[0].isnull = econtext->ecxt_innertuple->tts_isnull[attnum]; |
| 2791 |
|
| 2792 |
hashkey = DatumGetUInt32(setivop->d.hashdatum_initvalue.init_value); |
| 2793 |
hashkey = pg_rotate_left32(hashkey, 1); |
| 2794 |
|
| 2795 |
if (!fcinfo->args[0].isnull) |
| 2796 |
{ |
| 2797 |
uint32 hashvalue; |
| 2798 |
|
| 2799 |
hashvalue = DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo)); |
| 2800 |
hashkey = hashkey ^ hashvalue; |
| 2801 |
} |
| 2802 |
|
| 2803 |
*isnull = false; |
| 2804 |
return UInt32GetDatum(hashkey); |
| 2805 |
} |
| 2806 |
|
| 2807 |
/* implementation of ExecJustHash(Inner|Outer)Var */ |
| 2808 |
static pg_always_inline Datum |
| 2809 |
ExecJustHashVarImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
| 2810 |
{ |
| 2811 |
ExprEvalStep *fetchop = &state->steps[0]; |
| 2812 |
ExprEvalStep *var = &state->steps[1]; |
| 2813 |
ExprEvalStep *hashop = &state->steps[2]; |
| 2814 |
FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
| 2815 |
int attnum = var->d.var.attnum; |
| 2816 |
|
| 2817 |
CheckOpSlotCompatibility(fetchop, slot); |
| 2818 |
slot_getsomeattrs(slot, fetchop->d.fetch.last_var); |
| 2819 |
|
| 2820 |
fcinfo->args[0].value = slot->tts_values[attnum]; |
| 2821 |
fcinfo->args[0].isnull = slot->tts_isnull[attnum]; |
| 2822 |
|
| 2823 |
*isnull = false; |
| 2824 |
|
| 2825 |
if (!fcinfo->args[0].isnull) |
| 2826 |
return DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo)); |
| 2827 |
else |
| 2828 |
return (Datum) 0; |
| 2829 |
} |
| 2830 |
|
| 2831 |
/* implementation for hashing an outer Var */ |
| 2832 |
static Datum |
| 2833 |
ExecJustHashOuterVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2834 |
{ |
| 2835 |
return ExecJustHashVarImpl(state, econtext->ecxt_outertuple, isnull); |
| 2836 |
} |
| 2837 |
|
| 2838 |
/* implementation for hashing an inner Var */ |
| 2839 |
static Datum |
| 2840 |
ExecJustHashInnerVar(ExprState *state, ExprContext *econtext, bool *isnull) |
| 2841 |
{ |
| 2842 |
return ExecJustHashVarImpl(state, econtext->ecxt_innertuple, isnull); |
| 2843 |
} |
| 2844 |
|
| 2845 |
/* implementation of ExecJustHash(Inner|Outer)VarVirt */ |
| 2846 |
static pg_always_inline Datum |
| 2847 |
ExecJustHashVarVirtImpl(ExprState *state, TupleTableSlot *slot, bool *isnull) |
| 2848 |
{ |
| 2849 |
ExprEvalStep *var = &state->steps[0]; |
| 2850 |
ExprEvalStep *hashop = &state->steps[1]; |
| 2851 |
FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
| 2852 |
int attnum = var->d.var.attnum; |
| 2853 |
|
| 2854 |
fcinfo->args[0].value = slot->tts_values[attnum]; |
| 2855 |
fcinfo->args[0].isnull = slot->tts_isnull[attnum]; |
| 2856 |
|
| 2857 |
*isnull = false; |
| 2858 |
|
| 2859 |
if (!fcinfo->args[0].isnull) |
| 2860 |
return DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo)); |
| 2861 |
else |
| 2862 |
return (Datum) 0; |
| 2863 |
} |
| 2864 |
|
| 2865 |
/* Like ExecJustHashInnerVar, optimized for virtual slots */ |
| 2866 |
static Datum |
| 2867 |
ExecJustHashInnerVarVirt(ExprState *state, ExprContext *econtext, |
| 2868 |
bool *isnull) |
| 2869 |
{ |
| 2870 |
return ExecJustHashVarVirtImpl(state, econtext->ecxt_innertuple, isnull); |
| 2871 |
} |
| 2872 |
|
| 2873 |
/* Like ExecJustHashOuterVar, optimized for virtual slots */ |
| 2874 |
static Datum |
| 2875 |
ExecJustHashOuterVarVirt(ExprState *state, ExprContext *econtext, |
| 2876 |
bool *isnull) |
| 2877 |
{ |
| 2878 |
return ExecJustHashVarVirtImpl(state, econtext->ecxt_outertuple, isnull); |
| 2879 |
} |
| 2880 |
|
| 2881 |
/* |
| 2882 |
* implementation for hashing an outer Var. Returns NULL on NULL input. |
| 2883 |
*/ |
| 2884 |
static Datum |
| 2885 |
ExecJustHashOuterVarStrict(ExprState *state, ExprContext *econtext, |
| 2886 |
bool *isnull) |
| 2887 |
{ |
| 2888 |
ExprEvalStep *fetchop = &state->steps[0]; |
| 2889 |
ExprEvalStep *var = &state->steps[1]; |
| 2890 |
ExprEvalStep *hashop = &state->steps[2]; |
| 2891 |
FunctionCallInfo fcinfo = hashop->d.hashdatum.fcinfo_data; |
| 2892 |
int attnum = var->d.var.attnum; |
| 2893 |
|
| 2894 |
CheckOpSlotCompatibility(fetchop, econtext->ecxt_outertuple); |
| 2895 |
slot_getsomeattrs(econtext->ecxt_outertuple, fetchop->d.fetch.last_var); |
| 2896 |
|
| 2897 |
fcinfo->args[0].value = econtext->ecxt_outertuple->tts_values[attnum]; |
| 2898 |
fcinfo->args[0].isnull = econtext->ecxt_outertuple->tts_isnull[attnum]; |
| 2899 |
|
| 2900 |
if (!fcinfo->args[0].isnull) |
| 2901 |
{ |
| 2902 |
*isnull = false; |
| 2903 |
return DatumGetUInt32(hashop->d.hashdatum.fn_addr(fcinfo)); |
| 2904 |
} |
| 2905 |
else |
| 2906 |
{ |
| 2907 |
/* return NULL on NULL input */ |
| 2908 |
*isnull = true; |
| 2909 |
return (Datum) 0; |
| 2910 |
} |
| 2911 |
} |
| 2912 |
|
| 2913 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 2914 |
/* |
| 2915 |
* Comparator used when building address->opcode lookup table for |
| 2916 |
* ExecEvalStepOp() in the threaded dispatch case. |
| 2917 |
*/ |
| 2918 |
static int |
| 2919 |
dispatch_compare_ptr(const void *a, const void *b) |
| 2920 |
{ |
| 2921 |
const ExprEvalOpLookup *la = (const ExprEvalOpLookup *) a; |
| 2922 |
const ExprEvalOpLookup *lb = (const ExprEvalOpLookup *) b; |
| 2923 |
|
| 2924 |
if (la->opcode < lb->opcode) |
| 2925 |
return -1; |
| 2926 |
else if (la->opcode > lb->opcode) |
| 2927 |
return 1; |
| 2928 |
return 0; |
| 2929 |
} |
| 2930 |
#endif |
| 2931 |
|
| 2932 |
/* |
| 2933 |
* Do one-time initialization of interpretation machinery. |
| 2934 |
*/ |
| 2935 |
static void |
| 2936 |
ExecInitInterpreter(void) |
| 2937 |
{ |
| 2938 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 2939 |
/* Set up externally-visible pointer to dispatch table */ |
| 2940 |
if (dispatch_table == NULL) |
| 2941 |
{ |
| 2942 |
dispatch_table = (const void **) |
| 2943 |
DatumGetPointer(ExecInterpExpr(NULL, NULL, NULL)); |
| 2944 |
|
| 2945 |
/* build reverse lookup table */ |
| 2946 |
for (int i = 0; i < EEOP_LAST; i++) |
| 2947 |
{ |
| 2948 |
reverse_dispatch_table[i].opcode = dispatch_table[i]; |
| 2949 |
reverse_dispatch_table[i].op = (ExprEvalOp) i; |
| 2950 |
} |
| 2951 |
|
| 2952 |
/* make it bsearch()able */ |
| 2953 |
qsort(reverse_dispatch_table, |
| 2954 |
EEOP_LAST /* nmembers */ , |
| 2955 |
sizeof(ExprEvalOpLookup), |
| 2956 |
dispatch_compare_ptr); |
| 2957 |
} |
| 2958 |
#endif |
| 2959 |
} |
| 2960 |
|
| 2961 |
/* |
| 2962 |
* Function to return the opcode of an expression step. |
| 2963 |
* |
| 2964 |
* When direct-threading is in use, ExprState->opcode isn't easily |
| 2965 |
* decipherable. This function returns the appropriate enum member. |
| 2966 |
*/ |
| 2967 |
ExprEvalOp |
| 2968 |
ExecEvalStepOp(ExprState *state, ExprEvalStep *op) |
| 2969 |
{ |
| 2970 |
#if defined(EEO_USE_COMPUTED_GOTO) |
| 2971 |
if (state->flags & EEO_FLAG_DIRECT_THREADED) |
| 2972 |
{ |
| 2973 |
ExprEvalOpLookup key; |
| 2974 |
ExprEvalOpLookup *res; |
| 2975 |
|
| 2976 |
key.opcode = (void *) op->opcode; |
| 2977 |
res = bsearch(&key, |
| 2978 |
reverse_dispatch_table, |
| 2979 |
EEOP_LAST /* nmembers */ , |
| 2980 |
sizeof(ExprEvalOpLookup), |
| 2981 |
dispatch_compare_ptr); |
| 2982 |
Assert(res); /* unknown ops shouldn't get looked up */ |
| 2983 |
return res->op; |
| 2984 |
} |
| 2985 |
#endif |
| 2986 |
return (ExprEvalOp) op->opcode; |
| 2987 |
} |
| 2988 |
|
| 2989 |
|
| 2990 |
/* |
| 2991 |
* Out-of-line helper functions for complex instructions. |
| 2992 |
*/ |
| 2993 |
|
| 2994 |
/* |
| 2995 |
* Evaluate EEOP_FUNCEXPR_FUSAGE |
| 2996 |
*/ |
| 2997 |
void |
| 2998 |
ExecEvalFuncExprFusage(ExprState *state, ExprEvalStep *op, |
| 2999 |
ExprContext *econtext) |
| 3000 |
{ |
| 3001 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 3002 |
PgStat_FunctionCallUsage fcusage; |
| 3003 |
Datum d; |
| 3004 |
|
| 3005 |
pgstat_init_function_usage(fcinfo, &fcusage); |
| 3006 |
|
| 3007 |
fcinfo->isnull = false; |
| 3008 |
d = op->d.func.fn_addr(fcinfo); |
| 3009 |
*op->resvalue = d; |
| 3010 |
*op->resnull = fcinfo->isnull; |
| 3011 |
|
| 3012 |
pgstat_end_function_usage(&fcusage, true); |
| 3013 |
} |
| 3014 |
|
| 3015 |
/* |
| 3016 |
* Evaluate EEOP_FUNCEXPR_STRICT_FUSAGE |
| 3017 |
*/ |
| 3018 |
void |
| 3019 |
ExecEvalFuncExprStrictFusage(ExprState *state, ExprEvalStep *op, |
| 3020 |
ExprContext *econtext) |
| 3021 |
{ |
| 3022 |
|
| 3023 |
FunctionCallInfo fcinfo = op->d.func.fcinfo_data; |
| 3024 |
PgStat_FunctionCallUsage fcusage; |
| 3025 |
NullableDatum *args = fcinfo->args; |
| 3026 |
int nargs = op->d.func.nargs; |
| 3027 |
Datum d; |
| 3028 |
|
| 3029 |
/* strict function, so check for NULL args */ |
| 3030 |
for (int argno = 0; argno < nargs; argno++) |
| 3031 |
{ |
| 3032 |
if (args[argno].isnull) |
| 3033 |
{ |
| 3034 |
*op->resnull = true; |
| 3035 |
return; |
| 3036 |
} |
| 3037 |
} |
| 3038 |
|
| 3039 |
pgstat_init_function_usage(fcinfo, &fcusage); |
| 3040 |
|
| 3041 |
fcinfo->isnull = false; |
| 3042 |
d = op->d.func.fn_addr(fcinfo); |
| 3043 |
*op->resvalue = d; |
| 3044 |
*op->resnull = fcinfo->isnull; |
| 3045 |
|
| 3046 |
pgstat_end_function_usage(&fcusage, true); |
| 3047 |
} |
| 3048 |
|
| 3049 |
/* |
| 3050 |
* Evaluate a PARAM_EXEC parameter. |
| 3051 |
* |
| 3052 |
* PARAM_EXEC params (internal executor parameters) are stored in the |
| 3053 |
* ecxt_param_exec_vals array, and can be accessed by array index. |
| 3054 |
*/ |
| 3055 |
void |
| 3056 |
ExecEvalParamExec(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3057 |
{ |
| 3058 |
ParamExecData *prm; |
| 3059 |
|
| 3060 |
prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]); |
| 3061 |
if (unlikely(prm->execPlan != NULL)) |
| 3062 |
{ |
| 3063 |
/* Parameter not evaluated yet, so go do it */ |
| 3064 |
ExecSetParamPlan(prm->execPlan, econtext); |
| 3065 |
/* ExecSetParamPlan should have processed this param... */ |
| 3066 |
Assert(prm->execPlan == NULL); |
| 3067 |
} |
| 3068 |
*op->resvalue = prm->value; |
| 3069 |
*op->resnull = prm->isnull; |
| 3070 |
} |
| 3071 |
|
| 3072 |
/* |
| 3073 |
* Evaluate a PARAM_EXTERN parameter. |
| 3074 |
* |
| 3075 |
* PARAM_EXTERN parameters must be sought in ecxt_param_list_info. |
| 3076 |
*/ |
| 3077 |
void |
| 3078 |
ExecEvalParamExtern(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3079 |
{ |
| 3080 |
ParamListInfo paramInfo = econtext->ecxt_param_list_info; |
| 3081 |
int paramId = op->d.param.paramid; |
| 3082 |
|
| 3083 |
if (likely(paramInfo && |
| 3084 |
paramId > 0 && paramId <= paramInfo->numParams)) |
| 3085 |
{ |
| 3086 |
ParamExternData *prm; |
| 3087 |
ParamExternData prmdata; |
| 3088 |
|
| 3089 |
/* give hook a chance in case parameter is dynamic */ |
| 3090 |
if (paramInfo->paramFetch != NULL) |
| 3091 |
prm = paramInfo->paramFetch(paramInfo, paramId, false, &prmdata); |
| 3092 |
else |
| 3093 |
prm = ¶mInfo->params[paramId - 1]; |
| 3094 |
|
| 3095 |
if (likely(OidIsValid(prm->ptype))) |
| 3096 |
{ |
| 3097 |
/* safety check in case hook did something unexpected */ |
| 3098 |
if (unlikely(prm->ptype != op->d.param.paramtype)) |
| 3099 |
ereport(ERROR, |
| 3100 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 3101 |
errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)", |
| 3102 |
paramId, |
| 3103 |
format_type_be(prm->ptype), |
| 3104 |
format_type_be(op->d.param.paramtype)))); |
| 3105 |
*op->resvalue = prm->value; |
| 3106 |
*op->resnull = prm->isnull; |
| 3107 |
return; |
| 3108 |
} |
| 3109 |
} |
| 3110 |
|
| 3111 |
ereport(ERROR, |
| 3112 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 3113 |
errmsg("no value found for parameter %d", paramId))); |
| 3114 |
} |
| 3115 |
|
| 3116 |
/* |
| 3117 |
* Set value of a param (currently always PARAM_EXEC) from |
| 3118 |
* op->res{value,null}. |
| 3119 |
*/ |
| 3120 |
void |
| 3121 |
ExecEvalParamSet(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3122 |
{ |
| 3123 |
ParamExecData *prm; |
| 3124 |
|
| 3125 |
prm = &(econtext->ecxt_param_exec_vals[op->d.param.paramid]); |
| 3126 |
|
| 3127 |
/* Shouldn't have a pending evaluation anymore */ |
| 3128 |
Assert(prm->execPlan == NULL); |
| 3129 |
|
| 3130 |
prm->value = *op->resvalue; |
| 3131 |
prm->isnull = *op->resnull; |
| 3132 |
} |
| 3133 |
|
| 3134 |
/* |
| 3135 |
* Evaluate a CoerceViaIO node in soft-error mode. |
| 3136 |
* |
| 3137 |
* The source value is in op's result variable. |
| 3138 |
* |
| 3139 |
* Note: This implements EEOP_IOCOERCE_SAFE. If you change anything here, |
| 3140 |
* also look at the inline code for EEOP_IOCOERCE. |
| 3141 |
*/ |
| 3142 |
void |
| 3143 |
ExecEvalCoerceViaIOSafe(ExprState *state, ExprEvalStep *op) |
| 3144 |
{ |
| 3145 |
char *str; |
| 3146 |
|
| 3147 |
/* call output function (similar to OutputFunctionCall) */ |
| 3148 |
if (*op->resnull) |
| 3149 |
{ |
| 3150 |
/* output functions are not called on nulls */ |
| 3151 |
str = NULL; |
| 3152 |
} |
| 3153 |
else |
| 3154 |
{ |
| 3155 |
FunctionCallInfo fcinfo_out; |
| 3156 |
|
| 3157 |
fcinfo_out = op->d.iocoerce.fcinfo_data_out; |
| 3158 |
fcinfo_out->args[0].value = *op->resvalue; |
| 3159 |
fcinfo_out->args[0].isnull = false; |
| 3160 |
|
| 3161 |
fcinfo_out->isnull = false; |
| 3162 |
str = DatumGetCString(FunctionCallInvoke(fcinfo_out)); |
| 3163 |
|
| 3164 |
/* OutputFunctionCall assumes result isn't null */ |
| 3165 |
Assert(!fcinfo_out->isnull); |
| 3166 |
} |
| 3167 |
|
| 3168 |
/* call input function (similar to InputFunctionCallSafe) */ |
| 3169 |
if (!op->d.iocoerce.finfo_in->fn_strict || str != NULL) |
| 3170 |
{ |
| 3171 |
FunctionCallInfo fcinfo_in; |
| 3172 |
|
| 3173 |
fcinfo_in = op->d.iocoerce.fcinfo_data_in; |
| 3174 |
fcinfo_in->args[0].value = PointerGetDatum(str); |
| 3175 |
fcinfo_in->args[0].isnull = *op->resnull; |
| 3176 |
/* second and third arguments are already set up */ |
| 3177 |
|
| 3178 |
/* ErrorSaveContext must be present. */ |
| 3179 |
Assert(IsA(fcinfo_in->context, ErrorSaveContext)); |
| 3180 |
|
| 3181 |
fcinfo_in->isnull = false; |
| 3182 |
*op->resvalue = FunctionCallInvoke(fcinfo_in); |
| 3183 |
|
| 3184 |
if (SOFT_ERROR_OCCURRED(fcinfo_in->context)) |
| 3185 |
{ |
| 3186 |
*op->resnull = true; |
| 3187 |
*op->resvalue = (Datum) 0; |
| 3188 |
return; |
| 3189 |
} |
| 3190 |
|
| 3191 |
/* Should get null result if and only if str is NULL */ |
| 3192 |
if (str == NULL) |
| 3193 |
Assert(*op->resnull); |
| 3194 |
else |
| 3195 |
Assert(!*op->resnull); |
| 3196 |
} |
| 3197 |
} |
| 3198 |
|
| 3199 |
/* |
| 3200 |
* Evaluate a SQLValueFunction expression. |
| 3201 |
*/ |
| 3202 |
void |
| 3203 |
ExecEvalSQLValueFunction(ExprState *state, ExprEvalStep *op) |
| 3204 |
{ |
| 3205 |
LOCAL_FCINFO(fcinfo, 0); |
| 3206 |
SQLValueFunction *svf = op->d.sqlvaluefunction.svf; |
| 3207 |
|
| 3208 |
*op->resnull = false; |
| 3209 |
|
| 3210 |
/* |
| 3211 |
* Note: current_schema() can return NULL. current_user() etc currently |
| 3212 |
* cannot, but might as well code those cases the same way for safety. |
| 3213 |
*/ |
| 3214 |
switch (svf->op) |
| 3215 |
{ |
| 3216 |
case SVFOP_CURRENT_DATE: |
| 3217 |
*op->resvalue = DateADTGetDatum(GetSQLCurrentDate()); |
| 3218 |
break; |
| 3219 |
case SVFOP_CURRENT_TIME: |
| 3220 |
case SVFOP_CURRENT_TIME_N: |
| 3221 |
*op->resvalue = TimeTzADTPGetDatum(GetSQLCurrentTime(svf->typmod)); |
| 3222 |
break; |
| 3223 |
case SVFOP_CURRENT_TIMESTAMP: |
| 3224 |
case SVFOP_CURRENT_TIMESTAMP_N: |
| 3225 |
*op->resvalue = TimestampTzGetDatum(GetSQLCurrentTimestamp(svf->typmod)); |
| 3226 |
break; |
| 3227 |
case SVFOP_LOCALTIME: |
| 3228 |
case SVFOP_LOCALTIME_N: |
| 3229 |
*op->resvalue = TimeADTGetDatum(GetSQLLocalTime(svf->typmod)); |
| 3230 |
break; |
| 3231 |
case SVFOP_LOCALTIMESTAMP: |
| 3232 |
case SVFOP_LOCALTIMESTAMP_N: |
| 3233 |
*op->resvalue = TimestampGetDatum(GetSQLLocalTimestamp(svf->typmod)); |
| 3234 |
break; |
| 3235 |
case SVFOP_CURRENT_ROLE: |
| 3236 |
case SVFOP_CURRENT_USER: |
| 3237 |
case SVFOP_USER: |
| 3238 |
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3239 |
*op->resvalue = current_user(fcinfo); |
| 3240 |
*op->resnull = fcinfo->isnull; |
| 3241 |
break; |
| 3242 |
case SVFOP_SESSION_USER: |
| 3243 |
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3244 |
*op->resvalue = session_user(fcinfo); |
| 3245 |
*op->resnull = fcinfo->isnull; |
| 3246 |
break; |
| 3247 |
case SVFOP_CURRENT_CATALOG: |
| 3248 |
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3249 |
*op->resvalue = current_database(fcinfo); |
| 3250 |
*op->resnull = fcinfo->isnull; |
| 3251 |
break; |
| 3252 |
case SVFOP_CURRENT_SCHEMA: |
| 3253 |
InitFunctionCallInfoData(*fcinfo, NULL, 0, InvalidOid, NULL, NULL); |
| 3254 |
*op->resvalue = current_schema(fcinfo); |
| 3255 |
*op->resnull = fcinfo->isnull; |
| 3256 |
break; |
| 3257 |
} |
| 3258 |
} |
| 3259 |
|
| 3260 |
/* |
| 3261 |
* Raise error if a CURRENT OF expression is evaluated. |
| 3262 |
* |
| 3263 |
* The planner should convert CURRENT OF into a TidScan qualification, or some |
| 3264 |
* other special handling in a ForeignScan node. So we have to be able to do |
| 3265 |
* ExecInitExpr on a CurrentOfExpr, but we shouldn't ever actually execute it. |
| 3266 |
* If we get here, we suppose we must be dealing with CURRENT OF on a foreign |
| 3267 |
* table whose FDW doesn't handle it, and complain accordingly. |
| 3268 |
*/ |
| 3269 |
void |
| 3270 |
ExecEvalCurrentOfExpr(ExprState *state, ExprEvalStep *op) |
| 3271 |
{ |
| 3272 |
ereport(ERROR, |
| 3273 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 3274 |
errmsg("WHERE CURRENT OF is not supported for this table type"))); |
| 3275 |
} |
| 3276 |
|
| 3277 |
/* |
| 3278 |
* Evaluate NextValueExpr. |
| 3279 |
*/ |
| 3280 |
void |
| 3281 |
ExecEvalNextValueExpr(ExprState *state, ExprEvalStep *op) |
| 3282 |
{ |
| 3283 |
int64 newval = nextval_internal(op->d.nextvalueexpr.seqid, false); |
| 3284 |
|
| 3285 |
switch (op->d.nextvalueexpr.seqtypid) |
| 3286 |
{ |
| 3287 |
case INT2OID: |
| 3288 |
*op->resvalue = Int16GetDatum((int16) newval); |
| 3289 |
break; |
| 3290 |
case INT4OID: |
| 3291 |
*op->resvalue = Int32GetDatum((int32) newval); |
| 3292 |
break; |
| 3293 |
case INT8OID: |
| 3294 |
*op->resvalue = Int64GetDatum((int64) newval); |
| 3295 |
break; |
| 3296 |
default: |
| 3297 |
elog(ERROR, "unsupported sequence type %u", |
| 3298 |
op->d.nextvalueexpr.seqtypid); |
| 3299 |
} |
| 3300 |
*op->resnull = false; |
| 3301 |
} |
| 3302 |
|
| 3303 |
/* |
| 3304 |
* Evaluate NullTest / IS NULL for rows. |
| 3305 |
*/ |
| 3306 |
void |
| 3307 |
ExecEvalRowNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3308 |
{ |
| 3309 |
ExecEvalRowNullInt(state, op, econtext, true); |
| 3310 |
} |
| 3311 |
|
| 3312 |
/* |
| 3313 |
* Evaluate NullTest / IS NOT NULL for rows. |
| 3314 |
*/ |
| 3315 |
void |
| 3316 |
ExecEvalRowNotNull(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3317 |
{ |
| 3318 |
ExecEvalRowNullInt(state, op, econtext, false); |
| 3319 |
} |
| 3320 |
|
| 3321 |
/* Common code for IS [NOT] NULL on a row value */ |
| 3322 |
static void |
| 3323 |
ExecEvalRowNullInt(ExprState *state, ExprEvalStep *op, |
| 3324 |
ExprContext *econtext, bool checkisnull) |
| 3325 |
{ |
| 3326 |
Datum value = *op->resvalue; |
| 3327 |
bool isnull = *op->resnull; |
| 3328 |
HeapTupleHeader tuple; |
| 3329 |
Oid tupType; |
| 3330 |
int32 tupTypmod; |
| 3331 |
TupleDesc tupDesc; |
| 3332 |
HeapTupleData tmptup; |
| 3333 |
|
| 3334 |
*op->resnull = false; |
| 3335 |
|
| 3336 |
/* NULL row variables are treated just as NULL scalar columns */ |
| 3337 |
if (isnull) |
| 3338 |
{ |
| 3339 |
*op->resvalue = BoolGetDatum(checkisnull); |
| 3340 |
return; |
| 3341 |
} |
| 3342 |
|
| 3343 |
/* |
| 3344 |
* The SQL standard defines IS [NOT] NULL for a non-null rowtype argument |
| 3345 |
* as: |
| 3346 |
* |
| 3347 |
* "R IS NULL" is true if every field is the null value. |
| 3348 |
* |
| 3349 |
* "R IS NOT NULL" is true if no field is the null value. |
| 3350 |
* |
| 3351 |
* This definition is (apparently intentionally) not recursive; so our |
| 3352 |
* tests on the fields are primitive attisnull tests, not recursive checks |
| 3353 |
* to see if they are all-nulls or no-nulls rowtypes. |
| 3354 |
* |
| 3355 |
* The standard does not consider the possibility of zero-field rows, but |
| 3356 |
* here we consider them to vacuously satisfy both predicates. |
| 3357 |
*/ |
| 3358 |
|
| 3359 |
tuple = DatumGetHeapTupleHeader(value); |
| 3360 |
|
| 3361 |
tupType = HeapTupleHeaderGetTypeId(tuple); |
| 3362 |
tupTypmod = HeapTupleHeaderGetTypMod(tuple); |
| 3363 |
|
| 3364 |
/* Lookup tupdesc if first time through or if type changes */ |
| 3365 |
tupDesc = get_cached_rowtype(tupType, tupTypmod, |
| 3366 |
&op->d.nulltest_row.rowcache, NULL); |
| 3367 |
|
| 3368 |
/* |
| 3369 |
* heap_attisnull needs a HeapTuple not a bare HeapTupleHeader. |
| 3370 |
*/ |
| 3371 |
tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
| 3372 |
tmptup.t_data = tuple; |
| 3373 |
|
| 3374 |
for (int att = 1; att <= tupDesc->natts; att++) |
| 3375 |
{ |
| 3376 |
/* ignore dropped columns */ |
| 3377 |
if (TupleDescCompactAttr(tupDesc, att - 1)->attisdropped) |
| 3378 |
continue; |
| 3379 |
if (heap_attisnull(&tmptup, att, tupDesc)) |
| 3380 |
{ |
| 3381 |
/* null field disproves IS NOT NULL */ |
| 3382 |
if (!checkisnull) |
| 3383 |
{ |
| 3384 |
*op->resvalue = BoolGetDatum(false); |
| 3385 |
return; |
| 3386 |
} |
| 3387 |
} |
| 3388 |
else |
| 3389 |
{ |
| 3390 |
/* non-null field disproves IS NULL */ |
| 3391 |
if (checkisnull) |
| 3392 |
{ |
| 3393 |
*op->resvalue = BoolGetDatum(false); |
| 3394 |
return; |
| 3395 |
} |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
*op->resvalue = BoolGetDatum(true); |
| 3400 |
} |
| 3401 |
|
| 3402 |
/* |
| 3403 |
* Evaluate an ARRAY[] expression. |
| 3404 |
* |
| 3405 |
* The individual array elements (or subarrays) have already been evaluated |
| 3406 |
* into op->d.arrayexpr.elemvalues[]/elemnulls[]. |
| 3407 |
*/ |
| 3408 |
void |
| 3409 |
ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op) |
| 3410 |
{ |
| 3411 |
ArrayType *result; |
| 3412 |
Oid element_type = op->d.arrayexpr.elemtype; |
| 3413 |
int nelems = op->d.arrayexpr.nelems; |
| 3414 |
int ndims = 0; |
| 3415 |
int dims[MAXDIM]; |
| 3416 |
int lbs[MAXDIM]; |
| 3417 |
|
| 3418 |
/* Set non-null as default */ |
| 3419 |
*op->resnull = false; |
| 3420 |
|
| 3421 |
if (!op->d.arrayexpr.multidims) |
| 3422 |
{ |
| 3423 |
/* Elements are presumably of scalar type */ |
| 3424 |
Datum *dvalues = op->d.arrayexpr.elemvalues; |
| 3425 |
bool *dnulls = op->d.arrayexpr.elemnulls; |
| 3426 |
|
| 3427 |
/* setup for 1-D array of the given length */ |
| 3428 |
ndims = 1; |
| 3429 |
dims[0] = nelems; |
| 3430 |
lbs[0] = 1; |
| 3431 |
|
| 3432 |
result = construct_md_array(dvalues, dnulls, ndims, dims, lbs, |
| 3433 |
element_type, |
| 3434 |
op->d.arrayexpr.elemlength, |
| 3435 |
op->d.arrayexpr.elembyval, |
| 3436 |
op->d.arrayexpr.elemalign); |
| 3437 |
} |
| 3438 |
else |
| 3439 |
{ |
| 3440 |
/* Must be nested array expressions */ |
| 3441 |
int nbytes = 0; |
| 3442 |
int nitems; |
| 3443 |
int outer_nelems = 0; |
| 3444 |
int elem_ndims = 0; |
| 3445 |
int *elem_dims = NULL; |
| 3446 |
int *elem_lbs = NULL; |
| 3447 |
bool firstone = true; |
| 3448 |
bool havenulls = false; |
| 3449 |
bool haveempty = false; |
| 3450 |
char **subdata; |
| 3451 |
bits8 **subbitmaps; |
| 3452 |
int *subbytes; |
| 3453 |
int *subnitems; |
| 3454 |
int32 dataoffset; |
| 3455 |
char *dat; |
| 3456 |
int iitem; |
| 3457 |
|
| 3458 |
subdata = (char **) palloc(nelems * sizeof(char *)); |
| 3459 |
subbitmaps = (bits8 **) palloc(nelems * sizeof(bits8 *)); |
| 3460 |
subbytes = (int *) palloc(nelems * sizeof(int)); |
| 3461 |
subnitems = (int *) palloc(nelems * sizeof(int)); |
| 3462 |
|
| 3463 |
/* loop through and get data area from each element */ |
| 3464 |
for (int elemoff = 0; elemoff < nelems; elemoff++) |
| 3465 |
{ |
| 3466 |
Datum arraydatum; |
| 3467 |
bool eisnull; |
| 3468 |
ArrayType *array; |
| 3469 |
int this_ndims; |
| 3470 |
|
| 3471 |
arraydatum = op->d.arrayexpr.elemvalues[elemoff]; |
| 3472 |
eisnull = op->d.arrayexpr.elemnulls[elemoff]; |
| 3473 |
|
| 3474 |
/* temporarily ignore null subarrays */ |
| 3475 |
if (eisnull) |
| 3476 |
{ |
| 3477 |
haveempty = true; |
| 3478 |
continue; |
| 3479 |
} |
| 3480 |
|
| 3481 |
array = DatumGetArrayTypeP(arraydatum); |
| 3482 |
|
| 3483 |
/* run-time double-check on element type */ |
| 3484 |
if (element_type != ARR_ELEMTYPE(array)) |
| 3485 |
ereport(ERROR, |
| 3486 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 3487 |
errmsg("cannot merge incompatible arrays"), |
| 3488 |
errdetail("Array with element type %s cannot be " |
| 3489 |
"included in ARRAY construct with element type %s.", |
| 3490 |
format_type_be(ARR_ELEMTYPE(array)), |
| 3491 |
format_type_be(element_type)))); |
| 3492 |
|
| 3493 |
this_ndims = ARR_NDIM(array); |
| 3494 |
/* temporarily ignore zero-dimensional subarrays */ |
| 3495 |
if (this_ndims <= 0) |
| 3496 |
{ |
| 3497 |
haveempty = true; |
| 3498 |
continue; |
| 3499 |
} |
| 3500 |
|
| 3501 |
if (firstone) |
| 3502 |
{ |
| 3503 |
/* Get sub-array details from first member */ |
| 3504 |
elem_ndims = this_ndims; |
| 3505 |
ndims = elem_ndims + 1; |
| 3506 |
if (ndims <= 0 || ndims > MAXDIM) |
| 3507 |
ereport(ERROR, |
| 3508 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 3509 |
errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)", |
| 3510 |
ndims, MAXDIM))); |
| 3511 |
|
| 3512 |
elem_dims = (int *) palloc(elem_ndims * sizeof(int)); |
| 3513 |
memcpy(elem_dims, ARR_DIMS(array), elem_ndims * sizeof(int)); |
| 3514 |
elem_lbs = (int *) palloc(elem_ndims * sizeof(int)); |
| 3515 |
memcpy(elem_lbs, ARR_LBOUND(array), elem_ndims * sizeof(int)); |
| 3516 |
|
| 3517 |
firstone = false; |
| 3518 |
} |
| 3519 |
else |
| 3520 |
{ |
| 3521 |
/* Check other sub-arrays are compatible */ |
| 3522 |
if (elem_ndims != this_ndims || |
| 3523 |
memcmp(elem_dims, ARR_DIMS(array), |
| 3524 |
elem_ndims * sizeof(int)) != 0 || |
| 3525 |
memcmp(elem_lbs, ARR_LBOUND(array), |
| 3526 |
elem_ndims * sizeof(int)) != 0) |
| 3527 |
ereport(ERROR, |
| 3528 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 3529 |
errmsg("multidimensional arrays must have array " |
| 3530 |
"expressions with matching dimensions"))); |
| 3531 |
} |
| 3532 |
|
| 3533 |
subdata[outer_nelems] = ARR_DATA_PTR(array); |
| 3534 |
subbitmaps[outer_nelems] = ARR_NULLBITMAP(array); |
| 3535 |
subbytes[outer_nelems] = ARR_SIZE(array) - ARR_DATA_OFFSET(array); |
| 3536 |
nbytes += subbytes[outer_nelems]; |
| 3537 |
/* check for overflow of total request */ |
| 3538 |
if (!AllocSizeIsValid(nbytes)) |
| 3539 |
ereport(ERROR, |
| 3540 |
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
| 3541 |
errmsg("array size exceeds the maximum allowed (%d)", |
| 3542 |
(int) MaxAllocSize))); |
| 3543 |
subnitems[outer_nelems] = ArrayGetNItems(this_ndims, |
| 3544 |
ARR_DIMS(array)); |
| 3545 |
havenulls |= ARR_HASNULL(array); |
| 3546 |
outer_nelems++; |
| 3547 |
} |
| 3548 |
|
| 3549 |
/* |
| 3550 |
* If all items were null or empty arrays, return an empty array; |
| 3551 |
* otherwise, if some were and some weren't, raise error. (Note: we |
| 3552 |
* must special-case this somehow to avoid trying to generate a 1-D |
| 3553 |
* array formed from empty arrays. It's not ideal...) |
| 3554 |
*/ |
| 3555 |
if (haveempty) |
| 3556 |
{ |
| 3557 |
if (ndims == 0) /* didn't find any nonempty array */ |
| 3558 |
{ |
| 3559 |
*op->resvalue = PointerGetDatum(construct_empty_array(element_type)); |
| 3560 |
return; |
| 3561 |
} |
| 3562 |
ereport(ERROR, |
| 3563 |
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), |
| 3564 |
errmsg("multidimensional arrays must have array " |
| 3565 |
"expressions with matching dimensions"))); |
| 3566 |
} |
| 3567 |
|
| 3568 |
/* setup for multi-D array */ |
| 3569 |
dims[0] = outer_nelems; |
| 3570 |
lbs[0] = 1; |
| 3571 |
for (int i = 1; i < ndims; i++) |
| 3572 |
{ |
| 3573 |
dims[i] = elem_dims[i - 1]; |
| 3574 |
lbs[i] = elem_lbs[i - 1]; |
| 3575 |
} |
| 3576 |
|
| 3577 |
/* check for subscript overflow */ |
| 3578 |
nitems = ArrayGetNItems(ndims, dims); |
| 3579 |
ArrayCheckBounds(ndims, dims, lbs); |
| 3580 |
|
| 3581 |
if (havenulls) |
| 3582 |
{ |
| 3583 |
dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nitems); |
| 3584 |
nbytes += dataoffset; |
| 3585 |
} |
| 3586 |
else |
| 3587 |
{ |
| 3588 |
dataoffset = 0; /* marker for no null bitmap */ |
| 3589 |
nbytes += ARR_OVERHEAD_NONULLS(ndims); |
| 3590 |
} |
| 3591 |
|
| 3592 |
result = (ArrayType *) palloc0(nbytes); |
| 3593 |
SET_VARSIZE(result, nbytes); |
| 3594 |
result->ndim = ndims; |
| 3595 |
result->dataoffset = dataoffset; |
| 3596 |
result->elemtype = element_type; |
| 3597 |
memcpy(ARR_DIMS(result), dims, ndims * sizeof(int)); |
| 3598 |
memcpy(ARR_LBOUND(result), lbs, ndims * sizeof(int)); |
| 3599 |
|
| 3600 |
dat = ARR_DATA_PTR(result); |
| 3601 |
iitem = 0; |
| 3602 |
for (int i = 0; i < outer_nelems; i++) |
| 3603 |
{ |
| 3604 |
memcpy(dat, subdata[i], subbytes[i]); |
| 3605 |
dat += subbytes[i]; |
| 3606 |
if (havenulls) |
| 3607 |
array_bitmap_copy(ARR_NULLBITMAP(result), iitem, |
| 3608 |
subbitmaps[i], 0, |
| 3609 |
subnitems[i]); |
| 3610 |
iitem += subnitems[i]; |
| 3611 |
} |
| 3612 |
} |
| 3613 |
|
| 3614 |
*op->resvalue = PointerGetDatum(result); |
| 3615 |
} |
| 3616 |
|
| 3617 |
/* |
| 3618 |
* Evaluate an ArrayCoerceExpr expression. |
| 3619 |
* |
| 3620 |
* Source array is in step's result variable. |
| 3621 |
*/ |
| 3622 |
void |
| 3623 |
ExecEvalArrayCoerce(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3624 |
{ |
| 3625 |
Datum arraydatum; |
| 3626 |
|
| 3627 |
/* NULL array -> NULL result */ |
| 3628 |
if (*op->resnull) |
| 3629 |
return; |
| 3630 |
|
| 3631 |
arraydatum = *op->resvalue; |
| 3632 |
|
| 3633 |
/* |
| 3634 |
* If it's binary-compatible, modify the element type in the array header, |
| 3635 |
* but otherwise leave the array as we received it. |
| 3636 |
*/ |
| 3637 |
if (op->d.arraycoerce.elemexprstate == NULL) |
| 3638 |
{ |
| 3639 |
/* Detoast input array if necessary, and copy in any case */ |
| 3640 |
ArrayType *array = DatumGetArrayTypePCopy(arraydatum); |
| 3641 |
|
| 3642 |
ARR_ELEMTYPE(array) = op->d.arraycoerce.resultelemtype; |
| 3643 |
*op->resvalue = PointerGetDatum(array); |
| 3644 |
return; |
| 3645 |
} |
| 3646 |
|
| 3647 |
/* |
| 3648 |
* Use array_map to apply the sub-expression to each array element. |
| 3649 |
*/ |
| 3650 |
*op->resvalue = array_map(arraydatum, |
| 3651 |
op->d.arraycoerce.elemexprstate, |
| 3652 |
econtext, |
| 3653 |
op->d.arraycoerce.resultelemtype, |
| 3654 |
op->d.arraycoerce.amstate); |
| 3655 |
} |
| 3656 |
|
| 3657 |
/* |
| 3658 |
* Evaluate a ROW() expression. |
| 3659 |
* |
| 3660 |
* The individual columns have already been evaluated into |
| 3661 |
* op->d.row.elemvalues[]/elemnulls[]. |
| 3662 |
*/ |
| 3663 |
void |
| 3664 |
ExecEvalRow(ExprState *state, ExprEvalStep *op) |
| 3665 |
{ |
| 3666 |
HeapTuple tuple; |
| 3667 |
|
| 3668 |
/* build tuple from evaluated field values */ |
| 3669 |
tuple = heap_form_tuple(op->d.row.tupdesc, |
| 3670 |
op->d.row.elemvalues, |
| 3671 |
op->d.row.elemnulls); |
| 3672 |
|
| 3673 |
*op->resvalue = HeapTupleGetDatum(tuple); |
| 3674 |
*op->resnull = false; |
| 3675 |
} |
| 3676 |
|
| 3677 |
/* |
| 3678 |
* Evaluate GREATEST() or LEAST() expression (note this is *not* MIN()/MAX()). |
| 3679 |
* |
| 3680 |
* All of the to-be-compared expressions have already been evaluated into |
| 3681 |
* op->d.minmax.values[]/nulls[]. |
| 3682 |
*/ |
| 3683 |
void |
| 3684 |
ExecEvalMinMax(ExprState *state, ExprEvalStep *op) |
| 3685 |
{ |
| 3686 |
Datum *values = op->d.minmax.values; |
| 3687 |
bool *nulls = op->d.minmax.nulls; |
| 3688 |
FunctionCallInfo fcinfo = op->d.minmax.fcinfo_data; |
| 3689 |
MinMaxOp operator = op->d.minmax.op; |
| 3690 |
|
| 3691 |
/* set at initialization */ |
| 3692 |
Assert(fcinfo->args[0].isnull == false); |
| 3693 |
Assert(fcinfo->args[1].isnull == false); |
| 3694 |
|
| 3695 |
/* default to null result */ |
| 3696 |
*op->resnull = true; |
| 3697 |
|
| 3698 |
for (int off = 0; off < op->d.minmax.nelems; off++) |
| 3699 |
{ |
| 3700 |
/* ignore NULL inputs */ |
| 3701 |
if (nulls[off]) |
| 3702 |
continue; |
| 3703 |
|
| 3704 |
if (*op->resnull) |
| 3705 |
{ |
| 3706 |
/* first nonnull input, adopt value */ |
| 3707 |
*op->resvalue = values[off]; |
| 3708 |
*op->resnull = false; |
| 3709 |
} |
| 3710 |
else |
| 3711 |
{ |
| 3712 |
int cmpresult; |
| 3713 |
|
| 3714 |
/* apply comparison function */ |
| 3715 |
fcinfo->args[0].value = *op->resvalue; |
| 3716 |
fcinfo->args[1].value = values[off]; |
| 3717 |
|
| 3718 |
fcinfo->isnull = false; |
| 3719 |
cmpresult = DatumGetInt32(FunctionCallInvoke(fcinfo)); |
| 3720 |
if (fcinfo->isnull) /* probably should not happen */ |
| 3721 |
continue; |
| 3722 |
|
| 3723 |
if (cmpresult > 0 && operator == IS_LEAST) |
| 3724 |
*op->resvalue = values[off]; |
| 3725 |
else if (cmpresult < 0 && operator == IS_GREATEST) |
| 3726 |
*op->resvalue = values[off]; |
| 3727 |
} |
| 3728 |
} |
| 3729 |
} |
| 3730 |
|
| 3731 |
/* |
| 3732 |
* Evaluate a FieldSelect node. |
| 3733 |
* |
| 3734 |
* Source record is in step's result variable. |
| 3735 |
*/ |
| 3736 |
void |
| 3737 |
ExecEvalFieldSelect(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3738 |
{ |
| 3739 |
AttrNumber fieldnum = op->d.fieldselect.fieldnum; |
| 3740 |
Datum tupDatum; |
| 3741 |
HeapTupleHeader tuple; |
| 3742 |
Oid tupType; |
| 3743 |
int32 tupTypmod; |
| 3744 |
TupleDesc tupDesc; |
| 3745 |
Form_pg_attribute attr; |
| 3746 |
HeapTupleData tmptup; |
| 3747 |
|
| 3748 |
/* NULL record -> NULL result */ |
| 3749 |
if (*op->resnull) |
| 3750 |
return; |
| 3751 |
|
| 3752 |
tupDatum = *op->resvalue; |
| 3753 |
|
| 3754 |
/* We can special-case expanded records for speed */ |
| 3755 |
if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(tupDatum))) |
| 3756 |
{ |
| 3757 |
ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(tupDatum); |
| 3758 |
|
| 3759 |
Assert(erh->er_magic == ER_MAGIC); |
| 3760 |
|
| 3761 |
/* Extract record's TupleDesc */ |
| 3762 |
tupDesc = expanded_record_get_tupdesc(erh); |
| 3763 |
|
| 3764 |
/* |
| 3765 |
* Find field's attr record. Note we don't support system columns |
| 3766 |
* here: a datum tuple doesn't have valid values for most of the |
| 3767 |
* interesting system columns anyway. |
| 3768 |
*/ |
| 3769 |
if (fieldnum <= 0) /* should never happen */ |
| 3770 |
elog(ERROR, "unsupported reference to system column %d in FieldSelect", |
| 3771 |
fieldnum); |
| 3772 |
if (fieldnum > tupDesc->natts) /* should never happen */ |
| 3773 |
elog(ERROR, "attribute number %d exceeds number of columns %d", |
| 3774 |
fieldnum, tupDesc->natts); |
| 3775 |
attr = TupleDescAttr(tupDesc, fieldnum - 1); |
| 3776 |
|
| 3777 |
/* Check for dropped column, and force a NULL result if so */ |
| 3778 |
if (attr->attisdropped) |
| 3779 |
{ |
| 3780 |
*op->resnull = true; |
| 3781 |
return; |
| 3782 |
} |
| 3783 |
|
| 3784 |
/* Check for type mismatch --- possible after ALTER COLUMN TYPE? */ |
| 3785 |
/* As in CheckVarSlotCompatibility, we should but can't check typmod */ |
| 3786 |
if (op->d.fieldselect.resulttype != attr->atttypid) |
| 3787 |
ereport(ERROR, |
| 3788 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 3789 |
errmsg("attribute %d has wrong type", fieldnum), |
| 3790 |
errdetail("Table has type %s, but query expects %s.", |
| 3791 |
format_type_be(attr->atttypid), |
| 3792 |
format_type_be(op->d.fieldselect.resulttype)))); |
| 3793 |
|
| 3794 |
/* extract the field */ |
| 3795 |
*op->resvalue = expanded_record_get_field(erh, fieldnum, |
| 3796 |
op->resnull); |
| 3797 |
} |
| 3798 |
else |
| 3799 |
{ |
| 3800 |
/* Get the composite datum and extract its type fields */ |
| 3801 |
tuple = DatumGetHeapTupleHeader(tupDatum); |
| 3802 |
|
| 3803 |
tupType = HeapTupleHeaderGetTypeId(tuple); |
| 3804 |
tupTypmod = HeapTupleHeaderGetTypMod(tuple); |
| 3805 |
|
| 3806 |
/* Lookup tupdesc if first time through or if type changes */ |
| 3807 |
tupDesc = get_cached_rowtype(tupType, tupTypmod, |
| 3808 |
&op->d.fieldselect.rowcache, NULL); |
| 3809 |
|
| 3810 |
/* |
| 3811 |
* Find field's attr record. Note we don't support system columns |
| 3812 |
* here: a datum tuple doesn't have valid values for most of the |
| 3813 |
* interesting system columns anyway. |
| 3814 |
*/ |
| 3815 |
if (fieldnum <= 0) /* should never happen */ |
| 3816 |
elog(ERROR, "unsupported reference to system column %d in FieldSelect", |
| 3817 |
fieldnum); |
| 3818 |
if (fieldnum > tupDesc->natts) /* should never happen */ |
| 3819 |
elog(ERROR, "attribute number %d exceeds number of columns %d", |
| 3820 |
fieldnum, tupDesc->natts); |
| 3821 |
attr = TupleDescAttr(tupDesc, fieldnum - 1); |
| 3822 |
|
| 3823 |
/* Check for dropped column, and force a NULL result if so */ |
| 3824 |
if (attr->attisdropped) |
| 3825 |
{ |
| 3826 |
*op->resnull = true; |
| 3827 |
return; |
| 3828 |
} |
| 3829 |
|
| 3830 |
/* Check for type mismatch --- possible after ALTER COLUMN TYPE? */ |
| 3831 |
/* As in CheckVarSlotCompatibility, we should but can't check typmod */ |
| 3832 |
if (op->d.fieldselect.resulttype != attr->atttypid) |
| 3833 |
ereport(ERROR, |
| 3834 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 3835 |
errmsg("attribute %d has wrong type", fieldnum), |
| 3836 |
errdetail("Table has type %s, but query expects %s.", |
| 3837 |
format_type_be(attr->atttypid), |
| 3838 |
format_type_be(op->d.fieldselect.resulttype)))); |
| 3839 |
|
| 3840 |
/* heap_getattr needs a HeapTuple not a bare HeapTupleHeader */ |
| 3841 |
tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
| 3842 |
tmptup.t_data = tuple; |
| 3843 |
|
| 3844 |
/* extract the field */ |
| 3845 |
*op->resvalue = heap_getattr(&tmptup, |
| 3846 |
fieldnum, |
| 3847 |
tupDesc, |
| 3848 |
op->resnull); |
| 3849 |
} |
| 3850 |
} |
| 3851 |
|
| 3852 |
/* |
| 3853 |
* Deform source tuple, filling in the step's values/nulls arrays, before |
| 3854 |
* evaluating individual new values as part of a FieldStore expression. |
| 3855 |
* Subsequent steps will overwrite individual elements of the values/nulls |
| 3856 |
* arrays with the new field values, and then FIELDSTORE_FORM will build the |
| 3857 |
* new tuple value. |
| 3858 |
* |
| 3859 |
* Source record is in step's result variable. |
| 3860 |
*/ |
| 3861 |
void |
| 3862 |
ExecEvalFieldStoreDeForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3863 |
{ |
| 3864 |
if (*op->resnull) |
| 3865 |
{ |
| 3866 |
/* Convert null input tuple into an all-nulls row */ |
| 3867 |
memset(op->d.fieldstore.nulls, true, |
| 3868 |
op->d.fieldstore.ncolumns * sizeof(bool)); |
| 3869 |
} |
| 3870 |
else |
| 3871 |
{ |
| 3872 |
/* |
| 3873 |
* heap_deform_tuple needs a HeapTuple not a bare HeapTupleHeader. We |
| 3874 |
* set all the fields in the struct just in case. |
| 3875 |
*/ |
| 3876 |
Datum tupDatum = *op->resvalue; |
| 3877 |
HeapTupleHeader tuphdr; |
| 3878 |
HeapTupleData tmptup; |
| 3879 |
TupleDesc tupDesc; |
| 3880 |
|
| 3881 |
tuphdr = DatumGetHeapTupleHeader(tupDatum); |
| 3882 |
tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr); |
| 3883 |
ItemPointerSetInvalid(&(tmptup.t_self)); |
| 3884 |
tmptup.t_tableOid = InvalidOid; |
| 3885 |
tmptup.t_data = tuphdr; |
| 3886 |
|
| 3887 |
/* |
| 3888 |
* Lookup tupdesc if first time through or if type changes. Because |
| 3889 |
* we don't pin the tupdesc, we must not do this lookup until after |
| 3890 |
* doing DatumGetHeapTupleHeader: that could do database access while |
| 3891 |
* detoasting the datum. |
| 3892 |
*/ |
| 3893 |
tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1, |
| 3894 |
op->d.fieldstore.rowcache, NULL); |
| 3895 |
|
| 3896 |
/* Check that current tupdesc doesn't have more fields than allocated */ |
| 3897 |
if (unlikely(tupDesc->natts > op->d.fieldstore.ncolumns)) |
| 3898 |
elog(ERROR, "too many columns in composite type %u", |
| 3899 |
op->d.fieldstore.fstore->resulttype); |
| 3900 |
|
| 3901 |
heap_deform_tuple(&tmptup, tupDesc, |
| 3902 |
op->d.fieldstore.values, |
| 3903 |
op->d.fieldstore.nulls); |
| 3904 |
} |
| 3905 |
} |
| 3906 |
|
| 3907 |
/* |
| 3908 |
* Compute the new composite datum after each individual field value of a |
| 3909 |
* FieldStore expression has been evaluated. |
| 3910 |
*/ |
| 3911 |
void |
| 3912 |
ExecEvalFieldStoreForm(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3913 |
{ |
| 3914 |
TupleDesc tupDesc; |
| 3915 |
HeapTuple tuple; |
| 3916 |
|
| 3917 |
/* Lookup tupdesc (should be valid already) */ |
| 3918 |
tupDesc = get_cached_rowtype(op->d.fieldstore.fstore->resulttype, -1, |
| 3919 |
op->d.fieldstore.rowcache, NULL); |
| 3920 |
|
| 3921 |
tuple = heap_form_tuple(tupDesc, |
| 3922 |
op->d.fieldstore.values, |
| 3923 |
op->d.fieldstore.nulls); |
| 3924 |
|
| 3925 |
*op->resvalue = HeapTupleGetDatum(tuple); |
| 3926 |
*op->resnull = false; |
| 3927 |
} |
| 3928 |
|
| 3929 |
/* |
| 3930 |
* Evaluate a rowtype coercion operation. |
| 3931 |
* This may require rearranging field positions. |
| 3932 |
* |
| 3933 |
* Source record is in step's result variable. |
| 3934 |
*/ |
| 3935 |
void |
| 3936 |
ExecEvalConvertRowtype(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 3937 |
{ |
| 3938 |
HeapTuple result; |
| 3939 |
Datum tupDatum; |
| 3940 |
HeapTupleHeader tuple; |
| 3941 |
HeapTupleData tmptup; |
| 3942 |
TupleDesc indesc, |
| 3943 |
outdesc; |
| 3944 |
bool changed = false; |
| 3945 |
|
| 3946 |
/* NULL in -> NULL out */ |
| 3947 |
if (*op->resnull) |
| 3948 |
return; |
| 3949 |
|
| 3950 |
tupDatum = *op->resvalue; |
| 3951 |
tuple = DatumGetHeapTupleHeader(tupDatum); |
| 3952 |
|
| 3953 |
/* |
| 3954 |
* Lookup tupdescs if first time through or if type changes. We'd better |
| 3955 |
* pin them since type conversion functions could do catalog lookups and |
| 3956 |
* hence cause cache invalidation. |
| 3957 |
*/ |
| 3958 |
indesc = get_cached_rowtype(op->d.convert_rowtype.inputtype, -1, |
| 3959 |
op->d.convert_rowtype.incache, |
| 3960 |
&changed); |
| 3961 |
IncrTupleDescRefCount(indesc); |
| 3962 |
outdesc = get_cached_rowtype(op->d.convert_rowtype.outputtype, -1, |
| 3963 |
op->d.convert_rowtype.outcache, |
| 3964 |
&changed); |
| 3965 |
IncrTupleDescRefCount(outdesc); |
| 3966 |
|
| 3967 |
/* |
| 3968 |
* We used to be able to assert that incoming tuples are marked with |
| 3969 |
* exactly the rowtype of indesc. However, now that ExecEvalWholeRowVar |
| 3970 |
* might change the tuples' marking to plain RECORD due to inserting |
| 3971 |
* aliases, we can only make this weak test: |
| 3972 |
*/ |
| 3973 |
Assert(HeapTupleHeaderGetTypeId(tuple) == indesc->tdtypeid || |
| 3974 |
HeapTupleHeaderGetTypeId(tuple) == RECORDOID); |
| 3975 |
|
| 3976 |
/* if first time through, or after change, initialize conversion map */ |
| 3977 |
if (changed) |
| 3978 |
{ |
| 3979 |
MemoryContext old_cxt; |
| 3980 |
|
| 3981 |
/* allocate map in long-lived memory context */ |
| 3982 |
old_cxt = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 3983 |
|
| 3984 |
/* prepare map from old to new attribute numbers */ |
| 3985 |
op->d.convert_rowtype.map = convert_tuples_by_name(indesc, outdesc); |
| 3986 |
|
| 3987 |
MemoryContextSwitchTo(old_cxt); |
| 3988 |
} |
| 3989 |
|
| 3990 |
/* Following steps need a HeapTuple not a bare HeapTupleHeader */ |
| 3991 |
tmptup.t_len = HeapTupleHeaderGetDatumLength(tuple); |
| 3992 |
tmptup.t_data = tuple; |
| 3993 |
|
| 3994 |
if (op->d.convert_rowtype.map != NULL) |
| 3995 |
{ |
| 3996 |
/* Full conversion with attribute rearrangement needed */ |
| 3997 |
result = execute_attr_map_tuple(&tmptup, op->d.convert_rowtype.map); |
| 3998 |
/* Result already has appropriate composite-datum header fields */ |
| 3999 |
*op->resvalue = HeapTupleGetDatum(result); |
| 4000 |
} |
| 4001 |
else |
| 4002 |
{ |
| 4003 |
/* |
| 4004 |
* The tuple is physically compatible as-is, but we need to insert the |
| 4005 |
* destination rowtype OID in its composite-datum header field, so we |
| 4006 |
* have to copy it anyway. heap_copy_tuple_as_datum() is convenient |
| 4007 |
* for this since it will both make the physical copy and insert the |
| 4008 |
* correct composite header fields. Note that we aren't expecting to |
| 4009 |
* have to flatten any toasted fields: the input was a composite |
| 4010 |
* datum, so it shouldn't contain any. So heap_copy_tuple_as_datum() |
| 4011 |
* is overkill here, but its check for external fields is cheap. |
| 4012 |
*/ |
| 4013 |
*op->resvalue = heap_copy_tuple_as_datum(&tmptup, outdesc); |
| 4014 |
} |
| 4015 |
|
| 4016 |
DecrTupleDescRefCount(indesc); |
| 4017 |
DecrTupleDescRefCount(outdesc); |
| 4018 |
} |
| 4019 |
|
| 4020 |
/* |
| 4021 |
* Evaluate "scalar op ANY/ALL (array)". |
| 4022 |
* |
| 4023 |
* Source array is in our result area, scalar arg is already evaluated into |
| 4024 |
* fcinfo->args[0]. |
| 4025 |
* |
| 4026 |
* The operator always yields boolean, and we combine the results across all |
| 4027 |
* array elements using OR and AND (for ANY and ALL respectively). Of course |
| 4028 |
* we short-circuit as soon as the result is known. |
| 4029 |
*/ |
| 4030 |
void |
| 4031 |
ExecEvalScalarArrayOp(ExprState *state, ExprEvalStep *op) |
| 4032 |
{ |
| 4033 |
FunctionCallInfo fcinfo = op->d.scalararrayop.fcinfo_data; |
| 4034 |
bool useOr = op->d.scalararrayop.useOr; |
| 4035 |
bool strictfunc = op->d.scalararrayop.finfo->fn_strict; |
| 4036 |
ArrayType *arr; |
| 4037 |
int nitems; |
| 4038 |
Datum result; |
| 4039 |
bool resultnull; |
| 4040 |
|
| 4041 |
/* |
| 4042 |
* If the array is NULL then we return NULL --- it's not very meaningful |
| 4043 |
* to do anything else, even if the operator isn't strict. |
| 4044 |
*/ |
| 4045 |
if (*op->resnull) |
| 4046 |
return; |
| 4047 |
|
| 4048 |
/* Else okay to fetch and detoast the array */ |
| 4049 |
arr = DatumGetArrayTypeP(*op->resvalue); |
| 4050 |
|
| 4051 |
/* |
| 4052 |
* If the array is empty, we return either FALSE or TRUE per the useOr |
| 4053 |
* flag. This is correct even if the scalar is NULL; since we would |
| 4054 |
* evaluate the operator zero times, it matters not whether it would want |
| 4055 |
* to return NULL. |
| 4056 |
*/ |
| 4057 |
nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
| 4058 |
if (nitems <= 0) |
| 4059 |
{ |
| 4060 |
*op->resvalue = BoolGetDatum(!useOr); |
| 4061 |
*op->resnull = false; |
| 4062 |
return; |
| 4063 |
} |
| 4064 |
|
| 4065 |
/* |
| 4066 |
* If the scalar is NULL, and the function is strict, return NULL; no |
| 4067 |
* point in iterating the loop. |
| 4068 |
*/ |
| 4069 |
if (fcinfo->args[0].isnull && strictfunc) |
| 4070 |
{ |
| 4071 |
*op->resnull = true; |
| 4072 |
return; |
| 4073 |
} |
| 4074 |
|
| 4075 |
/* |
| 4076 |
* We arrange to look up info about the element type only once per series |
| 4077 |
* of calls, assuming the element type doesn't change underneath us. |
| 4078 |
*/ |
| 4079 |
if (op->d.scalararrayop.element_type != ARR_ELEMTYPE(arr)) |
| 4080 |
{ |
| 4081 |
get_typlenbyvalalign(ARR_ELEMTYPE(arr), |
| 4082 |
&op->d.scalararrayop.typlen, |
| 4083 |
&op->d.scalararrayop.typbyval, |
| 4084 |
&op->d.scalararrayop.typalign); |
| 4085 |
op->d.scalararrayop.element_type = ARR_ELEMTYPE(arr); |
| 4086 |
} |
| 4087 |
|
| 4088 |
ExecEvalArrayCompareInternal(fcinfo, |
| 4089 |
arr, |
| 4090 |
op->d.scalararrayop.typlen, |
| 4091 |
op->d.scalararrayop.typbyval, |
| 4092 |
op->d.scalararrayop.typalign, |
| 4093 |
useOr, |
| 4094 |
&result, |
| 4095 |
&resultnull); |
| 4096 |
|
| 4097 |
*op->resvalue = result; |
| 4098 |
*op->resnull = resultnull; |
| 4099 |
} |
| 4100 |
|
| 4101 |
/* |
| 4102 |
* Shared helper for ExecEvalScalarArrayOp() and the NULL-LHS fallback for |
| 4103 |
* non-strict ExecEvalHashedScalarArrayOp(). |
| 4104 |
* |
| 4105 |
* Callers must handle the strict LHS-is-NULL; return NULL fast path prior to |
| 4106 |
* calling this. |
| 4107 |
*/ |
| 4108 |
static pg_always_inline void |
| 4109 |
ExecEvalArrayCompareInternal(FunctionCallInfo fcinfo, ArrayType *arr, |
| 4110 |
int16 typlen, bool typbyval, char typalign, |
| 4111 |
bool useOr, Datum *result, bool *resultnull) |
| 4112 |
{ |
| 4113 |
int nitems; |
| 4114 |
char *s; |
| 4115 |
bits8 *bitmap; |
| 4116 |
int bitmask; |
| 4117 |
bool strictfunc = fcinfo->flinfo->fn_strict; |
| 4118 |
|
| 4119 |
nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
| 4120 |
|
| 4121 |
/* Initialize result appropriately depending on useOr */ |
| 4122 |
*result = BoolGetDatum(!useOr); |
| 4123 |
*resultnull = false; |
| 4124 |
|
| 4125 |
/* Loop over the array elements */ |
| 4126 |
s = (char *) ARR_DATA_PTR(arr); |
| 4127 |
bitmap = ARR_NULLBITMAP(arr); |
| 4128 |
bitmask = 1; |
| 4129 |
|
| 4130 |
for (int i = 0; i < nitems; i++) |
| 4131 |
{ |
| 4132 |
Datum elt; |
| 4133 |
Datum thisresult; |
| 4134 |
|
| 4135 |
/* Get array element, checking for NULL */ |
| 4136 |
if (bitmap && (*bitmap & bitmask) == 0) |
| 4137 |
{ |
| 4138 |
fcinfo->args[1].value = (Datum) 0; |
| 4139 |
fcinfo->args[1].isnull = true; |
| 4140 |
} |
| 4141 |
else |
| 4142 |
{ |
| 4143 |
elt = fetch_att(s, typbyval, typlen); |
| 4144 |
s = att_addlength_pointer(s, typlen, s); |
| 4145 |
s = (char *) att_align_nominal(s, typalign); |
| 4146 |
fcinfo->args[1].value = elt; |
| 4147 |
fcinfo->args[1].isnull = false; |
| 4148 |
} |
| 4149 |
|
| 4150 |
/* Call comparison function */ |
| 4151 |
if (fcinfo->args[1].isnull && strictfunc) |
| 4152 |
{ |
| 4153 |
fcinfo->isnull = true; |
| 4154 |
thisresult = (Datum) 0; |
| 4155 |
} |
| 4156 |
else |
| 4157 |
{ |
| 4158 |
fcinfo->isnull = false; |
| 4159 |
thisresult = fcinfo->flinfo->fn_addr(fcinfo); |
| 4160 |
} |
| 4161 |
|
| 4162 |
/* Combine results per OR or AND semantics */ |
| 4163 |
if (fcinfo->isnull) |
| 4164 |
*resultnull = true; |
| 4165 |
else if (useOr) |
| 4166 |
{ |
| 4167 |
if (DatumGetBool(thisresult)) |
| 4168 |
{ |
| 4169 |
*result = BoolGetDatum(true); |
| 4170 |
*resultnull = false; |
| 4171 |
break; /* needn't look at any more elements */ |
| 4172 |
} |
| 4173 |
} |
| 4174 |
else |
| 4175 |
{ |
| 4176 |
if (!DatumGetBool(thisresult)) |
| 4177 |
{ |
| 4178 |
*result = BoolGetDatum(false); |
| 4179 |
*resultnull = false; |
| 4180 |
break; /* needn't look at any more elements */ |
| 4181 |
} |
| 4182 |
} |
| 4183 |
|
| 4184 |
/* advance bitmap pointer if any */ |
| 4185 |
if (bitmap) |
| 4186 |
{ |
| 4187 |
bitmask <<= 1; |
| 4188 |
if (bitmask == 0x100) |
| 4189 |
{ |
| 4190 |
bitmap++; |
| 4191 |
bitmask = 1; |
| 4192 |
} |
| 4193 |
} |
| 4194 |
} |
| 4195 |
} |
| 4196 |
|
| 4197 |
/* |
| 4198 |
* Hash function for scalar array hash op elements. |
| 4199 |
* |
| 4200 |
* We use the element type's default hash opclass, and the column collation |
| 4201 |
* if the type is collation-sensitive. |
| 4202 |
*/ |
| 4203 |
static uint32 |
| 4204 |
saop_element_hash(struct saophash_hash *tb, Datum key) |
| 4205 |
{ |
| 4206 |
ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data; |
| 4207 |
FunctionCallInfo fcinfo = &elements_tab->hash_fcinfo_data; |
| 4208 |
Datum hash; |
| 4209 |
|
| 4210 |
fcinfo->args[0].value = key; |
| 4211 |
fcinfo->args[0].isnull = false; |
| 4212 |
|
| 4213 |
hash = elements_tab->hash_finfo.fn_addr(fcinfo); |
| 4214 |
|
| 4215 |
return DatumGetUInt32(hash); |
| 4216 |
} |
| 4217 |
|
| 4218 |
/* |
| 4219 |
* Matching function for scalar array hash op elements, to be used in hashtable |
| 4220 |
* lookups. |
| 4221 |
*/ |
| 4222 |
static bool |
| 4223 |
saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2) |
| 4224 |
{ |
| 4225 |
Datum result; |
| 4226 |
|
| 4227 |
ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data; |
| 4228 |
FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.fcinfo_data; |
| 4229 |
|
| 4230 |
fcinfo->args[0].value = key1; |
| 4231 |
fcinfo->args[0].isnull = false; |
| 4232 |
fcinfo->args[1].value = key2; |
| 4233 |
fcinfo->args[1].isnull = false; |
| 4234 |
|
| 4235 |
result = elements_tab->op->d.hashedscalararrayop.finfo->fn_addr(fcinfo); |
| 4236 |
|
| 4237 |
return DatumGetBool(result); |
| 4238 |
} |
| 4239 |
|
| 4240 |
/* |
| 4241 |
* Evaluate "scalar op ANY (const array)". |
| 4242 |
* |
| 4243 |
* Similar to ExecEvalScalarArrayOp, but optimized for faster repeat lookups |
| 4244 |
* by building a hashtable on the first lookup. This hashtable will be reused |
| 4245 |
* by subsequent lookups. Unlike ExecEvalScalarArrayOp, this version only |
| 4246 |
* supports OR semantics. |
| 4247 |
* |
| 4248 |
* Source array is in our result area, scalar arg is already evaluated into |
| 4249 |
* fcinfo->args[0]. |
| 4250 |
* |
| 4251 |
* The operator always yields boolean. |
| 4252 |
*/ |
| 4253 |
void |
| 4254 |
ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 4255 |
{ |
| 4256 |
ScalarArrayOpExprHashTable *elements_tab = op->d.hashedscalararrayop.elements_tab; |
| 4257 |
FunctionCallInfo fcinfo = op->d.hashedscalararrayop.fcinfo_data; |
| 4258 |
bool inclause = op->d.hashedscalararrayop.inclause; |
| 4259 |
bool strictfunc = op->d.hashedscalararrayop.finfo->fn_strict; |
| 4260 |
Datum scalar = fcinfo->args[0].value; |
| 4261 |
bool scalar_isnull = fcinfo->args[0].isnull; |
| 4262 |
Datum result; |
| 4263 |
bool resultnull; |
| 4264 |
bool hashfound; |
| 4265 |
|
| 4266 |
/* We don't setup a hashed scalar array op if the array const is null. */ |
| 4267 |
Assert(!*op->resnull); |
| 4268 |
|
| 4269 |
/* |
| 4270 |
* If the scalar is NULL, and the function is strict, return NULL; no |
| 4271 |
* point in executing the search. |
| 4272 |
*/ |
| 4273 |
if (scalar_isnull && strictfunc) |
| 4274 |
{ |
| 4275 |
*op->resnull = true; |
| 4276 |
return; |
| 4277 |
} |
| 4278 |
|
| 4279 |
/* Build the hash table on first evaluation */ |
| 4280 |
if (elements_tab == NULL) |
| 4281 |
{ |
| 4282 |
ScalarArrayOpExpr *saop; |
| 4283 |
int16 typlen; |
| 4284 |
bool typbyval; |
| 4285 |
char typalign; |
| 4286 |
int nitems; |
| 4287 |
bool has_nulls = false; |
| 4288 |
char *s; |
| 4289 |
bits8 *bitmap; |
| 4290 |
int bitmask; |
| 4291 |
MemoryContext oldcontext; |
| 4292 |
ArrayType *arr; |
| 4293 |
|
| 4294 |
saop = op->d.hashedscalararrayop.saop; |
| 4295 |
|
| 4296 |
arr = DatumGetArrayTypeP(*op->resvalue); |
| 4297 |
nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr)); |
| 4298 |
|
| 4299 |
get_typlenbyvalalign(ARR_ELEMTYPE(arr), |
| 4300 |
&typlen, |
| 4301 |
&typbyval, |
| 4302 |
&typalign); |
| 4303 |
|
| 4304 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 4305 |
|
| 4306 |
elements_tab = (ScalarArrayOpExprHashTable *) |
| 4307 |
palloc0(offsetof(ScalarArrayOpExprHashTable, hash_fcinfo_data) + |
| 4308 |
SizeForFunctionCallInfo(1)); |
| 4309 |
op->d.hashedscalararrayop.elements_tab = elements_tab; |
| 4310 |
elements_tab->op = op; |
| 4311 |
|
| 4312 |
fmgr_info(saop->hashfuncid, &elements_tab->hash_finfo); |
| 4313 |
fmgr_info_set_expr((Node *) saop, &elements_tab->hash_finfo); |
| 4314 |
|
| 4315 |
InitFunctionCallInfoData(elements_tab->hash_fcinfo_data, |
| 4316 |
&elements_tab->hash_finfo, |
| 4317 |
1, |
| 4318 |
saop->inputcollid, |
| 4319 |
NULL, |
| 4320 |
NULL); |
| 4321 |
|
| 4322 |
/* |
| 4323 |
* Create the hash table sizing it according to the number of elements |
| 4324 |
* in the array. This does assume that the array has no duplicates. |
| 4325 |
* If the array happens to contain many duplicate values then it'll |
| 4326 |
* just mean that we sized the table a bit on the large side. |
| 4327 |
*/ |
| 4328 |
elements_tab->hashtab = saophash_create(CurrentMemoryContext, nitems, |
| 4329 |
elements_tab); |
| 4330 |
|
| 4331 |
MemoryContextSwitchTo(oldcontext); |
| 4332 |
|
| 4333 |
s = (char *) ARR_DATA_PTR(arr); |
| 4334 |
bitmap = ARR_NULLBITMAP(arr); |
| 4335 |
bitmask = 1; |
| 4336 |
for (int i = 0; i < nitems; i++) |
| 4337 |
{ |
| 4338 |
/* Get array element, checking for NULL. */ |
| 4339 |
if (bitmap && (*bitmap & bitmask) == 0) |
| 4340 |
{ |
| 4341 |
has_nulls = true; |
| 4342 |
} |
| 4343 |
else |
| 4344 |
{ |
| 4345 |
Datum element; |
| 4346 |
|
| 4347 |
element = fetch_att(s, typbyval, typlen); |
| 4348 |
s = att_addlength_pointer(s, typlen, s); |
| 4349 |
s = (char *) att_align_nominal(s, typalign); |
| 4350 |
|
| 4351 |
saophash_insert(elements_tab->hashtab, element, &hashfound); |
| 4352 |
} |
| 4353 |
|
| 4354 |
/* Advance bitmap pointer if any. */ |
| 4355 |
if (bitmap) |
| 4356 |
{ |
| 4357 |
bitmask <<= 1; |
| 4358 |
if (bitmask == 0x100) |
| 4359 |
{ |
| 4360 |
bitmap++; |
| 4361 |
bitmask = 1; |
| 4362 |
} |
| 4363 |
} |
| 4364 |
} |
| 4365 |
|
| 4366 |
/* |
| 4367 |
* Remember if we had any nulls so that we know if we need to execute |
| 4368 |
* non-strict functions with a null lhs value if no match is found. |
| 4369 |
*/ |
| 4370 |
op->d.hashedscalararrayop.has_nulls = has_nulls; |
| 4371 |
|
| 4372 |
/* |
| 4373 |
* When we have a non-strict equality function, check and cache the |
| 4374 |
* result from looking up a NULL. Non-strict functions are free to |
| 4375 |
* treat a NULL as equal to any other value, e.g. a 0 or an empty |
| 4376 |
* string. Here we perform a linear search over the array and cache |
| 4377 |
* the outcome so that we can use that result any time we receive a |
| 4378 |
* NULL. |
| 4379 |
*/ |
| 4380 |
if (!strictfunc) |
| 4381 |
{ |
| 4382 |
bool null_lhs_result; |
| 4383 |
|
| 4384 |
fcinfo->args[0].value = (Datum) 0; |
| 4385 |
fcinfo->args[0].isnull = true; |
| 4386 |
|
| 4387 |
ExecEvalArrayCompareInternal(fcinfo, arr, typlen, typbyval, |
| 4388 |
typalign, true, &result, |
| 4389 |
&resultnull); |
| 4390 |
|
| 4391 |
null_lhs_result = DatumGetBool(result); |
| 4392 |
|
| 4393 |
/* invert non-NULL results for NOT IN */ |
| 4394 |
if (!resultnull && !inclause) |
| 4395 |
null_lhs_result = !null_lhs_result; |
| 4396 |
|
| 4397 |
op->d.hashedscalararrayop.null_lhs_isnull = resultnull; |
| 4398 |
op->d.hashedscalararrayop.null_lhs_result = null_lhs_result; |
| 4399 |
} |
| 4400 |
} |
| 4401 |
|
| 4402 |
/* |
| 4403 |
* When looking up an SQL NULL value with non-strict functions, we defer |
| 4404 |
* to the value we cached when building the hash table. |
| 4405 |
*/ |
| 4406 |
if (scalar_isnull) |
| 4407 |
{ |
| 4408 |
Assert(!strictfunc); |
| 4409 |
|
| 4410 |
*op->resnull = op->d.hashedscalararrayop.null_lhs_isnull; |
| 4411 |
*op->resvalue = BoolGetDatum(op->d.hashedscalararrayop.null_lhs_result); |
| 4412 |
return; |
| 4413 |
} |
| 4414 |
|
| 4415 |
|
| 4416 |
/* Check the hash to see if we have a match. */ |
| 4417 |
hashfound = NULL != saophash_lookup(elements_tab->hashtab, scalar); |
| 4418 |
|
| 4419 |
/* the result depends on if the clause is an IN or NOT IN clause */ |
| 4420 |
if (inclause) |
| 4421 |
result = BoolGetDatum(hashfound); /* IN */ |
| 4422 |
else |
| 4423 |
result = BoolGetDatum(!hashfound); /* NOT IN */ |
| 4424 |
|
| 4425 |
resultnull = false; |
| 4426 |
|
| 4427 |
/* |
| 4428 |
* If we didn't find a match in the array, we still might need to handle |
| 4429 |
* the possibility of null values. We didn't put any NULLs into the |
| 4430 |
* hashtable, but instead marked if we found any when building the table |
| 4431 |
* in has_nulls. |
| 4432 |
*/ |
| 4433 |
if (!hashfound && op->d.hashedscalararrayop.has_nulls) |
| 4434 |
{ |
| 4435 |
if (strictfunc) |
| 4436 |
{ |
| 4437 |
|
| 4438 |
/* |
| 4439 |
* We have nulls in the array so a non-null lhs and no match must |
| 4440 |
* yield NULL. |
| 4441 |
*/ |
| 4442 |
result = (Datum) 0; |
| 4443 |
resultnull = true; |
| 4444 |
} |
| 4445 |
else |
| 4446 |
{ |
| 4447 |
/* |
| 4448 |
* Execute function will null rhs just once. |
| 4449 |
* |
| 4450 |
* The hash lookup path will have scribbled on the lhs argument so |
| 4451 |
* we need to set it up also (even though we entered this function |
| 4452 |
* with it already set). |
| 4453 |
*/ |
| 4454 |
fcinfo->args[0].value = scalar; |
| 4455 |
fcinfo->args[0].isnull = scalar_isnull; |
| 4456 |
fcinfo->args[1].value = (Datum) 0; |
| 4457 |
fcinfo->args[1].isnull = true; |
| 4458 |
|
| 4459 |
result = op->d.hashedscalararrayop.finfo->fn_addr(fcinfo); |
| 4460 |
resultnull = fcinfo->isnull; |
| 4461 |
|
| 4462 |
/* |
| 4463 |
* Reverse the result for NOT IN clauses since the above function |
| 4464 |
* is the equality function and we need not-equals. |
| 4465 |
*/ |
| 4466 |
if (!inclause) |
| 4467 |
result = !result; |
| 4468 |
} |
| 4469 |
} |
| 4470 |
|
| 4471 |
*op->resvalue = result; |
| 4472 |
*op->resnull = resultnull; |
| 4473 |
} |
| 4474 |
|
| 4475 |
/* |
| 4476 |
* Evaluate a NOT NULL domain constraint. |
| 4477 |
*/ |
| 4478 |
void |
| 4479 |
ExecEvalConstraintNotNull(ExprState *state, ExprEvalStep *op) |
| 4480 |
{ |
| 4481 |
if (*op->resnull) |
| 4482 |
errsave((Node *) op->d.domaincheck.escontext, |
| 4483 |
(errcode(ERRCODE_NOT_NULL_VIOLATION), |
| 4484 |
errmsg("domain %s does not allow null values", |
| 4485 |
format_type_be(op->d.domaincheck.resulttype)), |
| 4486 |
errdatatype(op->d.domaincheck.resulttype))); |
| 4487 |
} |
| 4488 |
|
| 4489 |
/* |
| 4490 |
* Evaluate a CHECK domain constraint. |
| 4491 |
*/ |
| 4492 |
void |
| 4493 |
ExecEvalConstraintCheck(ExprState *state, ExprEvalStep *op) |
| 4494 |
{ |
| 4495 |
if (!*op->d.domaincheck.checknull && |
| 4496 |
!DatumGetBool(*op->d.domaincheck.checkvalue)) |
| 4497 |
errsave((Node *) op->d.domaincheck.escontext, |
| 4498 |
(errcode(ERRCODE_CHECK_VIOLATION), |
| 4499 |
errmsg("value for domain %s violates check constraint \"%s\"", |
| 4500 |
format_type_be(op->d.domaincheck.resulttype), |
| 4501 |
op->d.domaincheck.constraintname), |
| 4502 |
errdomainconstraint(op->d.domaincheck.resulttype, |
| 4503 |
op->d.domaincheck.constraintname))); |
| 4504 |
} |
| 4505 |
|
| 4506 |
/* |
| 4507 |
* Evaluate the various forms of XmlExpr. |
| 4508 |
* |
| 4509 |
* Arguments have been evaluated into named_argvalue/named_argnull |
| 4510 |
* and/or argvalue/argnull arrays. |
| 4511 |
*/ |
| 4512 |
void |
| 4513 |
ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op) |
| 4514 |
{ |
| 4515 |
XmlExpr *xexpr = op->d.xmlexpr.xexpr; |
| 4516 |
Datum value; |
| 4517 |
|
| 4518 |
*op->resnull = true; /* until we get a result */ |
| 4519 |
*op->resvalue = (Datum) 0; |
| 4520 |
|
| 4521 |
switch (xexpr->op) |
| 4522 |
{ |
| 4523 |
case IS_XMLCONCAT: |
| 4524 |
{ |
| 4525 |
Datum *argvalue = op->d.xmlexpr.argvalue; |
| 4526 |
bool *argnull = op->d.xmlexpr.argnull; |
| 4527 |
List *values = NIL; |
| 4528 |
|
| 4529 |
for (int i = 0; i < list_length(xexpr->args); i++) |
| 4530 |
{ |
| 4531 |
if (!argnull[i]) |
| 4532 |
values = lappend(values, DatumGetPointer(argvalue[i])); |
| 4533 |
} |
| 4534 |
|
| 4535 |
if (values != NIL) |
| 4536 |
{ |
| 4537 |
*op->resvalue = PointerGetDatum(xmlconcat(values)); |
| 4538 |
*op->resnull = false; |
| 4539 |
} |
| 4540 |
} |
| 4541 |
break; |
| 4542 |
|
| 4543 |
case IS_XMLFOREST: |
| 4544 |
{ |
| 4545 |
Datum *argvalue = op->d.xmlexpr.named_argvalue; |
| 4546 |
bool *argnull = op->d.xmlexpr.named_argnull; |
| 4547 |
StringInfoData buf; |
| 4548 |
ListCell *lc; |
| 4549 |
ListCell *lc2; |
| 4550 |
int i; |
| 4551 |
|
| 4552 |
initStringInfo(&buf); |
| 4553 |
|
| 4554 |
i = 0; |
| 4555 |
forboth(lc, xexpr->named_args, lc2, xexpr->arg_names) |
| 4556 |
{ |
| 4557 |
Expr *e = (Expr *) lfirst(lc); |
| 4558 |
char *argname = strVal(lfirst(lc2)); |
| 4559 |
|
| 4560 |
if (!argnull[i]) |
| 4561 |
{ |
| 4562 |
value = argvalue[i]; |
| 4563 |
appendStringInfo(&buf, "<%s>%s</%s>", |
| 4564 |
argname, |
| 4565 |
map_sql_value_to_xml_value(value, |
| 4566 |
exprType((Node *) e), true), |
| 4567 |
argname); |
| 4568 |
*op->resnull = false; |
| 4569 |
} |
| 4570 |
i++; |
| 4571 |
} |
| 4572 |
|
| 4573 |
if (!*op->resnull) |
| 4574 |
{ |
| 4575 |
text *result; |
| 4576 |
|
| 4577 |
result = cstring_to_text_with_len(buf.data, buf.len); |
| 4578 |
*op->resvalue = PointerGetDatum(result); |
| 4579 |
} |
| 4580 |
|
| 4581 |
pfree(buf.data); |
| 4582 |
} |
| 4583 |
break; |
| 4584 |
|
| 4585 |
case IS_XMLELEMENT: |
| 4586 |
*op->resvalue = PointerGetDatum(xmlelement(xexpr, |
| 4587 |
op->d.xmlexpr.named_argvalue, |
| 4588 |
op->d.xmlexpr.named_argnull, |
| 4589 |
op->d.xmlexpr.argvalue, |
| 4590 |
op->d.xmlexpr.argnull)); |
| 4591 |
*op->resnull = false; |
| 4592 |
break; |
| 4593 |
|
| 4594 |
case IS_XMLPARSE: |
| 4595 |
{ |
| 4596 |
Datum *argvalue = op->d.xmlexpr.argvalue; |
| 4597 |
bool *argnull = op->d.xmlexpr.argnull; |
| 4598 |
text *data; |
| 4599 |
bool preserve_whitespace; |
| 4600 |
|
| 4601 |
/* arguments are known to be text, bool */ |
| 4602 |
Assert(list_length(xexpr->args) == 2); |
| 4603 |
|
| 4604 |
if (argnull[0]) |
| 4605 |
return; |
| 4606 |
value = argvalue[0]; |
| 4607 |
data = DatumGetTextPP(value); |
| 4608 |
|
| 4609 |
if (argnull[1]) /* probably can't happen */ |
| 4610 |
return; |
| 4611 |
value = argvalue[1]; |
| 4612 |
preserve_whitespace = DatumGetBool(value); |
| 4613 |
|
| 4614 |
*op->resvalue = PointerGetDatum(xmlparse(data, |
| 4615 |
xexpr->xmloption, |
| 4616 |
preserve_whitespace)); |
| 4617 |
*op->resnull = false; |
| 4618 |
} |
| 4619 |
break; |
| 4620 |
|
| 4621 |
case IS_XMLPI: |
| 4622 |
{ |
| 4623 |
text *arg; |
| 4624 |
bool isnull; |
| 4625 |
|
| 4626 |
/* optional argument is known to be text */ |
| 4627 |
Assert(list_length(xexpr->args) <= 1); |
| 4628 |
|
| 4629 |
if (xexpr->args) |
| 4630 |
{ |
| 4631 |
isnull = op->d.xmlexpr.argnull[0]; |
| 4632 |
if (isnull) |
| 4633 |
arg = NULL; |
| 4634 |
else |
| 4635 |
arg = DatumGetTextPP(op->d.xmlexpr.argvalue[0]); |
| 4636 |
} |
| 4637 |
else |
| 4638 |
{ |
| 4639 |
arg = NULL; |
| 4640 |
isnull = false; |
| 4641 |
} |
| 4642 |
|
| 4643 |
*op->resvalue = PointerGetDatum(xmlpi(xexpr->name, |
| 4644 |
arg, |
| 4645 |
isnull, |
| 4646 |
op->resnull)); |
| 4647 |
} |
| 4648 |
break; |
| 4649 |
|
| 4650 |
case IS_XMLROOT: |
| 4651 |
{ |
| 4652 |
Datum *argvalue = op->d.xmlexpr.argvalue; |
| 4653 |
bool *argnull = op->d.xmlexpr.argnull; |
| 4654 |
xmltype *data; |
| 4655 |
text *version; |
| 4656 |
int standalone; |
| 4657 |
|
| 4658 |
/* arguments are known to be xml, text, int */ |
| 4659 |
Assert(list_length(xexpr->args) == 3); |
| 4660 |
|
| 4661 |
if (argnull[0]) |
| 4662 |
return; |
| 4663 |
data = DatumGetXmlP(argvalue[0]); |
| 4664 |
|
| 4665 |
if (argnull[1]) |
| 4666 |
version = NULL; |
| 4667 |
else |
| 4668 |
version = DatumGetTextPP(argvalue[1]); |
| 4669 |
|
| 4670 |
Assert(!argnull[2]); /* always present */ |
| 4671 |
standalone = DatumGetInt32(argvalue[2]); |
| 4672 |
|
| 4673 |
*op->resvalue = PointerGetDatum(xmlroot(data, |
| 4674 |
version, |
| 4675 |
standalone)); |
| 4676 |
*op->resnull = false; |
| 4677 |
} |
| 4678 |
break; |
| 4679 |
|
| 4680 |
case IS_XMLSERIALIZE: |
| 4681 |
{ |
| 4682 |
Datum *argvalue = op->d.xmlexpr.argvalue; |
| 4683 |
bool *argnull = op->d.xmlexpr.argnull; |
| 4684 |
|
| 4685 |
/* argument type is known to be xml */ |
| 4686 |
Assert(list_length(xexpr->args) == 1); |
| 4687 |
|
| 4688 |
if (argnull[0]) |
| 4689 |
return; |
| 4690 |
value = argvalue[0]; |
| 4691 |
|
| 4692 |
*op->resvalue = |
| 4693 |
PointerGetDatum(xmltotext_with_options(DatumGetXmlP(value), |
| 4694 |
xexpr->xmloption, |
| 4695 |
xexpr->indent)); |
| 4696 |
*op->resnull = false; |
| 4697 |
} |
| 4698 |
break; |
| 4699 |
|
| 4700 |
case IS_DOCUMENT: |
| 4701 |
{ |
| 4702 |
Datum *argvalue = op->d.xmlexpr.argvalue; |
| 4703 |
bool *argnull = op->d.xmlexpr.argnull; |
| 4704 |
|
| 4705 |
/* optional argument is known to be xml */ |
| 4706 |
Assert(list_length(xexpr->args) == 1); |
| 4707 |
|
| 4708 |
if (argnull[0]) |
| 4709 |
return; |
| 4710 |
value = argvalue[0]; |
| 4711 |
|
| 4712 |
*op->resvalue = |
| 4713 |
BoolGetDatum(xml_is_document(DatumGetXmlP(value))); |
| 4714 |
*op->resnull = false; |
| 4715 |
} |
| 4716 |
break; |
| 4717 |
|
| 4718 |
default: |
| 4719 |
elog(ERROR, "unrecognized XML operation"); |
| 4720 |
break; |
| 4721 |
} |
| 4722 |
} |
| 4723 |
|
| 4724 |
/* |
| 4725 |
* Evaluate a JSON constructor expression. |
| 4726 |
*/ |
| 4727 |
void |
| 4728 |
ExecEvalJsonConstructor(ExprState *state, ExprEvalStep *op, |
| 4729 |
ExprContext *econtext) |
| 4730 |
{ |
| 4731 |
Datum res; |
| 4732 |
JsonConstructorExprState *jcstate = op->d.json_constructor.jcstate; |
| 4733 |
JsonConstructorExpr *ctor = jcstate->constructor; |
| 4734 |
bool is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB; |
| 4735 |
bool isnull = false; |
| 4736 |
|
| 4737 |
if (ctor->type == JSCTOR_JSON_ARRAY) |
| 4738 |
res = (is_jsonb ? |
| 4739 |
jsonb_build_array_worker : |
| 4740 |
json_build_array_worker) (jcstate->nargs, |
| 4741 |
jcstate->arg_values, |
| 4742 |
jcstate->arg_nulls, |
| 4743 |
jcstate->arg_types, |
| 4744 |
jcstate->constructor->absent_on_null); |
| 4745 |
else if (ctor->type == JSCTOR_JSON_OBJECT) |
| 4746 |
res = (is_jsonb ? |
| 4747 |
jsonb_build_object_worker : |
| 4748 |
json_build_object_worker) (jcstate->nargs, |
| 4749 |
jcstate->arg_values, |
| 4750 |
jcstate->arg_nulls, |
| 4751 |
jcstate->arg_types, |
| 4752 |
jcstate->constructor->absent_on_null, |
| 4753 |
jcstate->constructor->unique); |
| 4754 |
else if (ctor->type == JSCTOR_JSON_SCALAR) |
| 4755 |
{ |
| 4756 |
if (jcstate->arg_nulls[0]) |
| 4757 |
{ |
| 4758 |
res = (Datum) 0; |
| 4759 |
isnull = true; |
| 4760 |
} |
| 4761 |
else |
| 4762 |
{ |
| 4763 |
Datum value = jcstate->arg_values[0]; |
| 4764 |
Oid outfuncid = jcstate->arg_type_cache[0].outfuncid; |
| 4765 |
JsonTypeCategory category = (JsonTypeCategory) |
| 4766 |
jcstate->arg_type_cache[0].category; |
| 4767 |
|
| 4768 |
if (is_jsonb) |
| 4769 |
res = datum_to_jsonb(value, category, outfuncid); |
| 4770 |
else |
| 4771 |
res = datum_to_json(value, category, outfuncid); |
| 4772 |
} |
| 4773 |
} |
| 4774 |
else if (ctor->type == JSCTOR_JSON_PARSE) |
| 4775 |
{ |
| 4776 |
if (jcstate->arg_nulls[0]) |
| 4777 |
{ |
| 4778 |
res = (Datum) 0; |
| 4779 |
isnull = true; |
| 4780 |
} |
| 4781 |
else |
| 4782 |
{ |
| 4783 |
Datum value = jcstate->arg_values[0]; |
| 4784 |
text *js = DatumGetTextP(value); |
| 4785 |
|
| 4786 |
if (is_jsonb) |
| 4787 |
res = jsonb_from_text(js, true); |
| 4788 |
else |
| 4789 |
{ |
| 4790 |
(void) json_validate(js, true, true); |
| 4791 |
res = value; |
| 4792 |
} |
| 4793 |
} |
| 4794 |
} |
| 4795 |
else |
| 4796 |
elog(ERROR, "invalid JsonConstructorExpr type %d", ctor->type); |
| 4797 |
|
| 4798 |
*op->resvalue = res; |
| 4799 |
*op->resnull = isnull; |
| 4800 |
} |
| 4801 |
|
| 4802 |
/* |
| 4803 |
* Evaluate a IS JSON predicate. |
| 4804 |
*/ |
| 4805 |
void |
| 4806 |
ExecEvalJsonIsPredicate(ExprState *state, ExprEvalStep *op) |
| 4807 |
{ |
| 4808 |
JsonIsPredicate *pred = op->d.is_json.pred; |
| 4809 |
Datum js = *op->resvalue; |
| 4810 |
Oid exprtype; |
| 4811 |
bool res; |
| 4812 |
|
| 4813 |
if (*op->resnull) |
| 4814 |
{ |
| 4815 |
*op->resvalue = BoolGetDatum(false); |
| 4816 |
return; |
| 4817 |
} |
| 4818 |
|
| 4819 |
exprtype = exprType(pred->expr); |
| 4820 |
|
| 4821 |
if (exprtype == TEXTOID || exprtype == JSONOID) |
| 4822 |
{ |
| 4823 |
text *json = DatumGetTextP(js); |
| 4824 |
|
| 4825 |
if (pred->item_type == JS_TYPE_ANY) |
| 4826 |
res = true; |
| 4827 |
else |
| 4828 |
{ |
| 4829 |
switch (json_get_first_token(json, false)) |
| 4830 |
{ |
| 4831 |
case JSON_TOKEN_OBJECT_START: |
| 4832 |
res = pred->item_type == JS_TYPE_OBJECT; |
| 4833 |
break; |
| 4834 |
case JSON_TOKEN_ARRAY_START: |
| 4835 |
res = pred->item_type == JS_TYPE_ARRAY; |
| 4836 |
break; |
| 4837 |
case JSON_TOKEN_STRING: |
| 4838 |
case JSON_TOKEN_NUMBER: |
| 4839 |
case JSON_TOKEN_TRUE: |
| 4840 |
case JSON_TOKEN_FALSE: |
| 4841 |
case JSON_TOKEN_NULL: |
| 4842 |
res = pred->item_type == JS_TYPE_SCALAR; |
| 4843 |
break; |
| 4844 |
default: |
| 4845 |
res = false; |
| 4846 |
break; |
| 4847 |
} |
| 4848 |
} |
| 4849 |
|
| 4850 |
/* |
| 4851 |
* Do full parsing pass only for uniqueness check or for JSON text |
| 4852 |
* validation. |
| 4853 |
*/ |
| 4854 |
if (res && (pred->unique_keys || exprtype == TEXTOID)) |
| 4855 |
res = json_validate(json, pred->unique_keys, false); |
| 4856 |
} |
| 4857 |
else if (exprtype == JSONBOID) |
| 4858 |
{ |
| 4859 |
if (pred->item_type == JS_TYPE_ANY) |
| 4860 |
res = true; |
| 4861 |
else |
| 4862 |
{ |
| 4863 |
Jsonb *jb = DatumGetJsonbP(js); |
| 4864 |
|
| 4865 |
switch (pred->item_type) |
| 4866 |
{ |
| 4867 |
case JS_TYPE_OBJECT: |
| 4868 |
res = JB_ROOT_IS_OBJECT(jb); |
| 4869 |
break; |
| 4870 |
case JS_TYPE_ARRAY: |
| 4871 |
res = JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb); |
| 4872 |
break; |
| 4873 |
case JS_TYPE_SCALAR: |
| 4874 |
res = JB_ROOT_IS_ARRAY(jb) && JB_ROOT_IS_SCALAR(jb); |
| 4875 |
break; |
| 4876 |
default: |
| 4877 |
res = false; |
| 4878 |
break; |
| 4879 |
} |
| 4880 |
} |
| 4881 |
|
| 4882 |
/* Key uniqueness check is redundant for jsonb */ |
| 4883 |
} |
| 4884 |
else |
| 4885 |
res = false; |
| 4886 |
|
| 4887 |
*op->resvalue = BoolGetDatum(res); |
| 4888 |
} |
| 4889 |
|
| 4890 |
/* |
| 4891 |
* Evaluate a jsonpath against a document, both of which must have been |
| 4892 |
* evaluated and their values saved in op->d.jsonexpr.jsestate. |
| 4893 |
* |
| 4894 |
* If an error occurs during JsonPath* evaluation or when coercing its result |
| 4895 |
* to the RETURNING type, JsonExprState.error is set to true, provided the |
| 4896 |
* ON ERROR behavior is not ERROR. Similarly, if JsonPath{Query|Value}() found |
| 4897 |
* no matching items, JsonExprState.empty is set to true, provided the ON EMPTY |
| 4898 |
* behavior is not ERROR. That is to signal to the subsequent steps that check |
| 4899 |
* those flags to return the ON ERROR / ON EMPTY expression. |
| 4900 |
* |
| 4901 |
* Return value is the step address to be performed next. It will be one of |
| 4902 |
* jump_error, jump_empty, jump_eval_coercion, or jump_end, all given in |
| 4903 |
* op->d.jsonexpr.jsestate. |
| 4904 |
*/ |
| 4905 |
int |
| 4906 |
ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op, |
| 4907 |
ExprContext *econtext) |
| 4908 |
{ |
| 4909 |
JsonExprState *jsestate = op->d.jsonexpr.jsestate; |
| 4910 |
JsonExpr *jsexpr = jsestate->jsexpr; |
| 4911 |
Datum item; |
| 4912 |
JsonPath *path; |
| 4913 |
bool throw_error = jsexpr->on_error->btype == JSON_BEHAVIOR_ERROR; |
| 4914 |
bool error = false, |
| 4915 |
empty = false; |
| 4916 |
int jump_eval_coercion = jsestate->jump_eval_coercion; |
| 4917 |
char *val_string = NULL; |
| 4918 |
|
| 4919 |
item = jsestate->formatted_expr.value; |
| 4920 |
path = DatumGetJsonPathP(jsestate->pathspec.value); |
| 4921 |
|
| 4922 |
/* Set error/empty to false. */ |
| 4923 |
memset(&jsestate->error, 0, sizeof(NullableDatum)); |
| 4924 |
memset(&jsestate->empty, 0, sizeof(NullableDatum)); |
| 4925 |
|
| 4926 |
/* Also reset ErrorSaveContext contents for the next row. */ |
| 4927 |
if (jsestate->escontext.details_wanted) |
| 4928 |
{ |
| 4929 |
jsestate->escontext.error_data = NULL; |
| 4930 |
jsestate->escontext.details_wanted = false; |
| 4931 |
} |
| 4932 |
jsestate->escontext.error_occurred = false; |
| 4933 |
|
| 4934 |
switch (jsexpr->op) |
| 4935 |
{ |
| 4936 |
case JSON_EXISTS_OP: |
| 4937 |
{ |
| 4938 |
bool exists = JsonPathExists(item, path, |
| 4939 |
!throw_error ? &error : NULL, |
| 4940 |
jsestate->args); |
| 4941 |
|
| 4942 |
if (!error) |
| 4943 |
{ |
| 4944 |
*op->resnull = false; |
| 4945 |
*op->resvalue = BoolGetDatum(exists); |
| 4946 |
} |
| 4947 |
} |
| 4948 |
break; |
| 4949 |
|
| 4950 |
case JSON_QUERY_OP: |
| 4951 |
*op->resvalue = JsonPathQuery(item, path, jsexpr->wrapper, &empty, |
| 4952 |
!throw_error ? &error : NULL, |
| 4953 |
jsestate->args, |
| 4954 |
jsexpr->column_name); |
| 4955 |
|
| 4956 |
*op->resnull = (DatumGetPointer(*op->resvalue) == NULL); |
| 4957 |
break; |
| 4958 |
|
| 4959 |
case JSON_VALUE_OP: |
| 4960 |
{ |
| 4961 |
JsonbValue *jbv = JsonPathValue(item, path, &empty, |
| 4962 |
!throw_error ? &error : NULL, |
| 4963 |
jsestate->args, |
| 4964 |
jsexpr->column_name); |
| 4965 |
|
| 4966 |
if (jbv == NULL) |
| 4967 |
{ |
| 4968 |
/* Will be coerced with json_populate_type(), if needed. */ |
| 4969 |
*op->resvalue = (Datum) 0; |
| 4970 |
*op->resnull = true; |
| 4971 |
} |
| 4972 |
else if (!error && !empty) |
| 4973 |
{ |
| 4974 |
if (jsexpr->returning->typid == JSONOID || |
| 4975 |
jsexpr->returning->typid == JSONBOID) |
| 4976 |
{ |
| 4977 |
val_string = DatumGetCString(DirectFunctionCall1(jsonb_out, |
| 4978 |
JsonbPGetDatum(JsonbValueToJsonb(jbv)))); |
| 4979 |
} |
| 4980 |
else if (jsexpr->use_json_coercion) |
| 4981 |
{ |
| 4982 |
*op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(jbv)); |
| 4983 |
*op->resnull = false; |
| 4984 |
} |
| 4985 |
else |
| 4986 |
{ |
| 4987 |
val_string = ExecGetJsonValueItemString(jbv, op->resnull); |
| 4988 |
|
| 4989 |
/* |
| 4990 |
* Simply convert to the default RETURNING type (text) |
| 4991 |
* if no coercion needed. |
| 4992 |
*/ |
| 4993 |
if (!jsexpr->use_io_coercion) |
| 4994 |
*op->resvalue = DirectFunctionCall1(textin, |
| 4995 |
CStringGetDatum(val_string)); |
| 4996 |
} |
| 4997 |
} |
| 4998 |
break; |
| 4999 |
} |
| 5000 |
|
| 5001 |
/* JSON_TABLE_OP can't happen here */ |
| 5002 |
|
| 5003 |
default: |
| 5004 |
elog(ERROR, "unrecognized SQL/JSON expression op %d", |
| 5005 |
(int) jsexpr->op); |
| 5006 |
return false; |
| 5007 |
} |
| 5008 |
|
| 5009 |
/* |
| 5010 |
* Coerce the result value to the RETURNING type by calling its input |
| 5011 |
* function. |
| 5012 |
*/ |
| 5013 |
if (!*op->resnull && jsexpr->use_io_coercion) |
| 5014 |
{ |
| 5015 |
FunctionCallInfo fcinfo; |
| 5016 |
|
| 5017 |
Assert(jump_eval_coercion == -1); |
| 5018 |
fcinfo = jsestate->input_fcinfo; |
| 5019 |
Assert(fcinfo != NULL); |
| 5020 |
Assert(val_string != NULL); |
| 5021 |
fcinfo->args[0].value = PointerGetDatum(val_string); |
| 5022 |
fcinfo->args[0].isnull = *op->resnull; |
| 5023 |
|
| 5024 |
/* |
| 5025 |
* Second and third arguments are already set up in |
| 5026 |
* ExecInitJsonExpr(). |
| 5027 |
*/ |
| 5028 |
|
| 5029 |
fcinfo->isnull = false; |
| 5030 |
*op->resvalue = FunctionCallInvoke(fcinfo); |
| 5031 |
if (SOFT_ERROR_OCCURRED(&jsestate->escontext)) |
| 5032 |
error = true; |
| 5033 |
} |
| 5034 |
|
| 5035 |
/* |
| 5036 |
* When setting up the ErrorSaveContext (if needed) for capturing the |
| 5037 |
* errors that occur when coercing the JsonBehavior expression, set |
| 5038 |
* details_wanted to be able to show the actual error message as the |
| 5039 |
* DETAIL of the error message that tells that it is the JsonBehavior |
| 5040 |
* expression that caused the error; see ExecEvalJsonCoercionFinish(). |
| 5041 |
*/ |
| 5042 |
|
| 5043 |
/* Handle ON EMPTY. */ |
| 5044 |
if (empty) |
| 5045 |
{ |
| 5046 |
*op->resvalue = (Datum) 0; |
| 5047 |
*op->resnull = true; |
| 5048 |
if (jsexpr->on_empty) |
| 5049 |
{ |
| 5050 |
if (jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR) |
| 5051 |
{ |
| 5052 |
jsestate->empty.value = BoolGetDatum(true); |
| 5053 |
/* Set up to catch coercion errors of the ON EMPTY value. */ |
| 5054 |
jsestate->escontext.error_occurred = false; |
| 5055 |
jsestate->escontext.details_wanted = true; |
| 5056 |
/* Jump to end if the ON EMPTY behavior is to return NULL */ |
| 5057 |
return jsestate->jump_empty >= 0 ? jsestate->jump_empty : jsestate->jump_end; |
| 5058 |
} |
| 5059 |
} |
| 5060 |
else if (jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR) |
| 5061 |
{ |
| 5062 |
jsestate->error.value = BoolGetDatum(true); |
| 5063 |
/* Set up to catch coercion errors of the ON ERROR value. */ |
| 5064 |
jsestate->escontext.error_occurred = false; |
| 5065 |
jsestate->escontext.details_wanted = true; |
| 5066 |
Assert(!throw_error); |
| 5067 |
/* Jump to end if the ON ERROR behavior is to return NULL */ |
| 5068 |
return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end; |
| 5069 |
} |
| 5070 |
|
| 5071 |
if (jsexpr->column_name) |
| 5072 |
ereport(ERROR, |
| 5073 |
errcode(ERRCODE_NO_SQL_JSON_ITEM), |
| 5074 |
errmsg("no SQL/JSON item found for specified path of column \"%s\"", |
| 5075 |
jsexpr->column_name)); |
| 5076 |
else |
| 5077 |
ereport(ERROR, |
| 5078 |
errcode(ERRCODE_NO_SQL_JSON_ITEM), |
| 5079 |
errmsg("no SQL/JSON item found for specified path")); |
| 5080 |
} |
| 5081 |
|
| 5082 |
/* |
| 5083 |
* ON ERROR. Wouldn't get here if the behavior is ERROR, because they |
| 5084 |
* would have already been thrown. |
| 5085 |
*/ |
| 5086 |
if (error) |
| 5087 |
{ |
| 5088 |
Assert(!throw_error); |
| 5089 |
*op->resvalue = (Datum) 0; |
| 5090 |
*op->resnull = true; |
| 5091 |
jsestate->error.value = BoolGetDatum(true); |
| 5092 |
/* Set up to catch coercion errors of the ON ERROR value. */ |
| 5093 |
jsestate->escontext.error_occurred = false; |
| 5094 |
jsestate->escontext.details_wanted = true; |
| 5095 |
/* Jump to end if the ON ERROR behavior is to return NULL */ |
| 5096 |
return jsestate->jump_error >= 0 ? jsestate->jump_error : jsestate->jump_end; |
| 5097 |
} |
| 5098 |
|
| 5099 |
return jump_eval_coercion >= 0 ? jump_eval_coercion : jsestate->jump_end; |
| 5100 |
} |
| 5101 |
|
| 5102 |
/* |
| 5103 |
* Convert the given JsonbValue to its C string representation |
| 5104 |
* |
| 5105 |
* *resnull is set if the JsonbValue is a jbvNull. |
| 5106 |
*/ |
| 5107 |
static char * |
| 5108 |
ExecGetJsonValueItemString(JsonbValue *item, bool *resnull) |
| 5109 |
{ |
| 5110 |
*resnull = false; |
| 5111 |
|
| 5112 |
/* get coercion state reference and datum of the corresponding SQL type */ |
| 5113 |
switch (item->type) |
| 5114 |
{ |
| 5115 |
case jbvNull: |
| 5116 |
*resnull = true; |
| 5117 |
return NULL; |
| 5118 |
|
| 5119 |
case jbvString: |
| 5120 |
{ |
| 5121 |
char *str = palloc(item->val.string.len + 1); |
| 5122 |
|
| 5123 |
memcpy(str, item->val.string.val, item->val.string.len); |
| 5124 |
str[item->val.string.len] = '\0'; |
| 5125 |
return str; |
| 5126 |
} |
| 5127 |
|
| 5128 |
case jbvNumeric: |
| 5129 |
return DatumGetCString(DirectFunctionCall1(numeric_out, |
| 5130 |
NumericGetDatum(item->val.numeric))); |
| 5131 |
|
| 5132 |
case jbvBool: |
| 5133 |
return DatumGetCString(DirectFunctionCall1(boolout, |
| 5134 |
BoolGetDatum(item->val.boolean))); |
| 5135 |
|
| 5136 |
case jbvDatetime: |
| 5137 |
switch (item->val.datetime.typid) |
| 5138 |
{ |
| 5139 |
case DATEOID: |
| 5140 |
return DatumGetCString(DirectFunctionCall1(date_out, |
| 5141 |
item->val.datetime.value)); |
| 5142 |
case TIMEOID: |
| 5143 |
return DatumGetCString(DirectFunctionCall1(time_out, |
| 5144 |
item->val.datetime.value)); |
| 5145 |
case TIMETZOID: |
| 5146 |
return DatumGetCString(DirectFunctionCall1(timetz_out, |
| 5147 |
item->val.datetime.value)); |
| 5148 |
case TIMESTAMPOID: |
| 5149 |
return DatumGetCString(DirectFunctionCall1(timestamp_out, |
| 5150 |
item->val.datetime.value)); |
| 5151 |
case TIMESTAMPTZOID: |
| 5152 |
return DatumGetCString(DirectFunctionCall1(timestamptz_out, |
| 5153 |
item->val.datetime.value)); |
| 5154 |
default: |
| 5155 |
elog(ERROR, "unexpected jsonb datetime type oid %u", |
| 5156 |
item->val.datetime.typid); |
| 5157 |
} |
| 5158 |
break; |
| 5159 |
|
| 5160 |
case jbvArray: |
| 5161 |
case jbvObject: |
| 5162 |
case jbvBinary: |
| 5163 |
return DatumGetCString(DirectFunctionCall1(jsonb_out, |
| 5164 |
JsonbPGetDatum(JsonbValueToJsonb(item)))); |
| 5165 |
|
| 5166 |
default: |
| 5167 |
elog(ERROR, "unexpected jsonb value type %d", item->type); |
| 5168 |
} |
| 5169 |
|
| 5170 |
Assert(false); |
| 5171 |
*resnull = true; |
| 5172 |
return NULL; |
| 5173 |
} |
| 5174 |
|
| 5175 |
/* |
| 5176 |
* Coerce a jsonb value produced by ExecEvalJsonExprPath() or an ON ERROR / |
| 5177 |
* ON EMPTY behavior expression to the target type. |
| 5178 |
* |
| 5179 |
* Any soft errors that occur here will be checked by |
| 5180 |
* EEOP_JSONEXPR_COERCION_FINISH that will run after this. |
| 5181 |
*/ |
| 5182 |
void |
| 5183 |
ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op, |
| 5184 |
ExprContext *econtext) |
| 5185 |
{ |
| 5186 |
ErrorSaveContext *escontext = op->d.jsonexpr_coercion.escontext; |
| 5187 |
|
| 5188 |
/* |
| 5189 |
* Prepare to call json_populate_type() to coerce the boolean result of |
| 5190 |
* JSON_EXISTS_OP to the target type. If the target type is integer or a |
| 5191 |
* domain over integer, call the boolean-to-integer cast function instead, |
| 5192 |
* because the integer's input function (which is what |
| 5193 |
* json_populate_type() calls to coerce to scalar target types) doesn't |
| 5194 |
* accept boolean literals as valid input. We only have a special case |
| 5195 |
* for integer and domains thereof as it seems common to use those types |
| 5196 |
* for EXISTS columns in JSON_TABLE(). |
| 5197 |
*/ |
| 5198 |
if (op->d.jsonexpr_coercion.exists_coerce) |
| 5199 |
{ |
| 5200 |
if (op->d.jsonexpr_coercion.exists_cast_to_int) |
| 5201 |
{ |
| 5202 |
/* Check domain constraints if any. */ |
| 5203 |
if (op->d.jsonexpr_coercion.exists_check_domain && |
| 5204 |
!domain_check_safe(*op->resvalue, *op->resnull, |
| 5205 |
op->d.jsonexpr_coercion.targettype, |
| 5206 |
&op->d.jsonexpr_coercion.json_coercion_cache, |
| 5207 |
econtext->ecxt_per_query_memory, |
| 5208 |
(Node *) escontext)) |
| 5209 |
{ |
| 5210 |
*op->resnull = true; |
| 5211 |
*op->resvalue = (Datum) 0; |
| 5212 |
} |
| 5213 |
else |
| 5214 |
*op->resvalue = DirectFunctionCall1(bool_int4, *op->resvalue); |
| 5215 |
return; |
| 5216 |
} |
| 5217 |
|
| 5218 |
*op->resvalue = DirectFunctionCall1(jsonb_in, |
| 5219 |
DatumGetBool(*op->resvalue) ? |
| 5220 |
CStringGetDatum("true") : |
| 5221 |
CStringGetDatum("false")); |
| 5222 |
} |
| 5223 |
|
| 5224 |
*op->resvalue = json_populate_type(*op->resvalue, JSONBOID, |
| 5225 |
op->d.jsonexpr_coercion.targettype, |
| 5226 |
op->d.jsonexpr_coercion.targettypmod, |
| 5227 |
&op->d.jsonexpr_coercion.json_coercion_cache, |
| 5228 |
econtext->ecxt_per_query_memory, |
| 5229 |
op->resnull, |
| 5230 |
op->d.jsonexpr_coercion.omit_quotes, |
| 5231 |
(Node *) escontext); |
| 5232 |
} |
| 5233 |
|
| 5234 |
static char * |
| 5235 |
GetJsonBehaviorValueString(JsonBehavior *behavior) |
| 5236 |
{ |
| 5237 |
/* |
| 5238 |
* The order of array elements must correspond to the order of |
| 5239 |
* JsonBehaviorType members. |
| 5240 |
*/ |
| 5241 |
const char *behavior_names[] = |
| 5242 |
{ |
| 5243 |
"NULL", |
| 5244 |
"ERROR", |
| 5245 |
"EMPTY", |
| 5246 |
"TRUE", |
| 5247 |
"FALSE", |
| 5248 |
"UNKNOWN", |
| 5249 |
"EMPTY ARRAY", |
| 5250 |
"EMPTY OBJECT", |
| 5251 |
"DEFAULT" |
| 5252 |
}; |
| 5253 |
|
| 5254 |
return pstrdup(behavior_names[behavior->btype]); |
| 5255 |
} |
| 5256 |
|
| 5257 |
/* |
| 5258 |
* Checks if an error occurred in ExecEvalJsonCoercion(). If so, this sets |
| 5259 |
* JsonExprState.error to trigger the ON ERROR handling steps, unless the |
| 5260 |
* error is thrown when coercing a JsonBehavior value. |
| 5261 |
*/ |
| 5262 |
void |
| 5263 |
ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op) |
| 5264 |
{ |
| 5265 |
JsonExprState *jsestate = op->d.jsonexpr.jsestate; |
| 5266 |
|
| 5267 |
if (SOFT_ERROR_OCCURRED(&jsestate->escontext)) |
| 5268 |
{ |
| 5269 |
/* |
| 5270 |
* jsestate->error or jsestate->empty being set means that the error |
| 5271 |
* occurred when coercing the JsonBehavior value. Throw the error in |
| 5272 |
* that case with the actual coercion error message shown in the |
| 5273 |
* DETAIL part. |
| 5274 |
*/ |
| 5275 |
if (DatumGetBool(jsestate->error.value)) |
| 5276 |
ereport(ERROR, |
| 5277 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 5278 |
/*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */ |
| 5279 |
errmsg("could not coerce %s expression (%s) to the RETURNING type", |
| 5280 |
"ON ERROR", |
| 5281 |
GetJsonBehaviorValueString(jsestate->jsexpr->on_error)), |
| 5282 |
errdetail("%s", jsestate->escontext.error_data->message))); |
| 5283 |
else if (DatumGetBool(jsestate->empty.value)) |
| 5284 |
ereport(ERROR, |
| 5285 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 5286 |
/*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */ |
| 5287 |
errmsg("could not coerce %s expression (%s) to the RETURNING type", |
| 5288 |
"ON EMPTY", |
| 5289 |
GetJsonBehaviorValueString(jsestate->jsexpr->on_empty)), |
| 5290 |
errdetail("%s", jsestate->escontext.error_data->message))); |
| 5291 |
|
| 5292 |
*op->resvalue = (Datum) 0; |
| 5293 |
*op->resnull = true; |
| 5294 |
|
| 5295 |
jsestate->error.value = BoolGetDatum(true); |
| 5296 |
|
| 5297 |
/* |
| 5298 |
* Reset for next use such as for catching errors when coercing a |
| 5299 |
* JsonBehavior expression. |
| 5300 |
*/ |
| 5301 |
jsestate->escontext.error_occurred = false; |
| 5302 |
jsestate->escontext.details_wanted = true; |
| 5303 |
} |
| 5304 |
} |
| 5305 |
|
| 5306 |
/* |
| 5307 |
* ExecEvalGroupingFunc |
| 5308 |
* |
| 5309 |
* Computes a bitmask with a bit for each (unevaluated) argument expression |
| 5310 |
* (rightmost arg is least significant bit). |
| 5311 |
* |
| 5312 |
* A bit is set if the corresponding expression is NOT part of the set of |
| 5313 |
* grouping expressions in the current grouping set. |
| 5314 |
*/ |
| 5315 |
void |
| 5316 |
ExecEvalGroupingFunc(ExprState *state, ExprEvalStep *op) |
| 5317 |
{ |
| 5318 |
AggState *aggstate = castNode(AggState, state->parent); |
| 5319 |
int result = 0; |
| 5320 |
Bitmapset *grouped_cols = aggstate->grouped_cols; |
| 5321 |
ListCell *lc; |
| 5322 |
|
| 5323 |
foreach(lc, op->d.grouping_func.clauses) |
| 5324 |
{ |
| 5325 |
int attnum = lfirst_int(lc); |
| 5326 |
|
| 5327 |
result <<= 1; |
| 5328 |
|
| 5329 |
if (!bms_is_member(attnum, grouped_cols)) |
| 5330 |
result |= 1; |
| 5331 |
} |
| 5332 |
|
| 5333 |
*op->resvalue = Int32GetDatum(result); |
| 5334 |
*op->resnull = false; |
| 5335 |
} |
| 5336 |
|
| 5337 |
/* |
| 5338 |
* ExecEvalMergeSupportFunc |
| 5339 |
* |
| 5340 |
* Returns information about the current MERGE action for its RETURNING list. |
| 5341 |
*/ |
| 5342 |
void |
| 5343 |
ExecEvalMergeSupportFunc(ExprState *state, ExprEvalStep *op, |
| 5344 |
ExprContext *econtext) |
| 5345 |
{ |
| 5346 |
ModifyTableState *mtstate = castNode(ModifyTableState, state->parent); |
| 5347 |
MergeActionState *relaction = mtstate->mt_merge_action; |
| 5348 |
|
| 5349 |
if (!relaction) |
| 5350 |
elog(ERROR, "no merge action in progress"); |
| 5351 |
|
| 5352 |
/* Return the MERGE action ("INSERT", "UPDATE", or "DELETE") */ |
| 5353 |
switch (relaction->mas_action->commandType) |
| 5354 |
{ |
| 5355 |
case CMD_INSERT: |
| 5356 |
*op->resvalue = PointerGetDatum(cstring_to_text_with_len("INSERT", 6)); |
| 5357 |
*op->resnull = false; |
| 5358 |
break; |
| 5359 |
case CMD_UPDATE: |
| 5360 |
*op->resvalue = PointerGetDatum(cstring_to_text_with_len("UPDATE", 6)); |
| 5361 |
*op->resnull = false; |
| 5362 |
break; |
| 5363 |
case CMD_DELETE: |
| 5364 |
*op->resvalue = PointerGetDatum(cstring_to_text_with_len("DELETE", 6)); |
| 5365 |
*op->resnull = false; |
| 5366 |
break; |
| 5367 |
case CMD_NOTHING: |
| 5368 |
elog(ERROR, "unexpected merge action: DO NOTHING"); |
| 5369 |
break; |
| 5370 |
default: |
| 5371 |
elog(ERROR, "unrecognized commandType: %d", |
| 5372 |
(int) relaction->mas_action->commandType); |
| 5373 |
} |
| 5374 |
} |
| 5375 |
|
| 5376 |
/* |
| 5377 |
* Hand off evaluation of a subplan to nodeSubplan.c |
| 5378 |
*/ |
| 5379 |
void |
| 5380 |
ExecEvalSubPlan(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 5381 |
{ |
| 5382 |
SubPlanState *sstate = op->d.subplan.sstate; |
| 5383 |
|
| 5384 |
/* could potentially be nested, so make sure there's enough stack */ |
| 5385 |
check_stack_depth(); |
| 5386 |
|
| 5387 |
*op->resvalue = ExecSubPlan(sstate, econtext, op->resnull); |
| 5388 |
} |
| 5389 |
|
| 5390 |
/* |
| 5391 |
* Evaluate a wholerow Var expression. |
| 5392 |
* |
| 5393 |
* Returns a Datum whose value is the value of a whole-row range variable |
| 5394 |
* with respect to given expression context. |
| 5395 |
*/ |
| 5396 |
void |
| 5397 |
ExecEvalWholeRowVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext) |
| 5398 |
{ |
| 5399 |
Var *variable = op->d.wholerow.var; |
| 5400 |
TupleTableSlot *slot = NULL; |
| 5401 |
TupleDesc output_tupdesc; |
| 5402 |
MemoryContext oldcontext; |
| 5403 |
HeapTupleHeader dtuple; |
| 5404 |
HeapTuple tuple; |
| 5405 |
|
| 5406 |
/* This was checked by ExecInitExpr */ |
| 5407 |
Assert(variable->varattno == InvalidAttrNumber); |
| 5408 |
|
| 5409 |
/* Get the input slot we want */ |
| 5410 |
switch (variable->varno) |
| 5411 |
{ |
| 5412 |
case INNER_VAR: |
| 5413 |
/* get the tuple from the inner node */ |
| 5414 |
slot = econtext->ecxt_innertuple; |
| 5415 |
break; |
| 5416 |
|
| 5417 |
case OUTER_VAR: |
| 5418 |
/* get the tuple from the outer node */ |
| 5419 |
slot = econtext->ecxt_outertuple; |
| 5420 |
break; |
| 5421 |
|
| 5422 |
/* INDEX_VAR is handled by default case */ |
| 5423 |
|
| 5424 |
default: |
| 5425 |
|
| 5426 |
/* |
| 5427 |
* Get the tuple from the relation being scanned. |
| 5428 |
* |
| 5429 |
* By default, this uses the "scan" tuple slot, but a wholerow Var |
| 5430 |
* in the RETURNING list may explicitly refer to OLD/NEW. If the |
| 5431 |
* OLD/NEW row doesn't exist, we just return NULL. |
| 5432 |
*/ |
| 5433 |
switch (variable->varreturningtype) |
| 5434 |
{ |
| 5435 |
case VAR_RETURNING_DEFAULT: |
| 5436 |
slot = econtext->ecxt_scantuple; |
| 5437 |
break; |
| 5438 |
|
| 5439 |
case VAR_RETURNING_OLD: |
| 5440 |
if (state->flags & EEO_FLAG_OLD_IS_NULL) |
| 5441 |
{ |
| 5442 |
*op->resvalue = (Datum) 0; |
| 5443 |
*op->resnull = true; |
| 5444 |
return; |
| 5445 |
} |
| 5446 |
slot = econtext->ecxt_oldtuple; |
| 5447 |
break; |
| 5448 |
|
| 5449 |
case VAR_RETURNING_NEW: |
| 5450 |
if (state->flags & EEO_FLAG_NEW_IS_NULL) |
| 5451 |
{ |
| 5452 |
*op->resvalue = (Datum) 0; |
| 5453 |
*op->resnull = true; |
| 5454 |
return; |
| 5455 |
} |
| 5456 |
slot = econtext->ecxt_newtuple; |
| 5457 |
break; |
| 5458 |
} |
| 5459 |
break; |
| 5460 |
} |
| 5461 |
|
| 5462 |
/* Apply the junkfilter if any */ |
| 5463 |
if (op->d.wholerow.junkFilter != NULL) |
| 5464 |
slot = ExecFilterJunk(op->d.wholerow.junkFilter, slot); |
| 5465 |
|
| 5466 |
/* |
| 5467 |
* If first time through, obtain tuple descriptor and check compatibility. |
| 5468 |
* |
| 5469 |
* XXX: It'd be great if this could be moved to the expression |
| 5470 |
* initialization phase, but due to using slots that's currently not |
| 5471 |
* feasible. |
| 5472 |
*/ |
| 5473 |
if (op->d.wholerow.first) |
| 5474 |
{ |
| 5475 |
/* optimistically assume we don't need slow path */ |
| 5476 |
op->d.wholerow.slow = false; |
| 5477 |
|
| 5478 |
/* |
| 5479 |
* If the Var identifies a named composite type, we must check that |
| 5480 |
* the actual tuple type is compatible with it. |
| 5481 |
*/ |
| 5482 |
if (variable->vartype != RECORDOID) |
| 5483 |
{ |
| 5484 |
TupleDesc var_tupdesc; |
| 5485 |
TupleDesc slot_tupdesc; |
| 5486 |
|
| 5487 |
/* |
| 5488 |
* We really only care about numbers of attributes and data types. |
| 5489 |
* Also, we can ignore type mismatch on columns that are dropped |
| 5490 |
* in the destination type, so long as (1) the physical storage |
| 5491 |
* matches or (2) the actual column value is NULL. Case (1) is |
| 5492 |
* helpful in some cases involving out-of-date cached plans, while |
| 5493 |
* case (2) is expected behavior in situations such as an INSERT |
| 5494 |
* into a table with dropped columns (the planner typically |
| 5495 |
* generates an INT4 NULL regardless of the dropped column type). |
| 5496 |
* If we find a dropped column and cannot verify that case (1) |
| 5497 |
* holds, we have to use the slow path to check (2) for each row. |
| 5498 |
* |
| 5499 |
* If vartype is a domain over composite, just look through that |
| 5500 |
* to the base composite type. |
| 5501 |
*/ |
| 5502 |
var_tupdesc = lookup_rowtype_tupdesc_domain(variable->vartype, |
| 5503 |
-1, false); |
| 5504 |
|
| 5505 |
slot_tupdesc = slot->tts_tupleDescriptor; |
| 5506 |
|
| 5507 |
if (var_tupdesc->natts != slot_tupdesc->natts) |
| 5508 |
ereport(ERROR, |
| 5509 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 5510 |
errmsg("table row type and query-specified row type do not match"), |
| 5511 |
errdetail_plural("Table row contains %d attribute, but query expects %d.", |
| 5512 |
"Table row contains %d attributes, but query expects %d.", |
| 5513 |
slot_tupdesc->natts, |
| 5514 |
slot_tupdesc->natts, |
| 5515 |
var_tupdesc->natts))); |
| 5516 |
|
| 5517 |
for (int i = 0; i < var_tupdesc->natts; i++) |
| 5518 |
{ |
| 5519 |
Form_pg_attribute vattr = TupleDescAttr(var_tupdesc, i); |
| 5520 |
Form_pg_attribute sattr = TupleDescAttr(slot_tupdesc, i); |
| 5521 |
|
| 5522 |
if (vattr->atttypid == sattr->atttypid) |
| 5523 |
continue; /* no worries */ |
| 5524 |
if (!vattr->attisdropped) |
| 5525 |
ereport(ERROR, |
| 5526 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 5527 |
errmsg("table row type and query-specified row type do not match"), |
| 5528 |
errdetail("Table has type %s at ordinal position %d, but query expects %s.", |
| 5529 |
format_type_be(sattr->atttypid), |
| 5530 |
i + 1, |
| 5531 |
format_type_be(vattr->atttypid)))); |
| 5532 |
|
| 5533 |
if (vattr->attlen != sattr->attlen || |
| 5534 |
vattr->attalign != sattr->attalign) |
| 5535 |
op->d.wholerow.slow = true; /* need to check for nulls */ |
| 5536 |
} |
| 5537 |
|
| 5538 |
/* |
| 5539 |
* Use the variable's declared rowtype as the descriptor for the |
| 5540 |
* output values. In particular, we *must* absorb any |
| 5541 |
* attisdropped markings. |
| 5542 |
*/ |
| 5543 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 5544 |
output_tupdesc = CreateTupleDescCopy(var_tupdesc); |
| 5545 |
MemoryContextSwitchTo(oldcontext); |
| 5546 |
|
| 5547 |
ReleaseTupleDesc(var_tupdesc); |
| 5548 |
} |
| 5549 |
else |
| 5550 |
{ |
| 5551 |
/* |
| 5552 |
* In the RECORD case, we use the input slot's rowtype as the |
| 5553 |
* descriptor for the output values, modulo possibly assigning new |
| 5554 |
* column names below. |
| 5555 |
*/ |
| 5556 |
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); |
| 5557 |
output_tupdesc = CreateTupleDescCopy(slot->tts_tupleDescriptor); |
| 5558 |
MemoryContextSwitchTo(oldcontext); |
| 5559 |
|
| 5560 |
/* |
| 5561 |
* It's possible that the input slot is a relation scan slot and |
| 5562 |
* so is marked with that relation's rowtype. But we're supposed |
| 5563 |
* to be returning RECORD, so reset to that. |
| 5564 |
*/ |
| 5565 |
output_tupdesc->tdtypeid = RECORDOID; |
| 5566 |
output_tupdesc->tdtypmod = -1; |
| 5567 |
|
| 5568 |
/* |
| 5569 |
* We already got the correct physical datatype info above, but |
| 5570 |
* now we should try to find the source RTE and adopt its column |
| 5571 |
* aliases, since it's unlikely that the input slot has the |
| 5572 |
* desired names. |
| 5573 |
* |
| 5574 |
* If we can't locate the RTE, assume the column names we've got |
| 5575 |
* are OK. (As of this writing, the only cases where we can't |
| 5576 |
* locate the RTE are in execution of trigger WHEN clauses, and |
| 5577 |
* then the Var will have the trigger's relation's rowtype, so its |
| 5578 |
* names are fine.) Also, if the creator of the RTE didn't bother |
| 5579 |
* to fill in an eref field, assume our column names are OK. (This |
| 5580 |
* happens in COPY, and perhaps other places.) |
| 5581 |
*/ |
| 5582 |
if (econtext->ecxt_estate && |
| 5583 |
variable->varno <= econtext->ecxt_estate->es_range_table_size) |
| 5584 |
{ |
| 5585 |
RangeTblEntry *rte = exec_rt_fetch(variable->varno, |
| 5586 |
econtext->ecxt_estate); |
| 5587 |
|
| 5588 |
if (rte->eref) |
| 5589 |
ExecTypeSetColNames(output_tupdesc, rte->eref->colnames); |
| 5590 |
} |
| 5591 |
} |
| 5592 |
|
| 5593 |
/* Bless the tupdesc if needed, and save it in the execution state */ |
| 5594 |
op->d.wholerow.tupdesc = BlessTupleDesc(output_tupdesc); |
| 5595 |
|
| 5596 |
op->d.wholerow.first = false; |
| 5597 |
} |
| 5598 |
|
| 5599 |
/* |
| 5600 |
* Make sure all columns of the slot are accessible in the slot's |
| 5601 |
* Datum/isnull arrays. |
| 5602 |
*/ |
| 5603 |
slot_getallattrs(slot); |
| 5604 |
|
| 5605 |
if (op->d.wholerow.slow) |
| 5606 |
{ |
| 5607 |
/* Check to see if any dropped attributes are non-null */ |
| 5608 |
TupleDesc tupleDesc = slot->tts_tupleDescriptor; |
| 5609 |
TupleDesc var_tupdesc = op->d.wholerow.tupdesc; |
| 5610 |
|
| 5611 |
Assert(var_tupdesc->natts == tupleDesc->natts); |
| 5612 |
|
| 5613 |
for (int i = 0; i < var_tupdesc->natts; i++) |
| 5614 |
{ |
| 5615 |
CompactAttribute *vattr = TupleDescCompactAttr(var_tupdesc, i); |
| 5616 |
CompactAttribute *sattr = TupleDescCompactAttr(tupleDesc, i); |
| 5617 |
|
| 5618 |
if (!vattr->attisdropped) |
| 5619 |
continue; /* already checked non-dropped cols */ |
| 5620 |
if (slot->tts_isnull[i]) |
| 5621 |
continue; /* null is always okay */ |
| 5622 |
if (vattr->attlen != sattr->attlen || |
| 5623 |
vattr->attalignby != sattr->attalignby) |
| 5624 |
ereport(ERROR, |
| 5625 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 5626 |
errmsg("table row type and query-specified row type do not match"), |
| 5627 |
errdetail("Physical storage mismatch on dropped attribute at ordinal position %d.", |
| 5628 |
i + 1))); |
| 5629 |
} |
| 5630 |
} |
| 5631 |
|
| 5632 |
/* |
| 5633 |
* Build a composite datum, making sure any toasted fields get detoasted. |
| 5634 |
* |
| 5635 |
* (Note: it is critical that we not change the slot's state here.) |
| 5636 |
*/ |
| 5637 |
tuple = toast_build_flattened_tuple(slot->tts_tupleDescriptor, |
| 5638 |
slot->tts_values, |
| 5639 |
slot->tts_isnull); |
| 5640 |
dtuple = tuple->t_data; |
| 5641 |
|
| 5642 |
/* |
| 5643 |
* Label the datum with the composite type info we identified before. |
| 5644 |
* |
| 5645 |
* (Note: we could skip doing this by passing op->d.wholerow.tupdesc to |
| 5646 |
* the tuple build step; but that seems a tad risky so let's not.) |
| 5647 |
*/ |
| 5648 |
HeapTupleHeaderSetTypeId(dtuple, op->d.wholerow.tupdesc->tdtypeid); |
| 5649 |
HeapTupleHeaderSetTypMod(dtuple, op->d.wholerow.tupdesc->tdtypmod); |
| 5650 |
|
| 5651 |
*op->resvalue = PointerGetDatum(dtuple); |
| 5652 |
*op->resnull = false; |
| 5653 |
} |
| 5654 |
|
| 5655 |
void |
| 5656 |
ExecEvalSysVar(ExprState *state, ExprEvalStep *op, ExprContext *econtext, |
| 5657 |
TupleTableSlot *slot) |
| 5658 |
{ |
| 5659 |
Datum d; |
| 5660 |
|
| 5661 |
/* OLD/NEW system attribute is NULL if OLD/NEW row is NULL */ |
| 5662 |
if ((op->d.var.varreturningtype == VAR_RETURNING_OLD && |
| 5663 |
state->flags & EEO_FLAG_OLD_IS_NULL) || |
| 5664 |
(op->d.var.varreturningtype == VAR_RETURNING_NEW && |
| 5665 |
state->flags & EEO_FLAG_NEW_IS_NULL)) |
| 5666 |
{ |
| 5667 |
*op->resvalue = (Datum) 0; |
| 5668 |
*op->resnull = true; |
| 5669 |
return; |
| 5670 |
} |
| 5671 |
|
| 5672 |
/* slot_getsysattr has sufficient defenses against bad attnums */ |
| 5673 |
d = slot_getsysattr(slot, |
| 5674 |
op->d.var.attnum, |
| 5675 |
op->resnull); |
| 5676 |
*op->resvalue = d; |
| 5677 |
/* this ought to be unreachable, but it's cheap enough to check */ |
| 5678 |
if (unlikely(*op->resnull)) |
| 5679 |
elog(ERROR, "failed to fetch attribute from slot"); |
| 5680 |
} |
| 5681 |
|
| 5682 |
/* |
| 5683 |
* Transition value has not been initialized. This is the first non-NULL input |
| 5684 |
* value for a group. We use it as the initial value for transValue. |
| 5685 |
*/ |
| 5686 |
void |
| 5687 |
ExecAggInitGroup(AggState *aggstate, AggStatePerTrans pertrans, AggStatePerGroup pergroup, |
| 5688 |
ExprContext *aggcontext) |
| 5689 |
{ |
| 5690 |
FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
| 5691 |
MemoryContext oldContext; |
| 5692 |
|
| 5693 |
/* |
| 5694 |
* We must copy the datum into aggcontext if it is pass-by-ref. We do not |
| 5695 |
* need to pfree the old transValue, since it's NULL. (We already checked |
| 5696 |
* that the agg's input type is binary-compatible with its transtype, so |
| 5697 |
* straight copy here is OK.) |
| 5698 |
*/ |
| 5699 |
oldContext = MemoryContextSwitchTo(aggcontext->ecxt_per_tuple_memory); |
| 5700 |
pergroup->transValue = datumCopy(fcinfo->args[1].value, |
| 5701 |
pertrans->transtypeByVal, |
| 5702 |
pertrans->transtypeLen); |
| 5703 |
pergroup->transValueIsNull = false; |
| 5704 |
pergroup->noTransValue = false; |
| 5705 |
MemoryContextSwitchTo(oldContext); |
| 5706 |
} |
| 5707 |
|
| 5708 |
/* |
| 5709 |
* Ensure that the new transition value is stored in the aggcontext, |
| 5710 |
* rather than the per-tuple context. This should be invoked only when |
| 5711 |
* we know (a) the transition data type is pass-by-reference, and (b) |
| 5712 |
* the newValue is distinct from the oldValue. |
| 5713 |
* |
| 5714 |
* NB: This can change the current memory context. |
| 5715 |
* |
| 5716 |
* We copy the presented newValue into the aggcontext, except when the datum |
| 5717 |
* points to a R/W expanded object that is already a child of the aggcontext, |
| 5718 |
* in which case we need not copy. We then delete the oldValue, if not null. |
| 5719 |
* |
| 5720 |
* If the presented datum points to a R/W expanded object that is a child of |
| 5721 |
* some other context, ideally we would just reparent it under the aggcontext. |
| 5722 |
* Unfortunately, that doesn't work easily, and it wouldn't help anyway for |
| 5723 |
* aggregate-aware transfns. We expect that a transfn that deals in expanded |
| 5724 |
* objects and is aware of the memory management conventions for aggregate |
| 5725 |
* transition values will (1) on first call, return a R/W expanded object that |
| 5726 |
* is already in the right context, allowing us to do nothing here, and (2) on |
| 5727 |
* subsequent calls, modify and return that same object, so that control |
| 5728 |
* doesn't even reach here. However, if we have a generic transfn that |
| 5729 |
* returns a new R/W expanded object (probably in the per-tuple context), |
| 5730 |
* reparenting that result would cause problems. We'd pass that R/W object to |
| 5731 |
* the next invocation of the transfn, and then it would be at liberty to |
| 5732 |
* change or delete that object, and if it deletes it then our own attempt to |
| 5733 |
* delete the now-old transvalue afterwards would be a double free. We avoid |
| 5734 |
* this problem by forcing the stored transvalue to always be a flat |
| 5735 |
* non-expanded object unless the transfn is visibly doing aggregate-aware |
| 5736 |
* memory management. This is somewhat inefficient, but the best answer to |
| 5737 |
* that is to write a smarter transfn. |
| 5738 |
*/ |
| 5739 |
Datum |
| 5740 |
ExecAggCopyTransValue(AggState *aggstate, AggStatePerTrans pertrans, |
| 5741 |
Datum newValue, bool newValueIsNull, |
| 5742 |
Datum oldValue, bool oldValueIsNull) |
| 5743 |
{ |
| 5744 |
Assert(newValue != oldValue); |
| 5745 |
|
| 5746 |
if (!newValueIsNull) |
| 5747 |
{ |
| 5748 |
MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory); |
| 5749 |
if (DatumIsReadWriteExpandedObject(newValue, |
| 5750 |
false, |
| 5751 |
pertrans->transtypeLen) && |
| 5752 |
MemoryContextGetParent(DatumGetEOHP(newValue)->eoh_context) == CurrentMemoryContext) |
| 5753 |
/* do nothing */ ; |
| 5754 |
else |
| 5755 |
newValue = datumCopy(newValue, |
| 5756 |
pertrans->transtypeByVal, |
| 5757 |
pertrans->transtypeLen); |
| 5758 |
} |
| 5759 |
else |
| 5760 |
{ |
| 5761 |
/* |
| 5762 |
* Ensure that AggStatePerGroup->transValue ends up being 0, so |
| 5763 |
* callers can safely compare newValue/oldValue without having to |
| 5764 |
* check their respective nullness. |
| 5765 |
*/ |
| 5766 |
newValue = (Datum) 0; |
| 5767 |
} |
| 5768 |
|
| 5769 |
if (!oldValueIsNull) |
| 5770 |
{ |
| 5771 |
if (DatumIsReadWriteExpandedObject(oldValue, |
| 5772 |
false, |
| 5773 |
pertrans->transtypeLen)) |
| 5774 |
DeleteExpandedObject(oldValue); |
| 5775 |
else |
| 5776 |
pfree(DatumGetPointer(oldValue)); |
| 5777 |
} |
| 5778 |
|
| 5779 |
return newValue; |
| 5780 |
} |
| 5781 |
|
| 5782 |
/* |
| 5783 |
* ExecEvalPreOrderedDistinctSingle |
| 5784 |
* Returns true when the aggregate transition value Datum is distinct |
| 5785 |
* from the previous input Datum and returns false when the input Datum |
| 5786 |
* matches the previous input Datum. |
| 5787 |
*/ |
| 5788 |
bool |
| 5789 |
ExecEvalPreOrderedDistinctSingle(AggState *aggstate, AggStatePerTrans pertrans) |
| 5790 |
{ |
| 5791 |
Datum value = pertrans->transfn_fcinfo->args[1].value; |
| 5792 |
bool isnull = pertrans->transfn_fcinfo->args[1].isnull; |
| 5793 |
|
| 5794 |
if (!pertrans->haslast || |
| 5795 |
pertrans->lastisnull != isnull || |
| 5796 |
(!isnull && !DatumGetBool(FunctionCall2Coll(&pertrans->equalfnOne, |
| 5797 |
pertrans->aggCollation, |
| 5798 |
pertrans->lastdatum, value)))) |
| 5799 |
{ |
| 5800 |
if (pertrans->haslast && !pertrans->inputtypeByVal && |
| 5801 |
!pertrans->lastisnull) |
| 5802 |
pfree(DatumGetPointer(pertrans->lastdatum)); |
| 5803 |
|
| 5804 |
pertrans->haslast = true; |
| 5805 |
if (!isnull) |
| 5806 |
{ |
| 5807 |
MemoryContext oldContext; |
| 5808 |
|
| 5809 |
oldContext = MemoryContextSwitchTo(aggstate->curaggcontext->ecxt_per_tuple_memory); |
| 5810 |
|
| 5811 |
pertrans->lastdatum = datumCopy(value, pertrans->inputtypeByVal, |
| 5812 |
pertrans->inputtypeLen); |
| 5813 |
|
| 5814 |
MemoryContextSwitchTo(oldContext); |
| 5815 |
} |
| 5816 |
else |
| 5817 |
pertrans->lastdatum = (Datum) 0; |
| 5818 |
pertrans->lastisnull = isnull; |
| 5819 |
return true; |
| 5820 |
} |
| 5821 |
|
| 5822 |
return false; |
| 5823 |
} |
| 5824 |
|
| 5825 |
/* |
| 5826 |
* ExecEvalPreOrderedDistinctMulti |
| 5827 |
* Returns true when the aggregate input is distinct from the previous |
| 5828 |
* input and returns false when the input matches the previous input, or |
| 5829 |
* when there was no previous input. |
| 5830 |
*/ |
| 5831 |
bool |
| 5832 |
ExecEvalPreOrderedDistinctMulti(AggState *aggstate, AggStatePerTrans pertrans) |
| 5833 |
{ |
| 5834 |
ExprContext *tmpcontext = aggstate->tmpcontext; |
| 5835 |
bool isdistinct = false; /* for now */ |
| 5836 |
TupleTableSlot *save_outer; |
| 5837 |
TupleTableSlot *save_inner; |
| 5838 |
|
| 5839 |
for (int i = 0; i < pertrans->numTransInputs; i++) |
| 5840 |
{ |
| 5841 |
pertrans->sortslot->tts_values[i] = pertrans->transfn_fcinfo->args[i + 1].value; |
| 5842 |
pertrans->sortslot->tts_isnull[i] = pertrans->transfn_fcinfo->args[i + 1].isnull; |
| 5843 |
} |
| 5844 |
|
| 5845 |
ExecClearTuple(pertrans->sortslot); |
| 5846 |
pertrans->sortslot->tts_nvalid = pertrans->numInputs; |
| 5847 |
ExecStoreVirtualTuple(pertrans->sortslot); |
| 5848 |
|
| 5849 |
/* save the previous slots before we overwrite them */ |
| 5850 |
save_outer = tmpcontext->ecxt_outertuple; |
| 5851 |
save_inner = tmpcontext->ecxt_innertuple; |
| 5852 |
|
| 5853 |
tmpcontext->ecxt_outertuple = pertrans->sortslot; |
| 5854 |
tmpcontext->ecxt_innertuple = pertrans->uniqslot; |
| 5855 |
|
| 5856 |
if (!pertrans->haslast || |
| 5857 |
!ExecQual(pertrans->equalfnMulti, tmpcontext)) |
| 5858 |
{ |
| 5859 |
if (pertrans->haslast) |
| 5860 |
ExecClearTuple(pertrans->uniqslot); |
| 5861 |
|
| 5862 |
pertrans->haslast = true; |
| 5863 |
ExecCopySlot(pertrans->uniqslot, pertrans->sortslot); |
| 5864 |
|
| 5865 |
isdistinct = true; |
| 5866 |
} |
| 5867 |
|
| 5868 |
/* restore the original slots */ |
| 5869 |
tmpcontext->ecxt_outertuple = save_outer; |
| 5870 |
tmpcontext->ecxt_innertuple = save_inner; |
| 5871 |
|
| 5872 |
return isdistinct; |
| 5873 |
} |
| 5874 |
|
| 5875 |
/* |
| 5876 |
* Invoke ordered transition function, with a datum argument. |
| 5877 |
*/ |
| 5878 |
void |
| 5879 |
ExecEvalAggOrderedTransDatum(ExprState *state, ExprEvalStep *op, |
| 5880 |
ExprContext *econtext) |
| 5881 |
{ |
| 5882 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 5883 |
int setno = op->d.agg_trans.setno; |
| 5884 |
|
| 5885 |
tuplesort_putdatum(pertrans->sortstates[setno], |
| 5886 |
*op->resvalue, *op->resnull); |
| 5887 |
} |
| 5888 |
|
| 5889 |
/* |
| 5890 |
* Invoke ordered transition function, with a tuple argument. |
| 5891 |
*/ |
| 5892 |
void |
| 5893 |
ExecEvalAggOrderedTransTuple(ExprState *state, ExprEvalStep *op, |
| 5894 |
ExprContext *econtext) |
| 5895 |
{ |
| 5896 |
AggStatePerTrans pertrans = op->d.agg_trans.pertrans; |
| 5897 |
int setno = op->d.agg_trans.setno; |
| 5898 |
|
| 5899 |
ExecClearTuple(pertrans->sortslot); |
| 5900 |
pertrans->sortslot->tts_nvalid = pertrans->numInputs; |
| 5901 |
ExecStoreVirtualTuple(pertrans->sortslot); |
| 5902 |
tuplesort_puttupleslot(pertrans->sortstates[setno], pertrans->sortslot); |
| 5903 |
} |
| 5904 |
|
| 5905 |
/* implementation of transition function invocation for byval types */ |
| 5906 |
static pg_always_inline void |
| 5907 |
ExecAggPlainTransByVal(AggState *aggstate, AggStatePerTrans pertrans, |
| 5908 |
AggStatePerGroup pergroup, |
| 5909 |
ExprContext *aggcontext, int setno) |
| 5910 |
{ |
| 5911 |
FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
| 5912 |
MemoryContext oldContext; |
| 5913 |
Datum newVal; |
| 5914 |
|
| 5915 |
/* cf. select_current_set() */ |
| 5916 |
aggstate->curaggcontext = aggcontext; |
| 5917 |
aggstate->current_set = setno; |
| 5918 |
|
| 5919 |
/* set up aggstate->curpertrans for AggGetAggref() */ |
| 5920 |
aggstate->curpertrans = pertrans; |
| 5921 |
|
| 5922 |
/* invoke transition function in per-tuple context */ |
| 5923 |
oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
| 5924 |
|
| 5925 |
fcinfo->args[0].value = pergroup->transValue; |
| 5926 |
fcinfo->args[0].isnull = pergroup->transValueIsNull; |
| 5927 |
fcinfo->isnull = false; /* just in case transfn doesn't set it */ |
| 5928 |
|
| 5929 |
newVal = FunctionCallInvoke(fcinfo); |
| 5930 |
|
| 5931 |
pergroup->transValue = newVal; |
| 5932 |
pergroup->transValueIsNull = fcinfo->isnull; |
| 5933 |
|
| 5934 |
MemoryContextSwitchTo(oldContext); |
| 5935 |
} |
| 5936 |
|
| 5937 |
/* implementation of transition function invocation for byref types */ |
| 5938 |
static pg_always_inline void |
| 5939 |
ExecAggPlainTransByRef(AggState *aggstate, AggStatePerTrans pertrans, |
| 5940 |
AggStatePerGroup pergroup, |
| 5941 |
ExprContext *aggcontext, int setno) |
| 5942 |
{ |
| 5943 |
FunctionCallInfo fcinfo = pertrans->transfn_fcinfo; |
| 5944 |
MemoryContext oldContext; |
| 5945 |
Datum newVal; |
| 5946 |
|
| 5947 |
/* cf. select_current_set() */ |
| 5948 |
aggstate->curaggcontext = aggcontext; |
| 5949 |
aggstate->current_set = setno; |
| 5950 |
|
| 5951 |
/* set up aggstate->curpertrans for AggGetAggref() */ |
| 5952 |
aggstate->curpertrans = pertrans; |
| 5953 |
|
| 5954 |
/* invoke transition function in per-tuple context */ |
| 5955 |
oldContext = MemoryContextSwitchTo(aggstate->tmpcontext->ecxt_per_tuple_memory); |
| 5956 |
|
| 5957 |
fcinfo->args[0].value = pergroup->transValue; |
| 5958 |
fcinfo->args[0].isnull = pergroup->transValueIsNull; |
| 5959 |
fcinfo->isnull = false; /* just in case transfn doesn't set it */ |
| 5960 |
|
| 5961 |
newVal = FunctionCallInvoke(fcinfo); |
| 5962 |
|
| 5963 |
/* |
| 5964 |
* For pass-by-ref datatype, must copy the new value into aggcontext and |
| 5965 |
* free the prior transValue. But if transfn returned a pointer to its |
| 5966 |
* first input, we don't need to do anything. |
| 5967 |
* |
| 5968 |
* It's safe to compare newVal with pergroup->transValue without regard |
| 5969 |
* for either being NULL, because ExecAggCopyTransValue takes care to set |
| 5970 |
* transValue to 0 when NULL. Otherwise we could end up accidentally not |
| 5971 |
* reparenting, when the transValue has the same numerical value as |
| 5972 |
* newValue, despite being NULL. This is a somewhat hot path, making it |
| 5973 |
* undesirable to instead solve this with another branch for the common |
| 5974 |
* case of the transition function returning its (modified) input |
| 5975 |
* argument. |
| 5976 |
*/ |
| 5977 |
if (DatumGetPointer(newVal) != DatumGetPointer(pergroup->transValue)) |
| 5978 |
newVal = ExecAggCopyTransValue(aggstate, pertrans, |
| 5979 |
newVal, fcinfo->isnull, |
| 5980 |
pergroup->transValue, |
| 5981 |
pergroup->transValueIsNull); |
| 5982 |
|
| 5983 |
pergroup->transValue = newVal; |
| 5984 |
pergroup->transValueIsNull = fcinfo->isnull; |
| 5985 |
|
| 5986 |
MemoryContextSwitchTo(oldContext); |
| 5987 |
} |