|
44 | 44 | #include "pgstat.h"
|
45 | 45 | #include "utils/builtins.h"
|
46 | 46 | #include "utils/datum.h"
|
| 47 | +#include "utils/jsonpath.h" |
47 | 48 | #include "utils/lsyscache.h"
|
48 | 49 | #include "utils/typcache.h"
|
49 | 50 |
|
@@ -2111,6 +2112,92 @@ ExecInitExprRec(Expr *node, ExprState *state,
|
2111 | 2112 | resnull);
|
2112 | 2113 | break;
|
2113 | 2114 |
|
| 2115 | + case T_JsonExpr: |
| 2116 | + { |
| 2117 | + JsonExpr *jexpr = castNode(JsonExpr, node); |
| 2118 | + ListCell *argexprlc; |
| 2119 | + ListCell *argnamelc; |
| 2120 | + |
| 2121 | + scratch.opcode = EEOP_JSONEXPR; |
| 2122 | + scratch.d.jsonexpr.jsexpr = jexpr; |
| 2123 | + |
| 2124 | + scratch.d.jsonexpr.raw_expr = |
| 2125 | + palloc(sizeof(*scratch.d.jsonexpr.raw_expr)); |
| 2126 | + |
| 2127 | + ExecInitExprRec((Expr *) jexpr->raw_expr, state, |
| 2128 | + &scratch.d.jsonexpr.raw_expr->value, |
| 2129 | + &scratch.d.jsonexpr.raw_expr->isnull); |
| 2130 | + |
| 2131 | + scratch.d.jsonexpr.formatted_expr = |
| 2132 | + ExecInitExpr((Expr *) jexpr->formatted_expr, state->parent); |
| 2133 | + |
| 2134 | + scratch.d.jsonexpr.result_expr = jexpr->result_coercion |
| 2135 | + ? ExecInitExpr((Expr *) jexpr->result_coercion->expr, |
| 2136 | + state->parent) |
| 2137 | + : NULL; |
| 2138 | + |
| 2139 | + scratch.d.jsonexpr.default_on_empty = |
| 2140 | + ExecInitExpr((Expr *) jexpr->on_empty.default_expr, |
| 2141 | + state->parent); |
| 2142 | + |
| 2143 | + scratch.d.jsonexpr.default_on_error = |
| 2144 | + ExecInitExpr((Expr *) jexpr->on_error.default_expr, |
| 2145 | + state->parent); |
| 2146 | + |
| 2147 | + if (jexpr->omit_quotes || |
| 2148 | + (jexpr->result_coercion && jexpr->result_coercion->via_io)) |
| 2149 | + { |
| 2150 | + Oid typinput; |
| 2151 | + |
| 2152 | + /* lookup the result type's input function */ |
| 2153 | + getTypeInputInfo(jexpr->returning.typid, &typinput, |
| 2154 | + &scratch.d.jsonexpr.input.typioparam); |
| 2155 | + fmgr_info(typinput, &scratch.d.jsonexpr.input.func); |
| 2156 | + } |
| 2157 | + |
| 2158 | + scratch.d.jsonexpr.args = NIL; |
| 2159 | + |
| 2160 | + forboth(argexprlc, jexpr->passing.values, |
| 2161 | + argnamelc, jexpr->passing.names) |
| 2162 | + { |
| 2163 | + Expr *argexpr = (Expr *) lfirst(argexprlc); |
| 2164 | + Value *argname = (Value *) lfirst(argnamelc); |
| 2165 | + JsonPathVariableEvalContext *var = palloc(sizeof(*var)); |
| 2166 | + |
| 2167 | + var->name = pstrdup(argname->val.str); |
| 2168 | + var->typid = exprType((Node *) argexpr); |
| 2169 | + var->typmod = exprTypmod((Node *) argexpr); |
| 2170 | + var->estate = ExecInitExpr(argexpr, state->parent); |
| 2171 | + var->econtext = NULL; |
| 2172 | + var->evaluated = false; |
| 2173 | + var->value = (Datum) 0; |
| 2174 | + var->isnull = true; |
| 2175 | + |
| 2176 | + scratch.d.jsonexpr.args = |
| 2177 | + lappend(scratch.d.jsonexpr.args, var); |
| 2178 | + } |
| 2179 | + |
| 2180 | + if (jexpr->coercions) |
| 2181 | + { |
| 2182 | + JsonCoercion **coercion; |
| 2183 | + struct JsonCoercionState *cstate; |
| 2184 | + |
| 2185 | + for (cstate = &scratch.d.jsonexpr.coercions.null, |
| 2186 | + coercion = &jexpr->coercions->null; |
| 2187 | + coercion <= &jexpr->coercions->composite; |
| 2188 | + coercion++, cstate++) |
| 2189 | + { |
| 2190 | + cstate->coercion = *coercion; |
| 2191 | + cstate->estate = *coercion ? |
| 2192 | + ExecInitExpr((Expr *)(*coercion)->expr, |
| 2193 | + state->parent) : NULL; |
| 2194 | + } |
| 2195 | + } |
| 2196 | + |
| 2197 | + ExprEvalPushStep(state, &scratch); |
| 2198 | + } |
| 2199 | + break; |
| 2200 | + |
2114 | 2201 | default:
|
2115 | 2202 | elog(ERROR, "unrecognized node type: %d",
|
2116 | 2203 | (int) nodeTag(node));
|
|
0 commit comments