1 /*-------------------------------------------------------------------- 2 * Symbols referenced in this file: 3 * - operator_precedence_warning 4 *-------------------------------------------------------------------- 5 */ 6 7 /*------------------------------------------------------------------------- 8 * 9 * parse_expr.c 10 * handle expressions in parser 11 * 12 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group 13 * Portions Copyright (c) 1994, Regents of the University of California 14 * 15 * 16 * IDENTIFICATION 17 * src/backend/parser/parse_expr.c 18 * 19 *------------------------------------------------------------------------- 20 */ 21 22 #include "postgres.h" 23 24 #include "catalog/pg_type.h" 25 #include "commands/dbcommands.h" 26 #include "miscadmin.h" 27 #include "nodes/makefuncs.h" 28 #include "nodes/nodeFuncs.h" 29 #include "optimizer/tlist.h" 30 #include "optimizer/var.h" 31 #include "parser/analyze.h" 32 #include "parser/parse_clause.h" 33 #include "parser/parse_coerce.h" 34 #include "parser/parse_collate.h" 35 #include "parser/parse_expr.h" 36 #include "parser/parse_func.h" 37 #include "parser/parse_oper.h" 38 #include "parser/parse_relation.h" 39 #include "parser/parse_target.h" 40 #include "parser/parse_type.h" 41 #include "parser/parse_agg.h" 42 #include "utils/builtins.h" 43 #include "utils/date.h" 44 #include "utils/lsyscache.h" 45 #include "utils/timestamp.h" 46 #include "utils/xml.h" 47 48 49 /* GUC parameters */ 50 __thread bool operator_precedence_warning = false; 51 52 53 54 /* 55 * Node-type groups for operator precedence warnings 56 * We use zero for everything not otherwise classified 57 */ 58 #define PREC_GROUP_POSTFIX_IS 1 /* postfix IS tests (NullTest, etc) */ 59 #define PREC_GROUP_INFIX_IS 2 /* infix IS (IS DISTINCT FROM, etc) */ 60 #define PREC_GROUP_LESS 3 /* < > */ 61 #define PREC_GROUP_EQUAL 4 /* = */ 62 #define PREC_GROUP_LESS_EQUAL 5 /* <= >= <> */ 63 #define PREC_GROUP_LIKE 6 /* LIKE ILIKE SIMILAR */ 64 #define PREC_GROUP_BETWEEN 7 /* BETWEEN */ 65 #define PREC_GROUP_IN 8 /* IN */ 66 #define PREC_GROUP_NOT_LIKE 9 /* NOT LIKE/ILIKE/SIMILAR */ 67 #define PREC_GROUP_NOT_BETWEEN 10 /* NOT BETWEEN */ 68 #define PREC_GROUP_NOT_IN 11 /* NOT IN */ 69 #define PREC_GROUP_POSTFIX_OP 12 /* generic postfix operators */ 70 #define PREC_GROUP_INFIX_OP 13 /* generic infix operators */ 71 #define PREC_GROUP_PREFIX_OP 14 /* generic prefix operators */ 72 73 /* 74 * Map precedence groupings to old precedence ordering 75 * 76 * Old precedence order: 77 * 1. NOT 78 * 2. = 79 * 3. < > 80 * 4. LIKE ILIKE SIMILAR 81 * 5. BETWEEN 82 * 6. IN 83 * 7. generic postfix Op 84 * 8. generic Op, including <= => <> 85 * 9. generic prefix Op 86 * 10. IS tests (NullTest, BooleanTest, etc) 87 * 88 * NOT BETWEEN etc map to BETWEEN etc when considered as being on the left, 89 * but to NOT when considered as being on the right, because of the buggy 90 * precedence handling of those productions in the old grammar. 91 */ 92 93 94 95 static Node *transformExprRecurse(ParseState *pstate, Node *expr); 96 static Node *transformParamRef(ParseState *pstate, ParamRef *pref); 97 static Node *transformAExprOp(ParseState *pstate, A_Expr *a); 98 static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a); 99 static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a); 100 static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a); 101 static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a); 102 static Node *transformAExprOf(ParseState *pstate, A_Expr *a); 103 static Node *transformAExprIn(ParseState *pstate, A_Expr *a); 104 static Node *transformAExprBetween(ParseState *pstate, A_Expr *a); 105 static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a); 106 static Node *transformFuncCall(ParseState *pstate, FuncCall *fn); 107 static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref); 108 static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c); 109 static Node *transformSubLink(ParseState *pstate, SubLink *sublink); 110 static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, 111 Oid array_type, Oid element_type, int32 typmod); 112 static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault); 113 static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c); 114 static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m); 115 static Node *transformSQLValueFunction(ParseState *pstate, 116 SQLValueFunction *svf); 117 static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x); 118 static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs); 119 static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b); 120 static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr); 121 static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref); 122 static Node *transformWholeRowRef(ParseState *pstate, RangeTblEntry *rte, 123 int location); 124 static Node *transformIndirection(ParseState *pstate, A_Indirection *ind); 125 static Node *transformTypeCast(ParseState *pstate, TypeCast *tc); 126 static Node *transformCollateClause(ParseState *pstate, CollateClause *c); 127 static Node *make_row_comparison_op(ParseState *pstate, List *opname, 128 List *largs, List *rargs, int location); 129 static Node *make_row_distinct_op(ParseState *pstate, List *opname, 130 RowExpr *lrow, RowExpr *rrow, int location); 131 static Expr *make_distinct_op(ParseState *pstate, List *opname, 132 Node *ltree, Node *rtree, int location); 133 static Node *make_nulltest_from_distinct(ParseState *pstate, 134 A_Expr *distincta, Node *arg); 135 static int operator_precedence_group(Node *node, const char **nodename); 136 static void emit_precedence_warnings(ParseState *pstate, 137 int opgroup, const char *opname, 138 Node *lchild, Node *rchild, 139 int location); 140 141 142 /* 143 * transformExpr - 144 * Analyze and transform expressions. Type checking and type casting is 145 * done here. This processing converts the raw grammar output into 146 * expression trees with fully determined semantics. 147 */ 148 149 150 151 152 /* 153 * helper routine for delivering "column does not exist" error message 154 * 155 * (Usually we don't have to work this hard, but the general case of field 156 * selection from an arbitrary node needs it.) 157 */ 158 159 160 161 162 /* 163 * Transform a ColumnRef. 164 * 165 * If you find yourself changing this code, see also ExpandColumnRefStar. 166 */ 167 168 169 170 171 /* Test whether an a_expr is a plain NULL constant or not */ 172 173 174 175 176 177 178 179 180 181 182 183 184 /* 185 * Checking an expression for match to a list of type names. Will result 186 * in a boolean constant node. 187 */ 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 /* 205 * transformArrayExpr 206 * 207 * If the caller specifies the target type, the resulting array will 208 * be of exactly that type. Otherwise we try to infer a common type 209 * for the elements using select_common_type(). 210 */ 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 /* 230 * Construct a whole-row reference to represent the notation "relation.*". 231 */ 232 233 234 /* 235 * Handle an explicit CAST construct. 236 * 237 * Transform the argument, look up the type name, and apply any necessary 238 * coercion function(s). 239 */ 240 241 242 /* 243 * Handle an explicit COLLATE clause. 244 * 245 * Transform the argument, and look up the collation name. 246 */ 247 248 249 /* 250 * Transform a "row compare-op row" construct 251 * 252 * The inputs are lists of already-transformed expressions. 253 * As with coerce_type, pstate may be NULL if no special unknown-Param 254 * processing is wanted. 255 * 256 * The output may be a single OpExpr, an AND or OR combination of OpExprs, 257 * or a RowCompareExpr. In all cases it is guaranteed to return boolean. 258 * The AND, OR, and RowCompareExpr cases further imply things about the 259 * behavior of the operators (ie, they behave as =, <>, or < <= > >=). 260 */ 261 262 263 /* 264 * Transform a "row IS DISTINCT FROM row" construct 265 * 266 * The input RowExprs are already transformed 267 */ 268 269 270 /* 271 * make the node for an IS DISTINCT FROM operator 272 */ 273 274 275 /* 276 * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct 277 * 278 * "arg" is the untransformed other argument 279 */ 280 281 282 /* 283 * Identify node's group for operator precedence warnings 284 * 285 * For items in nonzero groups, also return a suitable node name into *nodename 286 * 287 * Note: group zero is used for nodes that are higher or lower precedence 288 * than everything that changed precedence; we need never issue warnings 289 * related to such nodes. 290 */ 291 292 293 /* 294 * helper routine for delivering 9.4-to-9.5 operator precedence warnings 295 * 296 * opgroup/opname/location represent some parent node 297 * lchild, rchild are its left and right children (either could be NULL) 298 * 299 * This should be called before transforming the child nodes, since if a 300 * precedence-driven parsing change has occurred in a query that used to work, 301 * it's quite possible that we'll get a semantic failure while analyzing the 302 * child expression. We want to produce the warning before that happens. 303 * In any case, operator_precedence_group() expects untransformed input. 304 */ 305 306 307 /* 308 * Produce a string identifying an expression by kind. 309 * 310 * Note: when practical, use a simple SQL keyword for the result. If that 311 * doesn't work well, check call sites to see whether custom error message 312 * strings are required. 313 */ 314 315