2013-02-21 14:26:23 +04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* deparse.c
|
|
|
|
* Query deparser for postgres_fdw
|
|
|
|
*
|
|
|
|
* This file includes functions that examine query WHERE clauses to see
|
|
|
|
* whether they're safe to send to the remote server for execution, as
|
|
|
|
* well as functions to construct the query text to be sent. The latter
|
|
|
|
* functionality is annoyingly duplicative of ruleutils.c, but there are
|
|
|
|
* enough special considerations that it seems best to keep this separate.
|
|
|
|
* One saving grace is that we only need deparse logic for node types that
|
|
|
|
* we consider safe to send.
|
|
|
|
*
|
2013-02-22 15:03:46 +04:00
|
|
|
* We assume that the remote session's search_path is exactly "pg_catalog",
|
|
|
|
* and thus we need schema-qualify all and only names outside pg_catalog.
|
|
|
|
*
|
2013-03-14 03:46:31 +04:00
|
|
|
* We do not consider that it is ever safe to send COLLATE expressions to
|
|
|
|
* the remote server: it might not have the same collation names we do.
|
|
|
|
* (Later we might consider it safe to send COLLATE "C", but even that would
|
|
|
|
* fail on old remote servers.) An expression is considered safe to send only
|
|
|
|
* if all collations used in it are traceable to Var(s) of the foreign table.
|
|
|
|
* That implies that if the remote server gets a different answer than we do,
|
|
|
|
* the foreign table's columns are not marked with collations that match the
|
|
|
|
* remote table's columns, which we can consider to be user error.
|
|
|
|
*
|
2013-02-21 14:26:23 +04:00
|
|
|
* Portions Copyright (c) 2012-2013, PostgreSQL Global Development Group
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* contrib/postgres_fdw/deparse.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "postgres_fdw.h"
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
#include "access/heapam.h"
|
2013-02-21 14:26:23 +04:00
|
|
|
#include "access/htup_details.h"
|
|
|
|
#include "access/sysattr.h"
|
|
|
|
#include "access/transam.h"
|
2013-03-14 03:46:31 +04:00
|
|
|
#include "catalog/pg_collation.h"
|
2013-02-22 15:03:46 +04:00
|
|
|
#include "catalog/pg_namespace.h"
|
2013-02-21 14:26:23 +04:00
|
|
|
#include "catalog/pg_operator.h"
|
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "commands/defrem.h"
|
|
|
|
#include "nodes/nodeFuncs.h"
|
|
|
|
#include "optimizer/clauses.h"
|
|
|
|
#include "optimizer/var.h"
|
|
|
|
#include "parser/parsetree.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-03-14 03:46:31 +04:00
|
|
|
* Global context for foreign_expr_walker's search of an expression tree.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
2013-03-14 03:46:31 +04:00
|
|
|
typedef struct foreign_glob_cxt
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
PlannerInfo *root; /* global planner state */
|
|
|
|
RelOptInfo *foreignrel; /* the foreign relation we are planning for */
|
2013-03-14 03:46:31 +04:00
|
|
|
} 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 */
|
|
|
|
FDW_COLLATE_SAFE, /* collation derives from a foreign Var */
|
|
|
|
FDW_COLLATE_UNSAFE /* collation derives from something else */
|
|
|
|
} FDWCollateState;
|
|
|
|
|
|
|
|
typedef struct foreign_loc_cxt
|
|
|
|
{
|
|
|
|
Oid collation; /* OID of current collation, if any */
|
|
|
|
FDWCollateState state; /* state of current collation choice */
|
|
|
|
} foreign_loc_cxt;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
/*
|
|
|
|
* Context for deparseExpr
|
|
|
|
*/
|
|
|
|
typedef struct deparse_expr_cxt
|
|
|
|
{
|
|
|
|
PlannerInfo *root; /* global planner state */
|
|
|
|
RelOptInfo *foreignrel; /* the foreign relation we are planning for */
|
|
|
|
StringInfo buf; /* output buffer to append to */
|
|
|
|
List **params_list; /* exprs that will become remote Params */
|
|
|
|
} deparse_expr_cxt;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* Functions to determine whether an expression can be evaluated safely on
|
|
|
|
* remote server.
|
|
|
|
*/
|
2013-03-14 03:46:31 +04:00
|
|
|
static bool foreign_expr_walker(Node *node,
|
|
|
|
foreign_glob_cxt *glob_cxt,
|
|
|
|
foreign_loc_cxt *outer_cxt);
|
2013-02-21 14:26:23 +04:00
|
|
|
static bool is_builtin(Oid procid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions to construct string representation of a node tree.
|
|
|
|
*/
|
2013-03-10 22:14:53 +04:00
|
|
|
static void deparseTargetList(StringInfo buf,
|
|
|
|
PlannerInfo *root,
|
|
|
|
Index rtindex,
|
|
|
|
Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
Bitmapset *attrs_used,
|
|
|
|
List **retrieved_attrs);
|
2013-03-10 22:14:53 +04:00
|
|
|
static void deparseReturningList(StringInfo buf, PlannerInfo *root,
|
|
|
|
Index rtindex, Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
List *returningList,
|
|
|
|
List **retrieved_attrs);
|
2013-02-21 14:26:23 +04:00
|
|
|
static void deparseColumnRef(StringInfo buf, int varno, int varattno,
|
|
|
|
PlannerInfo *root);
|
2013-03-13 02:58:13 +04:00
|
|
|
static void deparseRelation(StringInfo buf, Relation rel);
|
2013-02-21 14:26:23 +04:00
|
|
|
static void deparseStringLiteral(StringInfo buf, const char *val);
|
2013-03-22 03:43:59 +04:00
|
|
|
static void deparseExpr(Expr *expr, deparse_expr_cxt *context);
|
|
|
|
static void deparseVar(Var *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseConst(Const *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseParam(Param *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseOpExpr(OpExpr *node, deparse_expr_cxt *context);
|
2013-02-22 15:03:46 +04:00
|
|
|
static void deparseOperatorName(StringInfo buf, Form_pg_operator opform);
|
2013-03-22 03:43:59 +04:00
|
|
|
static void deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context);
|
|
|
|
static void deparseScalarArrayOpExpr(ScalarArrayOpExpr *node,
|
|
|
|
deparse_expr_cxt *context);
|
|
|
|
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 deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Examine each restriction clause in baserel's baserestrictinfo list,
|
2013-03-22 03:43:59 +04:00
|
|
|
* 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
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
classifyConditions(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
|
|
|
List **remote_conds,
|
2013-03-22 03:43:59 +04:00
|
|
|
List **local_conds)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
*remote_conds = NIL;
|
|
|
|
*local_conds = NIL;
|
|
|
|
|
|
|
|
foreach(lc, baserel->baserestrictinfo)
|
|
|
|
{
|
|
|
|
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
|
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
if (is_foreign_expr(root, baserel, ri->clause))
|
|
|
|
*remote_conds = lappend(*remote_conds, ri);
|
2013-02-21 14:26:23 +04:00
|
|
|
else
|
|
|
|
*local_conds = lappend(*local_conds, ri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if given expr is safe to evaluate on the foreign server.
|
|
|
|
*/
|
2013-03-22 03:43:59 +04:00
|
|
|
bool
|
2013-02-21 14:26:23 +04:00
|
|
|
is_foreign_expr(PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
2013-03-22 03:43:59 +04:00
|
|
|
Expr *expr)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-14 03:46:31 +04:00
|
|
|
foreign_glob_cxt glob_cxt;
|
|
|
|
foreign_loc_cxt loc_cxt;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the expression consists of nodes that are safe to execute
|
|
|
|
* remotely.
|
|
|
|
*/
|
2013-03-14 03:46:31 +04:00
|
|
|
glob_cxt.root = root;
|
|
|
|
glob_cxt.foreignrel = baserel;
|
|
|
|
loc_cxt.collation = InvalidOid;
|
|
|
|
loc_cxt.state = FDW_COLLATE_NONE;
|
|
|
|
if (!foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt))
|
2013-02-21 14:26:23 +04:00
|
|
|
return false;
|
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
/* Expressions examined here should be boolean, ie noncollatable */
|
|
|
|
Assert(loc_cxt.collation == InvalidOid);
|
|
|
|
Assert(loc_cxt.state == FDW_COLLATE_NONE);
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* An expression which includes any mutable functions can't be sent over
|
|
|
|
* because its result is not stable. For example, sending now() remote
|
|
|
|
* side could cause confusion from clock offsets. Future versions might
|
|
|
|
* be able to make this choice with more granularity. (We check this last
|
|
|
|
* because it requires a lot of expensive catalog lookups.)
|
|
|
|
*/
|
|
|
|
if (contain_mutable_functions((Node *) expr))
|
|
|
|
return false;
|
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
/* OK to evaluate on the remote server */
|
2013-02-21 14:26:23 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-14 03:46:31 +04:00
|
|
|
* Check if expression is safe to execute remotely, and return true if so.
|
|
|
|
*
|
2013-03-22 03:43:59 +04:00
|
|
|
* In addition, *outer_cxt is updated with collation information.
|
2013-03-14 03:46:31 +04:00
|
|
|
*
|
|
|
|
* We must check that the expression contains only node types we can deparse,
|
|
|
|
* that all types/functions/operators are safe to send (which we approximate
|
|
|
|
* as being built-in), 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.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static bool
|
2013-03-14 03:46:31 +04:00
|
|
|
foreign_expr_walker(Node *node,
|
|
|
|
foreign_glob_cxt *glob_cxt,
|
|
|
|
foreign_loc_cxt *outer_cxt)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
|
|
|
bool check_type = true;
|
2013-03-14 03:46:31 +04:00
|
|
|
foreign_loc_cxt inner_cxt;
|
|
|
|
Oid collation;
|
|
|
|
FDWCollateState state;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
/* Need do nothing for empty subexpressions */
|
2013-02-21 14:26:23 +04:00
|
|
|
if (node == NULL)
|
2013-03-14 03:46:31 +04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Set up inner_cxt for possible recursion to child nodes */
|
|
|
|
inner_cxt.collation = InvalidOid;
|
|
|
|
inner_cxt.state = FDW_COLLATE_NONE;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
switch (nodeTag(node))
|
|
|
|
{
|
|
|
|
case T_Var:
|
|
|
|
{
|
2013-03-14 03:46:31 +04:00
|
|
|
Var *var = (Var *) node;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* 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.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
2013-03-22 03:43:59 +04:00
|
|
|
if (var->varno == glob_cxt->foreignrel->relid &&
|
|
|
|
var->varlevelsup == 0)
|
|
|
|
{
|
|
|
|
/* Var belongs to foreign table */
|
|
|
|
collation = var->varcollid;
|
|
|
|
state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Var belongs to some other table */
|
|
|
|
if (var->varcollid != InvalidOid &&
|
|
|
|
var->varcollid != DEFAULT_COLLATION_OID)
|
|
|
|
return false;
|
2013-03-14 03:46:31 +04:00
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
/* We can consider that it doesn't set collation */
|
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_Const:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
Const *c = (Const *) node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the constant has nondefault collation, either it's of a
|
|
|
|
* non-builtin type, or it reflects folding of a CollateExpr;
|
|
|
|
* either way, it's unsafe to send to the remote.
|
|
|
|
*/
|
|
|
|
if (c->constcollid != InvalidOid &&
|
|
|
|
c->constcollid != DEFAULT_COLLATION_OID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Otherwise, we can consider that it doesn't set collation */
|
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_Param:
|
|
|
|
{
|
|
|
|
Param *p = (Param *) node;
|
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
/*
|
|
|
|
* Collation handling is same as for Consts.
|
|
|
|
*/
|
|
|
|
if (p->paramcollid != InvalidOid &&
|
|
|
|
p->paramcollid != DEFAULT_COLLATION_OID)
|
|
|
|
return false;
|
2013-03-22 03:43:59 +04:00
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_ArrayRef:
|
|
|
|
{
|
|
|
|
ArrayRef *ar = (ArrayRef *) node;;
|
|
|
|
|
|
|
|
/* Assignment should not be in restrictions. */
|
|
|
|
if (ar->refassgnexpr != NULL)
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to remaining subexpressions. Since the array
|
|
|
|
* subscripts must yield (noncollatable) integers, they won't
|
|
|
|
* affect the inner_cxt state.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) ar->refupperindexpr,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
if (!foreign_expr_walker((Node *) ar->reflowerindexpr,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
if (!foreign_expr_walker((Node *) ar->refexpr,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Array subscripting should yield same collation as input,
|
|
|
|
* but for safety use same logic as for function nodes.
|
|
|
|
*/
|
|
|
|
collation = ar->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
|
|
|
|
state = FDW_COLLATE_UNSAFE;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_FuncExpr:
|
|
|
|
{
|
2013-03-14 03:46:31 +04:00
|
|
|
FuncExpr *fe = (FuncExpr *) node;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* If function used by the expression is not built-in, it
|
|
|
|
* can't be sent to remote because it might have incompatible
|
|
|
|
* semantics on remote side.
|
|
|
|
*/
|
|
|
|
if (!is_builtin(fe->funcid))
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) fe->args,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If function's input collation is not derived from a foreign
|
|
|
|
* Var, it can't be sent to remote.
|
|
|
|
*/
|
|
|
|
if (fe->inputcollid == InvalidOid)
|
|
|
|
/* OK, inputs are all noncollatable */ ;
|
|
|
|
else if (inner_cxt.state != FDW_COLLATE_SAFE ||
|
|
|
|
fe->inputcollid != inner_cxt.collation)
|
|
|
|
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
|
|
|
|
state = FDW_COLLATE_UNSAFE;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_OpExpr:
|
|
|
|
case T_DistinctExpr: /* struct-equivalent to OpExpr */
|
|
|
|
{
|
2013-03-14 03:46:31 +04:00
|
|
|
OpExpr *oe = (OpExpr *) node;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* Similarly, only built-in operators can be sent to remote.
|
|
|
|
* (If the operator is, surely its underlying function is
|
|
|
|
* too.)
|
|
|
|
*/
|
|
|
|
if (!is_builtin(oe->opno))
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) oe->args,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If operator's input collation is not derived from a foreign
|
|
|
|
* Var, it can't be sent to remote.
|
|
|
|
*/
|
|
|
|
if (oe->inputcollid == InvalidOid)
|
|
|
|
/* OK, inputs are all noncollatable */ ;
|
|
|
|
else if (inner_cxt.state != FDW_COLLATE_SAFE ||
|
|
|
|
oe->inputcollid != inner_cxt.collation)
|
|
|
|
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
|
|
|
|
state = FDW_COLLATE_UNSAFE;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_ScalarArrayOpExpr:
|
|
|
|
{
|
2013-03-14 03:46:31 +04:00
|
|
|
ScalarArrayOpExpr *oe = (ScalarArrayOpExpr *) node;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* Again, only built-in operators can be sent to remote.
|
|
|
|
*/
|
|
|
|
if (!is_builtin(oe->opno))
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) oe->args,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If operator's input collation is not derived from a foreign
|
|
|
|
* Var, it can't be sent to remote.
|
|
|
|
*/
|
|
|
|
if (oe->inputcollid == InvalidOid)
|
|
|
|
/* OK, inputs are all noncollatable */ ;
|
|
|
|
else if (inner_cxt.state != FDW_COLLATE_SAFE ||
|
|
|
|
oe->inputcollid != inner_cxt.collation)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Output is always boolean and so noncollatable. */
|
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_RelabelType:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
RelabelType *r = (RelabelType *) node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpression.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) r->arg,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelabelType must not introduce a collation not derived from
|
|
|
|
* an input foreign Var.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
state = FDW_COLLATE_UNSAFE;
|
|
|
|
}
|
|
|
|
break;
|
2013-02-21 14:26:23 +04:00
|
|
|
case T_BoolExpr:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
BoolExpr *b = (BoolExpr *) node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) b->args,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Output is always boolean and so noncollatable. */
|
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
|
|
|
}
|
|
|
|
break;
|
2013-02-21 14:26:23 +04:00
|
|
|
case T_NullTest:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
NullTest *nt = (NullTest *) node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) nt->arg,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Output is always boolean and so noncollatable. */
|
|
|
|
collation = InvalidOid;
|
|
|
|
state = FDW_COLLATE_NONE;
|
|
|
|
}
|
|
|
|
break;
|
2013-02-21 14:26:23 +04:00
|
|
|
case T_ArrayExpr:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
ArrayExpr *a = (ArrayExpr *) node;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recurse to input subexpressions.
|
|
|
|
*/
|
|
|
|
if (!foreign_expr_walker((Node *) a->elements,
|
|
|
|
glob_cxt, &inner_cxt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ArrayExpr must not introduce a collation not derived from
|
|
|
|
* an input foreign Var.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
state = FDW_COLLATE_UNSAFE;
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_List:
|
2013-03-14 03:46:31 +04:00
|
|
|
{
|
|
|
|
List *l = (List *) node;
|
|
|
|
ListCell *lc;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
/*
|
|
|
|
* Recurse to component subexpressions.
|
|
|
|
*/
|
|
|
|
foreach(lc, l)
|
|
|
|
{
|
|
|
|
if (!foreign_expr_walker((Node *) lfirst(lc),
|
|
|
|
glob_cxt, &inner_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;
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
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.
|
|
|
|
*/
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If result type of given expression is not built-in, it can't be sent to
|
|
|
|
* remote because it might have incompatible semantics on remote side.
|
|
|
|
*/
|
|
|
|
if (check_type && !is_builtin(exprType(node)))
|
2013-03-14 03:46:31 +04:00
|
|
|
return false;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-14 03:46:31 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if given object is one of PostgreSQL's built-in objects.
|
|
|
|
*
|
2013-02-22 15:03:46 +04:00
|
|
|
* We use FirstBootstrapObjectId as the cutoff, so that we only consider
|
|
|
|
* objects with hand-assigned OIDs to be "built in", not for instance any
|
|
|
|
* function or type defined in the information_schema.
|
|
|
|
*
|
|
|
|
* Our constraints for dealing with types are tighter than they are for
|
2013-02-22 15:36:09 +04:00
|
|
|
* functions or operators: we want to accept only types that are in pg_catalog,
|
|
|
|
* else format_type might incorrectly fail to schema-qualify their names.
|
|
|
|
* (This could be fixed with some changes to format_type, but for now there's
|
|
|
|
* no need.) Thus we must exclude information_schema types.
|
2013-02-22 15:03:46 +04:00
|
|
|
*
|
2013-02-21 14:26:23 +04:00
|
|
|
* XXX there is a problem with this, which is that the set of built-in
|
|
|
|
* objects expands over time. Something that is built-in to us might not
|
|
|
|
* be known to the remote server, if it's of an older version. But keeping
|
|
|
|
* track of that would be a huge exercise.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_builtin(Oid oid)
|
|
|
|
{
|
2013-02-22 15:03:46 +04:00
|
|
|
return (oid < FirstBootstrapObjectId);
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-03-10 22:14:53 +04:00
|
|
|
* Construct a simple SELECT statement that retrieves desired columns
|
2013-02-21 14:26:23 +04:00
|
|
|
* of the specified foreign table, and append it to "buf". The output
|
|
|
|
* contains just "SELECT ... FROM tablename".
|
2013-03-22 08:31:11 +04:00
|
|
|
*
|
|
|
|
* We also create an integer List of the columns being retrieved, which is
|
|
|
|
* returned to *retrieved_attrs.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2013-03-10 22:14:53 +04:00
|
|
|
deparseSelectSql(StringInfo buf,
|
2013-02-21 14:26:23 +04:00
|
|
|
PlannerInfo *root,
|
|
|
|
RelOptInfo *baserel,
|
2013-03-22 08:31:11 +04:00
|
|
|
Bitmapset *attrs_used,
|
|
|
|
List **retrieved_attrs)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-10 22:14:53 +04:00
|
|
|
RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
|
|
|
|
Relation rel;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
/*
|
|
|
|
* Core code already has some lock on each rel being planned, so we can
|
|
|
|
* use NoLock here.
|
|
|
|
*/
|
|
|
|
rel = heap_open(rte->relid, NoLock);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
/*
|
|
|
|
* Construct SELECT list
|
|
|
|
*/
|
|
|
|
appendStringInfoString(buf, "SELECT ");
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseTargetList(buf, root, baserel->relid, rel, attrs_used,
|
|
|
|
retrieved_attrs);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
/*
|
|
|
|
* Construct FROM clause
|
|
|
|
*/
|
|
|
|
appendStringInfoString(buf, " FROM ");
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(buf, rel);
|
2013-03-10 22:14:53 +04:00
|
|
|
|
|
|
|
heap_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Emit a target list that retrieves the columns specified in attrs_used.
|
|
|
|
* This is used for both SELECT and RETURNING targetlists.
|
|
|
|
*
|
2013-03-22 08:31:11 +04:00
|
|
|
* 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.
|
2013-03-10 22:14:53 +04:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deparseTargetList(StringInfo buf,
|
|
|
|
PlannerInfo *root,
|
|
|
|
Index rtindex,
|
|
|
|
Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
Bitmapset *attrs_used,
|
|
|
|
List **retrieved_attrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
|
|
|
TupleDesc tupdesc = RelationGetDescr(rel);
|
|
|
|
bool have_wholerow;
|
|
|
|
bool first;
|
|
|
|
int i;
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-03-22 08:31:11 +04:00
|
|
|
*retrieved_attrs = NIL;
|
|
|
|
|
2013-02-22 18:21:50 +04:00
|
|
|
/* If there's a whole-row reference, we'll need all the columns. */
|
|
|
|
have_wholerow = bms_is_member(0 - FirstLowInvalidHeapAttributeNumber,
|
|
|
|
attrs_used);
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
first = true;
|
2013-03-10 22:14:53 +04:00
|
|
|
for (i = 1; i <= tupdesc->natts; i++)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-10 22:14:53 +04:00
|
|
|
Form_pg_attribute attr = tupdesc->attrs[i - 1];
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/* Ignore dropped attributes. */
|
2013-03-10 22:14:53 +04:00
|
|
|
if (attr->attisdropped)
|
2013-02-21 14:26:23 +04:00
|
|
|
continue;
|
|
|
|
|
2013-02-22 18:21:50 +04:00
|
|
|
if (have_wholerow ||
|
2013-03-10 22:14:53 +04:00
|
|
|
bms_is_member(i - FirstLowInvalidHeapAttributeNumber,
|
2013-02-21 14:26:23 +04:00
|
|
|
attrs_used))
|
2013-03-22 08:31:11 +04:00
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
deparseColumnRef(buf, rtindex, i, root);
|
2013-03-22 08:31:11 +04:00
|
|
|
|
|
|
|
*retrieved_attrs = lappend_int(*retrieved_attrs, i);
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-10 22:14:53 +04:00
|
|
|
* Add ctid if needed. We currently don't support retrieving any other
|
|
|
|
* system columns.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
2013-03-10 22:14:53 +04:00
|
|
|
if (bms_is_member(SelfItemPointerAttributeNumber - FirstLowInvalidHeapAttributeNumber,
|
|
|
|
attrs_used))
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
appendStringInfoString(buf, "ctid");
|
2013-03-22 08:31:11 +04:00
|
|
|
|
|
|
|
*retrieved_attrs = lappend_int(*retrieved_attrs,
|
|
|
|
SelfItemPointerAttributeNumber);
|
2013-03-10 22:14:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't generate bad syntax if no undropped columns */
|
|
|
|
if (first)
|
|
|
|
appendStringInfoString(buf, "NULL");
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse WHERE clauses in given list of RestrictInfos and append them to buf.
|
|
|
|
*
|
2013-03-22 03:43:59 +04:00
|
|
|
* baserel is the foreign table we're planning for.
|
|
|
|
*
|
2013-02-21 14:26:23 +04:00
|
|
|
* If no WHERE clause already exists in the buffer, is_first should be true.
|
2013-03-22 03:43:59 +04:00
|
|
|
*
|
|
|
|
* If params 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 is NULL, we're generating the query for EXPLAIN purposes,
|
|
|
|
* so Params and other-relation Vars should be replaced by dummy values.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
appendWhereClause(StringInfo buf,
|
2013-03-10 22:14:53 +04:00
|
|
|
PlannerInfo *root,
|
2013-03-22 03:43:59 +04:00
|
|
|
RelOptInfo *baserel,
|
2013-02-21 14:26:23 +04:00
|
|
|
List *exprs,
|
2013-03-22 03:43:59 +04:00
|
|
|
bool is_first,
|
|
|
|
List **params)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
deparse_expr_cxt context;
|
2013-03-12 05:31:28 +04:00
|
|
|
int nestlevel;
|
2013-02-21 14:26:23 +04:00
|
|
|
ListCell *lc;
|
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
if (params)
|
|
|
|
*params = NIL; /* initialize result list to empty */
|
|
|
|
|
|
|
|
/* Set up context struct for recursion */
|
|
|
|
context.root = root;
|
|
|
|
context.foreignrel = baserel;
|
|
|
|
context.buf = buf;
|
|
|
|
context.params_list = params;
|
|
|
|
|
2013-03-12 05:31:28 +04:00
|
|
|
/* Make sure any constants in the exprs are printed portably */
|
|
|
|
nestlevel = set_transmission_modes();
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
foreach(lc, exprs)
|
|
|
|
{
|
|
|
|
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
|
|
|
|
|
|
|
|
/* Connect expressions with "AND" and parenthesize each condition. */
|
|
|
|
if (is_first)
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " WHERE ");
|
2013-02-21 14:26:23 +04:00
|
|
|
else
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " AND ");
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
appendStringInfoChar(buf, '(');
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(ri->clause, &context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
|
|
|
|
is_first = false;
|
|
|
|
}
|
2013-03-12 05:31:28 +04:00
|
|
|
|
|
|
|
reset_transmission_modes(nestlevel);
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
/*
|
|
|
|
* deparse remote INSERT statement
|
2013-03-22 08:31:11 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2013-03-10 22:14:53 +04:00
|
|
|
*/
|
|
|
|
void
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseInsertSql(StringInfo buf, PlannerInfo *root,
|
|
|
|
Index rtindex, Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
List *targetAttrs, List *returningList,
|
|
|
|
List **retrieved_attrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
|
|
|
AttrNumber pindex;
|
|
|
|
bool first;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
appendStringInfoString(buf, "INSERT INTO ");
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(buf, rel);
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
if (targetAttrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
2013-03-11 22:26:05 +04:00
|
|
|
appendStringInfoString(buf, "(");
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
first = true;
|
|
|
|
foreach(lc, targetAttrs)
|
|
|
|
{
|
|
|
|
int attnum = lfirst_int(lc);
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
deparseColumnRef(buf, rtindex, attnum, root);
|
|
|
|
}
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
appendStringInfoString(buf, ") VALUES (");
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
pindex = 1;
|
|
|
|
first = true;
|
|
|
|
foreach(lc, targetAttrs)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
appendStringInfo(buf, "$%d", pindex);
|
|
|
|
pindex++;
|
|
|
|
}
|
2013-03-10 22:14:53 +04:00
|
|
|
|
2013-03-11 22:26:05 +04:00
|
|
|
appendStringInfoString(buf, ")");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
appendStringInfoString(buf, " DEFAULT VALUES");
|
2013-03-10 22:14:53 +04:00
|
|
|
|
|
|
|
if (returningList)
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseReturningList(buf, root, rtindex, rel, returningList,
|
|
|
|
retrieved_attrs);
|
|
|
|
else
|
|
|
|
*retrieved_attrs = NIL;
|
2013-03-10 22:14:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deparse remote UPDATE statement
|
2013-03-22 08:31:11 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2013-03-10 22:14:53 +04:00
|
|
|
*/
|
|
|
|
void
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseUpdateSql(StringInfo buf, PlannerInfo *root,
|
|
|
|
Index rtindex, Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
List *targetAttrs, List *returningList,
|
|
|
|
List **retrieved_attrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
|
|
|
AttrNumber pindex;
|
|
|
|
bool first;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
appendStringInfoString(buf, "UPDATE ");
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(buf, rel);
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " SET ");
|
|
|
|
|
|
|
|
pindex = 2; /* ctid is always the first param */
|
|
|
|
first = true;
|
|
|
|
foreach(lc, targetAttrs)
|
|
|
|
{
|
|
|
|
int attnum = lfirst_int(lc);
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
deparseColumnRef(buf, rtindex, attnum, root);
|
|
|
|
appendStringInfo(buf, " = $%d", pindex);
|
|
|
|
pindex++;
|
|
|
|
}
|
|
|
|
appendStringInfoString(buf, " WHERE ctid = $1");
|
|
|
|
|
|
|
|
if (returningList)
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseReturningList(buf, root, rtindex, rel, returningList,
|
|
|
|
retrieved_attrs);
|
|
|
|
else
|
|
|
|
*retrieved_attrs = NIL;
|
2013-03-10 22:14:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deparse remote DELETE statement
|
2013-03-22 08:31:11 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2013-03-10 22:14:53 +04:00
|
|
|
*/
|
|
|
|
void
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseDeleteSql(StringInfo buf, PlannerInfo *root,
|
|
|
|
Index rtindex, Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
List *returningList,
|
|
|
|
List **retrieved_attrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
|
|
|
appendStringInfoString(buf, "DELETE FROM ");
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(buf, rel);
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " WHERE ctid = $1");
|
|
|
|
|
|
|
|
if (returningList)
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseReturningList(buf, root, rtindex, rel, returningList,
|
|
|
|
retrieved_attrs);
|
|
|
|
else
|
|
|
|
*retrieved_attrs = NIL;
|
2013-03-10 22:14:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* deparse RETURNING clause of INSERT/UPDATE/DELETE
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deparseReturningList(StringInfo buf, PlannerInfo *root,
|
|
|
|
Index rtindex, Relation rel,
|
2013-03-22 08:31:11 +04:00
|
|
|
List *returningList,
|
|
|
|
List **retrieved_attrs)
|
2013-03-10 22:14:53 +04:00
|
|
|
{
|
|
|
|
Bitmapset *attrs_used;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need the attrs mentioned in the query's RETURNING list.
|
|
|
|
*/
|
|
|
|
attrs_used = NULL;
|
|
|
|
pull_varattnos((Node *) returningList, rtindex,
|
|
|
|
&attrs_used);
|
|
|
|
|
|
|
|
appendStringInfoString(buf, " RETURNING ");
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseTargetList(buf, root, rtindex, rel, attrs_used,
|
|
|
|
retrieved_attrs);
|
2013-03-10 22:14:53 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 19:56:06 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(&relname, rel);
|
2013-02-22 19:56:06 +04:00
|
|
|
|
|
|
|
appendStringInfo(buf, "SELECT pg_catalog.pg_relation_size(");
|
|
|
|
deparseStringLiteral(buf, relname.data);
|
|
|
|
appendStringInfo(buf, "::pg_catalog.regclass) / %d", BLCKSZ);
|
|
|
|
}
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* Construct SELECT statement to acquire sample rows of given relation.
|
|
|
|
*
|
2013-03-22 08:31:11 +04:00
|
|
|
* SELECT command is appended to buf, and list of columns retrieved
|
|
|
|
* is returned to *retrieved_attrs.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
void
|
2013-03-22 08:31:11 +04:00
|
|
|
deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
|
|
|
Oid relid = RelationGetRelid(rel);
|
|
|
|
TupleDesc tupdesc = RelationGetDescr(rel);
|
|
|
|
int i;
|
|
|
|
char *colname;
|
|
|
|
List *options;
|
|
|
|
ListCell *lc;
|
|
|
|
bool first = true;
|
|
|
|
|
2013-03-22 08:31:11 +04:00
|
|
|
*retrieved_attrs = NIL;
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, "SELECT ");
|
2013-02-21 14:26:23 +04:00
|
|
|
for (i = 0; i < tupdesc->natts; i++)
|
|
|
|
{
|
|
|
|
/* Ignore dropped columns. */
|
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
first = false;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/* Use attribute name or column_name option. */
|
|
|
|
colname = NameStr(tupdesc->attrs[i]->attname);
|
|
|
|
options = GetForeignColumnOptions(relid, i + 1);
|
|
|
|
|
|
|
|
foreach(lc, options)
|
|
|
|
{
|
|
|
|
DefElem *def = (DefElem *) lfirst(lc);
|
|
|
|
|
|
|
|
if (strcmp(def->defname, "column_name") == 0)
|
|
|
|
{
|
|
|
|
colname = defGetString(def);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfoString(buf, quote_identifier(colname));
|
2013-03-22 08:31:11 +04:00
|
|
|
|
|
|
|
*retrieved_attrs = lappend_int(*retrieved_attrs, i + 1);
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't generate bad syntax for zero-column relation. */
|
|
|
|
if (first)
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, "NULL");
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct FROM clause
|
|
|
|
*/
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " FROM ");
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(buf, rel);
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deparseColumnRef(StringInfo buf, int varno, int varattno, PlannerInfo *root)
|
|
|
|
{
|
|
|
|
RangeTblEntry *rte;
|
|
|
|
char *colname = NULL;
|
|
|
|
List *options;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* varno must not be any of OUTER_VAR, INNER_VAR and INDEX_VAR. */
|
2013-03-10 22:14:53 +04:00
|
|
|
Assert(!IS_SPECIAL_VARNO(varno));
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/* Get RangeTblEntry from array in PlannerInfo. */
|
2013-03-10 22:14:53 +04:00
|
|
|
rte = planner_rt_fetch(varno, root);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's a column of a foreign table, and it has the column_name FDW
|
|
|
|
* option, use that value.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
colname = get_relid_attribute_name(rte->relid, varattno);
|
|
|
|
|
|
|
|
appendStringInfoString(buf, quote_identifier(colname));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2013-03-13 02:58:13 +04:00
|
|
|
deparseRelation(StringInfo buf, Relation rel)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
|
|
|
ForeignTable *table;
|
|
|
|
const char *nspname = NULL;
|
|
|
|
const char *relname = NULL;
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
/* obtain additional catalog information. */
|
2013-03-13 02:58:13 +04:00
|
|
|
table = GetForeignTable(RelationGetRelid(rel));
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-02-22 15:03:46 +04:00
|
|
|
/*
|
2013-03-10 22:14:53 +04:00
|
|
|
* Note: we could skip printing the schema name if it's pg_catalog, but
|
|
|
|
* that doesn't seem worth the trouble.
|
2013-02-22 15:03:46 +04:00
|
|
|
*/
|
2013-02-21 14:26:23 +04:00
|
|
|
if (nspname == NULL)
|
2013-03-13 02:58:13 +04:00
|
|
|
nspname = get_namespace_name(RelationGetNamespace(rel));
|
2013-02-21 14:26:23 +04:00
|
|
|
if (relname == NULL)
|
2013-03-13 02:58:13 +04:00
|
|
|
relname = RelationGetRelationName(rel);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
appendStringInfo(buf, "%s.%s",
|
|
|
|
quote_identifier(nspname), quote_identifier(relname));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append a SQL string literal representing "val" to buf.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deparseStringLiteral(StringInfo buf, const char *val)
|
|
|
|
{
|
|
|
|
const char *valptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rather than making assumptions about the remote server's value of
|
|
|
|
* standard_conforming_strings, always use E'foo' syntax if there are any
|
|
|
|
* backslashes. This will fail on remote servers before 8.1, but those
|
|
|
|
* are long out of support.
|
|
|
|
*/
|
|
|
|
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, '\'');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse given expression into context->buf.
|
2013-02-21 14:26:23 +04:00
|
|
|
*
|
|
|
|
* 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
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(Expr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
|
|
|
if (node == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (nodeTag(node))
|
|
|
|
{
|
|
|
|
case T_Var:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseVar((Var *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_Const:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseConst((Const *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_Param:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseParam((Param *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_ArrayRef:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseArrayRef((ArrayRef *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_FuncExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseFuncExpr((FuncExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_OpExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseOpExpr((OpExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_DistinctExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseDistinctExpr((DistinctExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_ScalarArrayOpExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseScalarArrayOpExpr((ScalarArrayOpExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_RelabelType:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseRelabelType((RelabelType *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_BoolExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseBoolExpr((BoolExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_NullTest:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseNullTest((NullTest *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
case T_ArrayExpr:
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseArrayExpr((ArrayExpr *) node, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unsupported expression type for deparse: %d",
|
|
|
|
(int) nodeTag(node));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* 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.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseVar(Var *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
|
|
|
|
|
|
|
if (node->varno == context->foreignrel->relid &&
|
|
|
|
node->varlevelsup == 0)
|
|
|
|
{
|
|
|
|
/* Var belongs to foreign table */
|
|
|
|
deparseColumnRef(buf, node->varno, node->varattno, context->root);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfo(buf, "$%d", pindex);
|
|
|
|
appendStringInfo(buf, "::%s",
|
|
|
|
format_type_with_typemod(node->vartype,
|
|
|
|
node->vartypmod));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appendStringInfo(buf, "(SELECT null::%s)",
|
|
|
|
format_type_with_typemod(node->vartype,
|
|
|
|
node->vartypmod));
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse given constant value into context->buf.
|
2013-02-21 14:26:23 +04:00
|
|
|
*
|
|
|
|
* This function has to be kept in sync with ruleutils.c's get_const_expr.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseConst(Const *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
Oid typoutput;
|
|
|
|
bool typIsVarlena;
|
|
|
|
char *extval;
|
|
|
|
bool isfloat = false;
|
|
|
|
bool needlabel;
|
|
|
|
|
|
|
|
if (node->constisnull)
|
|
|
|
{
|
|
|
|
appendStringInfo(buf, "NULL");
|
|
|
|
appendStringInfo(buf, "::%s",
|
|
|
|
format_type_with_typemod(node->consttype,
|
|
|
|
node->consttypmod));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTypeOutputInfo(node->consttype,
|
|
|
|
&typoutput, &typIsVarlena);
|
|
|
|
extval = OidOutputFunctionCall(typoutput, node->constvalue);
|
|
|
|
|
|
|
|
switch (node->consttype)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (strcspn(extval, "eE.") != strlen(extval))
|
|
|
|
isfloat = true; /* it looks like a float */
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append ::typename unless the constant will be implicitly typed as the
|
|
|
|
* right type when it is read in.
|
|
|
|
*
|
|
|
|
* XXX this code has to be kept in sync with the behavior of the parser,
|
|
|
|
* especially make_const.
|
|
|
|
*/
|
|
|
|
switch (node->consttype)
|
|
|
|
{
|
|
|
|
case BOOLOID:
|
|
|
|
case INT4OID:
|
|
|
|
case UNKNOWNOID:
|
|
|
|
needlabel = false;
|
|
|
|
break;
|
|
|
|
case NUMERICOID:
|
|
|
|
needlabel = !isfloat || (node->consttypmod >= 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
needlabel = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (needlabel)
|
|
|
|
appendStringInfo(buf, "::%s",
|
|
|
|
format_type_with_typemod(node->consttype,
|
|
|
|
node->consttypmod));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse given Param node.
|
2013-02-21 14:26:23 +04:00
|
|
|
*
|
2013-03-22 03:43:59 +04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* If we're just generating the query for EXPLAIN, replace the Param with
|
|
|
|
* a dummy expression "(SELECT null::<type>)". In all extant versions of
|
|
|
|
* Postgres, the planner will see that as an unknown constant value, which is
|
|
|
|
* what we want. (If we sent a Param, recent versions might try to use the
|
|
|
|
* value supplied for the Param as an estimated or even constant value, which
|
|
|
|
* we don't want.) This might need adjustment if we ever make the planner
|
|
|
|
* flatten scalar subqueries.
|
2013-02-22 15:36:09 +04:00
|
|
|
*
|
|
|
|
* Note: we 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.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseParam(Param *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfo(buf, "$%d", pindex);
|
|
|
|
appendStringInfo(buf, "::%s",
|
|
|
|
format_type_with_typemod(node->paramtype,
|
|
|
|
node->paramtypmod));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appendStringInfo(buf, "(SELECT null::%s)",
|
|
|
|
format_type_with_typemod(node->paramtype,
|
|
|
|
node->paramtypmod));
|
|
|
|
}
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse an array subscript expression.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseArrayRef(ArrayRef *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
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))
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(node->refexpr, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
appendStringInfoChar(buf, '(');
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(node->refexpr, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deparse subscript expressions. */
|
|
|
|
lowlist_item = list_head(node->reflowerindexpr); /* could be NULL */
|
|
|
|
foreach(uplist_item, node->refupperindexpr)
|
|
|
|
{
|
|
|
|
appendStringInfoChar(buf, '[');
|
|
|
|
if (lowlist_item)
|
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lfirst(lowlist_item), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ':');
|
|
|
|
lowlist_item = lnext(lowlist_item);
|
|
|
|
}
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lfirst(uplist_item), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse a function call.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseFuncExpr(FuncExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
HeapTuple proctup;
|
|
|
|
Form_pg_proc procform;
|
|
|
|
const char *proname;
|
|
|
|
bool use_variadic;
|
|
|
|
bool first;
|
|
|
|
ListCell *arg;
|
|
|
|
|
2013-02-22 16:30:21 +04:00
|
|
|
/*
|
|
|
|
* If the function call came from an implicit coercion, then just show the
|
|
|
|
* first argument.
|
|
|
|
*/
|
|
|
|
if (node->funcformat == COERCE_IMPLICIT_CAST)
|
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr((Expr *) linitial(node->args), context);
|
2013-02-22 16:30:21 +04:00
|
|
|
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);
|
|
|
|
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr((Expr *) linitial(node->args), context);
|
2013-02-22 16:30:21 +04:00
|
|
|
appendStringInfo(buf, "::%s",
|
|
|
|
format_type_with_typemod(rettype, coercedTypmod));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal function: display as proname(args).
|
|
|
|
*/
|
2013-02-21 14:26:23 +04:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* Check if need to print VARIADIC (cf. ruleutils.c) */
|
|
|
|
if (OidIsValid(procform->provariadic))
|
|
|
|
{
|
|
|
|
if (procform->provariadic != ANYOID)
|
|
|
|
use_variadic = true;
|
|
|
|
else
|
|
|
|
use_variadic = node->funcvariadic;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
use_variadic = false;
|
|
|
|
|
2013-02-22 15:03:46 +04:00
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/* Deparse the function name ... */
|
2013-02-22 15:03:46 +04:00
|
|
|
proname = NameStr(procform->proname);
|
|
|
|
appendStringInfo(buf, "%s(", quote_identifier(proname));
|
2013-02-21 14:26:23 +04:00
|
|
|
/* ... and all the arguments */
|
|
|
|
first = true;
|
|
|
|
foreach(arg, node->args)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
appendStringInfoString(buf, ", ");
|
|
|
|
if (use_variadic && lnext(arg) == NULL)
|
|
|
|
appendStringInfoString(buf, "VARIADIC ");
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr((Expr *) lfirst(arg), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
|
|
|
|
ReleaseSysCache(proctup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse given operator expression. To avoid problems around
|
2013-02-22 15:03:46 +04:00
|
|
|
* priority of operations, we always parenthesize the arguments.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseOpExpr(OpExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
HeapTuple tuple;
|
|
|
|
Form_pg_operator form;
|
|
|
|
char oprkind;
|
|
|
|
ListCell *arg;
|
|
|
|
|
|
|
|
/* 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 == 'r' && list_length(node->args) == 1) ||
|
|
|
|
(oprkind == 'l' && list_length(node->args) == 1) ||
|
|
|
|
(oprkind == 'b' && list_length(node->args) == 2));
|
|
|
|
|
|
|
|
/* Always parenthesize the expression. */
|
|
|
|
appendStringInfoChar(buf, '(');
|
|
|
|
|
|
|
|
/* Deparse left operand. */
|
|
|
|
if (oprkind == 'r' || oprkind == 'b')
|
|
|
|
{
|
|
|
|
arg = list_head(node->args);
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lfirst(arg), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ' ');
|
|
|
|
}
|
|
|
|
|
2013-02-22 15:03:46 +04:00
|
|
|
/* Deparse operator name. */
|
|
|
|
deparseOperatorName(buf, form);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/* Deparse right operand. */
|
|
|
|
if (oprkind == 'l' || oprkind == 'b')
|
|
|
|
{
|
|
|
|
arg = list_tail(node->args);
|
|
|
|
appendStringInfoChar(buf, ' ');
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lfirst(arg), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
}
|
|
|
|
|
2013-02-22 15:03:46 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
appendStringInfo(buf, "%s", opname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
/*
|
|
|
|
* Deparse IS DISTINCT FROM.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseDistinctExpr(DistinctExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
Assert(list_length(node->args) == 2);
|
|
|
|
|
|
|
|
appendStringInfoChar(buf, '(');
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(linitial(node->args), context);
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " IS DISTINCT FROM ");
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lsecond(node->args), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-22 03:43:59 +04:00
|
|
|
* Deparse given ScalarArrayOpExpr expression. To avoid problems
|
2013-02-22 15:03:46 +04:00
|
|
|
* around priority of operations, we always parenthesize the arguments.
|
2013-02-21 14:26:23 +04:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseScalarArrayOpExpr(ScalarArrayOpExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* Sanity check. */
|
|
|
|
Assert(list_length(node->args) == 2);
|
|
|
|
|
|
|
|
/* Always parenthesize the expression. */
|
|
|
|
appendStringInfoChar(buf, '(');
|
|
|
|
|
|
|
|
/* Deparse left operand. */
|
|
|
|
arg1 = linitial(node->args);
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(arg1, context);
|
2013-02-22 15:03:46 +04:00
|
|
|
appendStringInfoChar(buf, ' ');
|
2013-02-21 14:26:23 +04:00
|
|
|
|
2013-02-22 15:03:46 +04:00
|
|
|
/* Deparse operator name plus decoration. */
|
|
|
|
deparseOperatorName(buf, form);
|
|
|
|
appendStringInfo(buf, " %s (", node->useOr ? "ANY" : "ALL");
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
/* Deparse right operand. */
|
|
|
|
arg2 = lsecond(node->args);
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(arg2, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
|
|
|
|
/* Always parenthesize the expression. */
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
|
|
|
|
ReleaseSysCache(tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse a RelabelType (binary-compatible cast) node.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseRelabelType(RelabelType *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(node->arg, context);
|
2013-02-22 16:30:21 +04:00
|
|
|
if (node->relabelformat != COERCE_IMPLICIT_CAST)
|
2013-03-22 03:43:59 +04:00
|
|
|
appendStringInfo(context->buf, "::%s",
|
2013-02-22 16:30:21 +04:00
|
|
|
format_type_with_typemod(node->resulttype,
|
|
|
|
node->resulttypmod));
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse a BoolExpr node.
|
|
|
|
*
|
|
|
|
* Note: by the time we get here, AND and OR expressions have been flattened
|
|
|
|
* into N-argument form, so we'd better be prepared to deal with that.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseBoolExpr(BoolExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
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:
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, "(NOT ");
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(linitial(node->args), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
appendStringInfoChar(buf, '(');
|
|
|
|
first = true;
|
|
|
|
foreach(lc, node->args)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
appendStringInfo(buf, " %s ", op);
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr((Expr *) lfirst(lc), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
appendStringInfoChar(buf, ')');
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse IS [NOT] NULL expression.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseNullTest(NullTest *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
|
|
|
|
2013-02-21 14:26:23 +04:00
|
|
|
appendStringInfoChar(buf, '(');
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(node->arg, context);
|
2013-02-21 14:26:23 +04:00
|
|
|
if (node->nulltesttype == IS_NULL)
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " IS NULL)");
|
2013-02-21 14:26:23 +04:00
|
|
|
else
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, " IS NOT NULL)");
|
2013-02-21 14:26:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deparse ARRAY[...] construct.
|
|
|
|
*/
|
|
|
|
static void
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseArrayExpr(ArrayExpr *node, deparse_expr_cxt *context)
|
2013-02-21 14:26:23 +04:00
|
|
|
{
|
2013-03-22 03:43:59 +04:00
|
|
|
StringInfo buf = context->buf;
|
2013-02-21 14:26:23 +04:00
|
|
|
bool first = true;
|
|
|
|
ListCell *lc;
|
|
|
|
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, "ARRAY[");
|
2013-02-21 14:26:23 +04:00
|
|
|
foreach(lc, node->elements)
|
|
|
|
{
|
|
|
|
if (!first)
|
2013-03-10 22:14:53 +04:00
|
|
|
appendStringInfoString(buf, ", ");
|
2013-03-22 03:43:59 +04:00
|
|
|
deparseExpr(lfirst(lc), context);
|
2013-02-21 14:26:23 +04:00
|
|
|
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",
|
|
|
|
format_type_with_typemod(node->array_typeid, -1));
|
|
|
|
}
|