Browse Source

fixed string conversion to use reference

tags/v0.3.11-rc1
Haroon Tahir haixuantao 10 months ago
parent
commit
9530603944
3 changed files with 10 additions and 12 deletions
  1. +2
    -1
      libraries/arrow-convert/src/from_impls.rs
  2. +5
    -5
      libraries/arrow-convert/src/into_impls.rs
  3. +3
    -6
      libraries/arrow-convert/tests/conversion_test.rs

+ 2
- 1
libraries/arrow-convert/src/from_impls.rs View File

@@ -349,7 +349,8 @@ impl<'a> TryFrom<&'a ArrowData> for Vec<f64> {
impl<'a> TryFrom<&'a ArrowData> for String {
type Error = eyre::Report;
fn try_from(value: &'a ArrowData) -> Result<Self, Self::Error> {
let string_array: Vec<String> = <Arc<dyn arrow::array::Array> as Clone>::clone(&value).try_into_collection()?;
let string_array: Vec<String> =
<Arc<dyn arrow::array::Array> as Clone>::clone(&value).try_into_collection()?;
return Ok(string_array[0].clone());
}
}


+ 5
- 5
libraries/arrow-convert/src/into_impls.rs View File

@@ -1,6 +1,6 @@
use arrow::array::{PrimitiveArray, StringArray, ArrayRef, Array };
use arrow_convert::serialize::TryIntoArrow;
use crate::IntoArrow;
use arrow::array::{Array, ArrayRef, PrimitiveArray, StringArray};
use arrow_convert::serialize::TryIntoArrow;

impl IntoArrow for bool {
type A = arrow::array::BooleanArray;
@@ -146,7 +146,7 @@ impl IntoArrow for () {
}
}

impl IntoArrow for String {
impl IntoArrow for &String {
type A = StringArray;

fn into_arrow(self) -> Self::A {
@@ -154,12 +154,12 @@ impl IntoArrow for String {
Ok(array_ref) => {
let array_ref: ArrayRef = array_ref; // Ensuring explicit type annotation
let array: &dyn Array = array_ref.as_ref(); // Dereference Arc<dyn Array>
if let Some(string_array) = array.as_any().downcast_ref::<StringArray>() {
string_array.clone()
} else {
eprintln!("Failed to downcast to StringArray.");
StringArray::from(vec![self]) // Fallback in case of failure
StringArray::from(vec![""]) // Fallback in case of failure
}
}
Err(err) => {


+ 3
- 6
libraries/arrow-convert/tests/conversion_test.rs View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use std::convert::TryFrom;
use dora_arrow_convert::{ArrowData, IntoArrow};
use std::convert::TryFrom;
use std::sync::Arc;

#[cfg(test)]
mod tests {
@@ -17,7 +17,6 @@ mod tests {
Ok(())
}


#[test]
fn test_u8_round_trip() -> Result<(), Report> {
let value_u8: u8 = 42;
@@ -177,7 +176,6 @@ mod tests {
Ok(())
}

#[test]
fn test_vec_i8_round_trip() -> Result<(), Report> {
let value_vec_i8: Vec<i8> = vec![-1, -2, -3, -4, -5];
@@ -220,7 +218,7 @@ mod tests {

#[test]
fn test_vec_f32_round_trip() -> Result<(), Report> {
let value_vec_f32 : Vec<f32> = vec![-1.5, -2.6, -3.2, -4.5, -5.1];
let value_vec_f32: Vec<f32> = vec![-1.5, -2.6, -3.2, -4.5, -5.1];
let arrow_array = value_vec_f32.clone().into_arrow();
let data: ArrowData = ArrowData(Arc::new(arrow_array));
let result_vec_f32: Vec<f32> = TryFrom::try_from(&data)?;
@@ -238,4 +236,3 @@ mod tests {
Ok(())
}
}


Loading…
Cancel
Save