You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/30 18:29:12 UTC

[GitHub] [arrow-rs] viirya commented on a diff in pull request #3222: Move tests which require chrono-tz feature from `arrow-cast` to `arrow`

viirya commented on code in PR #3222:
URL: https://github.com/apache/arrow-rs/pull/3222#discussion_r1036313470


##########
arrow/tests/array_cast.rs:
##########
@@ -0,0 +1,417 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow_array::builder::{
+    PrimitiveDictionaryBuilder, StringDictionaryBuilder, UnionBuilder,
+};
+use arrow_array::types::{
+    ArrowDictionaryKeyType, Int16Type, Int32Type, Int64Type, Int8Type,
+    TimestampMicrosecondType, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
+};
+use arrow_array::{
+    Array, ArrayRef, BinaryArray, BooleanArray, Date32Array, Date64Array,
+    Decimal128Array, DurationMicrosecondArray, DurationMillisecondArray,
+    DurationNanosecondArray, DurationSecondArray, FixedSizeBinaryArray,
+    FixedSizeListArray, Float16Array, Float32Array, Float64Array, Int16Array, Int32Array,
+    Int64Array, Int8Array, IntervalDayTimeArray, IntervalMonthDayNanoArray,
+    IntervalYearMonthArray, LargeBinaryArray, LargeListArray, LargeStringArray,
+    ListArray, NullArray, PrimitiveArray, StringArray, StructArray,
+    Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
+    Time64NanosecondArray, TimestampMicrosecondArray, TimestampMillisecondArray,
+    TimestampNanosecondArray, TimestampSecondArray, UInt16Array, UInt32Array,
+    UInt64Array, UInt8Array, UnionArray,
+};
+use arrow_buffer::Buffer;
+use arrow_cast::{can_cast_types, cast};
+use arrow_data::ArrayData;
+use arrow_schema::{ArrowError, DataType, Field, IntervalUnit, TimeUnit, UnionMode};
+use half::f16;
+use std::sync::Arc;
+
+#[test]
+#[cfg(feature = "chrono-tz")]
+fn test_cast_timestamp_to_string() {
+    let a = TimestampMillisecondArray::from(vec![
+        Some(864000000005),
+        Some(1545696000001),
+        None,
+    ])
+    .with_timezone("UTC".to_string());
+    let array = Arc::new(a) as ArrayRef;
+    dbg!(&array);
+    let b = cast(&array, &DataType::Utf8).unwrap();
+    let c = b.as_any().downcast_ref::<StringArray>().unwrap();
+    assert_eq!(&DataType::Utf8, c.data_type());
+    assert_eq!("1997-05-19 00:00:00.005 +00:00", c.value(0));
+    assert_eq!("2018-12-25 00:00:00.001 +00:00", c.value(1));
+    assert!(c.is_null(2));
+}
+
+#[test]
+#[cfg_attr(miri, ignore)] // running forever
+#[cfg(feature = "chrono-tz")]
+fn test_can_cast_types() {
+    // this function attempts to ensure that can_cast_types stays
+    // in sync with cast.  It simply tries all combinations of
+    // types and makes sure that if `can_cast_types` returns
+    // true, so does `cast`
+
+    let all_types = get_all_types();
+
+    for array in get_arrays_of_all_types() {
+        for to_type in &all_types {
+            println!("Test casting {:?} --> {:?}", array.data_type(), to_type);
+            let cast_result = cast(&array, to_type);
+            let reported_cast_ability = can_cast_types(array.data_type(), to_type);
+
+            // check for mismatch
+            match (cast_result, reported_cast_ability) {
+                (Ok(_), false) => {
+                    panic!("Was able to cast array {:?} from {:?} to {:?} but can_cast_types reported false",
+                           array, array.data_type(), to_type)
+                }
+                (Err(e), true) => {
+                    panic!("Was not able to cast array {:?} from {:?} to {:?} but can_cast_types reported true. \
+                                Error was {:?}",
+                           array, array.data_type(), to_type, e)
+                }
+                // otherwise it was a match
+                _ => {}
+            };
+        }
+    }
+}
+
+/// Create instances of arrays with varying types for cast tests
+#[cfg(feature = "chrono-tz")]

Review Comment:
   Okay. Moved.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org