diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 43db430..d1c12a3 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -10327,9 +10327,9 @@ SELECT xml_is_well_formed_document(' @@ -10430,6 +10430,276 @@ SELECT xpath_exists('/my:a/text()', 'test + + + <literal>xmltable</literal> + + + xmltable + + + +xmltable( + xmlnamespaces(namespace uri AS namespace name, ...) + rowexpr PASSING BY REF xml BY REF + COLUMNS name type + PATH columnexpr + DEFAULT expr + NOT NULL | NULL , ... +) + + + + The xmltable produces a table based on the + passed XML value, an xpath filter to extract rows, and an + optional set of column definitions. + + + + For XML document: + + + AU + Australia + 3 + + + CN + China + 3 + + + HK + HongKong + 3 + + + IN + India + 3 + + + JP + Japan + 3Sinzo Abe + + + SG + Singapore + 3791 + + +]]> + + the following query produces the result: + + + + + + The optional xmlnamespaces clause is a comma-separated list of + namespaces of the form namespace-URI AS + namespace-name. It specifies the XML namespaces used in + the document and their aliases. The default namespace is not supported + yet. + +10' + COLUMNS a int PATH 'a'); + + a +---- + 10 +]]> + + + + The required rowexpr argument is an xpath + expression that is evaluated against the supplied XML document to + obtain an ordered sequence of XML nodes. This sequence is what + xmltable transforms into output rows. + + + + + The SQL/XML standard specifies the the + xmltable construct should take an XQuery + expression as the first argument, but PostgreSQL currently only + supports XPath, which is a subset of XQuery. + + + + + The PASSING xml clause provides the + XML document to operate on. + The BY REF clauses have no effect in + PostgreSQL, but are allowed for SQL conformance and compatibility + with other implementations. Per the SQL/XML standard, the first + BY REF is required, the second is optional. + Passing BY VALUE is not supported. Multiple + comma-separated terms in the PASSING clause are not supported. + AS aliases are not supported. The argument must be + a well-formed XML document; fragments/forests are not accepted. + + + + The optional COLUMNS clause specifies the list + of colums in the generated table. If the whole + COLUMNS list is omitted, then the result set is a + single column where each row is a field of xml type + containing the data matched by the rowexpr. + Otherwise each entry describes a single column. See the syntax + summary above for the format. The column name and type are + required; the path and not-null specifier are optional. + 10'); + + xmltable +------------------------------ + 10 +]]> + + + + + Column names and types are identifiers so unlike normal + function arguments they may not be specified as bind parameters, + column references, or expressions. The path and default are + ordinary values. + + + + + The PATH for a column is an xpath expression that is + evaluated for each row, relative to the result of the + rowexpr, to find the value of the column. + If no PATH is given then the column name is used as an + implicit path. + + + + The text body of the XML matched by the PATH expression is + used as the column value. Multiple text() nodes within + an element are concatenated in order. Any child elements, processing + instructions, and comments are ignored, but the text contents of child + elements are concatenated to the result: + a1aa2a bbbbxxxcccc' COLUMNS element text); +element +----------------- + a1aa2a bbbbcccc + (1 row) +]]> + Note that the whitespace-only text() node between two non-text + elements is preserved, and that leading whitespace on a text() + node is not flattened. + + + + A PATH expression that matches multiple top-level elements + results in an error: + 12' COLUMNS element text); +ERROR: more than one value returned by column XPath expression +]]> + This applies to text() nodes too, so avoid using explicit text nodes + in your path expressions unless this behaviour is desired: + a1aa2a' COLUMNS element text PATH 'element/text()'); +ERROR: more than one value returned by column XPath expression +]]> + If the PATH matches an empty tag the result is an empty string + (not NULL). Any xsi:nil attributes are ignored. + + + + The five predefined XML entities 'apos;, + 'quot;, 'amp;, 'lt; + and 'gt; are expanded to their corresponding characters + ', ", &, < + and > on processing. If the output column format is + xml then the characters <, > and + & are converted to XML entities, but " and + ' are left unchanged. This means that 'apos; + and 'quot; entities in XML input get expanded in xml + output columns. There is no way to prevent their expansion. + + + + If the path expression does not match for a given row but a + DEFAULT expression is specified, the resulting + default value is used. If no DEFAULT is given the + field will be NULL. Unlike normal queries, it is possible for + a DEFAULT expression to reference the value of output columns + that appear prior to it in the column-list, so the default of one + column may be based on the value of another column. + + + + Columns may be marked NOT NULL. If the xpath expression + for a NOT NULL column does not match anything and there + is no DEFAULT or the DEFAULT also evaluated + to null then the function terminates with an ERROR. + + + + + Unlike normal PostgreSQL functions, the PATH and + DEFAULT arguments to xmltable are not evaluated + to a simple value before calling the function. PATH + expressions are normally evaluated exactly once per input row + , and DEFAULT expressions each time a default is + needed for a field. If the expression qualifies as stable or immutable + the repeat evaluation may be skipped. Effectively xmltable + behaves more like a subquery than a function call. This means that you + can usefully use volatile functions like nextval in + DEFAULT expressions, your PATH expressions may + depend on other parts of the XML document, etc. + + + + + A column marked with the + FOR ORDINALITY keyword will be populated with + row numbers that match the order in which the the output rows appeared + in the original input XML document. Only one column should be + marked FOR ORDINALITY. + + + + Only XPath query language is supported. PostgreSQL doesn't support XQuery + language. Then the syntax of xmltable doesn't + allow to use XQuery related functionality - the name of xml expression + (clause AS) is not allowed, and only one xml expression + should be passed to xmltable function as parameter. + + diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c index 743e7d6..c322012 100644 --- a/src/backend/executor/execQual.c +++ b/src/backend/executor/execQual.c @@ -43,6 +43,7 @@ #include "catalog/pg_type.h" #include "executor/execdebug.h" #include "executor/nodeSubplan.h" +#include "executor/tableexpr.h" #include "funcapi.h" #include "miscadmin.h" #include "nodes/makefuncs.h" @@ -189,6 +190,13 @@ static Datum ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext, static Datum ExecEvalGroupingFuncExpr(GroupingFuncExprState *gstate, ExprContext *econtext, bool *isNull, ExprDoneCond *isDone); +static Datum ExecEvalTableExpr(TableExprState *tstate, + ExprContext *econtext, + bool *isnull, ExprDoneCond *isDone); +static Datum tabexprFetchRow(TableExprState *tstate, ExprContext *econtext, + bool *isNull, ExprDoneCond *isDone); +static void tabexprInitialize(TableExprState *tstate, ExprContext *econtext, + Datum value); /* ---------------------------------------------------------------- @@ -4500,6 +4508,253 @@ ExecEvalCurrentOfExpr(ExprState *exprstate, ExprContext *econtext, return 0; /* keep compiler quiet */ } +/* ---------------------------------------------------------------- + * ExecEvalTableExpr + * + * Evaluate a TableExpr node. One tuple is returned per call; this function + * is to be called until no more rows are returned. + * ---------------------------------------------------------------- + */ +static Datum +ExecEvalTableExpr(TableExprState *tstate, + ExprContext *econtext, + bool *isNull, ExprDoneCond *isDone) +{ + const TableExprRoutine *routine = tstate->routine; + Datum result; + + PG_TRY(); + { + MemoryContext oldcxt; + + /* + * The first time around, create the table builder context and + * initialize it with the document content. + */ + if (tstate->opaque == NULL) + { + Datum value; + + /* Fill in table builder opaque area */ + oldcxt = MemoryContextSwitchTo(tstate->buildercxt); + routine->InitializeBuilder(tstate); + MemoryContextSwitchTo(oldcxt); + + /* + * If evaluating the document expression returns NULL, the table + * expression is empty and we return immediately. + */ + value = ExecEvalExpr(tstate->doc_expr, econtext, isNull, NULL); + if (*isNull) + { + *isDone = ExprSingleResult; + return (Datum) 0; + } + + /* otherwise, pass the document value to the table builder */ + tabexprInitialize(tstate, econtext, value); + } + + /* Fetch and return one row per call from the table builder */ + result = tabexprFetchRow(tstate, econtext, isNull, isDone); + } + PG_CATCH(); + { + /* + * If any errors are raised by the table builder, clean up everything + * before jumping ship. + */ + if (tstate->opaque != NULL) + { + routine->DestroyBuilder(tstate); + ExecDropSingleTupleTableSlot(tstate->resultSlot); + } + + MemoryContextDelete(tstate->buildercxt); + tstate->buildercxt = NULL; + + PG_RE_THROW(); + } + PG_END_TRY(); + + return result; +} + +/* + * Fill in namespace declarations, the row filter, and column filters in a + * table expression builder context. + */ +static void +tabexprInitialize(TableExprState *tstate, ExprContext *econtext, Datum value) +{ + const TableExprRoutine *routine; + MemoryContext oldcxt; + ListCell *cell; + bool isnull; + int i; + + routine = tstate->routine; + + /* + * The content can be bigger document and transformation to cstring can be + * expensive. The table builder is better place for this task - pass value + * as Datum. Evaluate builder function in special memory context + */ + oldcxt = MemoryContextSwitchTo(tstate->buildercxt); + routine->SetContent(tstate, value); + MemoryContextSwitchTo(oldcxt); + + /* Evaluate namespace specifications */ + foreach(cell, tstate->namespaces) + { + Node *n = (Node *) lfirst(cell); + ExprState *expr; + char *ns_name; + char *ns_uri; + + if (IsA(n, NamedArgExpr)) + { + NamedArgExpr *na = (NamedArgExpr *) n; + + expr = (ExprState *) na->arg; + ns_name = na->name; + } + else + { + expr = (ExprState *) n; + ns_name = NULL; + } + + value = ExecEvalExpr((ExprState *) expr, econtext, &isnull, NULL); + if (isnull) + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("namespace URI must not be null"))); + ns_uri = TextDatumGetCString(value); + + MemoryContextSwitchTo(tstate->buildercxt); + routine->DeclareNamespace(tstate, ns_name, ns_uri); + MemoryContextSwitchTo(oldcxt); + } + + /* Install the row filter expression into the table builder context */ + value = ExecEvalExpr(tstate->row_expr, econtext, &isnull, NULL); + if (isnull) + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("row filter expression must not be null"))); + + MemoryContextSwitchTo(tstate->buildercxt); + routine->SetRowFilter(tstate, TextDatumGetCString(value)); + MemoryContextSwitchTo(oldcxt); + + /* + * Install the column filter expressions into the table builder context. + * If an expression is given, use that; otherwise the column name itself + * is the column filter. + */ + for (i = 0; i < tstate->resultSlot->tts_tupleDescriptor->natts; i++) + { + char *col_filter; + + if (tstate->col_expr[i] != NULL) + { + value = ExecEvalExpr(tstate->col_expr[i], econtext, &isnull, NULL); + if (isnull) + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("column filter expression must not be null"), + errdetail("Filter for column \"%s\" is null.", + NameStr(tstate->resultSlot->tts_tupleDescriptor->attrs[i]->attname)))); + col_filter = TextDatumGetCString(value); + } + else + col_filter = NameStr(tstate->resultSlot->tts_tupleDescriptor->attrs[i]->attname); + + MemoryContextSwitchTo(tstate->buildercxt); + routine->SetColumnFilter(tstate, col_filter, i); + MemoryContextSwitchTo(oldcxt); + } +} + +/* + * Fetch one more row from a TableExpr table builder. + */ +static Datum +tabexprFetchRow(TableExprState *tstate, ExprContext *econtext, + bool *isNull, ExprDoneCond *isDone) +{ + const TableExprRoutine *routine; + MemoryContext oldcxt; + Datum result; + + routine = tstate->routine; + oldcxt = MemoryContextSwitchTo(tstate->buildercxt); + + /* Prepare one more row */ + if (routine->FetchRow(tstate)) + { + Datum *values = tstate->resultSlot->tts_values; + bool *nulls = tstate->resultSlot->tts_isnull; + int i; + bool isnull; + int natts = tstate->resultSlot->tts_tupleDescriptor->natts; + + ExecClearTuple(tstate->resultSlot); + + for (i = 0; i < natts; i++) + { + if (i + 1 == tstate->for_ordinality_col) + { + values[i] = Int32GetDatum(tstate->rownum++); + nulls[i] = false; + } + else + { + MemoryContextSwitchTo(tstate->buildercxt); + values[i] = routine->GetValue(tstate, i, &isnull); + MemoryContextSwitchTo(oldcxt); + + if (isnull && tstate->def_expr[i] != NULL) + values[i] = ExecEvalExpr(tstate->def_expr[i], econtext, &isnull, NULL); + + if (isnull && tstate->not_null[i]) + ereport(ERROR, + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("null is not allowed in column \"%s\"", + NameStr(tstate->resultSlot->tts_tupleDescriptor->attrs[i]->attname)))); + nulls[i] = isnull; + } + } + + ExecStoreVirtualTuple(tstate->resultSlot); + + MemoryContextSwitchTo(oldcxt); + + *isNull = false; + *isDone = ExprMultipleResult; + result = ExecFetchSlotTupleDatum(tstate->resultSlot); + } + else + { + /* no more rows */ + routine->DestroyBuilder(tstate); + + /* make sure all memory is released */ + ExecDropSingleTupleTableSlot(tstate->resultSlot); + MemoryContextReset(tstate->buildercxt); + + *isNull = true; + *isDone = ExprEndResult; + + result = (Datum) 0; + } + + /* push back short life memory context */ + MemoryContextSwitchTo(oldcxt); + + return result; +} /* * ExecEvalExprSwitchContext @@ -5262,6 +5517,108 @@ ExecInitExpr(Expr *node, PlanState *parent) /* Don't fall through to the "common" code below */ return (ExprState *) outlist; } + case T_TableExpr: + { + TableExpr *te = (TableExpr *) node; + TableExprState *tstate = makeNode(TableExprState); + TupleDesc tupdesc; + int natts; + int i; + + tstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecEvalTableExpr; + tstate->opaque = NULL; + + /* Only XmlTableBuilder is supported currently */ + tstate->routine = &XmlTableExprRoutine; + + /* XXX this assumes the anon record type has been registered */ + tupdesc = lookup_rowtype_tupdesc_copy(exprType((Node *) te), + exprTypmod((Node *) te)); + natts = tupdesc->natts; + Assert(natts > 0); + tstate->resultSlot = MakeSingleTupleTableSlot(tupdesc); + tstate->in_functions = palloc(sizeof(FmgrInfo) * natts); + tstate->typioparams = palloc(sizeof(Oid) * natts); + + for (i = 0; i < natts; i++) + { + Oid in_funcid; + + getTypeInputInfo(tupdesc->attrs[i]->atttypid, + &in_funcid, &tstate->typioparams[i]); + fmgr_info(in_funcid, &tstate->in_functions[i]); + } + + tstate->row_expr = ExecInitExpr((Expr *) te->row_expr, parent); + tstate->doc_expr = ExecInitExpr((Expr *) te->document_expr, parent); + + if (te->columns) + { + ListCell *col; + + Assert(natts == list_length(te->columns)); + + tstate->def_expr = palloc0(sizeof(ExprState *) * natts); + tstate->col_expr = palloc0(sizeof(ExprState *) * natts); + tstate->not_null = palloc0(sizeof(bool) * natts); + tstate->rownum = 1; + + i = 0; + foreach(col, te->columns) + { + TableExprColumn *tec = (TableExprColumn *) lfirst(col); + + if (!tec->for_ordinality) + { + tstate->def_expr[i] = ExecInitExpr((Expr *) tec->default_expr, + parent); + tstate->col_expr[i] = ExecInitExpr((Expr *) tec->column_expr, + parent); + tstate->not_null[i] = tec->is_not_null; + } + else + tstate->for_ordinality_col = i + 1; + + i++; + } + } + + if (te->namespaces) + { + List *preparedlist = NIL; + ListCell *ns; + + foreach(ns, te->namespaces) + { + Node *n = (Node *) lfirst(ns); + + if (IsA(n, NamedArgExpr)) + { + NamedArgExpr *na = (NamedArgExpr *) n; + NamedArgExpr *nax = makeNode(NamedArgExpr); + + nax->name = na->name; + nax->arg = (Expr *) ExecInitExpr(na->arg, parent); + nax->location = na->location; + preparedlist = lappend(preparedlist, nax); + } + else + preparedlist = lappend(preparedlist, + ExecInitExpr((Expr *) n, parent)); + } + tstate->namespaces = preparedlist; + } + else + tstate->namespaces = NIL; + + tstate->buildercxt = + AllocSetContextCreate(CurrentMemoryContext, + "TableExpr builder context", + ALLOCSET_DEFAULT_SIZES); + + state = (ExprState *) tstate; + } + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.c index 533050d..8c722ed 100644 --- a/src/backend/executor/execTuples.c +++ b/src/backend/executor/execTuples.c @@ -975,6 +975,48 @@ ExecTypeFromExprList(List *exprList) } /* + * Build a tuple descriptor for TableExpr using its declared column list, and + * assign it a record's typmod. + */ +TupleDesc +ExecTypeFromTableExpr(const TableExpr *te) +{ + TupleDesc typeInfo; + + if (te->columns != NIL) + { + ListCell *col; + int i = 1; + + typeInfo = CreateTemplateTupleDesc(list_length(te->columns), false); + + foreach(col, te->columns) + { + TableExprColumn *tec = (TableExprColumn *) lfirst(col); + + TupleDescInitEntry(typeInfo, + (AttrNumber) i, + pstrdup(tec->colname), + tec->typid, + tec->typmod, + 0); + i++; + } + } + else + { + typeInfo = CreateTemplateTupleDesc(1, false); + TupleDescInitEntry(typeInfo, (AttrNumber) 1, + "xmltable", XMLOID, -1, 0); + } + + /* XXX OK to do this? looks a bit out of place ... */ + assign_record_type_typmod(typeInfo); + + return typeInfo; +} + +/* * ExecTypeSetColNames - set column names in a TupleDesc * * Column names must be provided as an alias list (list of String nodes). diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 04e49b7..c04febb 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -1987,6 +1987,63 @@ _copyOnConflictExpr(const OnConflictExpr *from) return newnode; } +/* + * _copyTableExpr + */ +static TableExpr * +_copyTableExpr(const TableExpr *from) +{ + TableExpr *newnode = makeNode(TableExpr); + + COPY_NODE_FIELD(namespaces); + COPY_NODE_FIELD(document_expr); + COPY_NODE_FIELD(row_expr); + COPY_NODE_FIELD(columns); + COPY_LOCATION_FIELD(location); + + return newnode; +} + +/* + * _copyTableExprRawCol + */ +static TableExprRawCol * +_copyTableExprRawCol(const TableExprRawCol *from) +{ + TableExprRawCol *newnode = makeNode(TableExprRawCol); + + COPY_STRING_FIELD(colname); + COPY_SCALAR_FIELD(for_ordinality); + COPY_NODE_FIELD(typeName); + COPY_SCALAR_FIELD(is_not_null); + COPY_NODE_FIELD(column_expr); + COPY_NODE_FIELD(default_expr); + COPY_LOCATION_FIELD(location); + + return newnode; +} + +/* + * _copyTableExprColumn + */ +static TableExprColumn * +_copyTableExprColumn(const TableExprColumn *from) +{ + TableExprColumn *newnode = makeNode(TableExprColumn); + + COPY_STRING_FIELD(colname); + COPY_SCALAR_FIELD(typid); + COPY_SCALAR_FIELD(typmod); + COPY_SCALAR_FIELD(collation); + COPY_SCALAR_FIELD(for_ordinality); + COPY_SCALAR_FIELD(is_not_null); + COPY_NODE_FIELD(column_expr); + COPY_NODE_FIELD(default_expr); + COPY_LOCATION_FIELD(location); + + return newnode; +} + /* **************************************************************** * relation.h copy functions * @@ -4600,6 +4657,12 @@ copyObject(const void *from) case T_OnConflictExpr: retval = _copyOnConflictExpr(from); break; + case T_TableExpr: + retval = _copyTableExpr(from); + break; + case T_TableExprColumn: + retval = _copyTableExprColumn(from); + break; /* * RELATION NODES @@ -5104,6 +5167,9 @@ copyObject(const void *from) case T_TriggerTransition: retval = _copyTriggerTransition(from); break; + case T_TableExprRawCol: + retval = _copyTableExprRawCol(from); + break; /* * MISCELLANEOUS NODES diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index 2eaf41c..238fc30 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -2615,6 +2615,36 @@ _equalCommonTableExpr(const CommonTableExpr *a, const CommonTableExpr *b) } static bool +_equalTableExprRawCol(const TableExprRawCol *a, const TableExprRawCol *b) +{ + COMPARE_STRING_FIELD(colname); + COMPARE_SCALAR_FIELD(for_ordinality); + COMPARE_NODE_FIELD(typeName); + COMPARE_SCALAR_FIELD(is_not_null); + COMPARE_NODE_FIELD(column_expr); + COMPARE_NODE_FIELD(default_expr); + COMPARE_LOCATION_FIELD(location); + + return true; +} + +static bool +_equalTableExprColumn(const TableExprColumn *a, const TableExprColumn *b) +{ + COMPARE_STRING_FIELD(colname); + COMPARE_SCALAR_FIELD(typid); + COMPARE_SCALAR_FIELD(typmod); + COMPARE_SCALAR_FIELD(collation); + COMPARE_SCALAR_FIELD(for_ordinality); + COMPARE_SCALAR_FIELD(is_not_null); + COMPARE_NODE_FIELD(column_expr); + COMPARE_NODE_FIELD(default_expr); + COMPARE_LOCATION_FIELD(location); + + return true; +} + +static bool _equalXmlSerialize(const XmlSerialize *a, const XmlSerialize *b) { COMPARE_SCALAR_FIELD(xmloption); @@ -2645,6 +2675,18 @@ _equalTriggerTransition(const TriggerTransition *a, const TriggerTransition *b) return true; } +static bool +_equalTableExpr(const TableExpr *a, const TableExpr *b) +{ + COMPARE_NODE_FIELD(row_expr); + COMPARE_NODE_FIELD(document_expr); + COMPARE_NODE_FIELD(columns); + COMPARE_NODE_FIELD(namespaces); + COMPARE_LOCATION_FIELD(location); + + return true; +} + /* * Stuff from pg_list.h */ @@ -2910,6 +2952,12 @@ equal(const void *a, const void *b) case T_JoinExpr: retval = _equalJoinExpr(a, b); break; + case T_TableExpr: + retval = _equalTableExpr(a, b); + break; + case T_TableExprColumn: + retval = _equalTableExprColumn(a, b); + break; /* * RELATION NODES @@ -3401,6 +3449,9 @@ equal(const void *a, const void *b) case T_TriggerTransition: retval = _equalTriggerTransition(a, b); break; + case T_TableExprRawCol: + retval = _equalTableExprRawCol(a, b); + break; default: elog(ERROR, "unrecognized node type: %d", diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c index 20e2dbd..592760f 100644 --- a/src/backend/nodes/makefuncs.c +++ b/src/backend/nodes/makefuncs.c @@ -17,6 +17,7 @@ #include "catalog/pg_class.h" #include "catalog/pg_type.h" +#include "executor/executor.h" #include "fmgr.h" #include "nodes/makefuncs.h" #include "nodes/nodeFuncs.h" diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index 3997441..6db3ccf 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -23,6 +23,7 @@ #include "nodes/relation.h" #include "utils/builtins.h" #include "utils/lsyscache.h" +#include "utils/typcache.h" static bool expression_returns_set_walker(Node *node, void *context); @@ -257,6 +258,12 @@ exprType(const Node *expr) case T_PlaceHolderVar: type = exprType((Node *) ((const PlaceHolderVar *) expr)->phexpr); break; + case T_TableExpr: + type = RECORDOID; + break; + case T_TableExprColumn: + type = ((const TableExprColumn *) expr)->typid; + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr)); type = InvalidOid; /* keep compiler quiet */ @@ -492,6 +499,19 @@ exprTypmod(const Node *expr) return ((const SetToDefault *) expr)->typeMod; case T_PlaceHolderVar: return exprTypmod((Node *) ((const PlaceHolderVar *) expr)->phexpr); + case T_TableExpr: + { + /* + * The result of table expression is pseudo-type record. + * Generate a blessed tupdesc from the column definitions, and + * returns its typmod. + */ + TupleDesc tupdesc = ExecTypeFromTableExpr((const TableExpr *) expr); + + return tupdesc->tdtypmod; + } + case T_TableExprColumn: + return ((const TableExprColumn *) expr)->typmod; default: break; } @@ -727,6 +747,8 @@ expression_returns_set_walker(Node *node, void *context) return false; if (IsA(node, XmlExpr)) return false; + if (IsA(node, TableExpr)) + return true; return expression_tree_walker(node, expression_returns_set_walker, context); @@ -929,6 +951,12 @@ exprCollation(const Node *expr) case T_PlaceHolderVar: coll = exprCollation((Node *) ((const PlaceHolderVar *) expr)->phexpr); break; + case T_TableExpr: + coll = InvalidOid; /* result is composite or XML or JSON */ + break; + case T_TableExprColumn: + coll = ((const TableExprColumn *) expr)->collation; + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr)); coll = InvalidOid; /* keep compiler quiet */ @@ -1127,6 +1155,13 @@ exprSetCollation(Node *expr, Oid collation) case T_CurrentOfExpr: Assert(!OidIsValid(collation)); /* result is always boolean */ break; + case T_TableExpr: + Assert(!OidIsValid(collation)); /* result is always composite + * or XML, .. */ + break; + case T_TableExprColumn: + ((TableExprColumn *) expr)->collation = collation; + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr)); break; @@ -1552,6 +1587,9 @@ exprLocation(const Node *expr) /* just use nested expr's location */ loc = exprLocation((Node *) ((const InferenceElem *) expr)->expr); break; + case T_TableExpr: + loc = ((const TableExpr *) expr)->location; + break; default: /* for any other node type it's just unknown... */ loc = -1; @@ -2211,6 +2249,30 @@ expression_tree_walker(Node *node, return true; } break; + case T_TableExpr: + { + TableExpr *te = (TableExpr *) node; + + if (walker(te->row_expr, context)) + return true; + if (walker(te->document_expr, context)) + return true; + if (walker(te->namespaces, context)) + return true; + if (walker(te->columns, context)) + return true; + } + break; + case T_TableExprColumn: + { + TableExprColumn *tec = (TableExprColumn *) node; + + if (walker(tec->column_expr, context)) + return true; + if (walker(tec->default_expr, context)) + return true; + } + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); @@ -3007,6 +3069,30 @@ expression_tree_mutator(Node *node, return (Node *) newnode; } break; + case T_TableExpr: + { + TableExpr *te = (TableExpr *) node; + TableExpr *newnode; + + FLATCOPY(newnode, te, TableExpr); + MUTATE(newnode->row_expr, te->row_expr, Node *); + MUTATE(newnode->document_expr, te->document_expr, Node *); + MUTATE(newnode->namespaces, te->namespaces, List *); + MUTATE(newnode->columns, te->columns, List *); + return (Node *) newnode; + } + break; + case T_TableExprColumn: + { + TableExprColumn *tec = (TableExprColumn *) node; + TableExprColumn *newnode; + + FLATCOPY(newnode, tec, TableExprColumn); + MUTATE(newnode->column_expr, tec->column_expr, Node *); + MUTATE(newnode->default_expr, tec->default_expr, Node *); + return (Node *) newnode; + } + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); @@ -3622,6 +3708,20 @@ raw_expression_tree_walker(Node *node, break; case T_CommonTableExpr: return walker(((CommonTableExpr *) node)->ctequery, context); + case T_TableExpr: + { + TableExpr *te = (TableExpr *) node; + + if (walker(te->row_expr, context)) + return true; + if (walker(te->document_expr, context)) + return true; + if (walker(te->columns, context)) + return true; + if (walker(te->namespaces, context)) + return true; + } + break; default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 748b687..a3ee9bd 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -1574,6 +1574,48 @@ _outOnConflictExpr(StringInfo str, const OnConflictExpr *node) WRITE_NODE_FIELD(exclRelTlist); } +static void +_outTableExpr(StringInfo str, const TableExpr *node) +{ + WRITE_NODE_TYPE("TABLEEXPR"); + + WRITE_NODE_FIELD(namespaces); + WRITE_NODE_FIELD(document_expr); + WRITE_NODE_FIELD(row_expr); + WRITE_NODE_FIELD(columns); + WRITE_LOCATION_FIELD(location); +} + +static void +_outTableExprColumn(StringInfo str, const TableExprColumn *node) +{ + WRITE_NODE_TYPE("TABLEEXPRCOLUMN"); + + WRITE_STRING_FIELD(colname); + WRITE_BOOL_FIELD(for_ordinality); + WRITE_OID_FIELD(typid); + WRITE_INT_FIELD(typmod); + WRITE_OID_FIELD(collation); + WRITE_BOOL_FIELD(is_not_null); + WRITE_NODE_FIELD(column_expr); + WRITE_NODE_FIELD(default_expr); + WRITE_LOCATION_FIELD(location); +} + +static void +_outTableExprRawCol(StringInfo str, const TableExprRawCol *node) +{ + WRITE_NODE_TYPE("TABLEEXPRCOLUMN"); + + WRITE_STRING_FIELD(colname); + WRITE_BOOL_FIELD(for_ordinality); + WRITE_NODE_FIELD(typeName); + WRITE_BOOL_FIELD(is_not_null); + WRITE_NODE_FIELD(column_expr); + WRITE_NODE_FIELD(default_expr); + WRITE_LOCATION_FIELD(location); +} + /***************************************************************************** * * Stuff from relation.h. @@ -3539,6 +3581,12 @@ outNode(StringInfo str, const void *obj) case T_XmlExpr: _outXmlExpr(str, obj); break; + case T_TableExpr: + _outTableExpr(str, obj); + break; + case T_TableExprColumn: + _outTableExprColumn(str, obj); + break; case T_NullTest: _outNullTest(str, obj); break; @@ -3865,6 +3913,9 @@ outNode(StringInfo str, const void *obj) case T_TriggerTransition: _outTriggerTransition(str, obj); break; + case T_TableExprRawCol: + _outTableExprRawCol(str, obj); + break; default: diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index 917e6c8..1aed810 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -2266,6 +2266,44 @@ _readExtensibleNode(void) } /* + * _readTableExprNode + */ +static TableExpr * +_readTableExprNode(void) +{ + READ_LOCALS(TableExpr); + + READ_NODE_FIELD(namespaces); + READ_NODE_FIELD(document_expr); + READ_NODE_FIELD(row_expr); + READ_NODE_FIELD(columns); + READ_LOCATION_FIELD(location); + + READ_DONE(); +} + +/* + * _readTableExprColumn + */ +static TableExprColumn * +_readTableExprColumnNode(void) +{ + READ_LOCALS(TableExprColumn); + + READ_STRING_FIELD(colname); + READ_BOOL_FIELD(for_ordinality); + READ_OID_FIELD(typid); + READ_INT_FIELD(typmod); + READ_OID_FIELD(collation); + READ_BOOL_FIELD(is_not_null); + READ_NODE_FIELD(column_expr); + READ_NODE_FIELD(default_expr); + READ_LOCATION_FIELD(location); + + READ_DONE(); +} + +/* * parseNodeString * * Given a character string representing a node tree, parseNodeString creates @@ -2497,6 +2535,10 @@ parseNodeString(void) return_value = _readAlternativeSubPlan(); else if (MATCH("EXTENSIBLENODE", 14)) return_value = _readExtensibleNode(); + else if (MATCH("TABLEEXPR", 9)) + return_value = _readTableExprNode(); + else if (MATCH("TABLEEXPRCOLUMN", 15)) + return_value = _readTableExprColumnNode(); else { elog(ERROR, "badly formatted node string \"%.32s\"...", token); diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c index 9af29dd..5d5fd41 100644 --- a/src/backend/optimizer/util/clauses.c +++ b/src/backend/optimizer/util/clauses.c @@ -829,6 +829,12 @@ expression_returns_set_rows_walker(Node *node, double *count) *count *= get_func_rows(expr->opfuncid); } } + if (IsA(node, TableExpr)) + { + /* we have not any method how to estimate it, use default */ + *count = 1000; + return false; + } /* Avoid recursion for some cases that can't return a set */ if (IsA(node, Aggref)) @@ -3598,6 +3604,33 @@ eval_const_expressions_mutator(Node *node, context); } break; + case T_TableExprColumn: + { + TableExprColumn *tec = (TableExprColumn *) node; + + if (tec->column_expr != NULL || tec->default_expr != NULL) + { + TableExprColumn *newtec = makeNode(TableExprColumn); + + newtec->colname = tec->colname; + newtec->typid = tec->typid; + newtec->typmod = tec->typmod; + newtec->collation = tec->collation; + newtec->for_ordinality = tec->for_ordinality; + newtec->is_not_null = tec->is_not_null; + newtec->column_expr = + eval_const_expressions_mutator((Node *) tec->column_expr, + context); + newtec->default_expr = + eval_const_expressions_mutator((Node *) tec->default_expr, + context); + newtec->location = tec->location; + + return (Node *) newtec; + } + } + break; + default: break; } diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index d6274b4..277fae8 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -545,6 +545,12 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type opt_existing_window_name %type opt_if_not_exists +%type XmlTableExprColList XmlTableExprColOptions +%type XmlTableExprCol +%type XmlTableExprColOption +%type XmlNamespaceList +%type XmlNamespace + /* * Non-keyword token types. These are hard-wired into the "flex" lexer. * They must be listed first so that their numeric codes do not depend on @@ -576,10 +582,10 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE - CLUSTER COALESCE COLLATE COLLATION COLUMN COMMENT COMMENTS COMMIT - COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION CONSTRAINT - CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE - CROSS CSV CUBE CURRENT_P + CLUSTER COALESCE COLLATE COLLATION COLUMN COLUMNS COMMENT COMMENTS + COMMIT COMMITTED CONCURRENTLY CONFIGURATION CONFLICT CONNECTION + CONSTRAINT CONSTRAINTS CONTENT_P CONTINUE_P CONVERSION_P COPY COST + CREATE CROSS CSV CUBE CURRENT_P CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE @@ -649,8 +655,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); WHEN WHERE WHITESPACE_P WINDOW WITH WITHIN WITHOUT WORK WRAPPER WRITE - XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLPARSE - XMLPI XMLROOT XMLSERIALIZE + XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLEXISTS XMLFOREST XMLNAMESPACES + XMLPARSE XMLPI XMLROOT XMLSERIALIZE XMLTABLE YEAR_P YES_P @@ -12588,6 +12594,174 @@ func_expr_common_subexpr: n->location = @1; $$ = (Node *)n; } + | XMLTABLE '(' c_expr xmlexists_argument ')' + { + TableExpr *n = makeNode(TableExpr); + n->row_expr = $3; + n->document_expr = $4; + n->columns = NIL; + n->namespaces = NIL; + n->location = @1; + $$ = (Node *)n; + } + | XMLTABLE '(' c_expr xmlexists_argument COLUMNS XmlTableExprColList ')' + { + TableExpr *n = makeNode(TableExpr); + n->row_expr = $3; + n->document_expr = $4; + n->columns = $6; + n->namespaces = NIL; + n->location = @1; + $$ = (Node *)n; + } + | XMLTABLE '(' XMLNAMESPACES '(' XmlNamespaceList ')' ',' + c_expr xmlexists_argument ')' + { + TableExpr *n = makeNode(TableExpr); + n->row_expr = $8; + n->document_expr = $9; + n->columns = NIL; + n->namespaces = $5; + n->location = @1; + $$ = (Node *)n; + } + | XMLTABLE '(' XMLNAMESPACES '(' XmlNamespaceList ')' ',' + c_expr xmlexists_argument COLUMNS XmlTableExprColList ')' + { + TableExpr *n = makeNode(TableExpr); + n->row_expr = $8; + n->document_expr = $9; + n->columns = $11; + n->namespaces = $5; + n->location = @1; + $$ = (Node *)n; + } + ; + +XmlTableExprColList: XmlTableExprCol { $$ = list_make1($1); } + | XmlTableExprColList ',' XmlTableExprCol { $$ = lappend($1, $3); } + ; + +XmlTableExprCol: + ColId Typename + { + TableExprRawCol *rawc = makeNode(TableExprRawCol); + + rawc->colname = $1; + rawc->for_ordinality = false; + rawc->typeName = $2; + rawc->is_not_null = false; + rawc->column_expr = NULL; + rawc->default_expr = NULL; + rawc->location = @1; + + $$ = (Node *) rawc; + } + | ColId Typename XmlTableExprColOptions + { + TableExprRawCol *rawc = makeNode(TableExprRawCol); + ListCell *option; + bool nullability_seen = false; + + rawc->colname = $1; + rawc->for_ordinality = false; + rawc->typeName = $2; + rawc->is_not_null = false; + rawc->column_expr = NULL; + rawc->default_expr = NULL; + rawc->location = @1; + + foreach(option, $3) + { + DefElem *defel = (DefElem *) lfirst(option); + + if (strcmp(defel->defname, "default") == 0) + { + if (rawc->default_expr != NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("only one DEFAULT value is allowed"), + parser_errposition(defel->location))); + rawc->default_expr = defel->arg; + } + else if (strcmp(defel->defname, "path") == 0) + { + if (rawc->column_expr != NULL) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("only one PATH value per column is allowed"), + parser_errposition(defel->location))); + rawc->column_expr = defel->arg; + } + else if (strcmp(defel->defname, "notnull") == 0) + { + if (nullability_seen) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting or redundant NULL / NOT NULL declarations for column \"%s\"", rawc->colname), + parser_errposition(defel->location))); + rawc->is_not_null = intVal(defel->arg); + nullability_seen = true; + } + else + { + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("unrecognized column option \"%s\"", + defel->defname), + parser_errposition(defel->location))); + } + } + $$ = (Node *) rawc; + } + | ColId FOR ORDINALITY + { + TableExprRawCol *rawc = makeNode(TableExprRawCol); + + rawc->colname = $1; + rawc->for_ordinality = true; + /* other fields are ignored, initialized by makeNode */ + rawc->location = @1; + + $$ = (Node *) rawc; + } + ; + +XmlTableExprColOptions: XmlTableExprColOption { $$ = list_make1($1); } + | XmlTableExprColOptions XmlTableExprColOption { $$ = lappend($1, $2); } + ; + +XmlTableExprColOption: + IDENT b_expr + { $$ = makeDefElem($1, $2, @1); } + | DEFAULT b_expr + { $$ = makeDefElem("default", $2, @1); } + | NOT NULL_P + { $$ = makeDefElem("notnull", (Node *) makeInteger(false), @1); } + | NULL_P + { $$ = makeDefElem("notnull", (Node *) makeInteger(true), @1); } + ; + +XmlNamespaceList: XmlNamespace { $$ = list_make1($1); } + | XmlNamespaceList ',' XmlNamespace { $$ = lappend($1, $3); } + ; + +XmlNamespace: + b_expr AS ColLabel + { + NamedArgExpr *na = makeNode(NamedArgExpr); + na->name = $3; + na->arg = (Expr *) $1; + na->location = @1; + $$ = (Node *) na; + } + | DEFAULT b_expr + { + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("DEFAULT namespace is not supported"), + parser_errposition(@1))); + } ; /* @@ -13696,6 +13870,7 @@ unreserved_keyword: | CLASS | CLOSE | CLUSTER + | COLUMNS | COMMENT | COMMENTS | COMMIT @@ -13997,10 +14172,12 @@ col_name_keyword: | XMLELEMENT | XMLEXISTS | XMLFOREST + | XMLNAMESPACES | XMLPARSE | XMLPI | XMLROOT | XMLSERIALIZE + | XMLTABLE ; /* Type/function identifier --- keywords that can be type or function names. diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index d277fd6..06739f6 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -1095,9 +1095,9 @@ coerce_to_boolean(ParseState *pstate, Node *node, } /* - * coerce_to_specific_type() - * Coerce an argument of a construct that requires a specific data type. - * Also check that input is not a set. + * coerce_to_specific_type_typmod() + * Coerce an argument of a construct that requires a specific data type, + * with a specific typmod. Also check that input is not a set. * * Returns the possibly-transformed node tree. * @@ -1105,9 +1105,9 @@ coerce_to_boolean(ParseState *pstate, Node *node, * processing is wanted. */ Node * -coerce_to_specific_type(ParseState *pstate, Node *node, - Oid targetTypeId, - const char *constructName) +coerce_to_specific_type_typmod(ParseState *pstate, Node *node, + Oid targetTypeId, int32 targetTypmod, + const char *constructName) { Oid inputTypeId = exprType(node); @@ -1116,7 +1116,7 @@ coerce_to_specific_type(ParseState *pstate, Node *node, Node *newnode; newnode = coerce_to_target_type(pstate, node, inputTypeId, - targetTypeId, -1, + targetTypeId, targetTypmod, COERCION_ASSIGNMENT, COERCE_IMPLICIT_CAST, -1); @@ -1143,6 +1143,25 @@ coerce_to_specific_type(ParseState *pstate, Node *node, return node; } +/* + * coerce_to_specific_type() + * Coerce an argument of a construct that requires a specific data type. + * Also check that input is not a set. + * + * Returns the possibly-transformed node tree. + * + * As with coerce_type, pstate may be NULL if no special unknown-Param + * processing is wanted. + */ +Node * +coerce_to_specific_type(ParseState *pstate, Node *node, + Oid targetTypeId, + const char *constructName) +{ + return coerce_to_specific_type_typmod(pstate, node, + targetTypeId, -1, + constructName); +} /* * parser_coercion_errposition - report coercion error location, if possible diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 17d1cbf..42269a8 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -37,6 +37,7 @@ #include "utils/date.h" #include "utils/lsyscache.h" #include "utils/timestamp.h" +#include "utils/typcache.h" #include "utils/xml.h" @@ -122,6 +123,7 @@ static Node *transformIndirection(ParseState *pstate, Node *basenode, List *indirection); static Node *transformTypeCast(ParseState *pstate, TypeCast *tc); static Node *transformCollateClause(ParseState *pstate, CollateClause *c); +static Node *transformTableExpr(ParseState *pstate, TableExpr *te); static Node *make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location); static Node *make_row_distinct_op(ParseState *pstate, List *opname, @@ -376,6 +378,10 @@ transformExprRecurse(ParseState *pstate, Node *expr) break; } + case T_TableExpr: + result = transformTableExpr(pstate, (TableExpr *) expr); + break; + default: /* should not reach here */ elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr)); @@ -2765,6 +2771,172 @@ transformCollateClause(ParseState *pstate, CollateClause *c) } /* + * Transform a table expression. + * + * Transform the namespace clauses, the document-generating expression, the + * row-generating expression, the column-generating expressions, and the + * default value expressions. + */ +static Node * +transformTableExpr(ParseState *pstate, TableExpr *te) +{ + TableExpr *newte = makeNode(TableExpr); + const char *constructName; + Oid exprType; + + /* Currently only XMLTABLE is supported */ + constructName = "XMLTABLE"; + exprType = XMLOID; + + /* TableExpr is SRF, check target SRF usage */ + check_srf_call_placement(pstate, te->location); + + /* Transform the row-generating Xpath expression ... */ + Assert(te->row_expr != NULL); + newte->row_expr = coerce_to_specific_type(pstate, + transformExprRecurse(pstate, te->row_expr), + TEXTOID, + constructName); + /* ... and the XML itself */ + Assert(te->document_expr != NULL); + newte->document_expr = coerce_to_specific_type(pstate, + transformExprRecurse(pstate, te->document_expr), + exprType, + constructName); + + /* Columns, if any, also need to be transformed */ + if (te->columns != NIL) + { + ListCell *col; + List *transformlist = NIL; + bool for_ordinality = false; + char **colnames; + int i = 0; + + colnames = palloc(sizeof(char *) * list_length(te->columns)); + + /* + * For each TableExprRawCol in the input TableExpr, generate one + * TableExprColumn in the output TableExpr. + */ + foreach(col, te->columns) + { + TableExprRawCol *rawc = (TableExprRawCol *) lfirst(col); + TableExprColumn *newc; + int j; + + /* Only one FOR ORDINALITY column is allowed; verify */ + if (rawc->for_ordinality) + { + if (for_ordinality) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("only one FOR ORDINALITY column is allowed"), + parser_errposition(pstate, rawc->location))); + for_ordinality = true; + } + + newc = makeNode(TableExprColumn); + newc->colname = pstrdup(rawc->colname); + newc->for_ordinality = rawc->for_ordinality; + newc->is_not_null = rawc->is_not_null; + newc->location = rawc->location; + + /* + * Determine the type and typmod for the new column. FOR + * ORDINALITY columns are INTEGER per spec; the others are + * user-specified. + */ + if (rawc->for_ordinality) + { + newc->typid = INT4OID; + newc->typmod = -1; + } + else + typenameTypeIdAndMod(pstate, rawc->typeName, + &newc->typid, &newc->typmod); + + /* Transform the PATH and DEFAULT expressions */ + if (rawc->column_expr) + newc->column_expr = coerce_to_specific_type(pstate, + transformExprRecurse(pstate, rawc->column_expr), + TEXTOID, + constructName); + if (rawc->default_expr) + newc->default_expr = coerce_to_specific_type_typmod(pstate, + transformExprRecurse(pstate, rawc->default_expr), + newc->typid, newc->typmod, + constructName); + + + transformlist = lappend(transformlist, newc); + + /* make sure column names are unique */ + for (j = 0; j < i; j++) + if (strcmp(colnames[j], rawc->colname) == 0) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("column name \"%s\" is not unique", + rawc->colname), + parser_errposition(pstate, rawc->location))); + colnames[i] = rawc->colname; + + i++; + } + + newte->columns = transformlist; + pfree(colnames); + } + else + newte->columns = NIL; + + /* Namespaces, if any, also need to be transformed */ + if (te->namespaces != NIL) + { + List *transformlist = NIL; + ListCell *ns; + int nnames = 0; + char **nsnames; + + nsnames = palloc(sizeof(char *) * list_length(te->namespaces)); + + foreach(ns, te->namespaces) + { + Node *n = (Node *) lfirst(ns); + NamedArgExpr *na = (NamedArgExpr *) n; + int i; + + Assert(IsA(na, NamedArgExpr)); + + /* make sure namespace names are unique */ + for (i = 0; i < nnames; i++) + if (strcmp(nsnames[i], na->name) == 0) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("namespace name \"%s\" is not unique", + na->name), + parser_errposition(pstate, na->location))); + nsnames[nnames++] = na->name; + + na->arg = (Expr *) coerce_to_specific_type(pstate, + transformExprRecurse(pstate, (Node *) na->arg), + TEXTOID, + constructName); + + transformlist = lappend(transformlist, n); + } + newte->namespaces = transformlist; + pfree(nsnames); + } + else + newte->namespaces = NIL; + + newte->location = te->location; + + return (Node *) newte; +} + +/* * Transform a "row compare-op row" construct * * The inputs are lists of already-transformed expressions. diff --git a/src/backend/parser/parse_target.c b/src/backend/parser/parse_target.c index d440dec..d59d83f 100644 --- a/src/backend/parser/parse_target.c +++ b/src/backend/parser/parse_target.c @@ -1868,6 +1868,14 @@ FigureColnameInternal(Node *node, char **name) case T_XmlSerialize: *name = "xmlserialize"; return 2; + case T_TableExpr: + + /* + * Make TableExpr act like a regular function. Only XMLTABLE expr + * is supported in this moment. + */ + *name = "xmltable"; + return 2; default: break; } diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index fecee85..ef12407 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -50,6 +50,7 @@ #include "parser/parse_agg.h" #include "parser/parse_func.h" #include "parser/parse_oper.h" +#include "parser/parse_type.h" #include "parser/parser.h" #include "parser/parsetree.h" #include "rewrite/rewriteHandler.h" @@ -8306,6 +8307,105 @@ get_rule_expr(Node *node, deparse_context *context, } break; + case T_TableExpr: + { + TableExpr *te = (TableExpr *) node; + + /* + * Deparse TableExpr - the is only one TableExpr producer, the + * function XMLTABLE. + */ + + /* c_expr shoud be closed in brackets */ + appendStringInfoString(buf, "XMLTABLE("); + + if (te->namespaces != NIL) + { + ListCell *ns; + bool first = true; + + appendStringInfoString(buf, "XMLNAMESPACES("); + foreach(ns, te->namespaces) + { + Node *n = (Node *) lfirst(ns); + + if (!first) + appendStringInfoString(buf, ", "); + else + first = false; + + if (IsA(n, NamedArgExpr)) + { + NamedArgExpr *na = (NamedArgExpr *) n; + + get_rule_expr((Node *) na->arg, context, true); + appendStringInfo(buf, " AS %s", quote_identifier(na->name)); + } + else + { + appendStringInfoString(buf, "DEFAULT "); + get_rule_expr(n, context, true); + } + } + appendStringInfoChar(buf, ')'); + } + + appendStringInfoChar(buf, '('); + get_rule_expr((Node *) te->row_expr, context, true); + appendStringInfoString(buf, ") PASSING ("); + get_rule_expr((Node *) te->document_expr, context, true); + appendStringInfoChar(buf, ')'); + + if (te->columns != NIL) + { + ListCell *col; + bool first = true; + + appendStringInfoString(buf, " COLUMNS "); + + foreach(col, te->columns) + { + TableExprColumn *tec = (TableExprColumn *) lfirst(col); + + if (!first) + appendStringInfoString(buf, ", "); + else + first = false; + + appendStringInfoString(buf, quote_identifier(tec->colname)); + appendStringInfoChar(buf, ' '); + + if (!tec->for_ordinality) + { + appendStringInfoString(buf, + format_type_with_typemod(tec->typid, tec->typmod)); + + if (tec->default_expr != NULL) + { + appendStringInfoString(buf, " DEFAULT ("); + get_rule_expr((Node *) tec->default_expr, context, true); + appendStringInfoChar(buf, ')'); + } + if (tec->column_expr != NULL) + { + appendStringInfoString(buf, " PATH ("); + get_rule_expr((Node *) tec->column_expr, context, true); + appendStringInfoChar(buf, ')'); + } + if (tec->is_not_null) + appendStringInfoString(buf, " NOT NULL"); + } + else + { + appendStringInfoString(buf, "FOR ORDINALITY"); + } + } + } + + appendStringInfoChar(buf, ')'); + } + break; + default: elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node)); break; diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 057c3bf..b9e5f18 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -73,6 +73,7 @@ #include "commands/dbcommands.h" #include "executor/executor.h" #include "executor/spi.h" +#include "executor/tableexpr.h" #include "fmgr.h" #include "lib/stringinfo.h" #include "libpq/pqformat.h" @@ -165,6 +166,49 @@ static void SPI_sql_row_to_xmlelement(uint64 rownum, StringInfo result, char *tablename, bool nulls, bool tableforest, const char *targetns, bool top_level); +/* XMLTABLE support */ +#ifdef USE_LIBXML +/* random number to identify XmlTableContext */ +#define XMLTABLE_CONTEXT_MAGIC 46922182 +typedef struct XmlTableBuilderData +{ + int magic; + char *def_namespace_name; + long int row_count; + PgXmlErrorContext *xmlerrcxt; + xmlParserCtxtPtr ctxt; + xmlDocPtr doc; + xmlXPathContextPtr xpathcxt; + xmlXPathCompExprPtr xpathcomp; + xmlXPathObjectPtr xpathobj; + xmlXPathCompExprPtr *xpathscomp; +} XmlTableBuilderData; +#endif + +static void XmlTableInitializeBuilder(TableExprState *state); +static void XmlTableSetContent(TableExprState *state, Datum value); +static void XmlTableDeclareNamespace(TableExprState *state, char *name, + char *uri); +static void XmlTableSetRowFilter(TableExprState *state, char *path); +static void XmlTableSetColumnFilter(TableExprState *state, char *path, + int colnum); +static bool XmlTableFetchRow(TableExprState *state); +static Datum XmlTableGetValue(TableExprState *state, int colnum, + bool *isnull); +static void XmlTableDestroyBuilder(TableExprState *state); + +const TableExprRoutine XmlTableExprRoutine = +{ + XmlTableInitializeBuilder, + XmlTableSetContent, + XmlTableDeclareNamespace, + XmlTableSetRowFilter, + XmlTableSetColumnFilter, + XmlTableFetchRow, + XmlTableGetValue, + XmlTableDestroyBuilder +}; + #define NO_XML_SUPPORT() \ ereport(ERROR, \ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \ @@ -4065,3 +4109,585 @@ xml_is_well_formed_content(PG_FUNCTION_ARGS) return 0; #endif /* not USE_LIBXML */ } + +/* + * support functions for XMLTABLE function + * + */ +#ifdef USE_LIBXML + +/* + * Functions for XmlTableExprRoutine + * + */ + +/* + * Convert XML node to cstring (dump subtree in case of element, + * return value otherwise) + */ +static char * +xml_xmlnodetostr(xmlNodePtr cur, PgXmlErrorContext *xmlerrcxt) +{ + char *result; + + if (cur->type == XML_ELEMENT_NODE) + { + xmlBufferPtr buf; + xmlNodePtr cur_copy; + + buf = xmlBufferCreate(); + + /* + * The result of xmlNodeDump() won't contain namespace definitions + * from parent nodes, but xmlCopyNode() duplicates a node along with + * its required namespace definitions. + */ + cur_copy = xmlCopyNode(cur, 1); + + if (cur_copy == NULL) + xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY, + "could not copy node"); + + PG_TRY(); + { + xmlNodeDump(buf, NULL, cur_copy, 0, 1); + result = pstrdup((const char *) xmlBufferContent(buf)); + } + PG_CATCH(); + { + xmlFreeNode(cur_copy); + xmlBufferFree(buf); + PG_RE_THROW(); + } + PG_END_TRY(); + xmlFreeNode(cur_copy); + xmlBufferFree(buf); + } + else + { + xmlChar *str; + + str = xmlXPathCastNodeToString(cur); + PG_TRY(); + { + /* Here we rely on XML having the same representation as TEXT */ + char *escaped = escape_xml((char *) str); + + result = escaped; + } + PG_CATCH(); + { + xmlFree(str); + PG_RE_THROW(); + } + PG_END_TRY(); + xmlFree(str); + } + + return result; +} + +/* + * Secure cstring for usage in libxml2 + */ +static xmlChar * +to_xmlstr(char *str, size_t len) +{ + xmlChar *result; + + result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar)); + memcpy(result, str, len); + result[len] = '\0'; + + return result; +} +#endif + +/* + * Fill in XmlTableBuilderData for XmlTable builder. Initialize XML parser. + */ +static void +XmlTableInitializeBuilder(TableExprState *state) +{ +#ifdef USE_LIBXML + volatile xmlParserCtxtPtr ctxt = NULL; + XmlTableBuilderData *xtCxt; + PgXmlErrorContext *xmlerrcxt; + + xtCxt = palloc0(sizeof(XmlTableBuilderData)); + xtCxt->magic = XMLTABLE_CONTEXT_MAGIC; + xtCxt->xpathscomp = palloc0(sizeof(xmlXPathCompExprPtr) * + state->resultSlot->tts_tupleDescriptor->natts); + + xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL); + + PG_TRY(); + { + xmlInitParser(); + + ctxt = xmlNewParserCtxt(); + if (ctxt == NULL || xmlerrcxt->err_occurred) + xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY, + "could not allocate parser context"); + } + PG_CATCH(); + { + if (ctxt != NULL) + xmlFreeParserCtxt(ctxt); + + pg_xml_done(xmlerrcxt, true); + + PG_RE_THROW(); + } + PG_END_TRY(); + + xtCxt->xmlerrcxt = xmlerrcxt; + xtCxt->ctxt = ctxt; + + state->opaque = xtCxt; +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * XmlTableSetContent + */ +static void +XmlTableSetContent(TableExprState *state, Datum value) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + xmltype *xmlval = DatumGetXmlP(value); + xmlChar *xstr; + int length; + volatile xmlDocPtr doc = NULL; + volatile xmlXPathContextPtr xpathcxt = NULL; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableSetContent called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableSetContent called with invalid TableExprState"); + + length = VARSIZE(xmlval) - VARHDRSZ; + xstr = to_xmlstr(VARDATA(xmlval), length); + + PG_TRY(); + { + doc = xmlCtxtReadMemory(xtCxt->ctxt, (char *) xstr, length, NULL, NULL, 0); + if (doc == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INVALID_XML_DOCUMENT, + "could not parse XML document"); + xpathcxt = xmlXPathNewContext(doc); + if (xpathcxt == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY, + "could not allocate XPath context"); + xpathcxt->node = xmlDocGetRootElement(doc); + if (xpathcxt->node == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR, + "could not find root XML element"); + } + PG_CATCH(); + { + if (xpathcxt != NULL) + xmlXPathFreeContext(xpathcxt); + if (doc != NULL) + xmlFreeDoc(doc); + + PG_RE_THROW(); + } + PG_END_TRY(); + + xtCxt->doc = doc; + xtCxt->xpathcxt = xpathcxt; +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * XmlTableDeclareNamespace. Add a namespace declaration. When name is NULL + * (the default namespace is being declared), store a fake namespace. + */ +static void +XmlTableDeclareNamespace(TableExprState *state, char *name, char *uri) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableDeclareNamespace called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableDeclareNamespace called with invalid TableExprState"); + + if (xmlXPathRegisterNs(xtCxt->xpathcxt, + to_xmlstr(name, strlen(name)), + to_xmlstr(uri, strlen(uri)))) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION, + "could not set XML namespace"); +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * XmlTableSetRowFilter + */ +static void +XmlTableSetRowFilter(TableExprState *state, char *path) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + xmlChar *xstr; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableSetRowFilter called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableSetRowFilter called with invalid TableExprState"); + + if (path == '\0') + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("row path filter must not be empty string"))); + + xstr = to_xmlstr(path, strlen(path)); + + xtCxt->xpathcomp = xmlXPathCompile(xstr); + if (xtCxt->xpathcomp == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION, + "invalid XPath expression"); +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * XmlTableSetColumnFilter + */ +static void +XmlTableSetColumnFilter(TableExprState *state, char *path, int colnum) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + xmlChar *xstr; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableSetColumnFilter called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableSetColumnFilter called with invalid TableExprState"); + + if (path == '\0') + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("column path filter must not be empty string"))); + + xstr = to_xmlstr(path, strlen(path)); + + xtCxt->xpathscomp[colnum] = xmlXPathCompile(xstr); + if (xtCxt->xpathscomp[colnum] == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_DATA_EXCEPTION, + "invalid XPath expression"); +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * XmlTableFetchRow - returns true when is possible to read + * values from XML document. + * + * First call evaluates row path over content. + * Next step and at other calls increment row counter and + * compare it with number values in result set. + */ +static bool +XmlTableFetchRow(TableExprState *state) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableFetchRow called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableFetchRow called with invalid TableExprState"); + + /* + * XmlTable returns table - set of composite values. The error context, is + * used for producement more values, between two calls, there can be + * created and used another libxml2 error context. It is libxml2 global + * value, so it should be refreshed any time before any libxml2 usage, + * that is finished by returning some value. + */ + xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler); + + if (xtCxt->xpathobj == NULL) + { + xtCxt->xpathobj = xmlXPathCompiledEval(xtCxt->xpathcomp, xtCxt->xpathcxt); + if (xtCxt->xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR, + "could not create XPath object"); + + xtCxt->row_count = 0; + } + + if (xtCxt->xpathobj->type == XPATH_NODESET) + { + if (xtCxt->xpathobj->nodesetval != NULL) + { + if (xtCxt->row_count++ < xtCxt->xpathobj->nodesetval->nodeNr) + return true; + } + } + + return false; +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ + + return false; +} + +/* + * Apply column path on current node and returns result. + * Allows multivalue when expected type is XML. + * When COLUMNS part was not entered, returns current node. + */ +static Datum +XmlTableGetValue(TableExprState *state, int colnum, bool *isnull) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt; + Datum result = (Datum) 0; + xmlNodePtr cur; + char *cstr = NULL; + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableSetContent called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableGetValue called with invalid TableExprState"); + + Assert(xtCxt->xpathobj && xtCxt->xpathobj->type == XPATH_NODESET && + xtCxt->xpathobj->nodesetval != NULL); + + /* Propagate context related error context to libxml2 */ + xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler); + + cur = xtCxt->xpathobj->nodesetval->nodeTab[xtCxt->row_count - 1]; + + if (xtCxt->xpathscomp[colnum] != NULL) + { + volatile xmlXPathObjectPtr column_xpathobj = NULL; + + /* fast leaving */ + if (cur->type != XML_ELEMENT_NODE) + elog(ERROR, "unexpected xmlNode type"); + + PG_TRY(); + { + Form_pg_attribute attr = state->resultSlot->tts_tupleDescriptor->attrs[colnum]; + + /* Set current node as entry point for XPath evaluation */ + xmlXPathSetContextNode(cur, xtCxt->xpathcxt); + + /* Evaluate column path */ + column_xpathobj = xmlXPathCompiledEval(xtCxt->xpathscomp[colnum], xtCxt->xpathcxt); + if (column_xpathobj == NULL || xtCxt->xmlerrcxt->err_occurred) + xml_ereport(xtCxt->xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR, + "could not create XPath object"); + + if (column_xpathobj->type == XPATH_NODESET) + { + int count; + + if (column_xpathobj->nodesetval != NULL) + count = column_xpathobj->nodesetval->nodeNr; + else + count = 0; + + if (count > 0) + { + Oid targettypid = attr->atttypid; + + if (count == 1) + { + if (targettypid != XMLOID) + { + xmlChar *str; + + str = xmlNodeListGetString(xtCxt->doc, + column_xpathobj->nodesetval->nodeTab[0]->xmlChildrenNode, + 1); + + if (str != NULL) + { + PG_TRY(); + { + /* + * Copy string to PostgreSQL controlled + * memory + */ + cstr = pstrdup((char *) str); + } + PG_CATCH(); + { + xmlFree(str); + PG_RE_THROW(); + } + PG_END_TRY(); + + xmlFree(str); + } + else + { + /* Return empty string when tag is empty */ + cstr = pstrdup(""); + } + } + else + { + /* simple case, result is one value */ + cstr = xml_xmlnodetostr(column_xpathobj->nodesetval->nodeTab[0], + xtCxt->xmlerrcxt); + } + } + else + { + StringInfoData str; + int i; + + /* + * When result is not one value, then we can work with + * concated values. But it requires XML as expected + * type. + */ + if (targettypid != XMLOID) + ereport(ERROR, + (errcode(ERRCODE_CARDINALITY_VIOLATION), + errmsg("more than one value returned by column XPath expression"))); + + /* Concate serialized values */ + initStringInfo(&str); + for (i = 0; i < count; i++) + { + appendStringInfoString(&str, + xml_xmlnodetostr(column_xpathobj->nodesetval->nodeTab[i], + xtCxt->xmlerrcxt)); + } + cstr = str.data; + } + + result = InputFunctionCall(&state->in_functions[colnum], + cstr, + state->typioparams[colnum], + attr->atttypmod); + *isnull = false; + } + else + *isnull = true; + } + else if (column_xpathobj->type == XPATH_STRING) + { + result = InputFunctionCall(&state->in_functions[colnum], + (char *) column_xpathobj->stringval, + state->typioparams[colnum], + attr->atttypmod); + *isnull = false; + } + else + elog(ERROR, "unexpected XPath object type"); + + xmlXPathFreeObject(column_xpathobj); + column_xpathobj = NULL; + } + PG_CATCH(); + { + if (column_xpathobj != NULL) + xmlXPathFreeObject(column_xpathobj); + PG_RE_THROW(); + } + PG_END_TRY(); + } + else + { + /* + * xtCxt->xpathscomp[ncol] is NULL when COLUMNS is empty. Result is a + * serialized current node. + */ + cstr = xml_xmlnodetostr(cur, xtCxt->xmlerrcxt); + result = InputFunctionCall(&state->in_functions[0], + cstr, + state->typioparams[0], + -1); /* target type is XML always */ + *isnull = false; + } + + if (cstr != NULL) + pfree(cstr); + + return result; +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} + +/* + * Release all libxml2 memory and related resources + */ +static void +XmlTableDestroyBuilder(TableExprState *state) +{ +#ifdef USE_LIBXML + XmlTableBuilderData *xtCxt = (XmlTableBuilderData *) state->opaque; + + /* Propagate context related error context to libxml2 */ + xmlSetStructuredErrorFunc((void *) xtCxt->xmlerrcxt, xml_errorHandler); + + /* Defend against someone passing us a bogus context struct */ + if (!IsA(state, TableExprState)) + elog(ERROR, "XmlTableDestroyBuilder called with invalid TableExprState"); + xtCxt = (XmlTableBuilderData *) state->opaque; + if (xtCxt->magic != XMLTABLE_CONTEXT_MAGIC) + elog(ERROR, "XmlTableDestroyBuilder called with invalid TableExprState"); + + if (xtCxt->xpathscomp != NULL) + { + int i; + + for (i = 0; i < state->resultSlot->tts_tupleDescriptor->natts; i++) + if (xtCxt->xpathscomp[i] != NULL) + xmlXPathFreeCompExpr(xtCxt->xpathscomp[i]); + } + + if (xtCxt->xpathobj != NULL) + xmlXPathFreeObject(xtCxt->xpathobj); + if (xtCxt->xpathcomp != NULL) + xmlXPathFreeCompExpr(xtCxt->xpathcomp); + if (xtCxt->xpathcxt != NULL) + xmlXPathFreeContext(xtCxt->xpathcxt); + if (xtCxt->doc != NULL) + xmlFreeDoc(xtCxt->doc); + if (xtCxt->ctxt != NULL) + xmlFreeParserCtxt(xtCxt->ctxt); + + pg_xml_done(xtCxt->xmlerrcxt, true); + + /* not valid anymore */ + xtCxt->magic = 0; + state->opaque = NULL; + +#else + NO_XML_SUPPORT(); +#endif /* not USE_LIBXML */ +} diff --git a/src/backend/utils/fmgr/funcapi.c b/src/backend/utils/fmgr/funcapi.c index 5d49fe5..979f3f8 100644 --- a/src/backend/utils/fmgr/funcapi.c +++ b/src/backend/utils/fmgr/funcapi.c @@ -241,6 +241,19 @@ get_expr_result_type(Node *expr, NULL, resultTypeId, resultTupleDesc); + else if (expr && IsA(expr, TableExpr)) + { + Oid typid = exprType(expr); + int32 typmod = exprTypmod(expr); + + if (resultTypeId) + *resultTypeId = typid; + + if (resultTupleDesc) + *resultTupleDesc = lookup_rowtype_tupdesc_copy(typid, typmod); + + return TYPEFUNC_COMPOSITE; + } else { /* handle as a generic expression; no chance to resolve RECORD */ diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 136276b..c6d69bc 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -272,6 +272,7 @@ extern TupleTableSlot *ExecInitNullTupleSlot(EState *estate, extern TupleDesc ExecTypeFromTL(List *targetList, bool hasoid); extern TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid); extern TupleDesc ExecTypeFromExprList(List *exprList); +extern TupleDesc ExecTypeFromTableExpr(const TableExpr *te); extern void ExecTypeSetColNames(TupleDesc typeInfo, List *namesList); extern void UpdateChangedParamSet(PlanState *node, Bitmapset *newchg); diff --git a/src/include/executor/tableexpr.h b/src/include/executor/tableexpr.h new file mode 100644 index 0000000..34f5f29 --- /dev/null +++ b/src/include/executor/tableexpr.h @@ -0,0 +1,68 @@ +/*------------------------------------------------------------------------- + * + * tableexpr.h + * interface for TableExpr builder + * + * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/executor/tableexpr.h + * + *------------------------------------------------------------------------- + */ +#ifndef TABLEEXPR_H +#define TABLEEXPR_H + +#include "access/tupdesc.h" +#include "fmgr.h" + +/* + * TableExprRoutine holds function pointers used for generating content of + * table-expression functions, such as XMLTABLE. + * + * CreateContext creates the table builder context. The output tuple + * descriptor, input functions for the columns, and typioparams are passed as + * created by the executor scaffold. The memory context being passed is an + * IMO bogus thing that shall be fixed by Pavel. + * + * SetContent is called to define the input document. The table builder may + * apply additional transformations not exposed outside the table builder + * context. + * + * DeclareNamespace is called to pass namespace declarations from the table + * expression. This function may be NULL if namespaces are not supported by + * the table builder. Namespaces must be given before setting the row and + * column filters. If the name is given as NULL, the entry shall be for the + * default namespace. + * + * SetRowFilter is called do define the row-generating filter, which shall be + * used to extract each row from the input document. + * + * SetColumnFilter is called once for each column, to define the column- + * generating filter for the given column. + * + * FetchRow shall be called repeatedly until it returns that no more rows are + * found in the document. On each invocation it shall set state in the table + * builder context such that each subsequent GetValue call returns the value + * for the indicated column for the row being processed. + * + * DestroyContext shall release all resources associated with a table builder + * context. It may be called either because all rows have been consumed, or + * because an error ocurred while processing the table expression. + */ +typedef struct TableExprRoutine +{ + void (*InitializeBuilder) (TableExprState *state); + void (*SetContent) (TableExprState *state, Datum value); + void (*DeclareNamespace) (TableExprState *state, char *name, + char *uri); + void (*SetRowFilter) (TableExprState *state, char *path); + void (*SetColumnFilter) (TableExprState *state, char *path, + int colnum); + bool (*FetchRow) (TableExprState *state); + Datum (*GetValue) (TableExprState *state, int colnum, + bool *isnull); + void (*DestroyBuilder) (TableExprState *state); +} TableExprRoutine; + +#endif /* TABLEEXPR_H */ diff --git a/src/include/funcapi.h b/src/include/funcapi.h index e73a824..e350316 100644 --- a/src/include/funcapi.h +++ b/src/include/funcapi.h @@ -184,7 +184,6 @@ extern TupleDesc build_function_result_tupdesc_d(Datum proallargtypes, Datum proargnames); extern TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple); - /*---------- * Support to ease writing functions returning composite types * diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 8004d85..a1ff800 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -1011,6 +1011,30 @@ typedef struct DomainConstraintState ExprState *check_expr; /* for CHECK, a boolean expression */ } DomainConstraintState; +/* ---------------- + * TableExprState node + * + * Used in table-expression functions like XMLTABLE. + * ---------------- + */ +typedef struct TableExprState +{ + ExprState xprstate; + struct TableExprRoutine *routine; /* table builder methods */ + MemoryContext buildercxt; /* memory context used by builder */ + void *opaque; /* content builder private space */ + TupleTableSlot *resultSlot; /* output tuple table slot */ + List *namespaces; /* list of prepared ResTarget fields */ + FmgrInfo *in_functions; /* input function for each column */ + Oid *typioparams; /* typioparam for each column */ + AttrNumber for_ordinality_col; /* FOR ORDINALITY column */ + int rownum; /* row number to be output next */ + ExprState *doc_expr; /* state for document expression */ + ExprState *row_expr; /* state for row-generating expression */ + ExprState **col_expr; /* state for column-generating expression */ + ExprState **def_expr; /* state for column default values */ + bool *not_null; /* nullability flag for each column */ +} TableExprState; /* ---------------------------------------------------------------- * Executor State Trees diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index cb9307c..b6e5151 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -181,6 +181,8 @@ typedef enum NodeTag T_FromExpr, T_OnConflictExpr, T_IntoClause, + T_TableExpr, + T_TableExprColumn, /* * TAGS FOR EXPRESSION STATE NODES (execnodes.h) @@ -216,6 +218,7 @@ typedef enum NodeTag T_NullTestState, T_CoerceToDomainState, T_DomainConstraintState, + T_TableExprState, /* * TAGS FOR PLANNER NODES (relation.h) @@ -454,6 +457,7 @@ typedef enum NodeTag T_CommonTableExpr, T_RoleSpec, T_TriggerTransition, + T_TableExprRawCol, /* * TAGS FOR REPLICATION GRAMMAR PARSE NODES (replnodes.h) diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 04b1c2f..bf42692 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -699,6 +699,23 @@ typedef struct XmlSerialize int location; /* token location, or -1 if unknown */ } XmlSerialize; +/* + * TableExprRawCol - one column in a TableExpr column list, in raw form. + * + * If for_ordinality is true (FOR ORDINALITY), then the column is an int4 + * column and the rest of the fields are ignored. + */ +typedef struct TableExprRawCol +{ + NodeTag type; + char *colname; /* name of generated column */ + bool for_ordinality; /* whether this is FOR ORDINALITY */ + TypeName *typeName; /* type of generated column */ + bool is_not_null; /* nullability flag */ + Node *column_expr; /* column filter expression */ + Node *default_expr; /* column default value expr */ + int location; /* token location, or -1 if unknown */ +} TableExprRawCol; /**************************************************************************** * Nodes for a Query tree diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 65510b0..9e149e2 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1467,4 +1467,37 @@ typedef struct OnConflictExpr List *exclRelTlist; /* tlist of the EXCLUDED pseudo relation */ } OnConflictExpr; +/*---------- + * TableExpr - for table expressions, such as XMLTABLE. + *---------- + */ +typedef struct TableExpr +{ + NodeTag type; + List *namespaces; /* list of namespaces */ + Node *document_expr; /* processed data */ + Node *row_expr; /* row filter expression */ + List *columns; /* columns definitions */ + int location; /* token location, or -1 if unknown */ +} TableExpr; + +/*---------- + * TableExprColumn - one column in a table expression. + * Raw form of this is TableExprRawCol. + *---------- + */ +typedef struct TableExprColumn +{ + NodeTag type; + char *colname; /* name of generated column */ + bool for_ordinality; /* whether this is FOR ORDINALITY */ + Oid typid; /* type of generated column */ + int32 typmod; /* typmod of generated column */ + Oid collation; /* column collation */ + bool is_not_null; /* nullability flag */ + Node *column_expr; /* column filter expression */ + Node *default_expr; /* column default value expr */ + int location; /* token location, or -1 if unknown */ +} TableExprColumn; + #endif /* PRIMNODES_H */ diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index 77d873b..de4b8b8 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -81,6 +81,7 @@ PG_KEYWORD("coalesce", COALESCE, COL_NAME_KEYWORD) PG_KEYWORD("collate", COLLATE, RESERVED_KEYWORD) PG_KEYWORD("collation", COLLATION, TYPE_FUNC_NAME_KEYWORD) PG_KEYWORD("column", COLUMN, RESERVED_KEYWORD) +PG_KEYWORD("columns", COLUMNS, UNRESERVED_KEYWORD) PG_KEYWORD("comment", COMMENT, UNRESERVED_KEYWORD) PG_KEYWORD("comments", COMMENTS, UNRESERVED_KEYWORD) PG_KEYWORD("commit", COMMIT, UNRESERVED_KEYWORD) @@ -441,10 +442,12 @@ PG_KEYWORD("xmlconcat", XMLCONCAT, COL_NAME_KEYWORD) PG_KEYWORD("xmlelement", XMLELEMENT, COL_NAME_KEYWORD) PG_KEYWORD("xmlexists", XMLEXISTS, COL_NAME_KEYWORD) PG_KEYWORD("xmlforest", XMLFOREST, COL_NAME_KEYWORD) +PG_KEYWORD("xmlnamespaces", XMLNAMESPACES, COL_NAME_KEYWORD) PG_KEYWORD("xmlparse", XMLPARSE, COL_NAME_KEYWORD) PG_KEYWORD("xmlpi", XMLPI, COL_NAME_KEYWORD) PG_KEYWORD("xmlroot", XMLROOT, COL_NAME_KEYWORD) PG_KEYWORD("xmlserialize", XMLSERIALIZE, COL_NAME_KEYWORD) +PG_KEYWORD("xmltable", XMLTABLE, COL_NAME_KEYWORD) PG_KEYWORD("year", YEAR_P, UNRESERVED_KEYWORD) PG_KEYWORD("yes", YES_P, UNRESERVED_KEYWORD) PG_KEYWORD("zone", ZONE, UNRESERVED_KEYWORD) diff --git a/src/include/parser/parse_coerce.h b/src/include/parser/parse_coerce.h index 66519e6..03502f8 100644 --- a/src/include/parser/parse_coerce.h +++ b/src/include/parser/parse_coerce.h @@ -58,6 +58,10 @@ extern Node *coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName); +extern Node *coerce_to_specific_type_typmod(ParseState *pstate, Node *node, + Oid targetTypeId, int32 targetTypmod, + const char *constructName); + extern int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr); diff --git a/src/include/utils/xml.h b/src/include/utils/xml.h index 2eab8a5..4ed5297 100644 --- a/src/include/utils/xml.h +++ b/src/include/utils/xml.h @@ -18,6 +18,7 @@ #include "fmgr.h" #include "nodes/execnodes.h" #include "nodes/primnodes.h" +#include "executor/tableexpr.h" typedef struct varlena xmltype; @@ -109,4 +110,6 @@ extern int xmlbinary; /* XmlBinaryType, but int for guc enum */ extern int xmloption; /* XmlOptionType, but int for guc enum */ +extern const TableExprRoutine XmlTableExprRoutine; + #endif /* XML_H */ diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out index f21e119..1adc2ad 100644 --- a/src/test/regress/expected/xml.out +++ b/src/test/regress/expected/xml.out @@ -948,3 +948,415 @@ SELECT XMLPARSE(DOCUMENT '  (1 row) +-- XMLPATH tests +CREATE TABLE xmldata(data xml); +INSERT INTO xmldata VALUES(' + + AU + Australia + 3 + + + CN + China + 3 + + + HK + HongKong + 3 + + + IN + India + 3 + + + JP + Japan + 3Sinzo Abe + + + SG + Singapore + 3791 + +'); +-- XMLTABLE without columns +SELECT * FROM XMLTABLE('/rows/row' PASSING '1020'); + xmltable +------------- + + + 10+ + 20+ + +(1 row) + +SELECT XMLTABLE('/rows/row' PASSING '1020'); + xmltable +------------- + (" + + 10+ + 20+ + ") +(1 row) + +-- XMLTABLE with columns +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +CREATE VIEW xmltableview1 AS SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); +SELECT * FROM xmltableview1; + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +\sv xmltableview1 +CREATE OR REPLACE VIEW public.xmltableview1 AS + SELECT "xmltable".id, + "xmltable"._id, + "xmltable".country_name, + "xmltable".country_id, + "xmltable".region_id, + "xmltable".size, + "xmltable".unit, + "xmltable".premier_name + FROM ( SELECT xmldata.data + FROM xmldata) x, + LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (x.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) "xmltable"(id, _id, country_name, country_id, region_id, size, unit, premier_name) +EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------- + Nested Loop + -> Seq Scan on xmldata + -> Function Scan on "xmltable" +(3 rows) + +EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Nested Loop + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + -> Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +-- XMLNAMESPACES tests +SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); + a +---- + 10 +(1 row) + +CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); +SELECT * FROM xmltableview2; + a +---- + 10 +(1 row) + +SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + '/rows/row' + PASSING '10' + COLUMNS a int PATH 'a'); +ERROR: DEFAULT namespace is not supported +LINE 1: SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + ^ +SELECT * FROM XMLTABLE('/rows/row/a/text()' PASSING '1020'); + xmltable +---------- + 10 + 20 +(2 rows) + +-- used in prepare statements +PREPARE pp AS +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); +EXECUTE pp; + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +SELECT xmltable('/ROWS/ROW' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + AU + + Australia + + 3 + + ") + (" + + CN + + China + + 3 + + ") + (" + + HK + + HongKong + + 3 + + ") + (" + + IN + + India + + 3 + + ") + (" + + JP + + Japan + + 3Sinzo Abe+ + ") + (" + + SG + + Singapore + + 3791 + + ") +(6 rows) + +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan"]' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + JP + + Japan + + 3Sinzo Abe+ + ") +(1 row) + +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + IN + + India + + 3 + + ") + (" + + JP + + Japan + + 3Sinzo Abe+ + ") +(2 rows) + +SELECT (xmltable).* FROM (SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int) FROM xmldata) s; + COUNTRY_NAME | REGION_ID +--------------+----------- + India | 3 + Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata,LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int); + COUNTRY_NAME | REGION_ID +--------------+----------- + India | 3 + Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY, "COUNTRY_NAME" text, "REGION_ID" int); + id | COUNTRY_NAME | REGION_ID +----+--------------+----------- + 1 | India | 3 + 2 | Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int); + id | COUNTRY_NAME | REGION_ID +----+--------------+----------- + 4 | India | 3 + 5 | Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id'); + id +---- + 4 + 5 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY); + id +---- + 1 + 2 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH '.'); + id | COUNTRY_NAME | REGION_ID | rawdata +----+--------------+-----------+------------------------------------------------------------------ + 4 | India | 3 | + + | | | IN + + | | | India + + | | | 3 + + | | | + 5 | Japan | 3 | + + | | | JP + + | | | Japan + + | | | 3Sinzo Abe+ + | | | +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH './*'); + id | COUNTRY_NAME | REGION_ID | rawdata +----+--------------+-----------+----------------------------------------------------------------------------------------------------------------------------- + 4 | India | 3 | INIndia3 + 5 | Japan | 3 | JPJapan3Sinzo Abe +(2 rows) + +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text); + element +------------------- + a1aa2a bbbbcccc +(1 row) + +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail +ERROR: more than one value returned by column XPath expression +-- CDATA test +select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text); + c +------------------------- + &"<>!foo + 2 +(2 rows) + +-- XML builtin entities +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent text); + ent +----- + ' + " + & + < + > +(5 rows) + +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent xml); + ent +------------------ + ' + " + & + < + > +(5 rows) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Nested Loop + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + -> Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +EXPLAIN (VERBOSE, COSTS OFF) SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata; + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(2 rows) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT (xmltable).* FROM + (SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata) s; + QUERY PLAN +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Subquery Scan on s + Output: (s."xmltable").id, (s."xmltable")._id, (s."xmltable").country_name, (s."xmltable").country_id, (s."xmltable").region_id, (s."xmltable").size, (s."xmltable").unit, (s."xmltable").premier_name + -> Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(4 rows) + diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out index d702703..0b720a7 100644 --- a/src/test/regress/expected/xml_1.out +++ b/src/test/regress/expected/xml_1.out @@ -827,3 +827,324 @@ SELECT XMLPARSE(DOCUMENT ' + + AU + Australia + 3 + + + CN + China + 3 + + + HK + HongKong + 3 + + + IN + India + 3 + + + JP + Japan + 3Sinzo Abe + + + SG + Singapore + 3791 + +'); +ERROR: unsupported XML feature +LINE 1: INSERT INTO xmldata VALUES(' + ^ +DETAIL: This functionality requires the server to be built with libxml support. +HINT: You need to rebuild PostgreSQL using --with-libxml. +-- XMLTABLE without columns +SELECT * FROM XMLTABLE('/rows/row' PASSING '1020'); +ERROR: unsupported XML feature +LINE 1: SELECT * FROM XMLTABLE('/rows/row' PASSING '10... + ^ +DETAIL: This functionality requires the server to be built with libxml support. +HINT: You need to rebuild PostgreSQL using --with-libxml. +SELECT XMLTABLE('/rows/row' PASSING '1020'); +ERROR: unsupported XML feature +LINE 1: SELECT XMLTABLE('/rows/row' PASSING '10... + ^ +DETAIL: This functionality requires the server to be built with libxml support. +HINT: You need to rebuild PostgreSQL using --with-libxml. +-- XMLTABLE with columns +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+-------------- +(0 rows) + +CREATE VIEW xmltableview1 AS SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); +SELECT * FROM xmltableview1; + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+-------------- +(0 rows) + +\sv xmltableview1 +CREATE OR REPLACE VIEW public.xmltableview1 AS + SELECT "xmltable".id, + "xmltable"._id, + "xmltable".country_name, + "xmltable".country_id, + "xmltable".region_id, + "xmltable".size, + "xmltable".unit, + "xmltable".premier_name + FROM ( SELECT xmldata.data + FROM xmldata) x, + LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (x.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) "xmltable"(id, _id, country_name, country_id, region_id, size, unit, premier_name) +EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------- + Nested Loop + -> Seq Scan on xmldata + -> Function Scan on "xmltable" +(3 rows) + +EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Nested Loop + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + -> Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +-- XMLNAMESPACES tests +SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); +ERROR: unsupported XML feature +LINE 3: PASSING '10' + COLUMNS a int PATH 'zz:a'); +ERROR: unsupported XML feature +LINE 3: PASSING '10' + COLUMNS a int PATH 'a'); +ERROR: DEFAULT namespace is not supported +LINE 1: SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + ^ +SELECT * FROM XMLTABLE('/rows/row/a/text()' PASSING '1020'); +ERROR: unsupported XML feature +LINE 1: ...LECT * FROM XMLTABLE('/rows/row/a/text()' PASSING 'a1aa2a bbbbxxxcccc' COLUMNS element text); +ERROR: unsupported XML feature +LINE 1: SELECT * FROM xmltable('/root' passing 'a1aa1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail +ERROR: unsupported XML feature +LINE 1: SELECT * FROM xmltable('/root' passing 'a1a &"<>!foo]]>2' columns c text); +ERROR: unsupported XML feature +LINE 1: select * from xmltable('r' passing ''"&<>' COLUMNS ent text); +ERROR: unsupported XML feature +LINE 1: SELECT * FROM xmltable('/x/a' PASSING '''"&<>' COLUMNS ent xml); +ERROR: unsupported XML feature +LINE 1: SELECT * FROM xmltable('/x/a' PASSING '' Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +EXPLAIN (VERBOSE, COSTS OFF) SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata; + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(2 rows) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT (xmltable).* FROM + (SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata) s; + QUERY PLAN +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Subquery Scan on s + Output: (s."xmltable").id, (s."xmltable")._id, (s."xmltable").country_name, (s."xmltable").country_id, (s."xmltable").region_id, (s."xmltable").size, (s."xmltable").unit, (s."xmltable").premier_name + -> Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(4 rows) + diff --git a/src/test/regress/expected/xml_2.out b/src/test/regress/expected/xml_2.out index 530faf5..2abe7e4 100644 --- a/src/test/regress/expected/xml_2.out +++ b/src/test/regress/expected/xml_2.out @@ -928,3 +928,415 @@ SELECT XMLPARSE(DOCUMENT '  (1 row) +-- XMLPATH tests +CREATE TABLE xmldata(data xml); +INSERT INTO xmldata VALUES(' + + AU + Australia + 3 + + + CN + China + 3 + + + HK + HongKong + 3 + + + IN + India + 3 + + + JP + Japan + 3Sinzo Abe + + + SG + Singapore + 3791 + +'); +-- XMLTABLE without columns +SELECT * FROM XMLTABLE('/rows/row' PASSING '1020'); + xmltable +------------- + + + 10+ + 20+ + +(1 row) + +SELECT XMLTABLE('/rows/row' PASSING '1020'); + xmltable +------------- + (" + + 10+ + 20+ + ") +(1 row) + +-- XMLTABLE with columns +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +CREATE VIEW xmltableview1 AS SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); +SELECT * FROM xmltableview1; + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +\sv xmltableview1 +CREATE OR REPLACE VIEW public.xmltableview1 AS + SELECT "xmltable".id, + "xmltable"._id, + "xmltable".country_name, + "xmltable".country_id, + "xmltable".region_id, + "xmltable".size, + "xmltable".unit, + "xmltable".premier_name + FROM ( SELECT xmldata.data + FROM xmldata) x, + LATERAL XMLTABLE(('/ROWS/ROW'::text) PASSING (x.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) "xmltable"(id, _id, country_name, country_id, region_id, size, unit, premier_name) +EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------- + Nested Loop + -> Seq Scan on xmldata + -> Function Scan on "xmltable" +(3 rows) + +EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1; + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Nested Loop + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + -> Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +-- XMLNAMESPACES tests +SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); + a +---- + 10 +(1 row) + +CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); +SELECT * FROM xmltableview2; + a +---- + 10 +(1 row) + +SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + '/rows/row' + PASSING '10' + COLUMNS a int PATH 'a'); +ERROR: DEFAULT namespace is not supported +LINE 1: SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + ^ +SELECT * FROM XMLTABLE('/rows/row/a/text()' PASSING '1020'); + xmltable +---------- + 10 + 20 +(2 rows) + +-- used in prepare statements +PREPARE pp AS +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); +EXECUTE pp; + id | _id | country_name | country_id | region_id | size | unit | premier_name +----+-----+--------------+------------+-----------+------+------+--------------- + 1 | 1 | Australia | AU | 3 | | | not specified + 2 | 2 | China | CN | 3 | | | not specified + 3 | 3 | HongKong | HK | 3 | | | not specified + 4 | 4 | India | IN | 3 | | | not specified + 5 | 5 | Japan | JP | 3 | | | Sinzo Abe + 6 | 6 | Singapore | SG | 3 | 791 | km | not specified +(6 rows) + +SELECT xmltable('/ROWS/ROW' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + AU + + Australia + + 3 + + ") + (" + + CN + + China + + 3 + + ") + (" + + HK + + HongKong + + 3 + + ") + (" + + IN + + India + + 3 + + ") + (" + + JP + + Japan + + 3Sinzo Abe+ + ") + (" + + SG + + Singapore + + 3791 + + ") +(6 rows) + +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan"]' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + JP + + Japan + + 3Sinzo Abe+ + ") +(1 row) + +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data) FROM xmldata; + xmltable +------------------------------------------------------------------ + (" + + IN + + India + + 3 + + ") + (" + + JP + + Japan + + 3Sinzo Abe+ + ") +(2 rows) + +SELECT (xmltable).* FROM (SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int) FROM xmldata) s; + COUNTRY_NAME | REGION_ID +--------------+----------- + India | 3 + Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata,LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int); + COUNTRY_NAME | REGION_ID +--------------+----------- + India | 3 + Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY, "COUNTRY_NAME" text, "REGION_ID" int); + id | COUNTRY_NAME | REGION_ID +----+--------------+----------- + 1 | India | 3 + 2 | Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int); + id | COUNTRY_NAME | REGION_ID +----+--------------+----------- + 4 | India | 3 + 5 | Japan | 3 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id'); + id +---- + 4 + 5 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY); + id +---- + 1 + 2 +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH '.'); + id | COUNTRY_NAME | REGION_ID | rawdata +----+--------------+-----------+------------------------------------------------------------------ + 4 | India | 3 | + + | | | IN + + | | | India + + | | | 3 + + | | | + 5 | Japan | 3 | + + | | | JP + + | | | Japan + + | | | 3Sinzo Abe+ + | | | +(2 rows) + +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH './*'); + id | COUNTRY_NAME | REGION_ID | rawdata +----+--------------+-----------+----------------------------------------------------------------------------------------------------------------------------- + 4 | India | 3 | INIndia3 + 5 | Japan | 3 | JPJapan3Sinzo Abe +(2 rows) + +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text); + element +------------------- + a1aa2a bbbbcccc +(1 row) + +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail +ERROR: more than one value returned by column XPath expression +-- CDATA test +select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text); + c +------------------------- + &"<>!foo + 2 +(2 rows) + +-- XML builtin entities +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent text); + ent +----- + ' + " + & + < + > +(5 rows) + +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent xml); + ent +------------------ + ' + " + & + < + > +(5 rows) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + QUERY PLAN +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Nested Loop + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + -> Seq Scan on public.xmldata + Output: xmldata.data + -> Function Scan on "xmltable" + Output: "xmltable".id, "xmltable"._id, "xmltable".country_name, "xmltable".country_id, "xmltable".region_id, "xmltable".size, "xmltable".unit, "xmltable".premier_name + Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(7 rows) + +EXPLAIN (VERBOSE, COSTS OFF) SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata; + QUERY PLAN +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(2 rows) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT (xmltable).* FROM + (SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata) s; + QUERY PLAN +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Subquery Scan on s + Output: (s."xmltable").id, (s."xmltable")._id, (s."xmltable").country_name, (s."xmltable").country_id, (s."xmltable").region_id, (s."xmltable").size, (s."xmltable").unit, (s."xmltable").premier_name + -> Seq Scan on public.xmldata + Output: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text)) +(4 rows) + diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql index 08a0b30..11d7402 100644 --- a/src/test/regress/sql/xml.sql +++ b/src/test/regress/sql/xml.sql @@ -270,3 +270,173 @@ SELECT XMLPARSE(DOCUMENT ']> SELECT XMLPARSE(DOCUMENT ']>&c;'); -- This might or might not load the requested DTD, but it mustn't throw error. SELECT XMLPARSE(DOCUMENT ' '); + +-- XMLPATH tests +CREATE TABLE xmldata(data xml); +INSERT INTO xmldata VALUES(' + + AU + Australia + 3 + + + CN + China + 3 + + + HK + HongKong + 3 + + + IN + India + 3 + + + JP + Japan + 3Sinzo Abe + + + SG + Singapore + 3791 + +'); + +-- XMLTABLE without columns +SELECT * FROM XMLTABLE('/rows/row' PASSING '1020'); +SELECT XMLTABLE('/rows/row' PASSING '1020'); + +-- XMLTABLE with columns +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + +CREATE VIEW xmltableview1 AS SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + +SELECT * FROM xmltableview1; + +\sv xmltableview1 + +EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1; +EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1; + +-- XMLNAMESPACES tests +SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); + +CREATE VIEW xmltableview2 AS SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz), + '/zz:rows/zz:row' + PASSING '10' + COLUMNS a int PATH 'zz:a'); + +SELECT * FROM xmltableview2; + +SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'), + '/rows/row' + PASSING '10' + COLUMNS a int PATH 'a'); + +SELECT * FROM XMLTABLE('/rows/row/a/text()' PASSING '1020'); + +-- used in prepare statements +PREPARE pp AS +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + +EXECUTE pp; + +SELECT xmltable('/ROWS/ROW' PASSING data) FROM xmldata; +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan"]' PASSING data) FROM xmldata; +SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data) FROM xmldata; +SELECT (xmltable).* FROM (SELECT xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int) FROM xmldata) s; +SELECT xmltable.* FROM xmldata,LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS "COUNTRY_NAME" text, "REGION_ID" int); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY, "COUNTRY_NAME" text, "REGION_ID" int); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id'); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id FOR ORDINALITY); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH '.'); +SELECT xmltable.* FROM xmldata, LATERAL xmltable('/ROWS/ROW[COUNTRY_NAME="Japan" or COUNTRY_NAME="India"]' PASSING data COLUMNS id int PATH '@id', "COUNTRY_NAME" text, "REGION_ID" int, rawdata xml PATH './*'); + +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text); +SELECT * FROM xmltable('/root' passing 'a1aa2a bbbbxxxcccc' COLUMNS element text PATH 'element/text()'); -- should fail + +-- CDATA test +select * from xmltable('r' passing ' &"<>!foo]]>2' columns c text); + +-- XML builtin entities +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent text); +SELECT * FROM xmltable('/x/a' PASSING ''"&<>' COLUMNS ent xml); + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT xmltable.* + FROM (SELECT data FROM xmldata) x, + LATERAL XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified'); + +EXPLAIN (VERBOSE, COSTS OFF) SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata; + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT (xmltable).* FROM + (SELECT XMLTABLE('/ROWS/ROW' + PASSING data + COLUMNS id int PATH '@id', + _id FOR ORDINALITY, + country_name text PATH 'COUNTRY_NAME' NOT NULL, + country_id text PATH 'COUNTRY_ID', + region_id int PATH 'REGION_ID', + size float PATH 'SIZE', + unit text PATH 'SIZE/@unit', + premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified') FROM xmldata) s; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index c680216..099c6ee 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -1,3 +1,15 @@ +XmlTableBuilderData +TableExprRoutine +XmlTableContext +TableExprBuilder +TableExprState +TableExprRawCol +TableExpr +TableExprColumn +SQLValueFunction +max_parallel_hazard_context +TriggerTransition +SQLValueFunctionOp ABITVEC ACCESS_ALLOWED_ACE ACL_SIZE_INFORMATION