comphead commented on code in PR #7781:
URL: https://github.com/apache/datafusion-comet/pull/7781#discussion_r4743955999
##########
native/spark-expr/src/utils.rs:
##########
@@ -152,12 +116,10 @@ pub fn array_with_timezone(
DataType::Dictionary(_, value_type)
if matches!(value_type.as_ref(), &DataType::Timestamp(_, _)) =>
{
- let dict = as_dictionary_array::<Int32Type>(&array);
- let array =
as_primitive_array::<TimestampMicrosecondType>(dict.values());
+ let dict = array.as_any_dictionary();
let array_with_timezone =
- array_with_timezone(Arc::new(array.clone()) as ArrayRef,
timezone, to_type)?;
- let dict = dict.with_values(array_with_timezone);
- Ok(Arc::new(dict))
+ array_with_timezone(Arc::clone(dict.values()), timezone,
to_type)?;
+ Ok(dict.with_values(array_with_timezone))
}
Review Comment:
Building on the review comment from sunchao: this arm runs `Cast` semantics
on the dictionary values, while a plain column goes through
`parquet_convert_array_impl`, which applies no session-zone shift and uses the
checked `millis` to `micros` arm. The values are cast once, which is right, but
a simpler place for the fix is `parquet_support.rs`. It already has a
`(Dictionary(_, Binary), Utf8)` arm (line 386) that converts the dictionary
values once and `take`s them. A timestamp counterpart that runs the existing
arms on the values, plus adding `(Dictionary(_, Timestamp), Timestamp)` to the
`matches!` in `replace_with_spark_cast` (`schema_adapter.rs` near line 1500) so
it reaches `CometCastColumnExpr`, would give plain and dictionary columns one
result and let the tests compare the two directly.
##########
native/spark-expr/src/utils.rs:
##########
@@ -475,6 +437,61 @@ mod tests {
assert!(array_with_timezone(ntz, String::new(), Some(&utc)).is_err());
}
+ #[test]
+ fn test_array_with_timezone_dictionary_non_microsecond_timestamp() {
+ use arrow::array::{
+ DictionaryArray, Int32Array, Int8Array, TimestampMillisecondArray,
TimestampSecondArray,
+ };
+ use arrow::datatypes::{Int32Type, Int8Type};
+
+ let millis_values =
Arc::new(TimestampMillisecondArray::from(vec![7i64]));
+ let millis_dict: ArrayRef = Arc::new(
+ DictionaryArray::<Int32Type>::try_new(Int32Array::from(vec![0]),
millis_values)
+ .unwrap(),
+ );
+ let ntz_us = DataType::Timestamp(TimeUnit::Microsecond, None);
+ let res = array_with_timezone(Arc::clone(&millis_dict),
"UTC".to_string(), Some(&ntz_us))
+ .unwrap();
+ assert_eq!(
+ res.data_type(),
+ &DataType::Dictionary(
+ Box::new(DataType::Int32),
+ Box::new(DataType::Timestamp(TimeUnit::Millisecond, None)),
+ )
+ );
+
+ let int8_millis_values =
Arc::new(TimestampMillisecondArray::from(vec![7i64]));
+ let int8_dict: ArrayRef = Arc::new(
+ DictionaryArray::<Int8Type>::try_new(Int8Array::from(vec![0]),
int8_millis_values)
+ .unwrap(),
+ );
+ let utc_us = DataType::Timestamp(TimeUnit::Microsecond,
Some("UTC".into()));
+ let res_utc = array_with_timezone(int8_dict, "UTC".to_string(),
Some(&utc_us)).unwrap();
+ assert_eq!(
+ res_utc.data_type(),
+ &DataType::Dictionary(
+ Box::new(DataType::Int8),
+ Box::new(DataType::Timestamp(
+ TimeUnit::Millisecond,
+ Some("UTC".into())
+ )),
+ )
+ );
+
+ let sec_values = Arc::new(TimestampSecondArray::from(vec![7i64]));
+ let sec_dict: ArrayRef = Arc::new(
+ DictionaryArray::<Int32Type>::try_new(Int32Array::from(vec![0]),
sec_values).unwrap(),
+ );
+ let res_sec = array_with_timezone(sec_dict, "UTC".to_string(),
Some(&ntz_us)).unwrap();
+ assert_eq!(
+ res_sec.data_type(),
+ &DataType::Dictionary(
+ Box::new(DataType::Int32),
+ Box::new(DataType::Timestamp(TimeUnit::Second, None)),
+ )
+ );
+ }
Review Comment:
The assertions only check `data_type()`, so the test passes when the values
are wrong. Please assert the values and use a non-UTC session zone with a
tz-aware target, because that is where `timestamp_ntz_to_timestamp` shifts
them. The `Second` case only covers an NTZ target. With a tz-aware target a
`Second` dictionary comes back unshifted from `timestamp_ntz_to_timestamp`, so
either drop the case or cover that target and say what is expected.
##########
native/spark-expr/src/utils.rs:
##########
@@ -72,10 +68,12 @@ pub fn array_with_timezone(
to_type: Option<&DataType>,
) -> Result<ArrayRef, ArrowError> {
match array.data_type() {
- DataType::Timestamp(TimeUnit::Millisecond, None) => {
+ DataType::Timestamp(_, None) => {
require_timezone(&timezone)?;
match to_type {
- Some(DataType::Utf8) | Some(DataType::Date32) => Ok(array),
+ Some(DataType::Utf8)
+ | Some(DataType::Date32)
+ | Some(DataType::Timestamp(_, None)) => Ok(array),
Review Comment:
This makes every NTZ array pass through for every unit, where `Timestamp(ms,
None)` and `Timestamp(us, None)` to another NTZ type returned the `Cannot
convert` error before. The table in the comment above `array_with_timezone`
still says NTZ to any other type is not supported, so it should list NTZ to NTZ
as a pass-through. The function also now returns `Ok` for a unit change it does
not perform, so the caller must run the Arrow cast afterwards. A short comment
saying that would keep the next reader from treating it as a conversion.
##########
native/core/src/parquet/schema_adapter.rs:
##########
@@ -3083,6 +3083,72 @@ pub(crate) mod test {
Ok(())
}
+ #[tokio::test]
+ async fn dictionary_timestamp_millis_unwrapped_to_micros() -> Result<(),
DataFusionError> {
+ let keys = Int32Array::from(vec![0]);
+ let values = TimestampMillisecondArray::from(vec![7i64]);
+ let dict = DictionaryArray::<Int32Type>::try_new(keys,
Arc::new(values))?;
+ let schema = Arc::new(Schema::new(vec![Field::new(
+ "ts",
+ DataType::Dictionary(
+ Box::new(DataType::Int32),
+ Box::new(DataType::Timestamp(TimeUnit::Millisecond, None)),
+ ),
+ true,
+ )]));
+ let batch = RecordBatch::try_new(schema, vec![Arc::new(dict)])?;
+ let required_schema = Arc::new(Schema::new(vec![Field::new(
+ "ts",
+ DataType::Timestamp(TimeUnit::Microsecond, None),
+ true,
+ )]));
+
+ let read = roundtrip(&batch, required_schema).await?;
+
+ assert_eq!(
+ read.column(0).data_type(),
+ &DataType::Timestamp(TimeUnit::Microsecond, None)
+ );
+ let ts = read.column(0).as_primitive::<TimestampMicrosecondType>();
+ assert_eq!(ts.value(0), 7_000);
+ Ok(())
+ }
+
+ #[tokio::test]
Review Comment:
The first test is a subset of the second, since `7` ms to `7_000` µs appears
in both. One test with the edge cases is enough. `roundtrip` also fixes the
session zone to `UTC` and the requested type is NTZ, so neither test can tell
the `Cast` path from the Parquet path. A required type of `Timestamp(us,
Some("UTC"))` under `America/Los_Angeles` through `scan_parquet`, compared with
the same data in a plain column, is the case sunchao reports as different.
--
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]