You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by vi...@apache.org on 2022/11/21 21:50:22 UTC

[arrow-rs] branch master updated: Add collect.rs example (#3153)

This is an automated email from the ASF dual-hosted git repository.

viirya pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 870f0fa7d Add collect.rs example (#3153)
870f0fa7d is described below

commit 870f0fa7d8975247df7bc734ce08e5f807027f53
Author: Liang-Chi Hsieh <vi...@gmail.com>
AuthorDate: Mon Nov 21 13:50:17 2022 -0800

    Add collect.rs example (#3153)
    
    * Add collect.rs example
    
    * Remove unnecessary extern crate.
---
 arrow/examples/README.md  |  2 +-
 arrow/examples/collect.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/arrow/examples/README.md b/arrow/examples/README.md
index 41ffd8233..314ce9c62 100644
--- a/arrow/examples/README.md
+++ b/arrow/examples/README.md
@@ -20,7 +20,7 @@
 # Examples
 
 - [`builders.rs`](builders.rs): Using the Builder API
-- `collect` (TODO): Using the `FromIter` API
+- [`collect.rs`](collect.rs): Using the `FromIter` API
 - [`dynamic_types.rs`](dynamic_types.rs):
 - [`read_csv.rs`](read_csv.rs): Reading CSV files with explict schema, pretty printing Arrays
 - [`read_csv_infer_schema.rs`](read_csv_infer_schema.rs): Reading CSV files, pretty printing Arrays
diff --git a/arrow/examples/collect.rs b/arrow/examples/collect.rs
new file mode 100644
index 000000000..d523a8036
--- /dev/null
+++ b/arrow/examples/collect.rs
@@ -0,0 +1,86 @@
+// 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.
+
+///! `FromIterator` API is implemented for different array types to easily create them
+/// from values.
+use arrow::array::Array;
+use arrow_array::types::Int32Type;
+use arrow_array::{Float32Array, Int32Array, Int8Array, ListArray};
+
+fn main() {
+    // Primitive Arrays
+    //
+    // Primitive arrays are arrays of fixed-width primitive types (u8, u16, u32,
+    // u64, i8, i16, i32, i64, f32, f64, etc.)
+
+    // Create an Int8Array with 4 values
+    let array: Int8Array = vec![1, 2, 3, 4].into_iter().collect();
+    println!("{:?}", array);
+
+    // Arrays can also be built from `Vec<Option<T>>`. `None`
+    // represents a null value in the array.
+    let array: Int8Array = vec![Some(1_i8), Some(2), None, Some(3)]
+        .into_iter()
+        .collect();
+    println!("{:?}", array);
+    assert!(array.is_null(2));
+
+    let array: Float32Array = [Some(1.0_f32), Some(2.3), None].into_iter().collect();
+    println!("{:?}", array);
+    assert_eq!(array.value(0), 1.0_f32);
+    assert_eq!(array.value(1), 2.3_f32);
+    assert!(array.is_null(2));
+
+    // Although not implementing `FromIterator`, ListArrays provides `from_iter_primitive`
+    // function to create ListArrays from `Vec<Option<Vec<Option<T>>>>`. The outer `None`
+    // represents a null list, the inner `None` represents a null value in a list.
+    let data = vec![
+        Some(vec![]),
+        None,
+        Some(vec![Some(3), None, Some(5), Some(19)]),
+        Some(vec![Some(6), Some(7)]),
+    ];
+    let list_array = ListArray::from_iter_primitive::<Int32Type, _, _>(data);
+
+    assert!(!list_array.is_valid(1));
+
+    let list0 = list_array.value(0);
+    let list2 = list_array.value(2);
+    let list3 = list_array.value(3);
+
+    assert_eq!(
+        &[] as &[i32],
+        list0
+            .as_any()
+            .downcast_ref::<Int32Array>()
+            .unwrap()
+            .values()
+    );
+    assert!(!list2
+        .as_any()
+        .downcast_ref::<Int32Array>()
+        .unwrap()
+        .is_valid(1));
+    assert_eq!(
+        &[6, 7],
+        list3
+            .as_any()
+            .downcast_ref::<Int32Array>()
+            .unwrap()
+            .values()
+    );
+}