joelrobin18 commented on code in PR #60218:
URL: https://github.com/apache/spark/pull/60218#discussion_r4547553679
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1169,25 +1169,35 @@ object DateTimeUtils extends SparkDateTimeUtils {
}
/**
- * Creates a TIME value from seconds since midnight (decimal type).
+ * Creates a TIME(6) value from seconds since midnight (decimal type). The
digits beyond
+ * microseconds are truncated.
* @param seconds Seconds (0 to 86399.999999)
* @return Nanoseconds since midnight
*/
def timeFromSeconds(seconds: Decimal): Long =
withTimeConversionErrorHandling {
- val operand = new java.math.BigDecimal(NANOS_PER_SECOND)
- seconds.toJavaBigDecimal.multiply(operand).longValueExact()
+ secondsToMicrosPrecisionNanos(seconds.toJavaBigDecimal)
}
/**
- * Creates a TIME value from seconds since midnight (floating point type).
+ * Creates a TIME(6) value from seconds since midnight (floating point
type). The digits beyond
+ * microseconds are truncated.
* @param seconds Seconds (0 to 86399.999999)
* @return Nanoseconds since midnight
*/
def timeFromSeconds(seconds: Double): Long = withTimeConversionErrorHandling
{
if (seconds.isNaN || seconds.isInfinite) {
throw new DateTimeException("Cannot convert NaN or Infinite value to
TIME")
}
- (seconds * NANOS_PER_SECOND).toLong
+ // Use the shortest decimal form of the double, as CAST(double AS DECIMAL)
does. Otherwise
+ // 3.0E-6 becomes 2.9999...E-6 after the multiplication, and 2
microseconds after truncation.
+ secondsToMicrosPrecisionNanos(java.math.BigDecimal.valueOf(seconds))
Review Comment:
I made this change in 78fdd4e.
Below 1e9 seconds, `secondsToFlooredMicros` now uses only primitive
arithmetic:
- It rounds `seconds * 1e6` to the nearest long. If that long divided by 1e6
is equal to `seconds`, it is the result.
- If not, it floors `seconds * 1e6`. If `Math.fma` shows that the exact
product is below the floor, it subtracts one.
At 1e9 seconds or more, the function uses the `BigDecimal.valueOf` path as
before. NaN and Infinity fail before the conversion, with the same message. The
results and the errors did not change.
I added these tests:
- `DateTimeUtilsSuite` compares the new conversion with `BigDecimal.valueOf`
on about 425,000 deterministic samples. The samples are microsecond multiples
and the doubles 1 to 3 ulps from them. They also include random values in and
out of range, tiny positive and negative values, and large values.
- `TimeExpressionsSuite` checks these inputs in interpreted and generated
code: 2.49E-4, 1.0E-6, 0.7, 52200.1234567, the two doubles next to 2.49E-4, and
0.7F. It also checks the errors for -1.0E-7, 0.3 - 0.1 - 0.2, NaN, Infinity and
1.0E10.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala:
##########
@@ -1169,25 +1169,35 @@ object DateTimeUtils extends SparkDateTimeUtils {
}
/**
- * Creates a TIME value from seconds since midnight (decimal type).
+ * Creates a TIME(6) value from seconds since midnight (decimal type). The
digits beyond
+ * microseconds are truncated.
* @param seconds Seconds (0 to 86399.999999)
* @return Nanoseconds since midnight
*/
def timeFromSeconds(seconds: Decimal): Long =
withTimeConversionErrorHandling {
- val operand = new java.math.BigDecimal(NANOS_PER_SECOND)
- seconds.toJavaBigDecimal.multiply(operand).longValueExact()
+ secondsToMicrosPrecisionNanos(seconds.toJavaBigDecimal)
}
/**
- * Creates a TIME value from seconds since midnight (floating point type).
+ * Creates a TIME(6) value from seconds since midnight (floating point
type). The digits beyond
+ * microseconds are truncated.
* @param seconds Seconds (0 to 86399.999999)
* @return Nanoseconds since midnight
*/
def timeFromSeconds(seconds: Double): Long = withTimeConversionErrorHandling
{
if (seconds.isNaN || seconds.isInfinite) {
throw new DateTimeException("Cannot convert NaN or Infinite value to
TIME")
}
- (seconds * NANOS_PER_SECOND).toLong
+ // Use the shortest decimal form of the double, as CAST(double AS DECIMAL)
does. Otherwise
+ // 3.0E-6 becomes 2.9999...E-6 after the multiplication, and 2
microseconds after truncation.
Review Comment:
I made this change in 78fdd4e.
The comment now uses 2.49E-4 as the example. `2.49E-4 * 1e6` is
`248.99999999999997`, so the floor of the product is 248 microseconds. The
exact binary value of `1.0E-6` is below one microsecond, so the floor of the
exact value is 0.
New checks in `TimeExpressionsSuite` and `time.sql`:
- `time_from_seconds(2.49E-4D)` returns `00:00:00.000249`. The floor of the
product gives `00:00:00.000248`.
- `time_from_seconds(1.0E-6D)` returns `00:00:00.000001`. The floor of the
exact value gives `00:00:00`.
- `time_from_seconds(0.7D)` returns `00:00:00.7`. The floor of the exact
value gives `00:00:00.699999`.
Each check fails if the conversion uses one of the two alternatives.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala:
##########
@@ -135,12 +135,19 @@ case class ToTimeParser(fmt: Option[String]) {
}
}
- def parse(s: UTF8String): Long = withErrorCondition(s,
fmt)(formatter.parse(s.toString))
+ // The parser keeps up to 9 fractional digits, but `ToTime` returns
`TimeType()`, so drop the
+ // digits beyond its precision as CAST does.
+ private def toResultPrecision(nanos: Long): Long =
+ DateTimeUtils.truncateTimeToPrecision(nanos, TimeType.DEFAULT_PRECISION)
Review Comment:
I made this change in 78fdd4e. Each producer that knows the target `TIME(p)`
now truncates to `p`:
- `TimeFormatter.parse(s, precision)` truncates the parsed value.
`ToTimeParser` uses it with `TIME(6)`. The JSON, CSV and XML readers use it
with the precision of the field, also for XML attributes. Thus `from_json`,
`from_csv` and `from_xml` truncate too.
- `Literal.apply(LocalTime)` truncates to `TIME(6)`.
`Literal.create(LocalTime, TimeType(p))` truncates to `p`.
- `TimeTypeOps` truncates to the precision of the type in the external
conversion and in the encoder serializer. Thus `createDataFrame`, a `Dataset`
of `LocalTime` and a Spark Connect literal keep at most `p` digits.
I also changed Arrow ingest in this PR. `ArrowColumnVector` now truncates a
`TIME` column to the precision of its field, as it already does for the
nanosecond timestamp types. This covers Spark Connect local relations,
Arrow-based `createDataFrame` in PySpark and Arrow UDF results. An Arrow field
without the Spark precision metadata reads as `TIME(6)`. Thus the TODO for the
follow-up is not necessary.
I did not change the schema-less `CatalystTypeConverters.convertToCatalyst`.
It does not know the target type. Its callers include `PythonSQLUtils.toPyRow`
and the filter values of Python data sources, and these can have `TIME(7)` to
`TIME(9)` values. A truncation to `TIME(6)` there removes digits that these
types keep. `Literal.create` now uses the conversion of the type for a
`LocalTime`, so these literals do not use the schema-less conversion.
Tests:
- JSON, CSV and XML reads, and `from_json`, `from_csv` and `from_xml`:
`00:00:00.1234567` read into `TIME(6)` gives `00:00:00.123456`, and
`12:00:00.123456` read into `TIME(3)` gives `12:00:00.123`. `TIME(9)` keeps
nine digits. The tests use the default format and a 9-digit `timeFormat`.
- `from_json`, `from_csv` and `from_xml` give the same value as `to_time`
for the same string.
- `lit(LocalTime.of(0, 0, 0, 123456789))` is equal to
`TIME'00:00:00.123456'`. A `Dataset` of `LocalTime` and `createDataFrame` with
`TIME(3)`, `TIME(6)` and `TIME(9)` columns keep at most `p` digits.
- Spark Connect: a `TIME` literal, and a local relation with `TIME(3)`,
`TIME(6)` and `TIME(9)` columns, a struct field and an array field, are
truncated to their precision.
- `ArrowColumnVector` truncates `TIME(3)`, `TIME(6)` and a field without
metadata, and keeps `TIME(9)`.
Some existing tests made `TIME(p)` values with more than `p` digits. I
changed them to use valid values. The Spark Connect `Time literal` test now
expects the truncated values.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]