Implement Spark-compatible `positive`

Aperta
#265 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, spark
Ambito
databases

Direzione di ricerca

The template is crates/spark/src/function/math/negative.rs: copy its signature, NumericAndInterval types and implicit-cast handling, and register the function with the other math functions. Read Spark's UnaryPositive and datafusion/sql/src/expr/unary_op.rs for the timestamp/Duration divergence, then add ScalarUDFImpl::simplify replacing the call with its child. Enable the commented query in crates/sqllogictest/test_files/math/positive.slt and extend it with the NULL, integer-boundary, float, decimal, string-cast, interval and rejected-type cases; done is that file passing.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement good first issue

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

What the function does

positive(expr) (also the unary +expr operator) returns expr unchanged. It accepts numeric and interval types (TypeCollection.NumericAndInterval, with ImplicitCastInputTypes, so a STRING argument is cast to DOUBLE). The result type equals the (cast) input type; NULL in gives NULL out. Available since 1.5.0.

In Spark 4.0+ UnaryPositive is RuntimeReplaceable with replacement = child, so it disappears from optimized plans. In 3.4 and 3.5 it is a regular expression that survives into physical plans.

Examples
SELECT positive(1);
-- 1
SELECT positive('-1.11'), positive(-1.11);
-- -1.11   -1.11      (double, decimal(3,2))
SELECT +interval '-1 year 1 month';
-- -0-11              interval year to month
SELECT +interval '-1 day 1 hour -1 minute 1 second';
-- -0 23:00:59.000000000
SELECT +date '1999-01-01';
-- error DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
Edge cases to test
  • NULL input of each type.
  • Every integer type keeps its type and value, including -2147483648 and -9223372036854775808 (no overflow, unlike negative).
  • FLOAT / DOUBLE: NaN, -0.0 (sign preserved), +/-Infinity returned unchanged.
  • DECIMAL keeps precision and scale (positive(-1.11) is DECIMAL(3,2)).
  • STRING argument is cast to DOUBLE (positive('-1.11') is DOUBLE -1.11). With ANSI on an invalid string fails in the cast (CAST_INVALID_INPUT); with ANSI off it is NULL.
  • Year-month and day-time intervals are returned unchanged (Interval(YearMonth) and Duration(Microsecond) in this crate).
  • DATE, TIMESTAMP, BOOLEAN, BINARY are rejected with DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE (requiredType ("NUMERIC" or "INTERVAL DAY TO SECOND" or "INTERVAL YEAR TO MONTH" or "INTERVAL")).
  • Scalar and array inputs.
Differences across Spark versions
  • 3.4 / 3.5: UnaryPositive is evaluated as an expression (identity nullSafeEval).
  • 4.0+: RuntimeReplaceable, replaced by its child during optimization. Behaviour is the same.
Implementation notes
  • Spark: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala, UnaryPositive.
  • DataFusion core has no positive function. Its SQL planner accepts +expr for numeric, interval and timestamp operands and returns the operand unchanged (datafusion/sql/src/expr/unary_op.rs); Spark rejects timestamps, and Spark day-time intervals arrive here as Duration, which that check may not cover.
  • Mirror crates/spark/src/function/math/negative.rs (same signature and types) and return the input as-is. Implementing ScalarUDFImpl::simplify to replace the call with its argument is also appropriate.
  • Tests: the placeholder crates/sqllogictest/test_files/math/positive.slt exists with a commented-out query; enable and extend it. Document 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.

Lingua principale
Rust
Stelle
7
Fork
15
Merge medio
16h 43m
PR unite (30g)
26

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/datafusion-spark

Tutte le issue di apache/datafusion-spark

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.