Implement Spark-compatible `xpath_int`

未关闭
#285 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
72/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust, sql, xml
领域
databases

调研方向

Start from the shared number() evaluator planned in crates/spark/src/function/xml/ (see the xpath and xpath_double issues) and compare against Spark's XPathInt/XPathIntEvaluator. Register xpath_int alongside the other XML functions, narrowing the double with as i32, and add #[user_doc]. Add crates/sqllogictest/test_files/xml/xpath_int.slt with the saturation, truncation, NaN and NULL cases listed here; done when those SLT tests pass.

由索引模型根据 Issue 内容生成。

描述

enhancement

Part of #271 and apache/datafusion-comet#7300.

What the function does

xpath_int(xml, path) evaluates an XPath 1.0 expression, converts the result with XPath number() into a
Java Double, and narrows it with Double.intValue(), giving an INT. 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. Parsing, DTD and entity handling, namespaces, NULL
handling and errors are in the xpath issue. This issue covers only the double-to-int step, which is Java's
narrowing conversion (int) d:

  • NaN becomes 0;
  • values are truncated toward zero;
  • out-of-range values saturate at Integer.MAX_VALUE / Integer.MIN_VALUE, and so do +/-Infinity.

This matches Rust's d as i32. Nothing raises an error, and ANSI mode has no effect.

Examples
SELECT xpath_int('<a><b>1</b><b>2</b></a>', 'sum(a/b)');
-- 3
SELECT xpath_int('<a>this is not a number</a>', 'a');
-- 0
SELECT xpath_int('<a>this 2 is not a number</a>', 'a');
-- 0
SELECT xpath_int('<a><b>2000000000</b><c>40000000000</c></a>', 'a/b * a/c');
-- 2147483647
SELECT xpath_int('<a>try a boolean</a>', 'a = 10');
-- 0
SELECT xpath_int('<a><b class="odd">1</b><b class="even">2</b><b class="odd">4</b><c>8</c></a>', 'a/b');
-- 1
SELECT xpath_int('<a><b class="odd">1</b><b class="even">2</b><b class="odd">4</b><c>8</c></a>', 'sum(a/*)');
-- 15
SELECT xpath_int('<a><b class="odd">100000</b><b class="even">2</b><b class="odd">4</b><c>8</c></a>', 'sum(a/b[@class="odd"])');
-- 100004

These come from the ExpressionDescription, the Hive udf_xpath_int golden files run by
HiveCompatibilitySuite, and XPathExpressionSuite.

Edge cases to test
  • Saturation at both ends: 8.0E19 gives 2147483647 (above). By the same rule, -1 * a/b * a/c gives
    -2147483648, 1 div 0 gives 2147483647, and -1 div 0 gives -2147483648.
  • Truncation: <a>2.7</a> gives 2 and <a>-2.7</a> gives -2, by code reading. Note that XPath round()
    and floor() give different results, so test xpath_int(x, 'round(a)') as well.
  • NaN gives 0 (non-numeric text, 0 div 0, and the empty node-set, which is NaN by XPath 1.0 rules).
    The usage string says "zero if no match is found, or a match is found but the value is non-numeric".
  • Values just at and beyond 2^31: 2147483647, 2147483648 and -2147483649.
  • 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: XPathInt in sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xml/xpath.scala.
    It calls XPathIntEvaluator (ret.intValue()) in .../xml/XmlExpressionEvalUtils.scala.
  • DataFusion core has no equivalent.
  • Reuse the shared number() evaluator from the xpath and xpath_double issues in
    crates/spark/src/function/xml/, and narrow with as i32. Rust's saturating float-to-int cast matches
    Java's here.
  • Tests: add crates/sqllogictest/test_files/xml/xpath_int.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.

主要语言
Rust
星标
7
派生
15
平均合并
16 小时 43 分钟
30 天内合并 PR
26

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/datafusion-spark 的其他 Issue

查看 apache/datafusion-spark 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。