Skip to content

Commit 8d438f9

Browse files
Add required functions and hook to support OPENXML with WITH clause (#587)
To add support for Openxml with WITH clause, this commit exposes wrapper hooks to facilitate support for T-SQL OPENXML functionalities by allowing Babelfish to customised XML table processing during query parsing. Added two new hooks: 1. pre_transform_openxml_columns_hook , to allow extensions to pre-process OPENXML column definitions before standard XMLTABLE transformation. 2. openxml_set_namespaces_hook, which fetches and register namespaces in xpath context. Extensions PR : babelfish-for-postgresql/babelfish_extensions#3820 Task: BABEL-3635, BABEL 6045 Signed-off-by: Harsh Dubey <[email protected]>
1 parent 3444c14 commit 8d438f9

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

src/backend/parser/parse_clause.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ sortby_nulls_hook_type sortby_nulls_hook = NULL;
5656

5757
optimize_explicit_cast_hook_type optimize_explicit_cast_hook = NULL;
5858

59+
pre_transform_openxml_columns_hook_type pre_transform_openxml_columns_hook = NULL;
60+
5961
static int extractRemainingColumns(ParseState *pstate,
6062
ParseNamespaceColumn *src_nscolumns,
6163
List *src_colnames,
@@ -711,6 +713,13 @@ transformRangeTableFunc(ParseState *pstate, RangeTableFunc *rtf)
711713
constructName = "XMLTABLE";
712714
docType = XMLOID;
713715

716+
/*
717+
* Hook to allow extensions to pre-process OPENXML column definitions
718+
* before standard XMLTABLE transformation.
719+
*/
720+
if (pre_transform_openxml_columns_hook && tf->functype == TFT_XMLTABLE)
721+
pre_transform_openxml_columns_hook(pstate, rtf);
722+
714723
/*
715724
* We make lateral_only names of this level visible, whether or not the
716725
* RangeTableFunc is explicitly marked LATERAL. This is needed for SQL

src/backend/utils/adt/xml.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ const TableFuncRoutine XmlTableRoutine =
232232
.DestroyOpaque = XmlTableDestroyOpaque
233233
};
234234

235+
#ifdef USE_LIBXML
236+
openxml_set_namespaces_hook_type openxml_set_namespaces_hook = NULL;
237+
#endif
238+
235239
#define NO_XML_SUPPORT() \
236240
ereport(ERROR, \
237241
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
@@ -4795,6 +4799,10 @@ XmlTableSetNamespace(TableFuncScanState *state, const char *name, const char *ur
47954799
errmsg("DEFAULT namespace is not supported")));
47964800
xtCxt = GetXmlTableBuilderPrivateData(state, "XmlTableSetNamespace");
47974801

4802+
/* For TSQL OPENXML, following hook will fetch and register namespaces in Xpath context. */
4803+
if (openxml_set_namespaces_hook)
4804+
return openxml_set_namespaces_hook(xtCxt->xpathcxt, xtCxt->xmlerrcxt, uri);
4805+
47984806
if (xmlXPathRegisterNs(xtCxt->xpathcxt,
47994807
pg_xmlCharStrndup(name, strlen(name)),
48004808
pg_xmlCharStrndup(uri, strlen(uri))))

src/include/parser/parse_clause.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,8 @@ extern PGDLLEXPORT optimize_explicit_cast_hook_type optimize_explicit_cast_hook;
6262
/* functions in parse_jsontable.c */
6363
extern ParseNamespaceItem *transformJsonTable(ParseState *pstate, JsonTable *jt);
6464

65+
/* Hook for preprocessing OPENXML column definitions before XMLTABLE transformation */
66+
typedef void (*pre_transform_openxml_columns_hook_type) (ParseState *pstate, RangeTableFunc *rtf);
67+
extern PGDLLIMPORT pre_transform_openxml_columns_hook_type pre_transform_openxml_columns_hook;
68+
6569
#endif /* PARSE_CLAUSE_H */

src/include/utils/xml.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "nodes/primnodes.h"
2222
#ifdef USE_LIBXML
2323
#include <libxml/tree.h>
24+
#include <libxml/xpath.h>
2425
#endif
2526

2627
typedef struct varlena xmltype;
@@ -96,6 +97,10 @@ extern xmlDocPtr xml_parse_wrapper(text *data, XmlOptionType xmloption_arg,
9697
extern xmlChar *pg_xmlCharStrndup_wrapper(const char *str, size_t len);
9798
extern int parse_xml_decl_wrapper(const xmlChar *str, size_t *lenp,
9899
xmlChar **version, xmlChar **encoding, int *standalone);
100+
101+
/* Hook function type for TSQL OPENXML namespace handling */
102+
typedef void (*openxml_set_namespaces_hook_type) (xmlXPathContext *xpathctx, PgXmlErrorContext *xmlerrcxt, char *doc_id_str);
103+
extern PGDLLIMPORT openxml_set_namespaces_hook_type openxml_set_namespaces_hook;
99104
#endif /* USE_LIBXML */
100105

101106
extern PGDLLIMPORT int xmlbinary; /* XmlBinaryType, but int for guc enum */

0 commit comments

Comments
 (0)