Skip to content
17 changes: 17 additions & 0 deletions rust/arrow/src/compute/kernels/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
//! assert_eq!(7.0, c.value(2));
//! ```

use std::str;
use std::sync::Arc;

use crate::array::*;
Expand Down Expand Up @@ -203,6 +204,22 @@ pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef> {
Int64 => cast_numeric_to_string::<Int64Type>(array),
Float32 => cast_numeric_to_string::<Float32Type>(array),
Float64 => cast_numeric_to_string::<Float64Type>(array),
Binary => {
let from = array.as_any().downcast_ref::<BinaryArray>().unwrap();
let mut b = StringBuilder::new(array.len());
for i in 0..array.len() {
if array.is_null(i) {
b.append_null()?;
} else {
match str::from_utf8(from.value(i)) {
Ok(s) => b.append_value(s)?,
Err(_) => b.append_null()?, // not valid UTF8
}
}
}

Ok(Arc::new(b.finish()) as ArrayRef)
}
_ => Err(ArrowError::ComputeError(format!(
"Casting from {:?} to {:?} not supported",
from_type, to_type,
Expand Down
2 changes: 1 addition & 1 deletion rust/datafusion/examples/parquet_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> Result<()> {
)?;

// simple selection
let sql = "SELECT int_col, double_col, date_string_col FROM alltypes_plain WHERE id > 1 AND tinyint_col < double_col";
let sql = "SELECT int_col, double_col, CAST(date_string_col as VARCHAR) FROM alltypes_plain WHERE id > 1 AND tinyint_col < double_col";

// create the query plan
let plan = ctx.create_logical_plan(&sql)?;
Expand Down
4 changes: 2 additions & 2 deletions rust/datafusion/src/datasource/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl CsvFile {
}

impl TableProvider for CsvFile {
fn schema(&self) -> &Arc<Schema> {
&self.schema
fn schema(&self) -> Arc<Schema> {
self.schema.clone()
}

fn scan(
Expand Down
2 changes: 1 addition & 1 deletion rust/datafusion/src/datasource/datasource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub type ScanResult = Arc<Mutex<dyn BatchIterator>>;
/// Source table
pub trait TableProvider {
/// Get a reference to the schema for this table
fn schema(&self) -> &Arc<Schema>;
fn schema(&self) -> Arc<Schema>;

/// Perform a scan of a table and return a sequence of iterators over the data (one
/// iterator per partition)
Expand Down
4 changes: 2 additions & 2 deletions rust/datafusion/src/datasource/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl MemTable {
}

impl TableProvider for MemTable {
fn schema(&self) -> &Arc<Schema> {
&self.schema
fn schema(&self) -> Arc<Schema> {
self.schema.clone()
}

fn scan(
Expand Down
Loading