andygrove opened a new issue, #284:
URL: https://github.com/apache/datafusion-spark/issues/284
Part of #271 and apache/datafusion-comet#7300.
### What the function does
`xpath_float(xml, path)` evaluates an XPath 1.0 expression, converts the
result with XPath `number()` into a
Java `Double`, and then narrows it with `Double.floatValue()`, giving a
`FLOAT`. Both arguments are strings
(collation-aware from 4.0), and `path` must be foldable. The result is
always nullable. Available since Spark
2.0.0.
The `number()` conversion is in the `xpath_double` issue, and `xpath_float`
is that value cast to `f32`.
Parsing, DTD and entity handling, namespaces, NULL handling and errors are
in the `xpath` issue. This issue
covers only the double-to-float step.
### Examples
```sql
SELECT xpath_float('<a><b>1</b><b>2</b></a>', 'sum(a/b)');
-- 3.0
SELECT xpath_float('<a>this is not a number</a>', 'a');
-- NaN
SELECT xpath_float('<a>this 2 is not a number</a>', 'a');
-- NaN
SELECT xpath_float('<a><b>2000000000</b><c>40000000000</c></a>', 'a/b *
a/c');
-- 8.0E19
SELECT xpath_float('<a>try a boolean</a>', 'a = 10');
-- 0.0
SELECT xpath_float('<a><b class="odd">1</b><b class="even">2</b><b
class="odd">4</b><c>8</c></a>', 'a/b');
-- 1.0
SELECT xpath_float('<a><b class="odd">1</b><b class="even">2</b><b
class="odd">4</b><c>8</c></a>', 'sum(a/*)');
-- 15.0
SELECT xpath_float('<a><b class="odd">1</b><b class="even">2</b><b
class="odd">4</b><c>8</c></a>', 'sum(a/b[@class="odd"])');
-- 5.0
SELECT xpath_float('<a><b>1.0</b><b>2.1</b></a>', 'sum(a/b)');
-- 3.1
```
These come from the `ExpressionDescription`, the Hive `udf_xpath_float`
golden files run by
`HiveCompatibilitySuite`, `XPathExpressionSuite` and `XPathFunctionsSuite`.
The last result is
`3.1.toFloat`, which is the rounded `f32` of the double sum `3.1`.
### Edge cases to test
- The narrowing is Java's `(float) d`, which uses IEEE
round-to-nearest-even, so in Rust it is `d as f32`.
A finite double above `f32::MAX` becomes `Infinity`. XPath has no exponent
literal, so build the large value
from element text, for example
`<a>1000000000000000000000000000000000000000</a>` with path `'a'`. A double
below the smallest `f32` subnormal becomes `0.0` or `-0.0`. `1 div 0`
stays `Infinity`.
- Check that rounding happens once, from the double result. `sum()` adds in
double and narrows at the end,
so it is not equivalent to adding `f32` values.
- `NaN` stays `NaN` (non-numeric text). A false boolean gives `0.0`.
- The empty node-set is `NaN` by XPath 1.0 rules, although the usage string
says zero. See the note in the
`xpath_double` issue and verify against Spark.
- `NULL` or `''` arguments give NULL. A non-literal path gives
`DATATYPE_MISMATCH.NON_FOLDABLE_INPUT`.
Malformed XML and an invalid path raise runtime errors (see the `xpath`
issue).
### Differences across Spark versions
None found beyond the shared ones in the `xpath` issue.
### Implementation notes
- Spark: `XPathFloat` in
`sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xml/xpath.scala`.
It calls `XPathFloatEvaluator` (`ret.floatValue()`) in
`.../xml/XmlExpressionEvalUtils.scala`.
- DataFusion core has no equivalent.
- Reuse the shared `number()` evaluator (see the `xpath` and `xpath_double`
issues) in
`crates/spark/src/function/xml/`, and cast the result to `f32`.
- Tests: add `crates/sqllogictest/test_files/xml/xpath_float.slt` and
document the function with `#[user_doc]`.
---
_This issue was generated by an LLM (Claude) from the Apache Spark source
code and tests. Examples come from Spark's `ExpressionDescription` examples,
test suites and `.sql.out` golden files, but please verify them against Spark
before relying on them._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]