conversion_funcs Expression Audits#
Audit notes for expressions in this category that have been audited. Absence of an entry means the expression has not been audited yet, not that it is unsupported. See the user guide Spark Expression Support for current support status.
cast#
Spark 3.4.3 (audited 2026-05-27): identical to 3.5.8 modulo
Cast.canUpCastrefactored to delegate toUpCastRule.canUpCast.Spark 3.5.8 (audited 2026-05-27): baseline.
Cast(child, dataType, timeZoneId, evalMode); eval modes areLEGACY,ANSI,TRY. The legacyCast.canCastmatrix and theCast.canAnsiCastmatrix decide acceptance per type pair. Comet routes viaCometCast(spark/src/main/scala/org/apache/comet/expressions/CometCast.scala) using a per-source-type support matrix that returnsCompatible,Incompatible(reason), orUnsupported(reason); literal children are short-circuited toCompatible()soCometLiteralvalidates them. The serializedCastproto carriesdatatype,evalMode,timezone(defaultUTC),allowIncompat(fromspark.comet.expression.Cast.allowIncompatible), andisSpark4Plus. The native side (native/spark-expr/src/conversion_funcs/cast.rs) implements explicit per-eval-mode branches for narrowing numeric casts that match Spark’s overflow exceptions, and falls through to DataFusioncast_with_options(safe = !ANSI)for the rest.Spark 4.0.1 (audited 2026-05-27):
VariantTypeadded;StringTypeliterals replaced with_: StringTypeto accommodate collated strings.(TimestampType, ByteType|ShortType|IntegerType)added tocanAnsiCast.NullIntolerant->nullIntolerant: Booleanrefactor. NewToPrettyString.BinaryFormattersemantics forBinary -> Stringare replicated natively viaspark_binary_formatter. Numeric-to-numeric matrix unchanged.Spark 4.1.1 (audited 2026-05-27):
TimeTypeadded; manyTimeTypearms incanCast/canAnsiCast. GeospatialGeographyType/GeometryTypetypes added with their own conversion rules. Numeric-to-numeric matrix unchanged.Known divergences and gaps:
CAST(<binary> AS STRING)decodes the bytes withdecode_utf8_spark_lossy, replacing invalid UTF-8 withU+FFFDexactly as the JVM’snew String(bytes, UTF_8)does (the same decoder as the native shuffle). It matches Spark’s rendered output, but decoding is not byte-preserving, so it diverges from Spark for operations that read the underlying bytes: round-trips such asCAST(CAST(x AS STRING) AS BINARY), and value identity, since distinct ill-formed byte sequences all decode toU+FFFDand can therefore compare equal in Comet where Spark compares the raw bytes (#4764).Spark 4.0 collated
StringTypeis not explicitly guarded; pattern equality is expected to keep collated-string casts falling back, but there is no test (#4489; umbrella #2190).Spark 4.1
TimeTypecasts have no explicitUnsupportedarm; they fall back implicitly but do not appear in the auto-generated compatibility doc (#4490).CAST(<map> AS <map>)runs natively viacast_map_to_map;CometCast.isSupportedrecurses into the key and value casts, so the map arm inherits whichever support level the inner casts report.spark.sql.legacy.castComplexTypesToString.enabled=trueisUnsupportedfor any array/map/struct-to-string cast, so those casts fall back to Spark while the flag is on. Comet only implements the default ({}-wrapped, NULL-rendering) formatting. The flag is internal from Spark 4.0 onward and defaults to false.CAST(<float|double> AS DECIMAL)isCompatible: it rounds the shortest decimal string form of the value (matching Spark’sDouble.toString+BigDecimal.setScale(HALF_UP)path) rather than the binary value, taking a guarded binary fast path for values provably far from a rounding tie. NaN and infinity are null even in ANSI mode, matching Spark, which swallows theNumberFormatExceptionfromDecimal(double)before the ANSI overflow check. The support level carries a note:Double.toStringonly emits the shortest round-trip form on JDK 19 and later, so on older JDKs Spark’s own result can differ for a value whose shortest form lands exactly on a rounding tie at the target scale.
Spark registers the type-name conversion functions (
bigint,binary,boolean,date,decimal,double,float,int,smallint,string,timestamp,tinyint) as cast aliases. Each lowers to the sameCastnode, so Comet handles it via thecastimplementation with the same compatibility profile.Performance (tuned 2026-07-14, PR #4920): narrowing integer casts (
spark_cast_int_to_int) map the values buffer in a single pass with Arrowunary/try_unaryand carry the null buffer over untouched, replacing an element-by-elementOption/Resultiterator-collect. Up to 100x faster on narrowing casts. Benchmark:benches/cast_numeric.rs.Performance (tuned 2026-07-15, PR #4940): float/double-to-decimal casts (
cast_floating_point_to_decimal128) now convert in a single vectorizedunary_optpass that maps out-of-range values (NaN, infinity, precision overflow) to null, replacing the per-elementDecimal128Builderloop. ANSI raises via an O(1) null-count check plus a rare element-wise rescan. 15-36% faster with no regression on any shape. Benchmark:benches/cast_float_to_decimal.rs.