xml_exists

Tests if the result of a query is empty.

Signature

xml_exists(
    STRING query, 
    { STRING | STRUCT } bindings
) as BOOLEAN

Description

query

An XQuery or XPath expression. It must be a constant value, because it is only read the first time the function is evaluated. The initial query string is compiled and reused in all subsequent calls.

You can access files that are stored in the Hadoop distributed cache and HTTP resources (http://...). Use the XQuery fn:doc function for XML documents, and the fn:unparsed-text and fn:parsed-text-lines functions to access plain text files.

If an error occurs while compiling the query, the function raises an error. If an error occurs while evaluating the query, the error is logged (not raised), and an empty array is returned.

bindings

The input that the query processes. The value can be an XML STRING or a STRUCT of variable values:

  • STRING: The string is bound to the initial context item of the query as XML.

  • STRUCT: A STRUCT with an even number of fields. Each pair of fields defines a variable binding (name, value) for the query. The name fields must be type STRING, and the value fields can be any supported primitive. See "Data Type Conversions."

Return Value

true if the result of the query is not empty; false if the result is empty or the query raises a dynamic error

Notes

The first dynamic error raised by a query is logged, but subsequent errors are suppressed.

Examples

Example 1   STRING Binding

This example parses and binds the input XML string to the initial context item of the query x/y:

Hive> SELECT xml_exists("x/y", "<x><y>123</y></x>") FROM src LIMIT 1;
     .
     .
     .
true
Example 2   STRUCT Binding

This example defines two query variables, $data and $value:

Hive> SELECT xml_exists(
      "parse-xml($data)/x/y[@id = $value]",
      struct(
         "data", "<x><y id='1'/><y id='2'/></x>",
         "value", 2
      )
   ) FROM src LIMIT 1;
     .
     .
     .
true
Example 3   Error Logging

In this example, an error is written to the log, because the input XML is invalid:

hive> SELECT xml_exists("x/y", "<x><y>123</invalid></x>") FROM src LIMIT 1;
     .
     .
     .
false