Answers NULL when the query is fine, or a heap message naming the first * variable that is not in scope. Checks one query; the caller walks a UNION. */
| 6462 | /* Answers NULL when the query is fine, or a heap message naming the first |
| 6463 | * variable that is not in scope. Checks one query; the caller walks a UNION. */ |
| 6464 | static char *check_projection_scope(const cbm_query_t *q) { |
| 6465 | /* Runs first, so the rest of this function can trust that the query names |
| 6466 | * no more variables than the arrays below can model. */ |
| 6467 | char *capacity_err = check_pattern_var_capacity(q); |
| 6468 | if (capacity_err) { |
| 6469 | return capacity_err; |
| 6470 | } |
| 6471 | |
| 6472 | const char *declared[CYP_SCOPE_MAX_NAMES]; |
| 6473 | int declared_n = collect_declared_names(q, declared, CYP_SCOPE_MAX_NAMES); |
| 6474 | if (declared_n < 0) { |
| 6475 | /* Unreachable while the capacity check above holds. Kept so the guard |
| 6476 | * still stands if either bound ever moves. Skipping the check was the |
| 6477 | * old behaviour, and it let an out-of-scope name through in silence. */ |
| 6478 | return NULL; |
| 6479 | } |
| 6480 | |
| 6481 | /* A WITH still reads the pattern variables. */ |
| 6482 | if (q->with_clause && !q->with_clause->star) { |
| 6483 | for (int i = 0; i < q->with_clause->count; i++) { |
| 6484 | const char *var = scope_checkable_var(&q->with_clause->items[i]); |
| 6485 | if (var && !scope_holds(declared, declared_n, var)) { |
| 6486 | return scope_error(var, "WITH", "no pattern in this query names it"); |
| 6487 | } |
| 6488 | } |
| 6489 | } |
| 6490 | |
| 6491 | if (!q->ret || q->ret->star) { |
| 6492 | return NULL; |
| 6493 | } |
| 6494 | |
| 6495 | /* A RETURN after a WITH reads only what the WITH left behind. */ |
| 6496 | const char *after_with[CYP_SCOPE_MAX_NAMES]; |
| 6497 | const char *const *scope = declared; |
| 6498 | int scope_n = declared_n; |
| 6499 | if (q->with_clause) { |
| 6500 | scope_n = collect_with_names(q->with_clause, after_with, CYP_SCOPE_MAX_NAMES); |
| 6501 | if (scope_n < 0) { |
| 6502 | return NULL; /* unreachable: a WITH holds at most CYP_SCOPE_MAX_NAMES items */ |
| 6503 | } |
| 6504 | scope = after_with; |
| 6505 | } |
| 6506 | |
| 6507 | for (int i = 0; i < q->ret->count; i++) { |
| 6508 | const char *var = scope_checkable_var(&q->ret->items[i]); |
| 6509 | if (var && !scope_holds(scope, scope_n, var)) { |
| 6510 | return scope_error(var, "RETURN", |
| 6511 | q->with_clause ? "the WITH clause did not carry it through" |
| 6512 | : "no pattern in this query names it"); |
| 6513 | } |
| 6514 | } |
| 6515 | return NULL; |
| 6516 | } |
| 6517 | |
| 6518 | /* The error of an execution execute_single aborted: a failed start-node scan |
| 6519 | * names the store's error, anything else was an allocation. */ |
no test coverage detected