* exprIsLengthCoercion * Detect whether an expression tree is an application of a datatype's * typmod-coercion function. Optionally extract the result's typmod. * * If coercedTypmod is not NULL, the typmod is stored there if the expression * is a length-coercion function, else -1 is stored there. * * Note that a combined type-and-length coercion will be treated as a * length coercion by
| 521 | * length coercion by this routine. |
| 522 | */ |
| 523 | bool |
| 524 | exprIsLengthCoercion(const Node *expr, int32 *coercedTypmod) |
| 525 | { |
| 526 | if (coercedTypmod != NULL) |
| 527 | *coercedTypmod = -1; /* default result on failure */ |
| 528 | |
| 529 | /* |
| 530 | * Scalar-type length coercions are FuncExprs, array-type length coercions |
| 531 | * are ArrayCoerceExprs |
| 532 | */ |
| 533 | if (expr && IsA(expr, FuncExpr)) |
| 534 | { |
| 535 | const FuncExpr *func = (const FuncExpr *) expr; |
| 536 | int nargs; |
| 537 | Const *second_arg; |
| 538 | |
| 539 | /* |
| 540 | * If it didn't come from a coercion context, reject. |
| 541 | */ |
| 542 | if (func->funcformat != COERCE_EXPLICIT_CAST && |
| 543 | func->funcformat != COERCE_IMPLICIT_CAST) |
| 544 | return false; |
| 545 | |
| 546 | /* |
| 547 | * If it's not a two-argument or three-argument function with the |
| 548 | * second argument being an int4 constant, it can't have been created |
| 549 | * from a length coercion (it must be a type coercion, instead). |
| 550 | */ |
| 551 | nargs = list_length(func->args); |
| 552 | if (nargs < 2 || nargs > 3) |
| 553 | return false; |
| 554 | |
| 555 | second_arg = (Const *) lsecond(func->args); |
| 556 | if (!IsA(second_arg, Const) || |
| 557 | second_arg->consttype != INT4OID || |
| 558 | second_arg->constisnull) |
| 559 | return false; |
| 560 | |
| 561 | /* |
| 562 | * OK, it is indeed a length-coercion function. |
| 563 | */ |
| 564 | if (coercedTypmod != NULL) |
| 565 | *coercedTypmod = DatumGetInt32(second_arg->constvalue); |
| 566 | |
| 567 | return true; |
| 568 | } |
| 569 | |
| 570 | if (expr && IsA(expr, ArrayCoerceExpr)) |
| 571 | { |
| 572 | const ArrayCoerceExpr *acoerce = (const ArrayCoerceExpr *) expr; |
| 573 | |
| 574 | /* It's not a length coercion unless there's a nondefault typmod */ |
| 575 | if (acoerce->resulttypmod < 0) |
| 576 | return false; |
| 577 | |
| 578 | /* |
| 579 | * OK, it is indeed a length-coercion expression. |
| 580 | */ |
no test coverage detected