Skip to main content

datafusion_common/utils/
string_utils.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Utilities for working with strings
6
7use arrow::array::{Array, AsArray};
8use arrow::datatypes::DataType;
9
10/// Convenient function to convert an Arrow string array to a vector of strings
11pub fn string_array_to_vec(array: &dyn Array) -> Vec<Option<&str>> {
12    match array.data_type() {
13        DataType::Utf8 => array.as_string::<i32>().iter().collect(),
14        DataType::LargeUtf8 => array.as_string::<i64>().iter().collect(),
15        DataType::Utf8View => array.as_string_view().iter().collect(),
16        _ => unreachable!(),
17    }
18}