aboutsummaryrefslogtreecommitdiff
path: root/contrib/postgres_fdw/deparse.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-11-12 11:50:40 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2021-11-12 11:50:47 -0500
commitf8abb0f5e114d8c309239f0faa277b97f696d829 (patch)
tree59d8de186271a28400980decb704f37bb76cf1e2 /contrib/postgres_fdw/deparse.c
parent1593998ae858902e805eb0f8bf3b019399044471 (diff)
downloadpostgresql-f8abb0f5e114d8c309239f0faa277b97f696d829.tar.gz
postgresql-f8abb0f5e114d8c309239f0faa277b97f696d829.zip
postgres_fdw: suppress casts on constants in limited cases.
When deparsing an expression of the form "remote_var OP constant", we'd normally apply a cast to the constant to make sure that the remote parser thinks it's of the same type we do. However, doing so is often not necessary, and it causes problems if the user has intentionally declared the local column as being of a different type than the remote column. A plausible use-case for that is using text to represent a type that's an enum on the remote side. A comparison on such a column will get shipped as "var = 'foo'::text", which blows up on the remote side because there's no enum = text operator. But if we simply leave off the explicit cast, the comparison will do exactly what the user wants. It's possible to do this without major risk of semantic problems, by relying on the longstanding parser heuristic that "if one operand of an operator is of type unknown, while the other one has a known type, assume that the unknown operand is also of that type". Hence, this patch leaves off the cast only if (a) the operator inputs have the same type locally; (b) the constant will print as a string literal or NULL, both of which are initially taken as type unknown; and (c) the non-Const input is a plain foreign Var. Rule (c) guarantees that the remote parser will know the type of the non-Const input; moreover, it means that if this cast-omission does cause any semantic surprises, that can only happen in cases where the local column has a different type than the remote column. That wasn't guaranteed to work anyway, and this patch should represent a net usability gain for such cases. One point that I (tgl) remain slightly uncomfortable with is that we will ignore an implicit RelabelType when deciding if the non-Const input is a plain Var. That makes it a little squishy to argue that the remote should resolve the Const as being of the same type as its Var, because then our Const is not the same type as our Var. However, if we don't do that, then this hack won't work as desired if the user chooses to use varchar rather than text to represent some remote column. That seems useful, so do it like this for now. We might have to give up the RelabelType-ignoring bit if any problems surface. Dian Fay, with review and kibitzing by me Discussion: https://postgr.es/m/C9LU294V7K4F.34LRRDU449O45@lamia
Diffstat (limited to 'contrib/postgres_fdw/deparse.c')
-rw-r--r--contrib/postgres_fdw/deparse.c116
1 files changed, 108 insertions, 8 deletions
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index d98bd666818..b27689d0864 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -152,6 +152,7 @@ static void deparseParam(Param *node, deparse_expr_cxt *context);
static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context);
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context);
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context);
+static bool isPlainForeignVar(Expr *node, deparse_expr_cxt *context);
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform);
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context);
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node,
@@ -2695,9 +2696,14 @@ deparseVar(Var *node, deparse_expr_cxt *context)
* Deparse given constant value into context->buf.
*
* This function has to be kept in sync with ruleutils.c's get_const_expr.
- * As for that function, showtype can be -1 to never show "::typename" decoration,
- * or +1 to always show it, or 0 to show it only if the constant wouldn't be assumed
- * to be the right type by default.
+ *
+ * As in that function, showtype can be -1 to never show "::typename"
+ * decoration, +1 to always show it, or 0 to show it only if the constant
+ * wouldn't be assumed to be the right type by default.
+ *
+ * In addition, this code allows showtype to be -2 to indicate that we should
+ * not show "::typename" decoration if the constant is printed as an untyped
+ * literal or NULL (while in other cases, behaving as for showtype == 0).
*/
static void
deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
@@ -2707,6 +2713,7 @@ deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
bool typIsVarlena;
char *extval;
bool isfloat = false;
+ bool isstring = false;
bool needlabel;
if (node->constisnull)
@@ -2762,13 +2769,14 @@ deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
break;
default:
deparseStringLiteral(buf, extval);
+ isstring = true;
break;
}
pfree(extval);
- if (showtype < 0)
- return;
+ if (showtype == -1)
+ return; /* never print type label */
/*
* For showtype == 0, append ::typename unless the constant will be
@@ -2788,7 +2796,13 @@ deparseConst(Const *node, deparse_expr_cxt *context, int showtype)
needlabel = !isfloat || (node->consttypmod >= 0);
break;
default:
- needlabel = true;
+ if (showtype == -2)
+ {
+ /* label unless we printed it as an untyped string */
+ needlabel = !isstring;
+ }
+ else
+ needlabel = true;
break;
}
if (needlabel || showtype > 0)
@@ -2953,6 +2967,8 @@ deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
StringInfo buf = context->buf;
HeapTuple tuple;
Form_pg_operator form;
+ Expr *right;
+ bool canSuppressRightConstCast = false;
char oprkind;
/* Retrieve information about the operator from system catalog. */
@@ -2966,13 +2982,58 @@ deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
Assert((oprkind == 'l' && list_length(node->args) == 1) ||
(oprkind == 'b' && list_length(node->args) == 2));
+ right = llast(node->args);
+
/* Always parenthesize the expression. */
appendStringInfoChar(buf, '(');
/* Deparse left operand, if any. */
if (oprkind == 'b')
{
- deparseExpr(linitial(node->args), context);
+ Expr *left = linitial(node->args);
+ Oid leftType = exprType((Node *) left);
+ Oid rightType = exprType((Node *) right);
+ bool canSuppressLeftConstCast = false;
+
+ /*
+ * When considering a binary operator, if one operand is a Const that
+ * can be printed as a bare string literal or NULL (i.e., it will look
+ * like type UNKNOWN to the remote parser), the Const normally
+ * receives an explicit cast to the operator's input type. However,
+ * in Const-to-Var comparisons where both operands are of the same
+ * type, we prefer to suppress the explicit cast, leaving the Const's
+ * type resolution up to the remote parser. The remote's resolution
+ * heuristic will assume that an unknown input type being compared to
+ * a known input type is of that known type as well.
+ *
+ * This hack allows some cases to succeed where a remote column is
+ * declared with a different type in the local (foreign) table. By
+ * emitting "foreigncol = 'foo'" not "foreigncol = 'foo'::text" or the
+ * like, we allow the remote parser to pick an "=" operator that's
+ * compatible with whatever type the remote column really is, such as
+ * an enum.
+ *
+ * We allow cast suppression to happen only when the other operand is
+ * a plain foreign Var. Although the remote's unknown-type heuristic
+ * would apply to other cases just as well, we would be taking a
+ * bigger risk that the inferred type is something unexpected. With
+ * this restriction, if anything goes wrong it's the user's fault for
+ * not declaring the local column with the same type as the remote
+ * column.
+ */
+ if (leftType == rightType)
+ {
+ if (IsA(left, Const))
+ canSuppressLeftConstCast = isPlainForeignVar(right, context);
+ else if (IsA(right, Const))
+ canSuppressRightConstCast = isPlainForeignVar(left, context);
+ }
+
+ if (canSuppressLeftConstCast)
+ deparseConst((Const *) left, context, -2);
+ else
+ deparseExpr(left, context);
+
appendStringInfoChar(buf, ' ');
}
@@ -2981,7 +3042,11 @@ deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
/* Deparse right operand. */
appendStringInfoChar(buf, ' ');
- deparseExpr(llast(node->args), context);
+
+ if (canSuppressRightConstCast)
+ deparseConst((Const *) right, context, -2);
+ else
+ deparseExpr(right, context);
appendStringInfoChar(buf, ')');
@@ -2989,6 +3054,41 @@ deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
}
/*
+ * Will "node" deparse as a plain foreign Var?
+ */
+static bool
+isPlainForeignVar(Expr *node, deparse_expr_cxt *context)
+{
+ /*
+ * We allow the foreign Var to have an implicit RelabelType, mainly so
+ * that this'll work with varchar columns. Note that deparseRelabelType
+ * will not print such a cast, so we're not breaking the restriction that
+ * the expression print as a plain Var. We won't risk it for an implicit
+ * cast that requires a function, nor for non-implicit RelabelType; such
+ * cases seem too likely to involve semantics changes compared to what
+ * would happen on the remote side.
+ */
+ if (IsA(node, RelabelType) &&
+ ((RelabelType *) node)->relabelformat == COERCE_IMPLICIT_CAST)
+ node = ((RelabelType *) node)->arg;
+
+ if (IsA(node, Var))
+ {
+ /*
+ * The Var must be one that'll deparse as a foreign column reference
+ * (cf. deparseVar).
+ */
+ Var *var = (Var *) node;
+ Relids relids = context->scanrel->relids;
+
+ if (bms_is_member(var->varno, relids) && var->varlevelsup == 0)
+ return true;
+ }
+
+ return false;
+}
+
+/*
* Print the name of an operator.
*/
static void