Skip to content

Commit 3617e6c

Browse files
Into arrow with hint (#1730)
1 parent 830f49d commit 3617e6c

File tree

5 files changed

+156
-73
lines changed

5 files changed

+156
-73
lines changed

vortex-array/src/array/varbin/canonical.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ impl IntoCanonical for VarBinArray {
2828
// Arrow representation.
2929
varbin_to_arrow(&self)
3030
}
31+
32+
fn into_arrow_with_data_type(self, data_type: &DataType) -> VortexResult<ArrayRef> {
33+
let array_ref = self.into_arrow()?;
34+
35+
Ok(if array_ref.data_type() != data_type {
36+
arrow_cast::cast(array_ref.as_ref(), data_type)?
37+
} else {
38+
array_ref
39+
})
40+
}
3141
}
3242

3343
#[cfg(test)]

vortex-array/src/arrow/datum.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use arrow_array::{Array, ArrayRef, Datum as ArrowDatum};
22
use vortex_error::VortexError;
33

44
use crate::compute::slice;
5-
use crate::stats::{ArrayStatistics, Stat};
65
use crate::{ArrayData, IntoCanonical};
76

87
/// A wrapper around a generic Arrow array that can be used as a Datum in Arrow compute.
8+
#[derive(Debug)]
99
pub struct Datum {
1010
array: ArrayRef,
1111
is_scalar: bool,
@@ -15,11 +15,7 @@ impl TryFrom<ArrayData> for Datum {
1515
type Error = VortexError;
1616

1717
fn try_from(array: ArrayData) -> Result<Self, Self::Error> {
18-
if array
19-
.statistics()
20-
.get_as::<bool>(Stat::IsConstant)
21-
.unwrap_or_default()
22-
{
18+
if array.is_constant() {
2319
Ok(Self {
2420
array: slice(array, 0, 1)?.into_arrow()?,
2521
is_scalar: true,

vortex-array/src/arrow/record_batch.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use arrow_array::cast::as_struct_array;
1+
use arrow_array::cast::AsArray;
22
use arrow_array::RecordBatch;
3+
use arrow_schema::{DataType, Schema};
34
use itertools::Itertools;
45
use vortex_error::{vortex_err, VortexError, VortexResult};
56

@@ -41,16 +42,19 @@ impl TryFrom<ArrayData> for RecordBatch {
4142
vortex_err!("RecordBatch can only be constructed from a Vortex StructArray: {err}")
4243
})?;
4344

44-
RecordBatch::try_from(struct_arr)
45+
struct_arr.into_record_batch()
4546
}
4647
}
4748

48-
impl TryFrom<StructArray> for RecordBatch {
49-
type Error = VortexError;
49+
impl StructArray {
50+
pub fn into_record_batch(self) -> VortexResult<RecordBatch> {
51+
let array_ref = self.into_array().into_arrow()?;
52+
Ok(RecordBatch::from(array_ref.as_struct()))
53+
}
5054

51-
fn try_from(value: StructArray) -> VortexResult<Self> {
52-
let array_ref = value.into_canonical()?.into_arrow()?;
53-
let struct_array = as_struct_array(array_ref.as_ref());
54-
Ok(Self::from(struct_array))
55+
pub fn into_record_batch_with_schema(self, schema: &Schema) -> VortexResult<RecordBatch> {
56+
let data_type = DataType::Struct(schema.fields.clone());
57+
let array_ref = self.into_array().into_arrow_with_data_type(&data_type)?;
58+
Ok(RecordBatch::from(array_ref.as_struct()))
5559
}
5660
}

0 commit comments

Comments
 (0)