Implement Spark-compatible `xpath_string`

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

First check that the shared XML evaluator from the xpath issue (#271) exists under crates/spark/src/function/xml/; this function builds on it and is blocked without it. Port Spark's XPathString / UDFXPathUtil.evalString semantics — XPath 1.0 string() conversion and number formatting — into a new function there, adding #[user_doc]. Enable and extend the stub crates/sqllogictest/test_files/xml/xpath_string.slt with the cases listed; done when that suite passes against Spark's outputs.

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

描述

enhancement

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

What the function does

xpath_string(xml, path) evaluates an XPath 1.0 expression and converts the result with the XPath string()
rules (XPathConstants.STRING), giving a STRING. For a node-set this is the string value of the first node in
document order, and for an element that is the concatenation of all its descendant text. Both arguments are
strings (collation-aware from 4.0, and the result has the session default string type). path must be
foldable. The result is always nullable. Available since Spark 2.0.0.

Parsing, DTD and entity handling, namespaces, NULL handling, invalid-XML and invalid-path errors, and lazy path
compilation are shared with the family. They are specified in the xpath issue. This issue covers only the
string conversion.

Examples
SELECT xpath_string('<a><b>b</b><c>cc</c></a>','a/c');
-- cc
SELECT xpath_string('<a><b>bb</b><c>cc</c></a>', 'a');
-- bbcc
SELECT xpath_string('<a><b>bb</b><c>cc</c></a>', 'a/b');
-- bb
SELECT xpath_string('<a><b>bb</b><c>cc</c></a>', 'a/d');
-- (empty string)
SELECT xpath_string('<a><b>b1</b><b>b2</b></a>', '//b');
-- b1
SELECT xpath_string('<a><b>b1</b><b>b2</b></a>', 'a/b[1]');
-- b1
SELECT xpath_string('<a><b>b1</b><b>b2</b></a>', 'a/b[2]');
-- b2
SELECT xpath_string('<a><b>b1</b><b id="b_2">b2</b></a>', 'a/b[@id="b_2"]');
-- b2

These come from the ExpressionDescription, the Hive udf_xpath_string golden files run by
HiveCompatibilitySuite, and XPathExpressionSuite. UDFXPathUtilSuite also has these cases:

  • a/b[3]/text() gives b3;
  • a/b[4]/text() (no match) gives '';
  • a/b[2]/@k gives foo;
  • an external entity (<!ENTITY embed SYSTEM "...">) expands to ''.
Edge cases to test
  • No match gives '', not NULL (a/d above). NULL comes only from a NULL or '' argument.
  • Conversion rules from XPath 1.0 string():
    • an element gives its concatenated descendant text, including whitespace-only text nodes and CDATA;
    • comment and processing-instruction text inside the element is not included;
    • an attribute gives its value;
    • a boolean gives true or false.
  • Numbers are formatted the XPath 1.0 way, not with Java's Double.toString. Integers have no decimal point
    (sum(a/b) over 1 and 2 gives 3), and the special values are NaN, Infinity and -Infinity.
    Derive the outputs from the spec and confirm them against Spark, because the formatting of large and small
    magnitudes differs between engines.
  • XPath string functions in the path: concat, substring (1-based, with rounding rules), normalize-space,
    translate, substring-before/after, and string-length (which XPath counts in characters; check
    supplementary code points).
  • Entity and character references (&amp;, &#x1F600;) are decoded in the output. So is an internal DTD
    entity.
  • 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. From 4.0 the result is the session default string
type rather than plain StringType.

Implementation notes
  • Spark: XPathString in sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xml/xpath.scala.
    It calls XPathStringEvaluator, which calls UDFXPathUtil.evalString.
  • DataFusion core has no equivalent.
  • Build it on the shared evaluator from the xpath issue in crates/spark/src/function/xml/. The existing
    crates/sqllogictest/test_files/xml/xpath_string.slt is a commented-out stub that should be enabled and
    extended. 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 摘要。