/*------------------------------------------------------------------------- * * deparse.c * Query deparser for pg_monetdb * * This file includes functions that examine query WHERE clauses to see * whether they're safe to send to the remote server for execution. * * pg_monetdb is a PostgreSQL foreign data wrapper for MonetDB, * derived from prior monetdb_fdw and PostgreSQL FDW work, with extended * and rewritten functionality. * * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following copyright notices: * * Portions Copyright (c) 2025-2026, Halo Tech Co.,Ltd. * Portions Copyright (c) 2012-2023, PostgreSQL Global Development Group * Portions Copyright (c) 2026, Saulo Jose Benvenutti * * Author: zengman * Additional contributions by Saulo Jose Benvenutti * * IDENTIFICATION * deparse.c * *------------------------------------------------------------------------- */ #include "postgres.h" #include "access/htup_details.h" #include "access/sysattr.h" #include "access/table.h" #include "catalog/pg_aggregate.h" #include "catalog/pg_authid.h" #include "catalog/pg_collation.h" #include "catalog/pg_namespace.h" #include "catalog/pg_operator.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_proc.h" #include "catalog/pg_ts_config.h" #include "catalog/pg_ts_dict.h" #include "catalog/pg_type.h" #include "commands/defrem.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" #include "nodes/plannodes.h" #include "optimizer/optimizer.h" #include "optimizer/prep.h" #include "optimizer/tlist.h" #include "parser/parse_relation.h" #include "parser/parsetree.h" #include "monetdb_fdw.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/syscache.h" #include "utils/timestamp.h" #include "utils/typcache.h" #include "commands/tablecmds.h" /* * Global context for foreign_expr_walker's search of an expression tree. */ typedef struct foreign_glob_cxt { PlannerInfo *root; /* global planner state */ RelOptInfo *foreignrel; /* the foreign relation we are planning for */ Relids relids; /* relids of base relations in the underlying * scan */ } foreign_glob_cxt; /* * Local (per-tree-level) context for foreign_expr_walker's search. * This is concerned with identifying collations used in the expression. */ typedef enum { FDW_COLLATE_NONE, /* expression is of a noncollatable type, or * it has default collation that is not * traceable to a foreign Var */ FDW_COLLATE_SAFE, /* collation derives from a foreign Var */ FDW_COLLATE_UNSAFE /* collation is non-default and derives from * something other than a foreign Var */ } FDWCollateState; typedef struct foreign_loc_cxt { Oid collation; /* OID of current collation, if any */ FDWCollateState state; /* state of current collation choice */ } foreign_loc_cxt; /* * Context for deparseExpr */ typedef struct deparse_expr_cxt { PlannerInfo *root; /* global planner state */ RelOptInfo *foreignrel; /* the foreign relation we are planning for */ RelOptInfo *scanrel; /* the underlying scan relation. Same as * foreignrel, when that represents a join or * a base relation. */ StringInfo buf; /* output buffer to append to */ List **params_list; /* exprs that will become remote Params */ bool grouped_subquery_inner; } deparse_expr_cxt; #define REL_ALIAS_PREFIX "r" /* Handy macro to add relation name qualification */ #define ADD_REL_QUALIFIER(buf, varno) \ appendStringInfo((buf), "%s%d.", REL_ALIAS_PREFIX, (varno)) #define SUBQUERY_REL_ALIAS_PREFIX "s" #define SUBQUERY_COL_ALIAS_PREFIX "c" /* * Functions to determine whether an expression can be evaluated safely on * remote server. */ static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt, foreign_loc_cxt *case_arg_cxt); static char *deparse_type_name(Oid type_oid, int32 typemod); static char *monetdb_remote_type_name(Oid type_oid, int32 typemod); /* * Functions to construct string representation of a node tree. */ static void deparseTargetList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs); static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context); static void deparseSubqueryTargetList(deparse_expr_cxt *context); static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs); static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col); static void deparseRelation(StringInfo buf, Relation rel); static bool deparse_subquery_column_ref(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col); static void deparseExpr(Expr *node, deparse_expr_cxt *context); static void deparseVar(Var *node, deparse_expr_cxt *context); static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype); 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 bool deparseExtractFuncExpr(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, deparse_expr_cxt *context); static bool deparseScalarArrayOpExprAsIn(ScalarArrayOpExpr *node, deparse_expr_cxt *context, Form_pg_operator opform); static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context); static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context); static void deparseNullTest(NullTest *node, deparse_expr_cxt *context); static void deparseCaseExpr(CaseExpr *node, deparse_expr_cxt *context); static void deparseNullIfExpr(NullIfExpr *node, deparse_expr_cxt *context); static void deparseCoalesceExpr(CoalesceExpr *node, deparse_expr_cxt *context); static void deparseMinMaxExpr(MinMaxExpr *node, deparse_expr_cxt *context); static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context); static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context); static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context); static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context); static void deparseLockingClause(deparse_expr_cxt *context); static void appendOrderByClause(List *pathkeys, bool has_final_sort, List *targetList, deparse_expr_cxt *context); static void appendLimitClause(deparse_expr_cxt *context); static void appendConditions(List *exprs, deparse_expr_cxt *context); static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list); static void deparseFromExpr(List *quals, deparse_expr_cxt *context); static void deparseFromExprForSemiJoin(List *quals, deparse_expr_cxt *context); static AttrNumber find_null_test_attno(RelOptInfo *rel, List *exprs); static void deparseExistsSubquery(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *extra_conds, bool negated, deparse_expr_cxt *context); static Expr *get_supported_any_sublink_outer_expr(SubPlan *subplan); static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list); static void deparseAggref(Aggref *node, deparse_expr_cxt *context); static void deparseSubPlan(SubPlan *node, deparse_expr_cxt *context); static void appendSubPlanSqlTemplate(SubPlan *subplan, ForeignScan *fscan, deparse_expr_cxt *context); static void appendGroupByClause(List *tlist, deparse_expr_cxt *context); static void appendGroupByClauseForQuery(List *tlist, Query *query, deparse_expr_cxt *context); static void appendGroupingSetContent(List *content, List *tlist, deparse_expr_cxt *context, bool in_grouping_sets); static void appendOrderBySuffix(Oid sortop, Oid sortcoltype, bool nulls_first, deparse_expr_cxt *context); static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context); static void appendFunctionName(Oid funcid, deparse_expr_cxt *context); static Node *deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context); /* * Helper functions */ static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno); static bool is_grouped_subquery_bridge(RelOptInfo *rel); static bool deparse_grouped_subquery_bridge_var(Var *node, deparse_expr_cxt *context); static char *deparseByteaHexLiteral(Datum value, Oid type); static void deparse_grouped_subquery_from_node(StringInfo buf, Query *subquery, Node *node, deparse_expr_cxt *context); static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno); static TargetEntry *get_nth_visible_tle(List *tlist, AttrNumber attno); static TargetEntry * get_nth_visible_tle(List *tlist, AttrNumber attno) { AttrNumber visible_attno = 0; ListCell *lc; if (attno <= 0) return NULL; foreach(lc, tlist) { TargetEntry *tle = lfirst_node(TargetEntry, lc); if (tle->resjunk) continue; visible_attno++; if (visible_attno == attno) return tle; } return NULL; } static Expr * get_supported_any_sublink_outer_expr(SubPlan *subplan) { Node *testexpr; OpExpr *opexpr; Node *leftarg; Node *rightarg; Param *param; char *oprname; if (subplan->subLinkType != ANY_SUBLINK || subplan->testexpr == NULL) return NULL; testexpr = strip_implicit_coercions(subplan->testexpr); if (!IsA(testexpr, OpExpr)) return NULL; opexpr = (OpExpr *) testexpr; if (list_length(opexpr->args) != 2) return NULL; leftarg = strip_implicit_coercions(linitial(opexpr->args)); rightarg = strip_implicit_coercions(lsecond(opexpr->args)); if (IsA(leftarg, Param) && ((Param *) leftarg)->paramkind == PARAM_EXEC) { Node *tmp = leftarg; leftarg = rightarg; rightarg = tmp; } if (!IsA(rightarg, Param)) return NULL; param = (Param *) rightarg; if (param->paramkind != PARAM_EXEC || !list_member_int(subplan->paramIds, param->paramid)) return NULL; oprname = get_opname(opexpr->opno); if (oprname == NULL || strcmp(oprname, "=") != 0) return NULL; return (Expr *) leftarg; } /* * Examine each qual clause in input_conds, and classify them into two groups, * which are returned as two lists: * - remote_conds contains expressions that can be evaluated remotely * - local_conds contains expressions that can't be evaluated remotely */ void classifyConditions(PlannerInfo *root, RelOptInfo *baserel, List *input_conds, List **remote_conds, List **local_conds) { ListCell *lc; *remote_conds = NIL; *local_conds = NIL; foreach(lc, input_conds) { RestrictInfo *ri = lfirst_node(RestrictInfo, lc); if (is_foreign_expr(root, baserel, ri->clause)) *remote_conds = lappend(*remote_conds, ri); else *local_conds = lappend(*local_conds, ri); } } /* * Returns true if given expr is safe to evaluate on the foreign server. */ bool is_foreign_expr(PlannerInfo *root, RelOptInfo *baserel, Expr *expr) { foreign_glob_cxt glob_cxt; foreign_loc_cxt loc_cxt; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) (baserel->fdw_private); /* * Check that the expression consists of nodes that are safe to execute * remotely. */ glob_cxt.root = root; glob_cxt.foreignrel = baserel; /* * For an upper relation, use relids from its underneath scan relation, * because the upperrel's own relids currently aren't set to anything * meaningful by the core code. For other relation, use their own relids. */ if (IS_UPPER_REL(baserel)) glob_cxt.relids = fpinfo->outerrel->relids; else glob_cxt.relids = baserel->relids; loc_cxt.collation = InvalidOid; loc_cxt.state = FDW_COLLATE_NONE; if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt, NULL)) return false; /* * If the expression has a valid collation that does not arise from a * foreign var, the expression can not be sent over. */ if (loc_cxt.state == FDW_COLLATE_UNSAFE) return false; /* * An expression which includes any volatile functions can't be sent over * because its result may differ per row. We allow STABLE functions since * they are consistent within a single query execution, which is all we * need for a single remote round-trip. VOLATILE functions (random(), * clock_timestamp(), etc.) are still blocked. */ if (contain_volatile_functions((Node *) expr)) return false; /* OK to evaluate on the remote server */ return true; } /* * Check if expression is safe to execute remotely, and return true if so. * * In addition, *outer_cxt is updated with collation information. * * case_arg_cxt is NULL if this subexpression is not inside a CASE-with-arg. * Otherwise, it points to the collation info derived from the arg expression, * which must be consulted by any CaseTestExpr. * * We must check that the expression contains only node types we can deparse, * that all types/functions/operators are safe to send (they are "shippable"), * and that all collations used in the expression derive from Vars of the * foreign table. Because of the latter, the logic is pretty close to * assign_collations_walker() in parse_collate.c, though we can assume here * that the given expression is valid. Note function mutability is not * currently considered here. */ static bool foreign_expr_walker(Node *node, foreign_glob_cxt *glob_cxt, foreign_loc_cxt *outer_cxt, foreign_loc_cxt *case_arg_cxt) { bool check_type = true; MonetdbFdwRelationInfo *fpinfo; foreign_loc_cxt inner_cxt; Oid collation; FDWCollateState state; /* Need do nothing for empty subexpressions */ if (node == NULL) return true; /* May need server info from baserel's fdw_private struct */ fpinfo = (MonetdbFdwRelationInfo *) (glob_cxt->foreignrel->fdw_private); /* Set up inner_cxt for possible recursion to child nodes */ inner_cxt.collation = InvalidOid; inner_cxt.state = FDW_COLLATE_NONE; switch (nodeTag(node)) { case T_Var: { Var *var = (Var *) node; /* * If the Var is from the foreign table, we consider its * collation (if any) safe to use. If it is from another * table, we treat its collation the same way as we would a * Param's collation, ie it's not safe for it to have a * non-default collation. */ if (bms_is_member(var->varno, glob_cxt->relids) && var->varlevelsup == 0) { /* Var belongs to foreign table */ /* * System columns should not be sent to the remote, * since we don't make any effort to ensure * that local and remote values match (tableoid, in * particular, almost certainly doesn't match). */ if (var->varattno < 0) return false; /* Else check the collation */ collation = var->varcollid; state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE; } else { /* Var belongs to some other table */ collation = var->varcollid; if (collation == InvalidOid || collation == DEFAULT_COLLATION_OID) { /* * It's noncollatable, or it's safe to combine with a * collatable foreign Var, so set state to NONE. */ state = FDW_COLLATE_NONE; } else { /* * Do not fail right away, since the Var might appear * in a collation-insensitive context. */ state = FDW_COLLATE_UNSAFE; } } } break; case T_Const: { Const *c = (Const *) node; /* * Constants of regproc and related types can't be shipped * unless the referenced object is shippable. But NULL's ok. * (See also the related code in dependency.c.) */ if (!c->constisnull) { switch (c->consttype) { case REGPROCOID: case REGPROCEDUREOID: if (!is_shippable(DatumGetObjectId(c->constvalue), ProcedureRelationId, fpinfo)) return false; break; case REGOPEROID: case REGOPERATOROID: if (!is_shippable(DatumGetObjectId(c->constvalue), OperatorRelationId, fpinfo)) return false; break; case REGCLASSOID: if (!is_shippable(DatumGetObjectId(c->constvalue), RelationRelationId, fpinfo)) return false; break; case REGTYPEOID: if (!is_shippable(DatumGetObjectId(c->constvalue), TypeRelationId, fpinfo)) return false; break; case REGCOLLATIONOID: if (!is_shippable(DatumGetObjectId(c->constvalue), CollationRelationId, fpinfo)) return false; break; case REGCONFIGOID: /* * For text search objects only, we weaken the * normal shippability criterion to allow all OIDs * below FirstNormalObjectId. Without this, none * of the initdb-installed TS configurations would * be shippable, which would be quite annoying. */ if (DatumGetObjectId(c->constvalue) >= FirstNormalObjectId && !is_shippable(DatumGetObjectId(c->constvalue), TSConfigRelationId, fpinfo)) return false; break; case REGDICTIONARYOID: if (DatumGetObjectId(c->constvalue) >= FirstNormalObjectId && !is_shippable(DatumGetObjectId(c->constvalue), TSDictionaryRelationId, fpinfo)) return false; break; case REGNAMESPACEOID: if (!is_shippable(DatumGetObjectId(c->constvalue), NamespaceRelationId, fpinfo)) return false; break; case REGROLEOID: if (!is_shippable(DatumGetObjectId(c->constvalue), AuthIdRelationId, fpinfo)) return false; break; } } /* * If the constant has nondefault collation, either it's of a * non-builtin type, or it reflects folding of a CollateExpr. * It's unsafe to send to the remote unless it's used in a * non-collation-sensitive context. */ collation = c->constcollid; if (collation == InvalidOid || collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_Param: { Param *p = (Param *) node; /* * If it's a MULTIEXPR Param, punt. We can't tell from here * whether the referenced sublink/subplan contains any remote * Vars; if it does, handling that is too complicated to * consider supporting at present. Fortunately, MULTIEXPR * Params are not reduced to plain PARAM_EXEC until the end of * planning, so we can easily detect this case. (Normal * PARAM_EXEC Params are safe to ship because their values * come from somewhere else in the plan tree; but a MULTIEXPR * references a sub-select elsewhere in the same targetlist, * so we'd be on the hook to evaluate it somehow if we wanted * to handle such cases as direct foreign updates.) */ if (p->paramkind == PARAM_MULTIEXPR) return false; /* * Collation rule is same as for Consts and non-foreign Vars. */ collation = p->paramcollid; if (collation == InvalidOid || collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_SubscriptingRef: { SubscriptingRef *sr = (SubscriptingRef *) node; /* Assignment should not be in restrictions. */ if (sr->refassgnexpr != NULL) return false; /* * Recurse into the remaining subexpressions. The container * subscripts will not affect collation of the SubscriptingRef * result, so do those first and reset inner_cxt afterwards. */ if (!foreign_expr_walker((Node *) sr->refupperindexpr, glob_cxt, &inner_cxt, case_arg_cxt)) return false; inner_cxt.collation = InvalidOid; inner_cxt.state = FDW_COLLATE_NONE; if (!foreign_expr_walker((Node *) sr->reflowerindexpr, glob_cxt, &inner_cxt, case_arg_cxt)) return false; inner_cxt.collation = InvalidOid; inner_cxt.state = FDW_COLLATE_NONE; if (!foreign_expr_walker((Node *) sr->refexpr, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * Container subscripting typically yields same collation as * refexpr's, but in case it doesn't, use same logic as for * function nodes. */ collation = sr->refcollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_FuncExpr: { FuncExpr *fe = (FuncExpr *) node; /* * If function used by the expression is not shippable, it * can't be sent to remote because it might have incompatible * semantics on remote side. */ if (!is_shippable(fe->funcid, ProcedureRelationId, fpinfo)) return false; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) fe->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * If function's input collation is not derived from a foreign * Var, it can't be sent to remote — unless all inputs use the * default collation (constants, params, noncollatable columns), * which is safe because MonetDB uses the same default collation. */ if (fe->inputcollid == InvalidOid) /* OK, inputs are all noncollatable */ ; else if (inner_cxt.state == FDW_COLLATE_SAFE && fe->inputcollid == inner_cxt.collation) /* OK, collation derived from a foreign Var */ ; else if (inner_cxt.state == FDW_COLLATE_NONE && fe->inputcollid == DEFAULT_COLLATION_OID) /* OK, inputs are constants/params with default collation */ ; else return false; /* * Detect whether node is introducing a collation not derived * from a foreign Var. (If so, we just mark it unsafe for now * rather than immediately returning false, since the parent * node might not care.) */ collation = fe->funccollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_OpExpr: case T_DistinctExpr: /* struct-equivalent to OpExpr */ case T_NullIfExpr: /* struct-equivalent to OpExpr */ { OpExpr *oe = (OpExpr *) node; /* * Similarly, only shippable operators can be sent to remote. * (If the operator is shippable, we assume its underlying * function is too.) */ if (!is_shippable(oe->opno, OperatorRelationId, fpinfo)) return false; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) oe->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * If operator's input collation is not derived from a foreign * Var, it can't be sent to remote — unless all inputs use the * default collation, which is safe for MonetDB. */ if (oe->inputcollid == InvalidOid) /* OK, inputs are all noncollatable */ ; else if (inner_cxt.state == FDW_COLLATE_SAFE && oe->inputcollid == inner_cxt.collation) /* OK, collation derived from a foreign Var */ ; else if (inner_cxt.state == FDW_COLLATE_NONE && oe->inputcollid == DEFAULT_COLLATION_OID) /* OK, inputs are constants/params with default collation */ ; else return false; /* Result-collation handling is same as for functions */ collation = oe->opcollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_ScalarArrayOpExpr: { ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node; /* * Again, only shippable operators can be sent to remote. */ if (!is_shippable(oe->opno, OperatorRelationId, fpinfo)) return false; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) oe->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * If operator's input collation is not derived from a foreign * Var, it can't be sent to remote — unless all inputs use the * default collation, which is safe for MonetDB. */ if (oe->inputcollid == InvalidOid) /* OK, inputs are all noncollatable */ ; else if (inner_cxt.state == FDW_COLLATE_SAFE && oe->inputcollid == inner_cxt.collation) /* OK, collation derived from a foreign Var */ ; else if (inner_cxt.state == FDW_COLLATE_NONE && oe->inputcollid == DEFAULT_COLLATION_OID) /* OK, inputs are constants/params with default collation */ ; else return false; /* Output is always boolean and so noncollatable. */ collation = InvalidOid; state = FDW_COLLATE_NONE; } break; case T_RelabelType: { RelabelType *r = (RelabelType *) node; /* * Recurse to input subexpression. */ if (!foreign_expr_walker((Node *) r->arg, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * RelabelType must not introduce a collation not derived from * an input foreign Var (same logic as for a real function). */ collation = r->resultcollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_BoolExpr: { BoolExpr *b = (BoolExpr *) node; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) b->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* Output is always boolean and so noncollatable. */ collation = InvalidOid; state = FDW_COLLATE_NONE; } break; case T_NullTest: { NullTest *nt = (NullTest *) node; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) nt->arg, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* Output is always boolean and so noncollatable. */ collation = InvalidOid; state = FDW_COLLATE_NONE; } break; case T_CaseExpr: { CaseExpr *ce = (CaseExpr *) node; foreign_loc_cxt arg_cxt; foreign_loc_cxt tmp_cxt; ListCell *lc; /* * Recurse to CASE's arg expression, if any. Its collation * has to be saved aside for use while examining CaseTestExprs * within the WHEN expressions. */ arg_cxt.collation = InvalidOid; arg_cxt.state = FDW_COLLATE_NONE; if (ce->arg) { if (!foreign_expr_walker((Node *) ce->arg, glob_cxt, &arg_cxt, case_arg_cxt)) return false; } /* Examine the CaseWhen subexpressions. */ foreach(lc, ce->args) { CaseWhen *cw = lfirst_node(CaseWhen, lc); if (ce->arg) { /* * In a CASE-with-arg, the parser should have produced * WHEN clauses of the form "CaseTestExpr = RHS", * possibly with an implicit coercion inserted above * the CaseTestExpr. However in an expression that's * been through the optimizer, the WHEN clause could * be almost anything (since the equality operator * could have been expanded into an inline function). * In such cases forbid pushdown, because * deparseCaseExpr can't handle it. */ Node *whenExpr = (Node *) cw->expr; List *opArgs; if (!IsA(whenExpr, OpExpr)) return false; opArgs = ((OpExpr *) whenExpr)->args; if (list_length(opArgs) != 2 || !IsA(strip_implicit_coercions(linitial(opArgs)), CaseTestExpr)) return false; } /* * Recurse to WHEN expression, passing down the arg info. * Its collation doesn't affect the result (really, it * should be boolean and thus not have a collation). */ tmp_cxt.collation = InvalidOid; tmp_cxt.state = FDW_COLLATE_NONE; if (!foreign_expr_walker((Node *) cw->expr, glob_cxt, &tmp_cxt, &arg_cxt)) return false; /* Recurse to THEN expression. */ if (!foreign_expr_walker((Node *) cw->result, glob_cxt, &inner_cxt, case_arg_cxt)) return false; } /* Recurse to ELSE expression. */ if (!foreign_expr_walker((Node *) ce->defresult, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * Detect whether node is introducing a collation not derived * from a foreign Var. (If so, we just mark it unsafe for now * rather than immediately returning false, since the parent * node might not care.) This is the same as for function * nodes, except that the input collation is derived from only * the THEN and ELSE subexpressions. */ collation = ce->casecollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_CoalesceExpr: { CoalesceExpr *ce = (CoalesceExpr *) node; if (!foreign_expr_walker((Node *) ce->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; collation = ce->coalescecollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_MinMaxExpr: { MinMaxExpr *mm = (MinMaxExpr *) node; if (!foreign_expr_walker((Node *) mm->args, glob_cxt, &inner_cxt, case_arg_cxt)) return false; if (mm->inputcollid == InvalidOid) /* OK, inputs are all noncollatable */ ; else if (inner_cxt.state == FDW_COLLATE_SAFE && mm->inputcollid == inner_cxt.collation) /* OK, collation derived from a foreign Var */ ; else if (inner_cxt.state == FDW_COLLATE_NONE && mm->inputcollid == DEFAULT_COLLATION_OID) /* OK, inputs are constants/params with default collation */ ; else return false; collation = mm->minmaxcollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_CaseTestExpr: { CaseTestExpr *c = (CaseTestExpr *) node; /* Punt if we seem not to be inside a CASE arg WHEN. */ if (!case_arg_cxt) return false; /* * Otherwise, any nondefault collation attached to the * CaseTestExpr node must be derived from foreign Var(s) in * the CASE arg. */ collation = c->collation; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (case_arg_cxt->state == FDW_COLLATE_SAFE && collation == case_arg_cxt->collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_ArrayExpr: { ArrayExpr *a = (ArrayExpr *) node; /* * Recurse to input subexpressions. */ if (!foreign_expr_walker((Node *) a->elements, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * ArrayExpr must not introduce a collation not derived from * an input foreign Var (same logic as for a function). */ collation = a->array_collid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_List: { List *l = (List *) node; ListCell *lc; /* * Recurse to component subexpressions. */ foreach(lc, l) { if (!foreign_expr_walker((Node *) lfirst(lc), glob_cxt, &inner_cxt, case_arg_cxt)) return false; } /* * When processing a list, collation state just bubbles up * from the list elements. */ collation = inner_cxt.collation; state = inner_cxt.state; /* Don't apply exprType() to the list. */ check_type = false; } break; case T_Aggref: { Aggref *agg = (Aggref *) node; ListCell *lc; /* Not safe to pushdown when not in grouping context */ if (!IS_UPPER_REL(glob_cxt->foreignrel)) return false; /* Only non-split aggregates are pushable. */ if (agg->aggsplit != AGGSPLIT_SIMPLE) return false; /* MonetDB does not support SQL FILTER on aggregates. */ if (agg->aggfilter != NULL) return false; /* As usual, it must be shippable. */ if (!is_shippable(agg->aggfnoid, ProcedureRelationId, fpinfo)) return false; /* * Recurse to input args. aggdirectargs, aggorder and * aggdistinct are all present in args, so no need to check * their shippability explicitly. */ foreach(lc, agg->args) { Node *n = (Node *) lfirst(lc); /* If TargetEntry, extract the expression from it */ if (IsA(n, TargetEntry)) { TargetEntry *tle = (TargetEntry *) n; n = (Node *) tle->expr; } if (!foreign_expr_walker(n, glob_cxt, &inner_cxt, case_arg_cxt)) return false; } /* * For aggorder elements, check whether the sort operator, if * specified, is shippable or not. */ if (agg->aggorder) { foreach(lc, agg->aggorder) { SortGroupClause *srt = (SortGroupClause *) lfirst(lc); Oid sortcoltype; TypeCacheEntry *typentry; TargetEntry *tle; tle = get_sortgroupref_tle(srt->tleSortGroupRef, agg->args); sortcoltype = exprType((Node *) tle->expr); typentry = lookup_type_cache(sortcoltype, TYPECACHE_LT_OPR | TYPECACHE_GT_OPR); /* Check shippability of non-default sort operator. */ if (srt->sortop != typentry->lt_opr && srt->sortop != typentry->gt_opr && !is_shippable(srt->sortop, OperatorRelationId, fpinfo)) return false; } } /* Check aggregate filter */ if (!foreign_expr_walker((Node *) agg->aggfilter, glob_cxt, &inner_cxt, case_arg_cxt)) return false; /* * If aggregate's input collation is not derived from a * foreign Var, it can't be sent to remote — unless all inputs * use the default collation, which is safe for MonetDB. */ if (agg->inputcollid == InvalidOid) /* OK, inputs are all noncollatable */ ; else if (inner_cxt.state == FDW_COLLATE_SAFE && agg->inputcollid == inner_cxt.collation) /* OK, collation derived from a foreign Var */ ; else if (inner_cxt.state == FDW_COLLATE_NONE && agg->inputcollid == DEFAULT_COLLATION_OID) /* OK, inputs are constants/params with default collation */ ; else return false; /* * Detect whether node is introducing a collation not derived * from a foreign Var. (If so, we just mark it unsafe for now * rather than immediately returning false, since the parent * node might not care.) */ collation = agg->aggcollid; if (collation == InvalidOid) state = FDW_COLLATE_NONE; else if (inner_cxt.state == FDW_COLLATE_SAFE && collation == inner_cxt.collation) state = FDW_COLLATE_SAFE; else if (collation == DEFAULT_COLLATION_OID) state = FDW_COLLATE_NONE; else state = FDW_COLLATE_UNSAFE; } break; case T_SubPlan: { SubPlan *subplan = (SubPlan *) node; Plan *subplan_plan; ForeignScan *fscan; ListCell *lc; Expr *any_outer_expr = NULL; /* * Support scalar subqueries and the simple ANY_SUBLINK equality * pattern used by Q16's NOT IN predicate. */ if (subplan->subLinkType == EXPR_SUBLINK) ; else if (subplan->subLinkType == ANY_SUBLINK) { any_outer_expr = get_supported_any_sublink_outer_expr(subplan); if (any_outer_expr == NULL) return false; } else return false; /* * The inner plan must be a ForeignScan. This means the inner * subquery was already pushed to MonetDB as a parameterised * query. We can then inline it as a correlated subquery in * the outer WHERE clause (see deparseSubPlan). */ subplan_plan = (Plan *) list_nth(glob_cxt->root->glob->subplans, subplan->plan_id - 1); if (!IsA(subplan_plan, ForeignScan)) return false; fscan = (ForeignScan *) subplan_plan; /* * Verify that the inner ForeignScan is against the same * remote server as the outer relation. For upper-rel scans * (pushed aggregates), fs_server is InvalidOid, so we instead * verify that the inner ForeignScan has an SQL string in * fdw_private[0] — this is only set by our own FDW, which * proves it was planned for the same MonetDB server. */ if (list_length(fscan->fdw_private) == 0 || !IsA(linitial(fscan->fdw_private), String)) return false; if (any_outer_expr != NULL && !foreign_expr_walker((Node *) any_outer_expr, glob_cxt, outer_cxt, case_arg_cxt)) return false; /* * All outer-query arguments that are substituted for $N * placeholders must themselves be shippable. */ foreach(lc, subplan->args) { if (!foreign_expr_walker((Node *) lfirst(lc), glob_cxt, outer_cxt, case_arg_cxt)) return false; } /* * The SubPlan returns a scalar value; it has no collation of * its own that could conflict. */ collation = InvalidOid; state = FDW_COLLATE_NONE; check_type = false; } break; default: /* * If it's anything else, assume it's unsafe. This list can be * expanded later, but don't forget to add deparse support below. */ return false; } /* * If result type of given expression is not shippable, it can't be sent * to remote because it might have incompatible semantics on remote side. */ if (check_type && !is_shippable(exprType(node), TypeRelationId, fpinfo)) return false; /* * Now, merge my collation information into my parent's state. */ if (state > outer_cxt->state) { /* Override previous parent state */ outer_cxt->collation = collation; outer_cxt->state = state; } else if (state == outer_cxt->state) { /* Merge, or detect error if there's a collation conflict */ switch (state) { case FDW_COLLATE_NONE: /* Nothing + nothing is still nothing */ break; case FDW_COLLATE_SAFE: if (collation != outer_cxt->collation) { /* * Non-default collation always beats default. */ if (outer_cxt->collation == DEFAULT_COLLATION_OID) { /* Override previous parent state */ outer_cxt->collation = collation; } else if (collation != DEFAULT_COLLATION_OID) { /* * Conflict; show state as indeterminate. We don't * want to "return false" right away, since parent * node might not care about collation. */ outer_cxt->state = FDW_COLLATE_UNSAFE; } } break; case FDW_COLLATE_UNSAFE: /* We're still conflicted ... */ break; } } /* It looks OK */ return true; } /* * Returns true if given expr is something we'd have to send the value of * to the foreign server. * * This should return true when the expression is a shippable node that * deparseExpr would add to context->params_list. Note that we don't care * if the expression *contains* such a node, only whether one appears at top * level. We need this to detect cases where setrefs.c would recognize a * false match between an fdw_exprs item (which came from the params_list) * and an entry in fdw_scan_tlist (which we're considering putting the given * expression into). */ bool is_foreign_param(PlannerInfo *root, RelOptInfo *baserel, Expr *expr) { if (expr == NULL) return false; switch (nodeTag(expr)) { case T_Var: { /* It would have to be sent unless it's a foreign Var */ Var *var = (Var *) expr; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) (baserel->fdw_private); Relids relids; if (IS_UPPER_REL(baserel)) relids = fpinfo->outerrel->relids; else relids = baserel->relids; if (bms_is_member(var->varno, relids) && var->varlevelsup == 0) return false; /* foreign Var, so not a param */ else return true; /* it'd have to be a param */ break; } case T_Param: /* Params always have to be sent to the foreign server */ return true; default: break; } return false; } /* * Returns true if it's safe to push down the sort expression described by * 'pathkey' to the foreign server. */ bool is_foreign_pathkey(PlannerInfo *root, RelOptInfo *baserel, PathKey *pathkey) { EquivalenceClass *pathkey_ec = pathkey->pk_eclass; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) baserel->fdw_private; /* * is_foreign_expr would detect volatile expressions as well, but checking * ec_has_volatile here saves some cycles. */ if (pathkey_ec->ec_has_volatile) return false; /* can't push down the sort if the pathkey's opfamily is not shippable */ if (!is_shippable(pathkey->pk_opfamily, OperatorFamilyRelationId, fpinfo)) return false; /* can push if a suitable EC member exists */ return (find_em_for_rel(root, pathkey_ec, baserel) != NULL); } /* * Convert type OID + typmod info into a type name we can ship to the remote * server. Someplace else had better have verified that this type name is * expected to be known on the remote end. * * This is almost just format_type_with_typemod(), except that if left to its * own devices, that function will make schema-qualification decisions based * on the local search_path, which is wrong. We must schema-qualify all * type names that are not in pg_catalog. We assume here that built-in types * are all in pg_catalog and need not be qualified; otherwise, qualify. */ static char * deparse_type_name(Oid type_oid, int32 typemod) { /* bits16 was renamed to uint16 in PostgreSQL 19 */ #if PG_VERSION_NUM >= 190000 uint16 flags = FORMAT_TYPE_TYPEMOD_GIVEN; #else bits16 flags = FORMAT_TYPE_TYPEMOD_GIVEN; #endif if (!is_builtin(type_oid)) flags |= FORMAT_TYPE_FORCE_QUALIFY; return format_type_extended(type_oid, typemod, flags); } static char * monetdb_remote_type_name(Oid type_oid, int32 typemod) { HeapTuple tup; Form_pg_type typeform; char *typname; tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid)); if (!HeapTupleIsValid(tup)) elog(ERROR, "cache lookup failed for type %u", type_oid); typeform = (Form_pg_type) GETSTRUCT(tup); if (typeform->typtype == TYPTYPE_DOMAIN) { typname = NameStr(typeform->typname); if (strcmp(typname, "tinyint") == 0) { ReleaseSysCache(tup); return pstrdup("TINYINT"); } if (strcmp(typname, "clob") == 0) { ReleaseSysCache(tup); return pstrdup("CLOB"); } if (strcmp(typname, "string") == 0) { ReleaseSysCache(tup); return pstrdup("STRING"); } if (strcmp(typname, "url") == 0) { ReleaseSysCache(tup); return pstrdup("URL"); } if (strcmp(typname, "blob") == 0) { ReleaseSysCache(tup); return pstrdup("BLOB"); } if (strcmp(typname, "hugeint") == 0) { ReleaseSysCache(tup); return pstrdup("HUGEINT"); } } ReleaseSysCache(tup); return deparse_type_name(type_oid, typemod); } /* * Build the targetlist for given relation to be deparsed as SELECT clause. * * The output targetlist contains the columns that need to be fetched from the * foreign server for the given relation. If foreignrel is an upper relation, * then the output targetlist can also contain expressions to be evaluated on * foreign server. */ List * build_tlist_to_deparse(RelOptInfo *foreignrel) { List *tlist = NIL; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; ListCell *lc; /* * GROUP_AGG upper rels precompute a dedicated grouped_tlist that carries * the correct ressortgroupref values. ORDERED/FINAL upper rels emit the * same SELECT columns, so they should use that same list. Walk up the * outerrel chain until we find one. */ if (IS_UPPER_REL(foreignrel)) { RelOptInfo *cur = foreignrel; while (cur != NULL) { MonetdbFdwRelationInfo *cur_fp = (MonetdbFdwRelationInfo *) cur->fdw_private; if (cur_fp == NULL) break; if (cur_fp->grouped_tlist != NIL) return cur_fp->grouped_tlist; if (!IS_UPPER_REL(cur)) break; cur = cur_fp->outerrel; } } /* * We require columns specified in foreignrel->reltarget->exprs and those * required for evaluating the local conditions. */ tlist = add_to_flat_tlist(tlist, pull_var_clause((Node *) foreignrel->reltarget->exprs, PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS)); foreach(lc, fpinfo->local_conds) { RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc); tlist = add_to_flat_tlist(tlist, pull_var_clause((Node *) rinfo->clause, PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS)); } return tlist; } /* * Deparse SELECT statement for given relation into buf. * * tlist contains the list of desired columns to be fetched from foreign server. * For a base relation fpinfo->attrs_used is used to construct SELECT clause, * hence the tlist is ignored for a base relation. * * remote_conds is the list of conditions to be deparsed into the WHERE clause * (or, in the case of upper relations, into the HAVING clause). * * If params_list is not NULL, it receives a list of Params and other-relation * Vars used in the clauses; these values must be transmitted to the remote * server as parameter values. * * If params_list is NULL, we're generating the query for EXPLAIN purposes, * so Params and other-relation Vars should be replaced by dummy values. * * pathkeys is the list of pathkeys to order the result by. * * is_subquery is the flag to indicate whether to deparse the specified * relation as a subquery. * * List of columns selected is returned in retrieved_attrs. */ void deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *tlist, List *remote_conds, List *pathkeys, bool has_final_sort, bool has_limit, bool is_subquery, List **retrieved_attrs, List **params_list) { deparse_expr_cxt context = {0}; deparse_expr_cxt grouped_context = {0}; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) rel->fdw_private; List *quals; bool grouped_bridge = is_grouped_subquery_bridge(rel); RangeTblEntry *bridge_rte = NULL; Query *bridge_query = NULL; List *bridge_having = NIL; List *bridge_where = NIL; List *groupby_tlist = tlist; if (grouped_bridge) { bridge_rte = planner_rt_fetch(rel->relid, root); Assert(bridge_rte->rtekind == RTE_SUBQUERY); Assert(bridge_rte->subquery != NULL); bridge_query = bridge_rte->subquery; groupby_tlist = bridge_query->targetList; if (bridge_query->jointree != NULL && bridge_query->jointree->quals != NULL) bridge_where = make_ands_implicit((Expr *) bridge_query->jointree->quals); if (bridge_query->havingQual != NULL) bridge_having = make_ands_implicit((Expr *) bridge_query->havingQual); } /* * We handle relations for foreign tables, joins between those and upper * relations. */ Assert(IS_JOIN_REL(rel) || IS_SIMPLE_REL(rel) || IS_UPPER_REL(rel)); /* Fill portions of context common to upper, join and base relation */ context.buf = buf; context.root = root; context.foreignrel = rel; /* * Determine the scan rel for the FROM clause. For stacked upper rels * (e.g. UPPERREL_ORDERED whose outerrel is UPPERREL_GROUP_AGG), we must * drill down through the chain until we reach an actual base/join rel, * because deparseFromExprForRel cannot handle an upper rel as input * (upper rels have relid=0 and no rangetable entry). */ { RelOptInfo *outer = IS_UPPER_REL(rel) ? fpinfo->outerrel : NULL; while (outer != NULL && IS_UPPER_REL(outer) && !is_grouped_subquery_bridge(outer)) { MonetdbFdwRelationInfo *outer_fp = (MonetdbFdwRelationInfo *) outer->fdw_private; if (outer_fp == NULL || outer_fp->outerrel == NULL) break; outer = outer_fp->outerrel; } context.scanrel = grouped_bridge ? rel : (IS_UPPER_REL(rel) ? outer : rel); } context.params_list = params_list; grouped_context = context; grouped_context.grouped_subquery_inner = grouped_bridge; /* Construct SELECT clause */ deparseSelectSql(tlist, is_subquery, retrieved_attrs, grouped_bridge ? &grouped_context : &context); /* * For upper relations, the WHERE clause is built from the remote * conditions of the underlying scan relation; otherwise, we can use the * supplied list of remote conditions directly. */ if (IS_UPPER_REL(rel) || grouped_bridge) { if (grouped_bridge) quals = bridge_where; else { MonetdbFdwRelationInfo *ofpinfo; /* * context.scanrel is the drilled-down base/join rel, so its * remote_conds contain the correct WHERE predicates regardless * of how many upper-rel levels sit above it. */ ofpinfo = (MonetdbFdwRelationInfo *) context.scanrel->fdw_private; quals = ofpinfo->remote_conds; } } else quals = remote_conds; /* Construct FROM and WHERE clauses */ deparseFromExpr(quals, grouped_bridge ? &grouped_context : &context); if (IS_UPPER_REL(rel) || grouped_bridge) { /* Append GROUP BY clause */ if (grouped_bridge) appendGroupByClauseForQuery(groupby_tlist, bridge_query, &context); else appendGroupByClause(tlist, &context); /* Append HAVING clause */ if (grouped_bridge ? bridge_having != NIL : remote_conds != NIL) { appendStringInfoString(buf, " HAVING "); appendConditions(grouped_bridge ? bridge_having : remote_conds, grouped_bridge ? &grouped_context : &context); } } /* Add ORDER BY clause if we found any useful pathkeys */ if (pathkeys) appendOrderByClause(pathkeys, has_final_sort, tlist, grouped_bridge ? &grouped_context : &context); /* Add LIMIT clause if necessary */ if (has_limit) appendLimitClause(grouped_bridge ? &grouped_context : &context); /* Add any necessary FOR UPDATE/SHARE. */ deparseLockingClause(grouped_bridge ? &grouped_context : &context); } static bool is_grouped_subquery_bridge(RelOptInfo *rel) { MonetdbFdwRelationInfo *fpinfo; if (rel == NULL || rel->fdw_private == NULL) return false; fpinfo = (MonetdbFdwRelationInfo *) rel->fdw_private; return (!IS_UPPER_REL(rel) && fpinfo->stage == UPPERREL_GROUP_AGG); } /* * Construct a simple SELECT statement that retrieves desired columns * of the specified foreign table, and append it to "buf". The output * contains just "SELECT ... ". * * We also create an integer List of the columns being retrieved, which is * returned to *retrieved_attrs, unless we deparse the specified relation * as a subquery. * * tlist is the list of desired columns. is_subquery is the flag to * indicate whether to deparse the specified relation as a subquery. * Read prologue of deparseSelectStmtForRel() for details. */ static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs, deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *foreignrel = context->foreignrel; PlannerInfo *root = context->root; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; /* * Construct SELECT list */ appendStringInfoString(buf, "SELECT "); if (retrieved_attrs) *retrieved_attrs = NIL; if (is_subquery || is_grouped_subquery_bridge(foreignrel)) { ListCell *lc; /* * For a relation that is deparsed as a subquery, emit expressions * specified in the relation's reltarget. Note that since this is for * the subquery, no need to care about *retrieved_attrs. */ deparseSubqueryTargetList(context); if (!is_subquery && is_grouped_subquery_bridge(foreignrel) && retrieved_attrs != NULL) { RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); ListCell *tlc; AttrNumber output_attno = 1; Assert(rte->rtekind == RTE_SUBQUERY); Assert(rte->subquery != NULL); foreach(lc, rte->subquery->targetList) { TargetEntry *tle = lfirst_node(TargetEntry, lc); TargetEntry *matching_tle = NULL; if (tle->resjunk) continue; foreach(tlc, tlist) { TargetEntry *scan_tle = lfirst_node(TargetEntry, tlc); Var *var; if (scan_tle->resjunk || !IsA(scan_tle->expr, Var)) continue; var = (Var *) scan_tle->expr; if (var->varlevelsup == 0 && var->varno == foreignrel->relid && var->varattno == output_attno) { matching_tle = scan_tle; break; } } if (matching_tle == NULL) elog(ERROR, "could not map grouped bridge output column %d into fdw_scan_tlist", output_attno); *retrieved_attrs = lappend_int(*retrieved_attrs, matching_tle->resno); output_attno++; } } } else if (IS_JOIN_REL(foreignrel) || IS_UPPER_REL(foreignrel)) { /* * For a join or upper relation the input tlist gives the list of * columns required to be fetched from the foreign server. */ deparseExplicitTargetList(tlist, false, retrieved_attrs, context); } else { /* * For a base relation fpinfo->attrs_used gives the list of columns * required to be fetched from the foreign server. */ RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); /* * Core code already has some lock on each rel being planned, so we * can use NoLock here. */ Relation rel = table_open(rte->relid, NoLock); deparseTargetList(buf, rte, foreignrel->relid, rel, false, fpinfo->attrs_used, false, retrieved_attrs); table_close(rel, NoLock); } } /* * Construct a FROM clause and, if needed, a WHERE clause, and append those to * "buf". * * quals is the list of clauses to be included in the WHERE clause. * (These may or may not include RestrictInfo decoration.) */ static void deparseFromExpr(List *quals, deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *scanrel = context->scanrel; /* For upper relations, scanrel must be either a joinrel or a baserel */ Assert(!IS_UPPER_REL(context->foreignrel) || IS_JOIN_REL(scanrel) || IS_SIMPLE_REL(scanrel)); /* Construct FROM clause */ appendStringInfoString(buf, " FROM "); if (IS_JOIN_REL(scanrel) && ((((MonetdbFdwRelationInfo *) scanrel->fdw_private)->jointype == JOIN_SEMI) || (((MonetdbFdwRelationInfo *) scanrel->fdw_private)->jointype == JOIN_ANTI))) { /* * Semi/anti-join: emit outer relation in FROM, then build WHERE with * EXISTS/NOT EXISTS for the inner relation. */ deparseFromExprForSemiJoin(quals, context); } else { if (scanrel != context->foreignrel && is_grouped_subquery_bridge(scanrel)) deparseRangeTblRef(buf, context->root, scanrel, true, (Index) 0, NULL, context->params_list); else deparseFromExprForRel(buf, context->root, scanrel, (bms_membership(scanrel->relids) == BMS_MULTIPLE), (Index) 0, NULL, context->params_list); /* Construct WHERE clause */ if (quals != NIL) { appendStringInfoString(buf, " WHERE "); appendConditions(quals, context); } } } /* * deparseFromExprForSemiJoin * * Generate FROM + WHERE for a top-level semi/anti-join relation. * The outer relation appears in FROM; the inner relation is expressed as * EXISTS/NOT EXISTS (SELECT 1 FROM inner WHERE joinclauses [AND inner_conds]). * Any additional quals (pushed-down upper-rel conditions) are added to WHERE. */ static AttrNumber find_null_test_attno(RelOptInfo *rel, List *exprs) { ListCell *lc; foreach(lc, exprs) { Node *node = (Node *) lfirst(lc); List *vars; ListCell *vlc; if (IsA(node, RestrictInfo)) node = (Node *) ((RestrictInfo *) node)->clause; vars = pull_var_clause(node, PVC_RECURSE_AGGREGATES | PVC_RECURSE_PLACEHOLDERS); foreach(vlc, vars) { Var *var = lfirst_node(Var, vlc); if (bms_is_member(var->varno, rel->relids) && var->varattno > 0) return var->varattno; } } return InvalidAttrNumber; } static void deparseFromExprForSemiJoin(List *quals, deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *scanrel = context->scanrel; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) scanrel->fdw_private; RelOptInfo *outerrel = fpinfo->outerrel; RelOptInfo *innerrel = fpinfo->innerrel; MonetdbFdwRelationInfo *fpinfo_o = (MonetdbFdwRelationInfo *) outerrel->fdw_private; MonetdbFdwRelationInfo *fpinfo_i = (MonetdbFdwRelationInfo *) innerrel->fdw_private; bool negated = (fpinfo->jointype == JOIN_ANTI); bool is_first = true; AttrNumber null_attno = InvalidAttrNumber; if (negated && IS_SIMPLE_REL(innerrel) && !fpinfo->make_innerrel_subquery) { null_attno = find_null_test_attno(innerrel, fpinfo->joinclauses); if (null_attno == InvalidAttrNumber) null_attno = find_null_test_attno(innerrel, fpinfo_i->remote_conds); } if (negated && null_attno != InvalidAttrNumber && fpinfo->make_outerrel_subquery) { RangeTblEntry *inner_rte = planner_rt_fetch(innerrel->relid, context->root); deparseRangeTblRef(buf, context->root, outerrel, fpinfo->make_outerrel_subquery, 0, NULL, context->params_list); appendStringInfoString(buf, " LEFT JOIN "); deparseRangeTblRef(buf, context->root, innerrel, false, 0, NULL, context->params_list); appendStringInfoString(buf, " ON "); appendConditions(fpinfo->joinclauses, context); if (fpinfo_i->remote_conds) { appendStringInfoString(buf, " AND "); appendConditions(fpinfo_i->remote_conds, context); } appendStringInfoString(buf, " WHERE "); if (quals) { appendConditions(quals, context); appendStringInfoString(buf, " AND "); } deparseColumnRef(buf, innerrel->relid, null_attno, inner_rte, true); appendStringInfoString(buf, " IS NULL"); return; } /* Emit the outer relation's FROM entry */ deparseRangeTblRef(buf, context->root, outerrel, fpinfo->make_outerrel_subquery, 0, NULL, context->params_list); /* Build WHERE: outer_conds AND EXISTS/NOT EXISTS(inner, joinclauses) AND quals */ if (fpinfo_o->remote_conds || fpinfo->joinclauses || quals) { appendStringInfoString(buf, " WHERE "); if (fpinfo_o->remote_conds) { appendConditions(fpinfo_o->remote_conds, context); is_first = false; } if (fpinfo->joinclauses) { if (!is_first) appendStringInfoString(buf, " AND "); deparseExistsSubquery(buf, context->root, innerrel, fpinfo->joinclauses, negated, context); is_first = false; } if (quals) { if (!is_first) appendStringInfoString(buf, " AND "); appendConditions(quals, context); } } } /* * deparseExistsSubquery * * Emit: EXISTS/NOT EXISTS (SELECT 1 FROM rel WHERE rel_conds AND extra_conds) * * For nested semi-join relations the function recurses, producing nested * EXISTS subqueries. For base relations fpinfo->remote_conds are the * relation's own filter conditions. Column references are resolved against * context->scanrel (the full outer semi-join rel), so they are always * correctly qualified (e.g. r2.ps_partkey). */ static void deparseExistsSubquery(StringInfo buf, PlannerInfo *root, RelOptInfo *rel, List *extra_conds, bool negated, deparse_expr_cxt *context) { MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) rel->fdw_private; appendStringInfoString(buf, negated ? "NOT EXISTS (SELECT 1 FROM " : "EXISTS (SELECT 1 FROM "); if (IS_JOIN_REL(rel) && (fpinfo->jointype == JOIN_SEMI || fpinfo->jointype == JOIN_ANTI)) { /* * Nested semi/anti-join: expand as outer_from WHERE outer_conds AND * EXISTS/NOT EXISTS(innerrel, inner_joinclauses) AND extra_conds. */ RelOptInfo *outerrel = fpinfo->outerrel; RelOptInfo *innerrel = fpinfo->innerrel; MonetdbFdwRelationInfo *fpinfo_o = (MonetdbFdwRelationInfo *) outerrel->fdw_private; bool inner_negated = (fpinfo->jointype == JOIN_ANTI); bool is_first = true; deparseRangeTblRef(buf, root, outerrel, fpinfo->make_outerrel_subquery, 0, NULL, context->params_list); if (fpinfo_o->remote_conds || fpinfo->joinclauses || extra_conds) { appendStringInfoString(buf, " WHERE "); if (fpinfo_o->remote_conds) { appendConditions(fpinfo_o->remote_conds, context); is_first = false; } if (fpinfo->joinclauses) { if (!is_first) appendStringInfoString(buf, " AND "); deparseExistsSubquery(buf, root, innerrel, fpinfo->joinclauses, inner_negated, context); is_first = false; } if (extra_conds) { if (!is_first) appendStringInfoString(buf, " AND "); appendConditions(extra_conds, context); } } } else { /* * Base relation or non-semi join: emit the standard FROM entry. * For base relations fpinfo->remote_conds are the rel's own filters. * For join relations the ON conditions are already embedded in the * FROM entry generated by deparseRangeTblRef. */ deparseRangeTblRef(buf, root, rel, IS_JOIN_REL(rel) && (fpinfo->make_outerrel_subquery || fpinfo->make_innerrel_subquery), 0, NULL, context->params_list); { List *all_conds = NIL; if (IS_SIMPLE_REL(rel) && fpinfo->remote_conds) all_conds = list_concat(list_copy(fpinfo->remote_conds), extra_conds); else all_conds = extra_conds; if (all_conds) { appendStringInfoString(buf, " WHERE "); appendConditions(all_conds, context); } } } appendStringInfoChar(buf, ')'); /* close EXISTS */ } /* * Emit a target list that retrieves the columns specified in attrs_used. * This is used for both SELECT and RETURNING targetlists; the is_returning * parameter is true only for a RETURNING targetlist. * * The tlist text is appended to buf, and we also create an integer List * of the columns being retrieved, which is returned to *retrieved_attrs. * * If qualify_col is true, add relation alias before the column name. */ static void deparseTargetList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool is_returning, Bitmapset *attrs_used, bool qualify_col, List **retrieved_attrs) { TupleDesc tupdesc = RelationGetDescr(rel); bool have_wholerow; bool first; int i; *retrieved_attrs = NIL; /* If there's a whole-row reference, we'll need all the columns. */ have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used); first = true; for (i = 1; i <= tupdesc->natts; i++) { Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1); /* Ignore dropped attributes. */ if (attr->attisdropped) continue; if (have_wholerow || bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used)) { if (!first) appendStringInfoString(buf, ", "); else if (is_returning) appendStringInfoString(buf, " RETURNING "); first = false; deparseColumnRef(buf, rtindex, i, rte, qualify_col); *retrieved_attrs = lappend_int(*retrieved_attrs, i); } } /* Don't generate bad syntax if no undropped columns */ if (first && !is_returning) appendStringInfoString(buf, "NULL"); } /* * Deparse the appropriate locking clause (FOR UPDATE or FOR SHARE) for a * given relation (context->scanrel). */ static void deparseLockingClause(deparse_expr_cxt *context) { StringInfo buf = context->buf; PlannerInfo *root = context->root; RelOptInfo *rel = context->scanrel; MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) rel->fdw_private; int relid = -1; while ((relid = bms_next_member(rel->relids, relid)) >= 0) { /* * Ignore relation if it appears in a lower subquery. Locking clause * for such a relation is included in the subquery if necessary. */ if (bms_is_member(relid, fpinfo->lower_subquery_rels)) continue; /* * Add FOR UPDATE/SHARE if appropriate. We apply locking during the * initial row fetch, rather than later on as is done for local * tables. The extra roundtrips involved in trying to duplicate the * local semantics exactly don't seem worthwhile (see also comments * for RowMarkType). * * Note: because we actually run the query as a cursor, this assumes * that DECLARE CURSOR ... FOR UPDATE is supported, which it isn't * before 8.3. */ if (bms_is_member(relid, root->all_result_relids) && (root->parse->commandType == CMD_UPDATE || root->parse->commandType == CMD_DELETE)) { /* Relation is UPDATE/DELETE target, so use FOR UPDATE (MonetDB not support FOR UPDATE) */ appendStringInfoString(buf, " /* FOR UPDATE */"); /* Add the relation alias if we are here for a join relation */ if (IS_JOIN_REL(rel)) appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid); } else { PlanRowMark *rc = get_plan_rowmark(root->rowMarks, relid); if (rc) { /* * Relation is specified as a FOR UPDATE/SHARE target, so * handle that. (But we could also see LCS_NONE, meaning this * isn't a target relation after all.) * * For now, just ignore any [NO] KEY specification, since (a) * it's not clear what that means for a remote table that we * don't have complete information about, and (b) it wouldn't * work anyway on older remote servers. Likewise, we don't * worry about NOWAIT. */ switch (rc->strength) { case LCS_NONE: /* No locking needed */ break; case LCS_FORKEYSHARE: case LCS_FORSHARE: appendStringInfoString(buf, " FOR SHARE"); break; case LCS_FORNOKEYUPDATE: case LCS_FORUPDATE: appendStringInfoString(buf, " FOR UPDATE"); break; } /* Add the relation alias if we are here for a join relation */ if (bms_membership(rel->relids) == BMS_MULTIPLE && rc->strength != LCS_NONE) appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid); } } } } /* * Deparse conditions from the provided list and append them to buf. * * The conditions in the list are assumed to be ANDed. This function is used to * deparse WHERE clauses, JOIN .. ON clauses and HAVING clauses. * * Depending on the caller, the list elements might be either RestrictInfos * or bare clauses. */ static void appendConditions(List *exprs, deparse_expr_cxt *context) { int nestlevel; ListCell *lc; bool is_first = true; StringInfo buf = context->buf; /* Make sure any constants in the exprs are printed portably */ nestlevel = set_transmission_modes(); foreach(lc, exprs) { Expr *expr = (Expr *) lfirst(lc); /* Extract clause from RestrictInfo, if required */ if (IsA(expr, RestrictInfo)) expr = ((RestrictInfo *) expr)->clause; /* Connect expressions with "AND" and parenthesize each condition. */ if (!is_first) appendStringInfoString(buf, " AND "); appendStringInfoChar(buf, '('); deparseExpr(expr, context); appendStringInfoChar(buf, ')'); is_first = false; } reset_transmission_modes(nestlevel); } /* Output join name for given join type */ const char * get_jointype_name(JoinType jointype) { switch (jointype) { case JOIN_INNER: return "INNER"; case JOIN_LEFT: return "LEFT"; case JOIN_RIGHT: return "RIGHT"; case JOIN_FULL: return "FULL"; case JOIN_SEMI: return "SEMI"; case JOIN_ANTI: return "ANTI"; default: /* Shouldn't come here, but protect from buggy code. */ elog(ERROR, "unsupported join type %d", jointype); } /* Keep compiler happy */ return NULL; } /* * Deparse given targetlist and append it to context->buf. * * tlist is list of TargetEntry's which in turn contain Var nodes. * * retrieved_attrs is the list of continuously increasing integers starting * from 1. It has same number of entries as tlist. * * This is used for both SELECT and RETURNING targetlists; the is_returning * parameter is true only for a RETURNING targetlist. */ static void deparseExplicitTargetList(List *tlist, bool is_returning, List **retrieved_attrs, deparse_expr_cxt *context) { ListCell *lc; StringInfo buf = context->buf; int i = 0; *retrieved_attrs = NIL; foreach(lc, tlist) { TargetEntry *tle = lfirst_node(TargetEntry, lc); if (i > 0) appendStringInfoString(buf, ", "); else if (is_returning) appendStringInfoString(buf, " RETURNING "); deparseExpr((Expr *) tle->expr, context); *retrieved_attrs = lappend_int(*retrieved_attrs, i + 1); i++; } if (i == 0 && !is_returning) appendStringInfoString(buf, "NULL"); } /* * Emit expressions specified in the given relation's reltarget. * * This is used for deparsing the given relation as a subquery. */ static void deparseSubqueryTargetList(deparse_expr_cxt *context) { StringInfo buf = context->buf; RelOptInfo *foreignrel = context->foreignrel; RangeTblEntry *rte = NULL; List *target_exprs = NIL; bool first; ListCell *lc; /* Should only be called in these cases. */ Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel)); if (is_grouped_subquery_bridge(foreignrel)) { rte = planner_rt_fetch(foreignrel->relid, context->root); Assert(rte->rtekind == RTE_SUBQUERY); Assert(rte->subquery != NULL); target_exprs = rte->subquery->targetList; } first = true; foreach(lc, target_exprs != NIL ? target_exprs : foreignrel->reltarget->exprs) { Node *node; deparse_expr_cxt inner_context = *context; if (target_exprs != NIL) { TargetEntry *tle = lfirst_node(TargetEntry, lc); if (tle->resjunk) continue; #if PG_VERSION_NUM >= 180000 node = flatten_group_exprs(context->root, rte->subquery, (Node *) tle->expr); #else node = (Node *) tle->expr; #endif } else node = (Node *) lfirst(lc); if (!first) appendStringInfoString(buf, ", "); first = false; if (target_exprs != NIL) inner_context.grouped_subquery_inner = true; deparseExpr((Expr *) node, &inner_context); } /* Don't generate bad syntax if no expressions */ if (first) appendStringInfoString(buf, "NULL"); } /* * Construct FROM clause for given relation * * The function constructs ... JOIN ... ON ... for join relation. For a base * relation it just returns schema-qualified tablename, with the appropriate * alias if so requested. * * 'ignore_rel' is either zero or the RT index of a target relation. In the * latter case the function constructs FROM clause of UPDATE or USING clause * of DELETE; it deparses the join relation as if the relation never contained * the target relation, and creates a List of conditions to be deparsed into * the top-level WHERE clause, which is returned to *ignore_conds. */ static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool use_alias, Index ignore_rel, List **ignore_conds, List **params_list) { MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; if (IS_JOIN_REL(foreignrel)) { StringInfoData join_sql_o; StringInfoData join_sql_i; RelOptInfo *outerrel = fpinfo->outerrel; RelOptInfo *innerrel = fpinfo->innerrel; bool outerrel_is_target = false; bool innerrel_is_target = false; if (ignore_rel > 0 && bms_is_member(ignore_rel, foreignrel->relids)) { /* * If this is an inner join, add joinclauses to *ignore_conds and * set it to empty so that those can be deparsed into the WHERE * clause. Note that since the target relation can never be * within the nullable side of an outer join, those could safely * be pulled up into the WHERE clause (see foreign_join_ok()). * Note also that since the target relation is only inner-joined * to any other relation in the query, all conditions in the join * tree mentioning the target relation could be deparsed into the * WHERE clause by doing this recursively. */ if (fpinfo->jointype == JOIN_INNER) { *ignore_conds = list_concat(*ignore_conds, fpinfo->joinclauses); fpinfo->joinclauses = NIL; } /* * Check if either of the input relations is the target relation. */ if (outerrel->relid == ignore_rel) outerrel_is_target = true; else if (innerrel->relid == ignore_rel) innerrel_is_target = true; } /* Deparse outer relation if not the target relation. */ if (!outerrel_is_target) { initStringInfo(&join_sql_o); deparseRangeTblRef(&join_sql_o, root, outerrel, fpinfo->make_outerrel_subquery, ignore_rel, ignore_conds, params_list); /* * If inner relation is the target relation, skip deparsing it. * Note that since the join of the target relation with any other * relation in the query is an inner join and can never be within * the nullable side of an outer join, the join could be * interchanged with higher-level joins (cf. identity 1 on outer * join reordering shown in src/backend/optimizer/README), which * means it's safe to skip the target-relation deparsing here. */ if (innerrel_is_target) { Assert(fpinfo->jointype == JOIN_INNER); Assert(fpinfo->joinclauses == NIL); appendBinaryStringInfo(buf, join_sql_o.data, join_sql_o.len); return; } } /* Deparse inner relation if not the target relation. */ if (!innerrel_is_target) { initStringInfo(&join_sql_i); deparseRangeTblRef(&join_sql_i, root, innerrel, fpinfo->make_innerrel_subquery, ignore_rel, ignore_conds, params_list); /* * If outer relation is the target relation, skip deparsing it. * See the above note about safety. */ if (outerrel_is_target) { Assert(fpinfo->jointype == JOIN_INNER); Assert(fpinfo->joinclauses == NIL); appendBinaryStringInfo(buf, join_sql_i.data, join_sql_i.len); return; } } /* Neither of the relations is the target relation. */ Assert(!outerrel_is_target && !innerrel_is_target); /* * For a join relation FROM clause entry is deparsed as * * ((outer relation) (inner relation) ON (joinclauses)) */ appendStringInfo(buf, "(%s %s JOIN %s ON ", join_sql_o.data, get_jointype_name(fpinfo->jointype), join_sql_i.data); /* Append join clause; (TRUE) if no join clause */ if (fpinfo->joinclauses) { deparse_expr_cxt context = {0}; context.buf = buf; context.foreignrel = foreignrel; context.scanrel = foreignrel; context.root = root; context.params_list = params_list; appendStringInfoChar(buf, '('); appendConditions(fpinfo->joinclauses, &context); appendStringInfoChar(buf, ')'); } else appendStringInfoString(buf, "(TRUE)"); /* End the FROM clause entry. */ appendStringInfoChar(buf, ')'); } else { RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); if (is_grouped_subquery_bridge(foreignrel)) { Assert(rte->rtekind == RTE_SUBQUERY); Assert(rte->subquery != NULL); deparse_grouped_subquery_from_node(buf, rte->subquery, linitial(rte->subquery->jointree->fromlist), &(deparse_expr_cxt) { .root = root, .foreignrel = foreignrel, .scanrel = foreignrel, .buf = buf, .params_list = params_list, }); return; } else { Relation rel; /* * Core code already has some lock on each rel being planned, so we * can use NoLock here. */ rel = table_open(rte->relid, NoLock); deparseRelation(buf, rel); /* * Add a unique alias to avoid any conflict in relation names due to * pulled up subqueries in the query being built for a pushed down * join. */ if (use_alias) appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, foreignrel->relid); table_close(rel, NoLock); } } } static void deparse_grouped_subquery_from_node(StringInfo buf, Query *subquery, Node *node, deparse_expr_cxt *context) { if (IsA(node, RangeTblRef)) { int rtindex = ((RangeTblRef *) node)->rtindex; RangeTblEntry *rte = rt_fetch(rtindex, subquery->rtable); Relation rel; Assert(rte->rtekind == RTE_RELATION); rel = table_open(rte->relid, NoLock); deparseRelation(buf, rel); appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex); table_close(rel, NoLock); return; } if (IsA(node, JoinExpr)) { JoinExpr *joinexpr = (JoinExpr *) node; appendStringInfoChar(buf, '('); deparse_grouped_subquery_from_node(buf, subquery, joinexpr->larg, context); appendStringInfo(buf, " %s JOIN ", get_jointype_name(joinexpr->jointype)); deparse_grouped_subquery_from_node(buf, subquery, joinexpr->rarg, context); appendStringInfoString(buf, " ON "); if (joinexpr->quals != NULL) { deparse_expr_cxt inner_context = *context; inner_context.grouped_subquery_inner = true; appendStringInfoChar(buf, '('); appendConditions(make_ands_implicit((Expr *) joinexpr->quals), &inner_context); appendStringInfoChar(buf, ')'); } else appendStringInfoString(buf, "(TRUE)"); appendStringInfoChar(buf, ')'); return; } elog(ERROR, "unsupported grouped subquery FROM node: %d", (int) nodeTag(node)); } /* * Append FROM clause entry for the given relation into buf. */ static void deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel, bool make_subquery, Index ignore_rel, List **ignore_conds, List **params_list) { MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; /* Should only be called in these cases. */ Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel)); Assert(fpinfo->local_conds == NIL); /* If make_subquery is true, deparse the relation as a subquery. */ if (make_subquery) { List *retrieved_attrs; int ncols; /* * The given relation shouldn't contain the target relation, because * this should only happen for input relations for a full join, and * such relations can never contain an UPDATE/DELETE target. */ Assert(ignore_rel == 0 || !bms_is_member(ignore_rel, foreignrel->relids)); /* Deparse the subquery representing the relation. */ appendStringInfoChar(buf, '('); deparseSelectStmtForRel(buf, root, foreignrel, NIL, fpinfo->remote_conds, NIL, false, false, true, &retrieved_attrs, params_list); appendStringInfoChar(buf, ')'); /* Append the relation alias. */ appendStringInfo(buf, " %s%d", SUBQUERY_REL_ALIAS_PREFIX, fpinfo->relation_index); /* * Append the column aliases if needed. Note that the subquery emits * expressions specified in the relation's reltarget (see * deparseSubqueryTargetList). */ if (is_grouped_subquery_bridge(foreignrel)) { RangeTblEntry *rte = planner_rt_fetch(foreignrel->relid, root); ListCell *lc; ncols = 0; Assert(rte->rtekind == RTE_SUBQUERY); Assert(rte->subquery != NULL); foreach(lc, rte->subquery->targetList) { TargetEntry *tle = lfirst_node(TargetEntry, lc); if (!tle->resjunk) ncols++; } } else ncols = list_length(foreignrel->reltarget->exprs); if (ncols > 0) { int i; appendStringInfoChar(buf, '('); for (i = 1; i <= ncols; i++) { if (i > 1) appendStringInfoString(buf, ", "); appendStringInfo(buf, "%s%d", SUBQUERY_COL_ALIAS_PREFIX, i); } appendStringInfoChar(buf, ')'); } } else { /* * Semi/anti-join rels cannot be emitted as "X SEMI/ANTI JOIN Y ON ..." * because that is not valid SQL. Force the subquery path so that * deparseSelectStmtForRel → deparseFromExpr → deparseFromExprForSemiJoin * generates the correct EXISTS-based SQL. */ if (IS_JOIN_REL(foreignrel) && (fpinfo->jointype == JOIN_SEMI || fpinfo->jointype == JOIN_ANTI)) { List *retrieved_attrs; Assert(ignore_rel == 0 || !bms_is_member(ignore_rel, foreignrel->relids)); appendStringInfoChar(buf, '('); deparseSelectStmtForRel(buf, root, foreignrel, NIL, fpinfo->remote_conds, NIL, false, false, true, &retrieved_attrs, params_list); appendStringInfoChar(buf, ')'); appendStringInfo(buf, " %s%d", SUBQUERY_REL_ALIAS_PREFIX, fpinfo->relation_index); } else deparseFromExprForRel(buf, root, foreignrel, true, ignore_rel, ignore_conds, params_list); } } /* * deparse remote INSERT statement * * The statement text is appended to buf, and we also create an integer List * of the columns being retrieved by WITH CHECK OPTION or RETURNING (if any), * which is returned to *retrieved_attrs. * * This also stores end position of the VALUES clause, so that we can rebuild * an INSERT for a batch of rows later. */ void deparseInsertSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, bool doNothing, List *withCheckOptionList, List *returningList, List **retrieved_attrs, int *values_end_len) { TupleDesc tupdesc = RelationGetDescr(rel); bool first; ListCell *lc; appendStringInfoString(buf, "INSERT INTO "); deparseRelation(buf, rel); if (targetAttrs) { appendStringInfoChar(buf, '('); first = true; foreach(lc, targetAttrs) { int attnum = lfirst_int(lc); if (!first) appendStringInfoString(buf, ", "); first = false; deparseColumnRef(buf, rtindex, attnum, rte, false); } appendStringInfoString(buf, ") VALUES ("); first = true; foreach(lc, targetAttrs) { int attnum = lfirst_int(lc); Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1); if (!first) appendStringInfoString(buf, ", "); first = false; if (attr->attgenerated) appendStringInfoString(buf, "DEFAULT"); else appendStringInfoString(buf, "?"); } appendStringInfoChar(buf, ')'); } else appendStringInfoString(buf, " DEFAULT VALUES"); *values_end_len = buf->len; if (doNothing) appendStringInfoString(buf, " ON CONFLICT DO NOTHING"); deparseReturningList(buf, rte, rtindex, rel, rel->trigdesc && rel->trigdesc->trig_insert_after_row, withCheckOptionList, returningList, retrieved_attrs); } /* * rebuild remote INSERT statement * * Provided a number of rows in a batch, builds INSERT statement with the * right number of parameters. */ void rebuildInsertSql(StringInfo buf, Relation rel, char *orig_query, List *target_attrs, int values_end_len, int num_params, int num_rows) { TupleDesc tupdesc = RelationGetDescr(rel); int i; int pindex; bool first; ListCell *lc; /* Make sure the values_end_len is sensible */ Assert((values_end_len > 0) && (values_end_len <= strlen(orig_query))); /* Copy up to the end of the first record from the original query */ appendBinaryStringInfo(buf, orig_query, values_end_len); /* * Add records to VALUES clause (we already have parameters for the first * row, so start at the right offset). */ pindex = num_params + 1; for (i = 0; i < num_rows; i++) { appendStringInfoString(buf, ", ("); first = true; foreach(lc, target_attrs) { int attnum = lfirst_int(lc); Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1); if (!first) appendStringInfoString(buf, ", "); first = false; if (attr->attgenerated) appendStringInfoString(buf, "DEFAULT"); else { appendStringInfo(buf, "$%d", pindex); pindex++; } } appendStringInfoChar(buf, ')'); } /* Copy stuff after VALUES clause from the original query */ appendStringInfoString(buf, orig_query + values_end_len); } /* * deparse remote UPDATE statement * * The statement text is appended to buf, and we also create an integer List * of the columns being retrieved by WITH CHECK OPTION or RETURNING (if any), * which is returned to *retrieved_attrs. */ void deparseUpdateSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *targetAttrs, List *withCheckOptionList, List *returningList, List **retrieved_attrs) { TupleDesc tupdesc = RelationGetDescr(rel); bool first; ListCell *lc; Oid relid = RelationGetRelid(rel); bool has_key = false; StringInfoData where_expr; initStringInfo(&where_expr); /* loop through all columns of the foreign table */ for (int i = 0; i < tupdesc->natts; ++i) { Form_pg_attribute att = TupleDescAttr(tupdesc, i); ListCell *option; /* look for the "key" option on this column */ List *option_list = GetForeignColumnOptions(relid, att->attnum); foreach(option, option_list) { DefElem *def = (DefElem *)lfirst(option); /* if "key" is set, add a resjunk for this column */ if (strcmp(def->defname, OPT_KEY) == 0 && getBoolVal(def)) { if (!has_key) { has_key = true; appendStringInfo(&where_expr, " WHERE %s = ?", pstrdup(NameStr(att->attname))); continue; } appendStringInfo(&where_expr, " AND %s = ?", pstrdup(NameStr(att->attname))); } } } if (!has_key) ereport(ERROR, (errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION), errmsg("no primary key column specified for foreign MonetDB table"), errdetail("For UPDATE or DELETE, at least one foreign table column must be marked as primary key column."))); appendStringInfoString(buf, "UPDATE "); deparseRelation(buf, rel); appendStringInfoString(buf, " SET "); first = true; foreach(lc, targetAttrs) { int attnum = lfirst_int(lc); Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1); if (!first) appendStringInfoString(buf, ", "); first = false; deparseColumnRef(buf, rtindex, attnum, rte, false); if (attr->attgenerated) appendStringInfoString(buf, " = DEFAULT"); else appendStringInfoString(buf, " = ?"); } appendStringInfoString(buf, where_expr.data); deparseReturningList(buf, rte, rtindex, rel, rel->trigdesc && rel->trigdesc->trig_update_after_row, withCheckOptionList, returningList, retrieved_attrs); pfree(where_expr.data); } /* * deparse remote UPDATE statement * * 'buf' is the output buffer to append the statement to * 'rtindex' is the RT index of the associated target relation * 'rel' is the relation descriptor for the target relation * 'foreignrel' is the RelOptInfo for the target relation or the join relation * containing all base relations in the query * 'targetlist' is the tlist of the underlying foreign-scan plan node * (note that this only contains new-value expressions and junk attrs) * 'targetAttrs' is the target columns of the UPDATE * 'remote_conds' is the qual clauses that must be evaluated remotely * '*params_list' is an output list of exprs that will become remote Params * 'returningList' is the RETURNING targetlist * '*retrieved_attrs' is an output list of integers of columns being retrieved * by RETURNING (if any) */ void deparseDirectUpdateSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *targetlist, List *targetAttrs, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs) { deparse_expr_cxt context = {0}; int nestlevel; bool first; RangeTblEntry *rte = planner_rt_fetch(rtindex, root); ListCell *lc, *lc2; /* Set up context struct for recursion */ context.root = root; context.foreignrel = foreignrel; context.scanrel = foreignrel; context.buf = buf; context.params_list = params_list; appendStringInfoString(buf, "UPDATE "); deparseRelation(buf, rel); if (foreignrel->reloptkind == RELOPT_JOINREL) appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex); appendStringInfoString(buf, " SET "); /* Make sure any constants in the exprs are printed portably */ nestlevel = set_transmission_modes(); first = true; forboth(lc, targetlist, lc2, targetAttrs) { TargetEntry *tle = lfirst_node(TargetEntry, lc); int attnum = lfirst_int(lc2); /* update's new-value expressions shouldn't be resjunk */ Assert(!tle->resjunk); if (!first) appendStringInfoString(buf, ", "); first = false; deparseColumnRef(buf, rtindex, attnum, rte, false); appendStringInfoString(buf, " = "); deparseExpr((Expr *) tle->expr, &context); } reset_transmission_modes(nestlevel); if (foreignrel->reloptkind == RELOPT_JOINREL) { List *ignore_conds = NIL; appendStringInfoString(buf, " FROM "); deparseFromExprForRel(buf, root, foreignrel, true, rtindex, &ignore_conds, params_list); remote_conds = list_concat(remote_conds, ignore_conds); } if (remote_conds) { appendStringInfoString(buf, " WHERE "); appendConditions(remote_conds, &context); } if (foreignrel->reloptkind == RELOPT_JOINREL) deparseExplicitTargetList(returningList, true, retrieved_attrs, &context); else deparseReturningList(buf, rte, rtindex, rel, false, NIL, returningList, retrieved_attrs); } /* * deparse remote DELETE statement * * The statement text is appended to buf, and we also create an integer List * of the columns being retrieved by RETURNING (if any), which is returned * to *retrieved_attrs. */ void deparseDeleteSql(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, List *returningList, List **retrieved_attrs) { bool has_key = false; StringInfoData where_expr; Oid relid = RelationGetRelid(rel); TupleDesc tupdesc = rel->rd_att; initStringInfo(&where_expr); /* loop through all columns of the foreign table */ for (int i = 0; i < tupdesc->natts; ++i) { Form_pg_attribute att = TupleDescAttr(tupdesc, i); ListCell *option; /* look for the "key" option on this column */ List *option_list = GetForeignColumnOptions(relid, att->attnum); foreach(option, option_list) { DefElem *def = (DefElem *)lfirst(option); /* if "key" is set, add a resjunk for this column */ if (strcmp(def->defname, OPT_KEY) == 0 && getBoolVal(def)) { if (!has_key) { has_key = true; appendStringInfo(&where_expr, " WHERE %s = ?", pstrdup(NameStr(att->attname))); continue; } appendStringInfo(&where_expr, " AND %s = ?", pstrdup(NameStr(att->attname))); } } } if (!has_key) ereport(ERROR, (errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION), errmsg("no primary key column specified for foreign MonetDB table"), errdetail("For UPDATE or DELETE, at least one foreign table column must be marked as primary key column."))); appendStringInfoString(buf, "DELETE FROM "); deparseRelation(buf, rel); appendStringInfoString(buf, where_expr.data); deparseReturningList(buf, rte, rtindex, rel, rel->trigdesc && rel->trigdesc->trig_delete_after_row, NIL, returningList, retrieved_attrs); pfree(where_expr.data); } /* * deparse remote DELETE statement * * 'buf' is the output buffer to append the statement to * 'rtindex' is the RT index of the associated target relation * 'rel' is the relation descriptor for the target relation * 'foreignrel' is the RelOptInfo for the target relation or the join relation * containing all base relations in the query * 'remote_conds' is the qual clauses that must be evaluated remotely * '*params_list' is an output list of exprs that will become remote Params * 'returningList' is the RETURNING targetlist * '*retrieved_attrs' is an output list of integers of columns being retrieved * by RETURNING (if any) */ void deparseDirectDeleteSql(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel, RelOptInfo *foreignrel, List *remote_conds, List **params_list, List *returningList, List **retrieved_attrs) { deparse_expr_cxt context = {0}; /* Set up context struct for recursion */ context.root = root; context.foreignrel = foreignrel; context.scanrel = foreignrel; context.buf = buf; context.params_list = params_list; appendStringInfoString(buf, "DELETE FROM "); deparseRelation(buf, rel); if (foreignrel->reloptkind == RELOPT_JOINREL) appendStringInfo(buf, " %s%d", REL_ALIAS_PREFIX, rtindex); if (foreignrel->reloptkind == RELOPT_JOINREL) { List *ignore_conds = NIL; appendStringInfoString(buf, " USING "); deparseFromExprForRel(buf, root, foreignrel, true, rtindex, &ignore_conds, params_list); remote_conds = list_concat(remote_conds, ignore_conds); } if (remote_conds) { appendStringInfoString(buf, " WHERE "); appendConditions(remote_conds, &context); } if (foreignrel->reloptkind == RELOPT_JOINREL) deparseExplicitTargetList(returningList, true, retrieved_attrs, &context); else deparseReturningList(buf, planner_rt_fetch(rtindex, root), rtindex, rel, false, NIL, returningList, retrieved_attrs); } /* * Add a RETURNING clause, if needed, to an INSERT/UPDATE/DELETE. */ static void deparseReturningList(StringInfo buf, RangeTblEntry *rte, Index rtindex, Relation rel, bool trig_after_row, List *withCheckOptionList, List *returningList, List **retrieved_attrs) { Bitmapset *attrs_used = NULL; if (trig_after_row) { /* whole-row reference acquires all non-system columns */ attrs_used = bms_make_singleton(0 - FirstLowInvalidHeapAttributeNumber); } if (withCheckOptionList != NIL) { /* * We need the attrs, non-system and system, mentioned in the local * query's WITH CHECK OPTION list. * * Note: we do this to ensure that WCO constraints will be evaluated * on the data actually inserted/updated on the remote side, which * might differ from the data supplied by the core code, for example * as a result of remote triggers. */ pull_varattnos((Node *) withCheckOptionList, rtindex, &attrs_used); } if (returningList != NIL) { /* * We need the attrs, non-system and system, mentioned in the local * query's RETURNING list. */ pull_varattnos((Node *) returningList, rtindex, &attrs_used); } if (attrs_used != NULL) deparseTargetList(buf, rte, rtindex, rel, true, attrs_used, false, retrieved_attrs); else *retrieved_attrs = NIL; } /* * Construct SELECT statement to acquire size in blocks of given relation. * * Note: we use local definition of block size, not remote definition. * This is perhaps debatable. * * Note: pg_relation_size() exists in 8.1 and later. */ void deparseAnalyzeSizeSql(StringInfo buf, Relation rel) { StringInfoData relname; /* We'll need the remote relation name as a literal. */ initStringInfo(&relname); deparseRelation(&relname, rel); appendStringInfoString(buf, "SELECT pg_catalog.pg_relation_size("); deparseStringLiteral(buf, relname.data); appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ); } /* * Construct SELECT statement to acquire the number of rows and the relkind of * a relation. * * Note: we just return the remote server's reltuples value, which might * be off a good deal, but it doesn't seem worth working harder. See * comments in postgresAcquireSampleRowsFunc. */ void deparseAnalyzeInfoSql(StringInfo buf, Relation rel) { StringInfoData relname; /* We'll need the remote relation name as a literal. */ initStringInfo(&relname); deparseRelation(&relname, rel); appendStringInfoString(buf, "SELECT reltuples, relkind FROM pg_catalog.pg_class WHERE oid = "); deparseStringLiteral(buf, relname.data); appendStringInfoString(buf, "::pg_catalog.regclass"); } /* * Construct a simple "TRUNCATE rel" statement */ void deparseTruncateSql(StringInfo buf, List *rels, DropBehavior behavior, bool restart_seqs) { ListCell *cell; appendStringInfoString(buf, "TRUNCATE "); foreach(cell, rels) { Relation rel = lfirst(cell); if (cell != list_head(rels)) appendStringInfoString(buf, ", "); deparseRelation(buf, rel); } appendStringInfo(buf, " %s IDENTITY", restart_seqs ? "RESTART" : "CONTINUE"); if (behavior == DROP_RESTRICT) appendStringInfoString(buf, " RESTRICT"); else if (behavior == DROP_CASCADE) appendStringInfoString(buf, " CASCADE"); } /* * Construct name to use for given column, and emit it into buf. * If it has a column_name FDW option, use that instead of attribute name. * * If qualify_col is true, qualify column name with the alias of relation. */ static void deparseColumnRef(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col) { if (rte->rtekind == RTE_SUBQUERY && rte->subquery != NULL && varattno > 0 && deparse_subquery_column_ref(buf, varno, varattno, rte, qualify_col)) return; if (varattno < 0) { /* * All other system attributes are fetched as 0, except for table OID, * which is fetched as the local table OID. However, we must be * careful; the table could be beneath an outer join, in which case it * must go to NULL whenever the rest of the row does. */ Oid fetchval = 0; if (varattno == TableOidAttributeNumber) fetchval = rte->relid; if (qualify_col) { appendStringInfoString(buf, "CASE WHEN ("); ADD_REL_QUALIFIER(buf, varno); appendStringInfo(buf, "*)::text IS NOT NULL THEN %u END", fetchval); } else appendStringInfo(buf, "%u", fetchval); } else if (varattno == 0) { /* Whole row reference */ Relation rel; Bitmapset *attrs_used; /* Required only to be passed down to deparseTargetList(). */ List *retrieved_attrs; /* * The lock on the relation will be held by upper callers, so it's * fine to open it with no lock here. */ rel = table_open(rte->relid, NoLock); /* * The local name of the foreign table can not be recognized by the * foreign server and the table it references on foreign server might * have different column ordering or different columns than those * declared locally. Hence we have to deparse whole-row reference as * ROW(columns referenced locally). Construct this by deparsing a * "whole row" attribute. */ attrs_used = bms_add_member(NULL, 0 - FirstLowInvalidHeapAttributeNumber); /* * In case the whole-row reference is under an outer join then it has * to go NULL whenever the rest of the row goes NULL. Deparsing a join * query would always involve multiple relations, thus qualify_col * would be true. */ if (qualify_col) { appendStringInfoString(buf, "CASE WHEN ("); ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, "*)::text IS NOT NULL THEN "); } appendStringInfoString(buf, "ROW("); deparseTargetList(buf, rte, varno, rel, false, attrs_used, qualify_col, &retrieved_attrs); appendStringInfoChar(buf, ')'); /* Complete the CASE WHEN statement started above. */ if (qualify_col) appendStringInfoString(buf, " END"); table_close(rel, NoLock); bms_free(attrs_used); } else { char *colname = NULL; List *options; ListCell *lc; /* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */ Assert(!IS_SPECIAL_VARNO(varno)); /* * If it's a column of a foreign table, and it has the column_name FDW * option, use that value. */ options = NIL; if (OidIsValid(rte->relid)) options = GetForeignColumnOptions(rte->relid, varattno); foreach(lc, options) { DefElem *def = (DefElem *) lfirst(lc); if (strcmp(def->defname, "column_name") == 0) { colname = defGetString(def); break; } } /* * If it's a column of a regular table or it doesn't have column_name * FDW option, use attribute name. */ if (colname == NULL) { if (OidIsValid(rte->relid)) colname = get_attname(rte->relid, varattno, false); else colname = get_rte_attribute_name(rte, varattno); } if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, quote_identifier(colname)); } } static bool deparse_subquery_column_ref(StringInfo buf, int varno, int varattno, RangeTblEntry *rte, bool qualify_col) { TargetEntry *tle; TargetEntry *raw_tle; ListCell *lc; RangeTblEntry *base_rte = NULL; Var *inner_var; RangeTblEntry *inner_rte; char *colname; tle = get_nth_visible_tle(rte->subquery->targetList, varattno); raw_tle = get_tle_by_resno(rte->subquery->targetList, varattno); if (tle == NULL && raw_tle != NULL) { Node *reference_expr = strip_implicit_coercions((Node *) raw_tle->expr); foreach(lc, rte->subquery->targetList) { TargetEntry *candidate = lfirst_node(TargetEntry, lc); Node *candidate_expr; if (candidate->resjunk) continue; candidate_expr = strip_implicit_coercions((Node *) candidate->expr); if (equal(candidate_expr, reference_expr)) { tle = candidate; break; } } if (tle == NULL && !raw_tle->resjunk) tle = raw_tle; } if (tle == NULL && !OidIsValid(rte->relid)) { foreach(lc, rte->subquery->targetList) { TargetEntry *candidate = lfirst_node(TargetEntry, lc); Node *candidate_expr; if (candidate->resjunk) continue; candidate_expr = strip_implicit_coercions((Node *) candidate->expr); if (!IsA(candidate_expr, Var)) continue; inner_var = (Var *) candidate_expr; if (inner_var->varattno == varattno) { tle = candidate; break; } } } if (tle == NULL && !OidIsValid(rte->relid)) { if (rte->subquery->jointree != NULL && list_length(rte->subquery->jointree->fromlist) == 1 && IsA(linitial(rte->subquery->jointree->fromlist), RangeTblRef)) { base_rte = rt_fetch(((RangeTblRef *) linitial(rte->subquery->jointree->fromlist))->rtindex, rte->subquery->rtable); if (base_rte->rtekind == RTE_RELATION) { const char *base_colname = get_attname(base_rte->relid, varattno, false); foreach(lc, rte->subquery->targetList) { TargetEntry *candidate = lfirst_node(TargetEntry, lc); if (candidate->resjunk || candidate->resname == NULL) continue; if (strcmp(candidate->resname, base_colname) == 0) { tle = candidate; break; } } if (tle == NULL) { if (qualify_col) ADD_REL_QUALIFIER(buf, varno); appendStringInfoString(buf, quote_identifier(base_colname)); return true; } } } } if (tle == NULL || tle->resjunk) return false; if (qualify_col) ADD_REL_QUALIFIER(buf, varno); if (tle->resname != NULL) { appendStringInfoString(buf, quote_identifier(tle->resname)); return true; } if (!IsA(tle->expr, Var)) return false; inner_var = (Var *) tle->expr; inner_rte = rt_fetch(inner_var->varno, rte->subquery->rtable); if (inner_rte->rtekind != RTE_RELATION) return false; colname = get_attname(inner_rte->relid, inner_var->varattno, false); appendStringInfoString(buf, quote_identifier(colname)); return true; } /* * Append remote name of specified foreign table to buf. * Use value of table_name FDW option (if any) instead of relation's name. * Similarly, schema_name FDW option overrides schema name. */ static void deparseRelation(StringInfo buf, Relation rel) { ForeignTable *table; const char *nspname = NULL; const char *relname = NULL; ListCell *lc; /* obtain additional catalog information. */ table = GetForeignTable(RelationGetRelid(rel)); /* * Use value of FDW options if any, instead of the name of object itself. */ foreach(lc, table->options) { DefElem *def = (DefElem *) lfirst(lc); if (strcmp(def->defname, "schema_name") == 0) nspname = defGetString(def); else if (strcmp(def->defname, "table_name") == 0) relname = defGetString(def); } /* * Note: we could skip printing the schema name if it's pg_catalog, but * that doesn't seem worth the trouble. */ if (nspname == NULL) nspname = get_namespace_name(RelationGetNamespace(rel)); if (relname == NULL) relname = RelationGetRelationName(rel); appendStringInfo(buf, "%s.%s", quote_identifier(nspname), quote_identifier(relname)); } /* * Append a SQL string literal representing "val" to buf. */ void deparseStringLiteral(StringInfo buf, const char *val) { const char *valptr; /* Use escape string syntax when backslashes need to be preserved. */ if (strchr(val, '\\') != NULL) appendStringInfoChar(buf, ESCAPE_STRING_SYNTAX); appendStringInfoChar(buf, '\''); for (valptr = val; *valptr; valptr++) { char ch = *valptr; if (SQL_STR_DOUBLE(ch, true)) appendStringInfoChar(buf, ch); appendStringInfoChar(buf, ch); } appendStringInfoChar(buf, '\''); } /* * Deparse given expression into context->buf. * * This function must support all the same node types that foreign_expr_walker * accepts. * * Note: unlike ruleutils.c, we just use a simple hard-wired parenthesization * scheme: anything more complex than a Var, Const, function call or cast * should be self-parenthesized. */ static void deparseExpr(Expr *node, deparse_expr_cxt *context) { if (node == NULL) return; switch (nodeTag(node)) { case T_Var: deparseVar((Var *) node, context); break; case T_Const: deparseConst((Const *) node, context, 0); break; case T_Param: deparseParam((Param *) node, context); break; case T_SubscriptingRef: deparseSubscriptingRef((SubscriptingRef *) node, context); break; case T_FuncExpr: deparseFuncExpr((FuncExpr *) node, context); break; case T_OpExpr: deparseOpExpr((OpExpr *) node, context); break; case T_DistinctExpr: deparseDistinctExpr((DistinctExpr *) node, context); break; case T_ScalarArrayOpExpr: deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context); break; case T_RelabelType: deparseRelabelType((RelabelType *) node, context); break; case T_BoolExpr: deparseBoolExpr((BoolExpr *) node, context); break; case T_NullTest: deparseNullTest((NullTest *) node, context); break; case T_CaseExpr: deparseCaseExpr((CaseExpr *) node, context); break; case T_NullIfExpr: deparseNullIfExpr((NullIfExpr *) node, context); break; case T_CoalesceExpr: deparseCoalesceExpr((CoalesceExpr *) node, context); break; case T_MinMaxExpr: deparseMinMaxExpr((MinMaxExpr *) node, context); break; case T_ArrayExpr: deparseArrayExpr((ArrayExpr *) node, context); break; case T_Aggref: deparseAggref((Aggref *) node, context); break; case T_SubPlan: deparseSubPlan((SubPlan *) node, context); break; default: elog(ERROR, "unsupported expression type for deparse: %d", (int) nodeTag(node)); break; } } /* * Deparse given Var node into context->buf. * * If the Var belongs to the foreign relation, just print its remote name. * Otherwise, it's effectively a Param (and will in fact be a Param at * run time). Handle it the same way we handle plain Params --- see * deparseParam for comments. */ static void deparseVar(Var *node, deparse_expr_cxt *context) { Relids relids = context->scanrel->relids; int relno; int colno; /* Qualify columns when multiple relations are involved. */ bool qualify_col = (bms_membership(relids) == BMS_MULTIPLE); if (is_grouped_subquery_bridge(context->scanrel) && context->foreignrel != context->scanrel && node->varlevelsup == 0 && bms_is_member(node->varno, relids) && node->varattno > 0) { get_relation_column_alias_ids(node, context->scanrel, &relno, &colno); appendStringInfo(context->buf, "%s%d.%s%d", SUBQUERY_REL_ALIAS_PREFIX, relno, SUBQUERY_COL_ALIAS_PREFIX, colno); return; } if (is_grouped_subquery_bridge(context->scanrel) && deparse_grouped_subquery_bridge_var(node, context)) return; /* * If the Var belongs to the foreign relation that is deparsed as a * subquery, use the relation and column alias to the Var provided by the * subquery, instead of the remote name. */ if (is_subquery_var(node, context->scanrel, &relno, &colno)) { appendStringInfo(context->buf, "%s%d.%s%d", SUBQUERY_REL_ALIAS_PREFIX, relno, SUBQUERY_COL_ALIAS_PREFIX, colno); return; } if (bms_is_member(node->varno, relids) && node->varlevelsup == 0) deparseColumnRef(context->buf, node->varno, node->varattno, planner_rt_fetch(node->varno, context->root), qualify_col); else { /* Treat like a Param */ if (context->params_list) { int pindex = 0; ListCell *lc; /* find its index in params_list */ foreach(lc, *context->params_list) { pindex++; if (equal(node, (Node *) lfirst(lc))) break; } if (lc == NULL) { /* not in list, so add it */ pindex++; *context->params_list = lappend(*context->params_list, node); } printRemoteParam(pindex, node->vartype, node->vartypmod, context); } else { printRemotePlaceholder(node->vartype, node->vartypmod, context); } } } static bool deparse_grouped_subquery_bridge_var(Var *node, deparse_expr_cxt *context) { RangeTblEntry *bridge_rte; Query *subquery; TargetEntry *tle; Var *inner_var; RangeTblEntry *inner_rte; if (node->varlevelsup != 0) return false; bridge_rte = planner_rt_fetch(context->scanrel->relid, context->root); if (bridge_rte->rtekind != RTE_SUBQUERY || bridge_rte->subquery == NULL) return false; subquery = bridge_rte->subquery; if (context->grouped_subquery_inner) { if (node->varno < 1 || node->varno > list_length(subquery->rtable)) return false; inner_rte = rt_fetch(node->varno, subquery->rtable); #if PG_VERSION_NUM >= 180000 if (inner_rte->rtekind == RTE_GROUP) return false; #endif if (inner_rte->rtekind != RTE_RELATION) return false; deparseColumnRef(context->buf, node->varno, node->varattno, inner_rte, true); return true; } if (node->varno == context->scanrel->relid) { Node *reference_expr = NULL; tle = get_nth_visible_tle(subquery->targetList, node->varattno); if (tle != NULL) { reference_expr = (Node *) tle->expr; #if PG_VERSION_NUM >= 180000 reference_expr = flatten_group_exprs(context->root, subquery, reference_expr); #endif reference_expr = strip_implicit_coercions(reference_expr); if (!tle->resjunk) reference_expr = NULL; else tle = NULL; } if (tle == NULL) { ListCell *lc; foreach(lc, subquery->targetList) { TargetEntry *candidate = lfirst_node(TargetEntry, lc); Node *candidate_expr; if (candidate->resjunk) continue; candidate_expr = (Node *) candidate->expr; #if PG_VERSION_NUM >= 180000 candidate_expr = flatten_group_exprs(context->root, subquery, candidate_expr); #endif candidate_expr = strip_implicit_coercions(candidate_expr); if (reference_expr != NULL) { if (equal(candidate_expr, reference_expr)) { tle = candidate; break; } continue; } if (!IsA(candidate_expr, Var)) continue; inner_var = (Var *) candidate_expr; if (inner_var->varattno == node->varattno) { tle = candidate; break; } } } if (tle == NULL || tle->resjunk) return false; if (tle->resname != NULL) { appendStringInfoString(context->buf, quote_identifier(tle->resname)); return true; } if (!IsA(tle->expr, Var)) return false; inner_var = (Var *) tle->expr; inner_rte = rt_fetch(inner_var->varno, subquery->rtable); #if PG_VERSION_NUM >= 180000 if (inner_rte->rtekind == RTE_GROUP) return false; #endif deparseColumnRef(context->buf, inner_var->varno, inner_var->varattno, inner_rte, true); return true; } if (node->varno < 1 || node->varno > list_length(subquery->rtable)) return false; inner_rte = rt_fetch(node->varno, subquery->rtable); #if PG_VERSION_NUM >= 180000 if (inner_rte->rtekind == RTE_GROUP) { tle = get_nth_visible_tle(subquery->targetList, node->varattno); if (tle != NULL && !tle->resjunk && tle->resname != NULL) { appendStringInfoString(context->buf, quote_identifier(tle->resname)); return true; } return false; } #endif if (inner_rte->rtekind != RTE_RELATION) return false; deparseColumnRef(context->buf, node->varno, node->varattno, inner_rte, true); return true; } /* * Deparse given constant value into context->buf. * * As in that function, never show "::typename" */ static void deparseConst(Const *node, deparse_expr_cxt *context, int showtype) { StringInfo buf = context->buf; Oid typoutput; bool typIsVarlena; char *extval; Oid basetype = getBaseType(node->consttype); if (node->constisnull) { appendStringInfoString(buf, "NULL"); return; } if (basetype == BYTEAOID) { extval = deparseByteaHexLiteral(node->constvalue, node->consttype); deparseStringLiteral(buf, extval); pfree(extval); return; } getTypeOutputInfo(node->consttype, &typoutput, &typIsVarlena); extval = OidOutputFunctionCall(typoutput, node->constvalue); switch (basetype) { case DATEOID: appendStringInfoString(buf, "DATE "); deparseStringLiteral(buf, extval); break; case INTERVALOID: { Interval *interval = DatumGetIntervalP(node->constvalue); bool finite_interval = true; #if PG_VERSION_NUM >= 170000 finite_interval = !INTERVAL_NOT_FINITE(interval); #endif if (finite_interval && interval->day == 0 && interval->time == 0) appendStringInfo(buf, "INTERVAL '%d' MONTH", interval->month); else if (finite_interval && interval->month == 0 && interval->time == 0) appendStringInfo(buf, "INTERVAL '%d' DAY", interval->day); else deparseStringLiteral(buf, extval); } break; case INT2OID: case INT4OID: case INT8OID: case OIDOID: case FLOAT4OID: case FLOAT8OID: case NUMERICOID: { /* * No need to quote unless it's a special value such as 'NaN'. * See comments in get_const_expr(). */ if (strspn(extval, "0123456789+-eE.") == strlen(extval)) { if (extval[0] == '+' || extval[0] == '-') appendStringInfo(buf, "(%s)", extval); else appendStringInfoString(buf, extval); } else appendStringInfo(buf, "'%s'", extval); } break; case BITOID: case VARBITOID: appendStringInfo(buf, "B'%s'", extval); break; case BOOLOID: if (strcmp(extval, "t") == 0) appendStringInfoString(buf, "true"); else appendStringInfoString(buf, "false"); break; default: deparseStringLiteral(buf, extval); break; } pfree(extval); } static char * deparseByteaHexLiteral(Datum value, Oid type) { bytea *bytea_value; char *hex; char *dst; char *src; static const char hexdigits[] = "0123456789abcdef"; int len; Assert(getBaseType(type) == BYTEAOID); bytea_value = DatumGetByteaPP(value); len = VARSIZE_ANY_EXHDR(bytea_value); hex = palloc(len * 2 + 1); dst = hex; src = VARDATA_ANY(bytea_value); for (int i = 0; i < len; i++) { unsigned char byte = (unsigned char) src[i]; *dst++ = hexdigits[byte >> 4]; *dst++ = hexdigits[byte & 0x0F]; } *dst = '\0'; if ((Pointer) bytea_value != DatumGetPointer(value)) pfree(bytea_value); return hex; } /* * Deparse given Param node. * * If we're generating the query "for real", add the Param to * context->params_list if it's not already present, and then use its index * in that list as the remote parameter number. During EXPLAIN, there's * no need to identify a parameter number. */ static void deparseParam(Param *node, deparse_expr_cxt *context) { if (context->params_list) { int pindex = 0; ListCell *lc; /* find its index in params_list */ foreach(lc, *context->params_list) { pindex++; if (equal(node, (Node *) lfirst(lc))) break; } if (lc == NULL) { /* not in list, so add it */ pindex++; *context->params_list = lappend(*context->params_list, node); } printRemoteParam(pindex, node->paramtype, node->paramtypmod, context); } else { printRemotePlaceholder(node->paramtype, node->paramtypmod, context); } } /* * Deparse a container subscript expression. */ static void deparseSubscriptingRef(SubscriptingRef *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lowlist_item; ListCell *uplist_item; /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); /* * Deparse referenced array expression first. If that expression includes * a cast, we have to parenthesize to prevent the array subscript from * being taken as typename decoration. We can avoid that in the typical * case of subscripting a Var, but otherwise do it. */ if (IsA(node->refexpr, Var)) deparseExpr(node->refexpr, context); else { appendStringInfoChar(buf, '('); deparseExpr(node->refexpr, context); appendStringInfoChar(buf, ')'); } /* Deparse subscript expressions. */ lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */ foreach(uplist_item, node->refupperindexpr) { appendStringInfoChar(buf, '['); if (lowlist_item) { deparseExpr(lfirst(lowlist_item), context); appendStringInfoChar(buf, ':'); lowlist_item = lnext(node->reflowerindexpr, lowlist_item); } deparseExpr(lfirst(uplist_item), context); appendStringInfoChar(buf, ']'); } appendStringInfoChar(buf, ')'); } /* * Deparse a function call. */ static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple proctup; Form_pg_proc procform; bool use_variadic; bool first; ListCell *arg; if (deparseExtractFuncExpr(node, context)) return; proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(node->funcid)); if (!HeapTupleIsValid(proctup)) elog(ERROR, "cache lookup failed for function %u", node->funcid); procform = (Form_pg_proc) GETSTRUCT(proctup); if (list_length(node->args) == 1 && node->funcresulttype == DATEOID && strcmp(NameStr(procform->proname), "date") == 0) { appendStringInfoString(buf, "CAST("); deparseExpr((Expr *) linitial(node->args), context); appendStringInfoString(buf, " AS date)"); ReleaseSysCache(proctup); return; } /* * If the function call came from an implicit coercion, then just show the * first argument. */ if (node->funcformat == COERCE_IMPLICIT_CAST) { deparseExpr((Expr *) linitial(node->args), context); ReleaseSysCache(proctup); return; } /* * If the function call came from a cast, then show the first argument * plus an explicit cast operation. */ if (node->funcformat == COERCE_EXPLICIT_CAST) { Oid rettype = node->funcresulttype; int32 coercedTypmod; /* Get the typmod if this is a length-coercion function */ (void) exprIsLengthCoercion((Node *) node, &coercedTypmod); deparseExpr((Expr *) linitial(node->args), context); appendStringInfo(buf, "::%s", deparse_type_name(rettype, coercedTypmod)); ReleaseSysCache(proctup); return; } /* Check if need to print VARIADIC (cf. ruleutils.c) */ use_variadic = node->funcvariadic; /* * Normal function: display as proname(args). */ appendFunctionName(node->funcid, context); appendStringInfoChar(buf, '('); /* ... and all the arguments */ first = true; foreach(arg, node->args) { if (!first) appendStringInfoString(buf, ", "); if (use_variadic && lnext(node->args, arg) == NULL) appendStringInfoString(buf, "VARIADIC "); deparseExpr((Expr *) lfirst(arg), context); first = false; } appendStringInfoChar(buf, ')'); ReleaseSysCache(proctup); } static bool deparseExtractFuncExpr(FuncExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple proctup; Form_pg_proc procform; Const *field; char *fieldname; if (list_length(node->args) != 2) return false; proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(node->funcid)); if (!HeapTupleIsValid(proctup)) elog(ERROR, "cache lookup failed for function %u", node->funcid); procform = (Form_pg_proc) GETSTRUCT(proctup); if (strcmp(NameStr(procform->proname), "extract") != 0) { ReleaseSysCache(proctup); return false; } if (!IsA(linitial(node->args), Const)) { ReleaseSysCache(proctup); return false; } field = castNode(Const, linitial(node->args)); if (field->constisnull) { ReleaseSysCache(proctup); return false; } fieldname = TextDatumGetCString(field->constvalue); appendStringInfoString(buf, "EXTRACT("); appendStringInfoString(buf, fieldname); appendStringInfoString(buf, " FROM "); deparseExpr(lsecond(node->args), context); appendStringInfoChar(buf, ')'); pfree(fieldname); ReleaseSysCache(proctup); return true; } /* * Deparse given operator expression. To avoid problems around * priority of operations, we always parenthesize the arguments. */ static void 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. */ tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for operator %u", node->opno); form = (Form_pg_operator) GETSTRUCT(tuple); oprkind = form->oprkind; /* Sanity check. */ 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') { 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, ' '); } /* Deparse operator name. */ deparseOperatorName(buf, form); /* Deparse right operand. */ appendStringInfoChar(buf, ' '); if (canSuppressRightConstCast) deparseConst((Const *) right, context, -2); else deparseExpr(right, context); appendStringInfoChar(buf, ')'); ReleaseSysCache(tuple); } /* * 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 deparseOperatorName(StringInfo buf, Form_pg_operator opform) { char *opname; /* opname is not a SQL identifier, so we should not quote it. */ opname = NameStr(opform->oprname); /* * Translate PostgreSQL LIKE/ILIKE operator symbols to standard SQL * keywords that MonetDB understands. PostgreSQL uses internal operator * names (~~, !~~, ~~*, !~~*) that would confuse MonetDB, which maps ~~ * to a geometry function. */ if (opform->oprnamespace == PG_CATALOG_NAMESPACE) { if (strcmp(opname, "~~") == 0) { appendStringInfoString(buf, "LIKE"); return; } if (strcmp(opname, "!~~") == 0) { appendStringInfoString(buf, "NOT LIKE"); return; } if (strcmp(opname, "~~*") == 0) { appendStringInfoString(buf, "ILIKE"); return; } if (strcmp(opname, "!~~*") == 0) { appendStringInfoString(buf, "NOT ILIKE"); return; } } /* Print schema name only if it's not pg_catalog */ if (opform->oprnamespace != PG_CATALOG_NAMESPACE) { const char *opnspname; opnspname = get_namespace_name(opform->oprnamespace); /* Print fully qualified operator name. */ appendStringInfo(buf, "OPERATOR(%s.%s)", quote_identifier(opnspname), opname); } else { /* Just print operator name. */ appendStringInfoString(buf, opname); } } /* * Deparse IS DISTINCT FROM. */ static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; Assert(list_length(node->args) == 2); appendStringInfoChar(buf, '('); deparseExpr(linitial(node->args), context); appendStringInfoString(buf, " IS DISTINCT FROM "); deparseExpr(lsecond(node->args), context); appendStringInfoChar(buf, ')'); } /* * Deparse given ScalarArrayOpExpr expression. To avoid problems * around priority of operations, we always parenthesize the arguments. */ static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple tuple; Form_pg_operator form; Expr *arg1; Expr *arg2; /* Retrieve information about the operator from system catalog. */ tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(node->opno)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for operator %u", node->opno); form = (Form_pg_operator) GETSTRUCT(tuple); /* * MonetDB does not implement PostgreSQL's x = ANY('{...}') array syntax * with compatible semantics. For literal arrays, emit IN / NOT IN * instead, which matches the originating PostgreSQL expression. */ if (deparseScalarArrayOpExprAsIn(node, context, form)) { ReleaseSysCache(tuple); return; } /* Sanity check. */ Assert(list_length(node->args) == 2); /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); /* Deparse left operand. */ arg1 = linitial(node->args); deparseExpr(arg1, context); appendStringInfoChar(buf, ' '); /* Deparse operator name plus decoration. */ deparseOperatorName(buf, form); appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL"); /* Deparse right operand. */ arg2 = lsecond(node->args); deparseExpr(arg2, context); appendStringInfoChar(buf, ')'); /* Always parenthesize the expression. */ appendStringInfoChar(buf, ')'); ReleaseSysCache(tuple); } static bool deparseScalarArrayOpExprAsIn(ScalarArrayOpExpr *node, deparse_expr_cxt *context, Form_pg_operator opform) { StringInfo buf = context->buf; Expr *arg1; Expr *arg2; const char *opname = NameStr(opform->oprname); bool use_in; bool first = true; int nelems = 0; /* Support the common PostgreSQL rewrites for IN and NOT IN only. */ if (node->useOr) { if (strcmp(opname, "=") != 0) return false; use_in = true; } else { if (strcmp(opname, "<>") != 0) return false; use_in = false; } arg1 = linitial(node->args); arg2 = lsecond(node->args); appendStringInfoChar(buf, '('); deparseExpr(arg1, context); appendStringInfoString(buf, use_in ? " IN (" : " NOT IN ("); if (IsA(arg2, ArrayExpr)) { ArrayExpr *arrayexpr = castNode(ArrayExpr, arg2); ListCell *lc; foreach(lc, arrayexpr->elements) { Node *elem = lfirst(lc); if (IsA(elem, Const) && castNode(Const, elem)->constisnull) return false; if (!first) appendStringInfoString(buf, ", "); deparseExpr((Expr *) elem, context); first = false; nelems++; } } else if (IsA(arg2, Const) && type_is_array(((Const *) arg2)->consttype)) { Const *arrayconst = castNode(Const, arg2); ArrayType *arr; Oid elemtype; int16 elmlen; bool elembyval; char elemalign; Datum *elem_values; bool *elem_nulls; int i; arr = DatumGetArrayTypeP(arrayconst->constvalue); elemtype = ARR_ELEMTYPE(arr); get_typlenbyvalalign(elemtype, &elmlen, &elembyval, &elemalign); deconstruct_array(arr, elemtype, elmlen, elembyval, elemalign, &elem_values, &elem_nulls, &nelems); for (i = 0; i < nelems; i++) { Const *elemconst; if (elem_nulls[i]) return false; elemconst = makeConst(elemtype, -1, InvalidOid, elmlen, elem_values[i], false, elembyval); if (!first) appendStringInfoString(buf, ", "); deparseConst(elemconst, context, 0); first = false; } } else return false; if (nelems == 0) return false; appendStringInfoString(buf, "))"); return true; } /* * Deparse a RelabelType (binary-compatible cast) node. */ static void deparseRelabelType(RelabelType *node, deparse_expr_cxt *context) { deparseExpr(node->arg, context); if (node->relabelformat != COERCE_IMPLICIT_CAST) appendStringInfo(context->buf, "::%s", deparse_type_name(node->resulttype, node->resulttypmod)); } /* * Deparse a BoolExpr node. */ static void deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; const char *op = NULL; /* keep compiler quiet */ bool first; ListCell *lc; switch (node->boolop) { case AND_EXPR: op = "AND"; break; case OR_EXPR: op = "OR"; break; case NOT_EXPR: appendStringInfoString(buf, "(NOT "); deparseExpr(linitial(node->args), context); appendStringInfoChar(buf, ')'); return; } appendStringInfoChar(buf, '('); first = true; foreach(lc, node->args) { if (!first) appendStringInfo(buf, " %s ", op); deparseExpr((Expr *) lfirst(lc), context); first = false; } appendStringInfoChar(buf, ')'); } /* * Deparse IS [NOT] NULL expression. */ static void deparseNullTest(NullTest *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; appendStringInfoChar(buf, '('); deparseExpr(node->arg, context); /* * For scalar inputs, we prefer to print as IS [NOT] NULL, which is * shorter and traditional. If it's a rowtype input but we're applying a * scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically * correct. */ if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg))) { if (node->nulltesttype == IS_NULL) appendStringInfoString(buf, " IS NULL)"); else appendStringInfoString(buf, " IS NOT NULL)"); } else { if (node->nulltesttype == IS_NULL) appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)"); else appendStringInfoString(buf, " IS DISTINCT FROM NULL)"); } } /* * Deparse CASE expression */ static void deparseCaseExpr(CaseExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; appendStringInfoString(buf, "(CASE"); /* If this is a CASE arg WHEN then emit the arg expression */ if (node->arg != NULL) { appendStringInfoChar(buf, ' '); deparseExpr(node->arg, context); } /* Add each condition/result of the CASE clause */ foreach(lc, node->args) { CaseWhen *whenclause = (CaseWhen *) lfirst(lc); /* WHEN */ appendStringInfoString(buf, " WHEN "); if (node->arg == NULL) /* CASE WHEN */ deparseExpr(whenclause->expr, context); else /* CASE arg WHEN */ { /* Ignore the CaseTestExpr and equality operator. */ deparseExpr(lsecond(castNode(OpExpr, whenclause->expr)->args), context); } /* THEN */ appendStringInfoString(buf, " THEN "); deparseExpr(whenclause->result, context); } /* add ELSE if present */ if (node->defresult != NULL) { appendStringInfoString(buf, " ELSE "); deparseExpr(node->defresult, context); } /* append END */ appendStringInfoString(buf, " END)"); } /* * Deparse NULLIF(a, b). */ static void deparseNullIfExpr(NullIfExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; Expr *left; Expr *right; Assert(list_length(node->args) == 2); left = linitial(node->args); right = lsecond(node->args); appendStringInfoString(buf, "NULLIF("); deparseExpr(left, context); appendStringInfoString(buf, ", "); deparseExpr(right, context); appendStringInfoChar(buf, ')'); } /* * Deparse LEAST(...) / GREATEST(...). */ static void deparseMinMaxExpr(MinMaxExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; const char *funcname; if (node->op == IS_GREATEST) funcname = "GREATEST"; else { Assert(node->op == IS_LEAST); funcname = "LEAST"; } appendStringInfo(buf, "%s(", funcname); foreach(lc, node->args) { if (lc != list_head(node->args)) appendStringInfoString(buf, ", "); deparseExpr((Expr *) lfirst(lc), context); } appendStringInfoChar(buf, ')'); } /* * Deparse COALESCE(...). */ static void deparseCoalesceExpr(CoalesceExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; appendStringInfoString(buf, "COALESCE("); foreach(lc, node->args) { if (lc != list_head(node->args)) appendStringInfoString(buf, ", "); deparseExpr((Expr *) lfirst(lc), context); } appendStringInfoChar(buf, ')'); } /* * Deparse ARRAY[...] construct. */ static void deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; bool first = true; ListCell *lc; appendStringInfoString(buf, "ARRAY["); foreach(lc, node->elements) { if (!first) appendStringInfoString(buf, ", "); deparseExpr(lfirst(lc), context); first = false; } appendStringInfoChar(buf, ']'); /* If the array is empty, we need an explicit cast to the array type. */ if (node->elements == NIL) appendStringInfo(buf, "::%s", deparse_type_name(node->array_typeid, -1)); } /* * Deparse an Aggref node. */ static void deparseAggref(Aggref *node, deparse_expr_cxt *context) { StringInfo buf = context->buf; bool use_variadic; /* Only basic, non-split aggregation accepted. */ Assert(node->aggsplit == AGGSPLIT_SIMPLE); /* Check if need to print VARIADIC (cf. ruleutils.c) */ use_variadic = node->aggvariadic; /* Find aggregate name from aggfnoid which is a pg_proc entry */ appendFunctionName(node->aggfnoid, context); appendStringInfoChar(buf, '('); /* Add DISTINCT */ appendStringInfoString(buf, (node->aggdistinct != NIL) ? "DISTINCT " : ""); if (AGGKIND_IS_ORDERED_SET(node->aggkind)) { /* Add WITHIN GROUP (ORDER BY ..) */ ListCell *arg; bool first = true; Assert(!node->aggvariadic); Assert(node->aggorder != NIL); foreach(arg, node->aggdirectargs) { if (!first) appendStringInfoString(buf, ", "); first = false; deparseExpr((Expr *) lfirst(arg), context); } appendStringInfoString(buf, ") WITHIN GROUP (ORDER BY "); appendAggOrderBy(node->aggorder, node->args, context); } else { /* aggstar can be set only in zero-argument aggregates */ if (node->aggstar) appendStringInfoChar(buf, '*'); else { ListCell *arg; bool first = true; /* Add all the arguments */ foreach(arg, node->args) { TargetEntry *tle = (TargetEntry *) lfirst(arg); Node *n = (Node *) tle->expr; if (tle->resjunk) continue; if (!first) appendStringInfoString(buf, ", "); first = false; /* Add VARIADIC */ if (use_variadic && lnext(node->args, arg) == NULL) appendStringInfoString(buf, "VARIADIC "); deparseExpr((Expr *) n, context); } } /* Add ORDER BY */ if (node->aggorder != NIL) { appendStringInfoString(buf, " ORDER BY "); appendAggOrderBy(node->aggorder, node->args, context); } } /* Add FILTER (WHERE ..) */ if (node->aggfilter != NULL) { appendStringInfoString(buf, ") FILTER (WHERE "); deparseExpr((Expr *) node->aggfilter, context); } appendStringInfoChar(buf, ')'); } static void appendSubPlanSqlTemplate(SubPlan *subplan, ForeignScan *fscan, deparse_expr_cxt *context) { StringInfo buf = context->buf; char *inner_sql; const char *p; int n_fdw_exprs; inner_sql = strVal(linitial(fscan->fdw_private)); n_fdw_exprs = list_length(fscan->fdw_exprs); p = inner_sql; while (*p) { if (*p == '$' && isdigit((unsigned char) *(p + 1))) { int paramno = 0; p++; while (isdigit((unsigned char) *p)) paramno = paramno * 10 + (*p++ - '0'); if (p[0] == ':' && p[1] == ':') { p += 2; while (*p && (isalnum((unsigned char) *p) || *p == '_' || *p == '.')) p++; if (*p == ' ' && (pg_strncasecmp(p + 1, "precision", 9) == 0 || pg_strncasecmp(p + 1, "varying", 7) == 0)) { p++; while (*p && (isalnum((unsigned char) *p) || *p == '_')) p++; } if (*p == '(') { p++; while (*p && *p != ')') p++; if (*p) p++; } } if (paramno >= 1 && paramno <= n_fdw_exprs) { Param *param = (Param *) list_nth(fscan->fdw_exprs, paramno - 1); ListCell *lc_id; ListCell *lc_arg; bool found = false; forboth(lc_id, subplan->parParam, lc_arg, subplan->args) { if (lfirst_int(lc_id) == param->paramid) { deparseExpr((Expr *) lfirst(lc_arg), context); found = true; break; } } if (!found) appendStringInfo(buf, "$%d", paramno); } else appendStringInfo(buf, "$%d", paramno); } else appendStringInfoChar(buf, *p++); } } /* * Deparse a pushed-down SubPlan. */ static void deparseSubPlan(SubPlan *subplan, deparse_expr_cxt *context) { StringInfo buf = context->buf; Plan *subplan_plan; ForeignScan *fscan; Expr *any_outer_expr; Assert(subplan->subLinkType == EXPR_SUBLINK || subplan->subLinkType == ANY_SUBLINK); subplan_plan = (Plan *) list_nth(context->root->glob->subplans, subplan->plan_id - 1); Assert(IsA(subplan_plan, ForeignScan)); fscan = (ForeignScan *) subplan_plan; if (subplan->subLinkType == EXPR_SUBLINK) { appendStringInfoChar(buf, '('); appendSubPlanSqlTemplate(subplan, fscan, context); appendStringInfoChar(buf, ')'); return; } any_outer_expr = get_supported_any_sublink_outer_expr(subplan); Assert(any_outer_expr != NULL); appendStringInfoChar(buf, '('); deparseExpr(any_outer_expr, context); appendStringInfoString(buf, " IN ("); appendSubPlanSqlTemplate(subplan, fscan, context); appendStringInfoString(buf, "))"); } /* * Append ORDER BY within aggregate function. */ static void appendAggOrderBy(List *orderList, List *targetList, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; bool first = true; foreach(lc, orderList) { SortGroupClause *srt = (SortGroupClause *) lfirst(lc); Node *sortexpr; if (!first) appendStringInfoString(buf, ", "); first = false; /* Deparse the sort expression proper. */ sortexpr = deparseSortGroupClause(srt->tleSortGroupRef, targetList, false, context); /* Add decoration as needed. */ appendOrderBySuffix(srt->sortop, exprType(sortexpr), srt->nulls_first, context); } } /* * Append the ASC, DESC, USING and NULLS FIRST / NULLS LAST parts * of an ORDER BY clause. */ static void appendOrderBySuffix(Oid sortop, Oid sortcoltype, bool nulls_first, deparse_expr_cxt *context) { StringInfo buf = context->buf; TypeCacheEntry *typentry; /* See whether operator is default < or > for sort expr's datatype. */ typentry = lookup_type_cache(sortcoltype, TYPECACHE_LT_OPR | TYPECACHE_GT_OPR); if (sortop == typentry->lt_opr) appendStringInfoString(buf, " ASC"); else if (sortop == typentry->gt_opr) appendStringInfoString(buf, " DESC"); else { HeapTuple opertup; Form_pg_operator operform; appendStringInfoString(buf, " USING "); /* Append operator name. */ opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(sortop)); if (!HeapTupleIsValid(opertup)) elog(ERROR, "cache lookup failed for operator %u", sortop); operform = (Form_pg_operator) GETSTRUCT(opertup); deparseOperatorName(buf, operform); ReleaseSysCache(opertup); } if (nulls_first) appendStringInfoString(buf, " NULLS FIRST"); else appendStringInfoString(buf, " NULLS LAST"); } /* * Print the representation of a parameter to be sent to the remote side. * * Note: we always label the Param's type explicitly rather than relying on * transmitting a numeric type OID in PQexecParams(). This allows us to * avoid assuming that types have the same OIDs on the remote side as they * do locally --- they need only have the same names. */ static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context) { StringInfo buf = context->buf; char *ptypename = monetdb_remote_type_name(paramtype, paramtypmod); appendStringInfo(buf, "$%d::%s", paramindex, ptypename); } /* * Print the representation of a placeholder for a parameter that will be * sent to the remote side at execution time. * * This is used when we're just trying to EXPLAIN the remote query. * We don't have the actual value of the runtime parameter yet, and we don't * want the remote planner to generate a plan that depends on such a value * anyway. Thus, we can't do something simple like "$1::paramtype". * Instead, we emit "((SELECT null::paramtype)::paramtype)". * In all extant versions of Postgres, the planner will see that as an unknown * constant value, which is what we want. This might need adjustment if we * ever make the planner flatten scalar subqueries. Note: the reason for the * apparently useless outer cast is to ensure that the representation as a * whole will be parsed as an a_expr and not a select_with_parens; the latter * would do the wrong thing in the context "x = ANY(...)". */ static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod, deparse_expr_cxt *context) { StringInfo buf = context->buf; char *ptypename = monetdb_remote_type_name(paramtype, paramtypmod); appendStringInfo(buf, "((SELECT null::%s)::%s)", ptypename, ptypename); } /* * Deparse GROUP BY clause. */ static void appendGroupByClause(List *tlist, deparse_expr_cxt *context) { appendGroupByClauseForQuery(tlist, context->root->parse, context); } static void appendGroupByClauseForQuery(List *tlist, Query *query, deparse_expr_cxt *context) { StringInfo buf = context->buf; ListCell *lc; bool first = true; if (query == NULL || (!query->groupClause && !query->groupingSets)) return; appendStringInfoString(buf, " GROUP BY "); if (query->groupingSets) { appendStringInfoString(buf, "GROUPING SETS ("); foreach(lc, query->groupingSets) { List *grouping_set = lfirst(lc); if (!first) appendStringInfoString(buf, ", "); first = false; if (grouping_set == NIL) appendStringInfoString(buf, "()"); else appendGroupingSetContent(grouping_set, tlist, context, true); } appendStringInfoChar(buf, ')'); return; } foreach(lc, query->groupClause) { SortGroupClause *grp = (SortGroupClause *) lfirst(lc); if (!first) appendStringInfoString(buf, ", "); first = false; deparseSortGroupClause(grp->tleSortGroupRef, tlist, true, context); } } static void appendGroupingSetContent(List *content, List *tlist, deparse_expr_cxt *context, bool in_grouping_sets) { StringInfo buf = context->buf; ListCell *lc; bool first = true; if (content == NIL) return; if (content->type == T_IntList) { if (in_grouping_sets || list_length(content) != 1) appendStringInfoChar(buf, '('); foreach(lc, content) { if (!first) appendStringInfoString(buf, ", "); first = false; deparseSortGroupClause(lfirst_int(lc), tlist, false, context); } if (in_grouping_sets || list_length(content) != 1) appendStringInfoChar(buf, ')'); return; } elog(ERROR, "unsupported grouping set content type: %d", (int) content->type); } /* * Deparse ORDER BY clause defined by the given pathkeys. * * The clause should use Vars from context->scanrel if !has_final_sort, * or from context->foreignrel's targetlist if has_final_sort. * * We find a suitable pathkey expression (some earlier step * should have verified that there is one) and deparse it. */ static void appendOrderByClause(List *pathkeys, bool has_final_sort, List *targetList, deparse_expr_cxt *context) { ListCell *lcell; int nestlevel; StringInfo buf = context->buf; bool gotone = false; /* Make sure any constants in the exprs are printed portably */ nestlevel = set_transmission_modes(); foreach(lcell, pathkeys) { PathKey *pathkey = lfirst(lcell); EquivalenceMember *em; Expr *em_expr; Oid oprid; TargetEntry *matching_tle = NULL; ListCell *tlc; if (has_final_sort) { /* * By construction, context->foreignrel is the input relation to * the final sort. */ em = find_em_for_rel_target(context->root, pathkey->pk_eclass, context->foreignrel); /* * For ORDERED upper rels whose outerrel is a GROUP_AGG upper rel, * the ORDERED reltarget may not have the correct sortgroupref * annotations (the shippability check used the GROUP_AGG rel's * target instead). Retry against outerrel (GROUP_AGG) first, * which mirrors what add_foreign_ordered_paths checked, then fall * back to find_em_for_rel against the underlying scan rel (covers * plain-Var ORDER BY keys). */ if (em == NULL && IS_UPPER_REL(context->foreignrel)) { MonetdbFdwRelationInfo *ffp = (MonetdbFdwRelationInfo *) context->foreignrel->fdw_private; if (ffp != NULL && ffp->outerrel != NULL) em = find_em_for_rel_target(context->root, pathkey->pk_eclass, ffp->outerrel); if (em == NULL && context->scanrel != NULL && !IS_UPPER_REL(context->scanrel)) em = find_em_for_rel(context->root, pathkey->pk_eclass, context->scanrel); } } else em = find_em_for_rel(context->root, pathkey->pk_eclass, context->scanrel); /* * We don't expect any error here; it would mean that shippability * wasn't verified earlier. For the same reason, we don't recheck * shippability of the sort operator. */ if (em == NULL) elog(ERROR, "could not find pathkey item to sort"); em_expr = em->em_expr; /* * If the member is a Const expression then we needn't add it to the * ORDER BY clause. This can happen in UNION ALL queries where the * union child targetlist has a Const. Adding these would be * wasteful, but also, for INT columns, an integer literal would be * seen as an ordinal column position rather than a value to sort by. * deparseConst() does have code to handle this, but it seems less * effort on all accounts just to skip these for ORDER BY clauses. */ if (IsA(em_expr, Const)) continue; if (!gotone) { appendStringInfoString(buf, " ORDER BY "); gotone = true; } else appendStringInfoString(buf, ", "); /* * Lookup the operator corresponding to the strategy/compare type in the opclass. * The datatype used by the opfamily is not necessarily the same as * the expression type (for array types for example). */ #if PG_VERSION_NUM >= 180000 oprid = get_opfamily_member_for_cmptype(pathkey->pk_opfamily, em->em_datatype, em->em_datatype, pathkey->pk_cmptype); if (!OidIsValid(oprid)) elog(ERROR, "missing operator %d(%u,%u) in opfamily %u", pathkey->pk_cmptype, em->em_datatype, em->em_datatype, pathkey->pk_opfamily); #else oprid = get_opfamily_member(pathkey->pk_opfamily, em->em_datatype, em->em_datatype, pathkey->pk_strategy); if (!OidIsValid(oprid)) elog(ERROR, "missing operator %d(%u,%u) in opfamily %u", pathkey->pk_strategy, em->em_datatype, em->em_datatype, pathkey->pk_opfamily); #endif /* * MonetDB is happier when grouped upper queries sort by select-list * position instead of repeating the grouping expression. */ if (IS_UPPER_REL(context->foreignrel) && targetList != NIL) { foreach(tlc, targetList) { TargetEntry *tle = lfirst_node(TargetEntry, tlc); if (!tle->resjunk && equal(em_expr, tle->expr)) { matching_tle = tle; break; } } } if (matching_tle) appendStringInfo(buf, "%d", matching_tle->resno); else if (is_grouped_subquery_bridge(context->scanrel) && !has_final_sort && IsA(em_expr, Var)) { Var *sortvar = (Var *) em_expr; if (sortvar->varlevelsup == 0 && sortvar->varno == context->scanrel->relid && sortvar->varattno > 0) appendStringInfo(buf, "%d", sortvar->varattno); else deparseExpr(em_expr, context); } else deparseExpr(em_expr, context); /* * Here we need to use the expression's actual type to discover * whether the desired operator will be the default or not. */ appendOrderBySuffix(oprid, exprType((Node *) em_expr), pathkey->pk_nulls_first, context); } reset_transmission_modes(nestlevel); } /* * Deparse LIMIT/OFFSET clause. */ static void appendLimitClause(deparse_expr_cxt *context) { PlannerInfo *root = context->root; StringInfo buf = context->buf; int nestlevel; /* Make sure any constants in the exprs are printed portably */ nestlevel = set_transmission_modes(); if (root->parse->limitCount) { appendStringInfoString(buf, " LIMIT "); deparseExpr((Expr *) root->parse->limitCount, context); } if (root->parse->limitOffset) { appendStringInfoString(buf, " OFFSET "); deparseExpr((Expr *) root->parse->limitOffset, context); } reset_transmission_modes(nestlevel); } /* * appendFunctionName * Deparses function name from given function oid. */ static void appendFunctionName(Oid funcid, deparse_expr_cxt *context) { StringInfo buf = context->buf; HeapTuple proctup; Form_pg_proc procform; const char *proname; proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid)); if (!HeapTupleIsValid(proctup)) elog(ERROR, "cache lookup failed for function %u", funcid); procform = (Form_pg_proc) GETSTRUCT(proctup); /* Print schema name only if it's not pg_catalog */ if (procform->pronamespace != PG_CATALOG_NAMESPACE) { const char *schemaname; schemaname = get_namespace_name(procform->pronamespace); appendStringInfo(buf, "%s.", quote_identifier(schemaname)); } /* Always print the function name */ proname = NameStr(procform->proname); appendStringInfoString(buf, quote_identifier(proname)); ReleaseSysCache(proctup); } /* * Appends a sort or group clause. * * Like get_rule_sortgroupclause(), returns the expression tree, so caller * need not find it again. */ static Node * deparseSortGroupClause(Index ref, List *tlist, bool force_colno, deparse_expr_cxt *context) { StringInfo buf = context->buf; TargetEntry *tle; Expr *expr; tle = get_sortgroupref_tle(ref, tlist); expr = tle->expr; if (force_colno) { /* Use column-number form when requested by caller. */ Assert(!tle->resjunk); appendStringInfo(buf, "%d", tle->resno); } else if (expr && IsA(expr, Const)) { /* * Force a typecast here so that we don't emit something like "GROUP * BY 2", which will be misconstrued as a column position rather than * a constant. */ deparseConst((Const *) expr, context, 1); } else if (!expr || IsA(expr, Var)) deparseExpr(expr, context); else { /* Always parenthesize the expression. */ appendStringInfoChar(buf, '('); deparseExpr(expr, context); appendStringInfoChar(buf, ')'); } return (Node *) expr; } /* * Returns true if given Var is deparsed as a subquery output column, in * which case, *relno and *colno are set to the IDs for the relation and * column alias to the Var provided by the subquery. */ static bool is_subquery_var(Var *node, RelOptInfo *foreignrel, int *relno, int *colno) { MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; RelOptInfo *outerrel = fpinfo->outerrel; RelOptInfo *innerrel = fpinfo->innerrel; /* Should only be called in these cases. */ Assert(IS_SIMPLE_REL(foreignrel) || IS_JOIN_REL(foreignrel)); /* * If the given relation isn't a join relation, it doesn't have any lower * subqueries, so the Var isn't a subquery output column. */ if (!IS_JOIN_REL(foreignrel)) return false; /* * If the Var doesn't belong to any lower subqueries, it isn't a subquery * output column. */ if (!bms_is_member(node->varno, fpinfo->lower_subquery_rels)) return false; if (bms_is_member(node->varno, outerrel->relids)) { /* * If outer relation is deparsed as a subquery, the Var is an output * column of the subquery; get the IDs for the relation/column alias. */ if (fpinfo->make_outerrel_subquery) { get_relation_column_alias_ids(node, outerrel, relno, colno); return true; } /* Otherwise, recurse into the outer relation. */ return is_subquery_var(node, outerrel, relno, colno); } else { Assert(bms_is_member(node->varno, innerrel->relids)); /* * If inner relation is deparsed as a subquery, the Var is an output * column of the subquery; get the IDs for the relation/column alias. */ if (fpinfo->make_innerrel_subquery) { get_relation_column_alias_ids(node, innerrel, relno, colno); return true; } /* Otherwise, recurse into the inner relation. */ return is_subquery_var(node, innerrel, relno, colno); } } /* * Get the IDs for the relation and column alias to given Var belonging to * given relation, which are returned into *relno and *colno. */ static void get_relation_column_alias_ids(Var *node, RelOptInfo *foreignrel, int *relno, int *colno) { MonetdbFdwRelationInfo *fpinfo = (MonetdbFdwRelationInfo *) foreignrel->fdw_private; int i; ListCell *lc; /* Get the relation alias ID */ *relno = fpinfo->relation_index; /* * Grouped bridges emitted as subqueries expose columns in the original * subquery output order, so preserve the bridge output attno directly. */ if (is_grouped_subquery_bridge(foreignrel) && node->varattno > 0) { *colno = node->varattno; return; } /* Get the column alias ID */ i = 1; foreach(lc, foreignrel->reltarget->exprs) { Var *tlvar = (Var *) lfirst(lc); /* * Match reltarget entries only on varno/varattno. Ideally there * would be some cross-check on varnullingrels, but it's unclear what * to do exactly; we don't have enough context to know what that value * should be. */ if (IsA(tlvar, Var) && tlvar->varno == node->varno && tlvar->varattno == node->varattno) { *colno = i; return; } i++; } /* Shouldn't get here */ elog(ERROR, "unexpected expression in subquery output"); } /* ----------------------------------------------------------------------- * Whole-query pushdown helpers * * These functions generate MonetDB-compatible SQL directly from a Query* * tree (with CTEs intact), bypassing the planner-level RelOptInfo machinery. * Used when the entire query is fully-foreign and has reused CTEs that * cannot be safely inlined without triggering a MonetDB join bug. * ----------------------------------------------------------------------- */ /* * Build a minimal PlannerInfo that contains only the fields deparseExpr * needs: parse (for planner_rt_fetch) and simple_rte_array. */ static PlannerInfo * build_minimal_plannerinfo_for_query(Query *query) { PlannerInfo *root = makeNode(PlannerInfo); int n = list_length(query->rtable) + 1; int i = 1; ListCell *lc; root->parse = query; root->query_level = 1; root->simple_rte_array = (RangeTblEntry **) palloc0(n * sizeof(RangeTblEntry *)); root->simple_rel_array_size = n; foreach(lc, query->rtable) root->simple_rte_array[i++] = lfirst_node(RangeTblEntry, lc); return root; } /* * Build a fake RelOptInfo that covers all relations in the query. * deparseVar checks context->scanrel->relids to decide whether a Var * is remote; by including every varno we ensure all columns are emitted. * A zeroed MonetdbFdwRelationInfo as fdw_private makes * is_subquery_var() and is_grouped_subquery_bridge() return false. */ static RelOptInfo * build_allrels_reloptinfo(int n_rels) { RelOptInfo *rel = makeNode(RelOptInfo); MonetdbFdwRelationInfo *fpinfo; rel->reloptkind = RELOPT_JOINREL; rel->relids = bms_add_range(NULL, 1, n_rels); fpinfo = palloc0(sizeof(MonetdbFdwRelationInfo)); /* lower_subquery_rels = NULL → is_subquery_var returns false immediately */ /* stage = 0 ≠ UPPERREL_GROUP_AGG → is_grouped_subquery_bridge = false */ rel->fdw_private = fpinfo; return rel; } /* * PG19 GROUP BY resolution * * PostgreSQL 19 introduces RTE_GROUP: when a query has GROUP BY, an extra * RTE_GROUP entry is appended to the query's rtable. Non-aggregate columns * in the SELECT list (and ORDER BY / HAVING) are represented as Vars whose * varno points to this RTE_GROUP entry. The RTE_GROUP stores the actual * underlying expressions in rte->groupexprs. * * For MonetDB SQL generation we must deparse the UNDERLYING expressions * (e.g. r2.c_custkey) rather than the unresolvable r4.c_custkey alias. * resolve_group_vars_mutator does this substitution recursively. */ typedef struct ResolveGroupVarsCtx { PlannerInfo *root; } ResolveGroupVarsCtx; static Node * resolve_group_vars_mutator(Node *node, ResolveGroupVarsCtx *ctx) { if (node == NULL) return NULL; if (IsA(node, Var)) { Var *var = (Var *) node; #if PG_VERSION_NUM >= 180000 RangeTblEntry *rte; #endif if (var->varlevelsup == 0 && var->varno >= 1 && var->varno < ctx->root->simple_rel_array_size) { #if PG_VERSION_NUM >= 180000 rte = ctx->root->simple_rte_array[var->varno]; if (rte != NULL && rte->rtekind == RTE_GROUP && var->varattno >= 1 && var->varattno <= list_length(rte->groupexprs)) { /* * Substitute the Var with the underlying GROUP expression, * then recurse in case that expression also contains RTE_GROUP * Vars (nested grouping). */ return resolve_group_vars_mutator( (Node *) list_nth(rte->groupexprs, var->varattno - 1), ctx); } #endif } return (Node *) copyObject(node); } return expression_tree_mutator(node, resolve_group_vars_mutator, ctx); } /* * Resolve all RTE_GROUP Vars in an expression tree, returning a new tree. */ static Expr * resolve_group_vars(Expr *expr, PlannerInfo *root) { ResolveGroupVarsCtx ctx; ctx.root = root; return (Expr *) resolve_group_vars_mutator((Node *) expr, &ctx); } /* * Resolve RTE_GROUP Vars and then deparse. Used for all expressions in * deparse_query_body_for_monetdb and deparse_fromnode_for_query. */ static void deparse_resolved(Expr *expr, deparse_expr_cxt *context) { deparseExpr(resolve_group_vars(expr, context->root), context); } /* * Recursively deparse a join-tree node (RangeTblRef or JoinExpr) into buf. * RTE_RELATION entries are emitted as "monet_schema.table_name rN". * RTE_CTE entries are emitted as "cte_name rN". */ static void deparse_fromnode_for_query(StringInfo buf, Query *query, Node *node, deparse_expr_cxt *context, bool is_nested) { if (IsA(node, RangeTblRef)) { RangeTblRef *rtr = (RangeTblRef *) node; RangeTblEntry *rte; Assert(rtr->rtindex >= 1 && rtr->rtindex <= list_length(query->rtable)); rte = rt_fetch(rtr->rtindex, query->rtable); if (rte->rtekind == RTE_RELATION) { ForeignTable *ft = GetForeignTable(rte->relid); const char *schema = NULL; const char *tabname = NULL; ListCell *lc; foreach(lc, ft->options) { DefElem *def = (DefElem *) lfirst(lc); if (strcmp(def->defname, "schema_name") == 0) schema = defGetString(def); else if (strcmp(def->defname, "table_name") == 0) tabname = defGetString(def); } if (schema == NULL) schema = get_namespace_name(get_rel_namespace(rte->relid)); if (tabname == NULL) tabname = get_rel_name(rte->relid); appendStringInfo(buf, "%s.%s %s%d", quote_identifier(schema), quote_identifier(tabname), REL_ALIAS_PREFIX, rtr->rtindex); } else if (rte->rtekind == RTE_CTE) { appendStringInfo(buf, "%s %s%d", quote_identifier(rte->ctename), REL_ALIAS_PREFIX, rtr->rtindex); } else { elog(ERROR, "unsupported RTE kind %d in whole-query MonetDB pushdown", (int) rte->rtekind); } } else if (IsA(node, JoinExpr)) { JoinExpr *join = (JoinExpr *) node; const char *join_str; switch (join->jointype) { case JOIN_INNER: join_str = " JOIN "; break; case JOIN_LEFT: join_str = " LEFT JOIN "; break; case JOIN_FULL: join_str = " FULL JOIN "; break; case JOIN_RIGHT: join_str = " RIGHT JOIN "; break; default: join_str = " JOIN "; break; } if (is_nested) appendStringInfoChar(buf, '('); deparse_fromnode_for_query(buf, query, join->larg, context, true); appendStringInfoString(buf, join_str); deparse_fromnode_for_query(buf, query, join->rarg, context, true); if (join->quals) { appendStringInfoString(buf, " ON ("); deparse_resolved((Expr *) join->quals, context); appendStringInfoChar(buf, ')'); } if (is_nested) appendStringInfoChar(buf, ')'); } else { elog(ERROR, "unsupported join tree node type %d in whole-query MonetDB pushdown", (int) nodeTag(node)); } } /* * Deparse a single Query body (SELECT … FROM … WHERE … GROUP BY … HAVING * … ORDER BY … LIMIT …) into MonetDB SQL. Does NOT emit the leading WITH. */ static void deparse_query_body_for_monetdb(StringInfo buf, Query *query) { deparse_expr_cxt context = {0}; PlannerInfo *root; RelOptInfo *allrels; ListCell *lc; bool first; root = build_minimal_plannerinfo_for_query(query); allrels = build_allrels_reloptinfo(list_length(query->rtable)); context.root = root; context.foreignrel = allrels; context.scanrel = allrels; context.buf = buf; context.params_list = NULL; /* SELECT */ appendStringInfoString(buf, "SELECT "); first = true; foreach(lc, query->targetList) { TargetEntry *tle = lfirst_node(TargetEntry, lc); if (tle->resjunk) continue; if (!first) appendStringInfoString(buf, ", "); first = false; deparse_resolved(tle->expr, &context); if (tle->resname) appendStringInfo(buf, " AS %s", quote_identifier(tle->resname)); } /* FROM */ if (query->jointree != NULL && query->jointree->fromlist != NIL) { appendStringInfoString(buf, " FROM "); first = true; foreach(lc, query->jointree->fromlist) { Node *from_item = (Node *) lfirst(lc); if (!first) appendStringInfoString(buf, ", "); first = false; deparse_fromnode_for_query(buf, query, from_item, &context, false); } } /* WHERE */ if (query->jointree != NULL && query->jointree->quals != NULL) { appendStringInfoString(buf, " WHERE "); deparse_resolved((Expr *) query->jointree->quals, &context); } /* GROUP BY */ if (query->groupClause != NIL) { appendStringInfoString(buf, " GROUP BY "); first = true; foreach(lc, query->groupClause) { SortGroupClause *sgc = lfirst_node(SortGroupClause, lc); TargetEntry *tle = get_sortgroupclause_tle(sgc, query->targetList); if (!first) appendStringInfoString(buf, ", "); first = false; deparse_resolved(tle->expr, &context); } } /* HAVING */ if (query->havingQual != NULL) { appendStringInfoString(buf, " HAVING "); deparse_resolved((Expr *) query->havingQual, &context); } /* ORDER BY */ if (query->sortClause != NIL) { appendStringInfoString(buf, " ORDER BY "); first = true; foreach(lc, query->sortClause) { SortGroupClause *sgc = lfirst_node(SortGroupClause, lc); TargetEntry *tle = get_sortgroupclause_tle(sgc, query->targetList); bool reverse_sort = false; if (!first) appendStringInfoString(buf, ", "); first = false; deparse_resolved(tle->expr, &context); #if PG_VERSION_NUM >= 180000 reverse_sort = sgc->reverse_sort; #else if (OidIsValid(sgc->sortop)) { Oid sortcoltype; TypeCacheEntry *typentry; sortcoltype = exprType((Node *) tle->expr); typentry = lookup_type_cache(sortcoltype, TYPECACHE_LT_OPR | TYPECACHE_GT_OPR); reverse_sort = (sgc->sortop == typentry->gt_opr); } #endif if (reverse_sort) appendStringInfoString(buf, " DESC"); if (sgc->nulls_first) appendStringInfoString(buf, " NULLS FIRST"); } } /* LIMIT */ if (query->limitCount != NULL) { appendStringInfoString(buf, " LIMIT "); deparseExpr((Expr *) query->limitCount, &context); } /* OFFSET */ if (query->limitOffset != NULL) { appendStringInfoString(buf, " OFFSET "); deparseExpr((Expr *) query->limitOffset, &context); } } /* * deparseQueryForMonetDB * * Generate a complete MonetDB-compatible SQL string for a fully-foreign query * that includes CTEs (WITH clause). Each CTE body and the main query body * are recursively deparsed using the existing deparseExpr infrastructure. * * Returns a palloc'd SQL string. */ char * deparseQueryForMonetDB(Query *parse) { StringInfoData buf; ListCell *lc; bool first; initStringInfo(&buf); /* WITH clause */ if (parse->cteList != NIL) { appendStringInfoString(&buf, "WITH "); first = true; foreach(lc, parse->cteList) { CommonTableExpr *cte = lfirst_node(CommonTableExpr, lc); Query *cte_query = castNode(Query, cte->ctequery); if (!first) appendStringInfoString(&buf, ", "); first = false; appendStringInfo(&buf, "%s AS (", quote_identifier(cte->ctename)); deparse_query_body_for_monetdb(&buf, cte_query); appendStringInfoChar(&buf, ')'); } appendStringInfoChar(&buf, ' '); } /* Main query body */ deparse_query_body_for_monetdb(&buf, parse); return buf.data; }