Implement Spark-compatible `xpath_long`

Abierto
#286 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
65/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust, sql
Área
backend

Línea de trabajo

Start in crates/spark/src/function/xml/ and check whether the shared number() evaluator from the xpath and xpath_double issues (#271) already exists — this issue only adds the as i64 narrowing step, which Rust already saturates and truncates as required. The spec's examples come from Spark's XPathLong/XPathLongEvaluator and XPathExpressionSuite; mirror them in a new crates/sqllogictest/test_files/xml/xpath_long.slt and annotate the function with #[user_doc]. Done means that slt file passes, covering NaN-to-0, saturation, truncation and the 2^53 precision case.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement

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

What the function does

xpath_long(xml, path) evaluates an XPath 1.0 expression, converts the result with XPath number() into a
Java Double, and narrows it with Double.longValue(), giving a BIGINT. 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-long step, which is Java's
(long) d:

  • NaN becomes 0;
  • values are truncated toward zero;
  • out-of-range values saturate at Long.MAX_VALUE / Long.MIN_VALUE.

This matches Rust's d as i64.

Examples
SELECT xpath_long('<a><b>1</b><b>2</b></a>', 'sum(a/b)');
-- 3
SELECT xpath_long('<a>this is not a number</a>', 'a');
-- 0
SELECT xpath_long('<a><b>2000000000</b><c>40000000000</c></a>', 'a/b * a/c');
-- 9223372036854775807
SELECT xpath_long('<a>try a boolean</a>', 'a = 10');
-- 0
SELECT xpath_long('<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_long('<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_long('<a><b class="odd">9000000000</b><b class="even">2</b><b class="odd">4</b><c>8</c></a>', 'sum(a/b[@class="odd"])');
-- 9000000004

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

Edge cases to test
  • Precision. The value always goes through a double, so integers above 2^53 lose precision before the cast.
    <a>9007199254740993</a> with 'a' gives 9007199254740992, by code reading (the nearest double). Spark
    does not read the text directly as a long.
  • Saturation: 8.0E19 gives 9223372036854775807 (above). By the same rule, -1 div 0 gives
    -9223372036854775808 and 1 div 0 gives 9223372036854775807.
  • Truncation toward zero (<a>-2.7</a> gives -2), and NaN gives 0. This covers non-numeric text,
    0 div 0, and the empty node-set, which is NaN in XPath 1.0.
  • 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: XPathLong in sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/xml/xpath.scala.
    It calls XPathLongEvaluator (ret.longValue()) 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 i64.
  • Tests: add crates/sqllogictest/test_files/xml/xpath_long.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.

Lenguaje dominante
Rust
Estrellas
7
Forks
15
Merge medio
16 h 43 min
PR fusionados (30 d)
26

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apache/datafusion-spark

Todos los issues de apache/datafusion-spark

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.