proof_of_sql/base/arrow/owned_and_arrow_conversions.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
//! This module provide `From` and `TryFrom` implementations to go between arrow and owned types
//! The mapping is as follows:
//! `OwnedType` <-> `Array/ArrayRef`
//! `OwnedTable` <-> `RecordBatch`
//! `Boolean` <-> `Boolean`
//! `BigInt` <-> `Int64`
//! `VarChar` <-> `Utf8/String`
//! `Int128` <-> `Decimal128(38,0)`
//! `Decimal75` <-> `S`
//!
//! Note: this converts `Int128` values to `Decimal128(38,0)`, which are backed by `i128`.
//! This is because there is no `Int128` type in Arrow.
//! This does not check that the values are less than 39 digits.
//! However, the actual arrow backing `i128` is the correct value.
use super::scalar_and_i256_conversions::{convert_i256_to_scalar, convert_scalar_to_i256};
use crate::base::{
database::{OwnedColumn, OwnedTable, OwnedTableError},
map::IndexMap,
math::decimal::Precision,
scalar::Scalar,
};
use alloc::sync::Arc;
use arrow::{
array::{
ArrayRef, BooleanArray, Decimal128Array, Decimal256Array, Int16Array, Int32Array,
Int64Array, Int8Array, StringArray, TimestampMicrosecondArray, TimestampMillisecondArray,
TimestampNanosecondArray, TimestampSecondArray,
},
datatypes::{i256, DataType, Schema, SchemaRef, TimeUnit as ArrowTimeUnit},
error::ArrowError,
record_batch::RecordBatch,
};
use proof_of_sql_parser::{
posql_time::{PoSQLTimeUnit, PoSQLTimeZone, PoSQLTimestampError},
Identifier, ParseError,
};
use snafu::Snafu;
#[derive(Snafu, Debug)]
#[non_exhaustive]
/// Errors cause by conversions between Arrow and owned types.
pub enum OwnedArrowConversionError {
/// This error occurs when trying to convert from an unsupported arrow type.
#[snafu(display(
"unsupported type: attempted conversion from ArrayRef of type {datatype} to OwnedColumn"
))]
UnsupportedType {
/// The unsupported datatype
datatype: DataType,
},
/// This error occurs when trying to convert from a record batch with duplicate identifiers (e.g. `"a"` and `"A"`).
#[snafu(display("conversion resulted in duplicate identifiers"))]
DuplicateIdentifiers,
/// This error occurs when convering from a record batch name to an identifier fails. (Which may my impossible.)
#[snafu(transparent)]
FieldParseFail {
/// The underlying source error
source: ParseError,
},
/// This error occurs when creating an owned table fails, which should only occur when there are zero columns.
#[snafu(transparent)]
InvalidTable {
/// The underlying source error
source: OwnedTableError,
},
/// This error occurs when trying to convert from an Arrow array with nulls.
#[snafu(display("null values are not supported in OwnedColumn yet"))]
NullNotSupportedYet,
/// Using `TimeError` to handle all time-related errors
#[snafu(transparent)]
TimestampConversionError {
/// The underlying source error
source: PoSQLTimestampError,
},
}
/// # Panics
///
/// Will panic if setting precision and scale fails when converting `OwnedColumn::Int128`.
/// Will panic if setting precision and scale fails when converting `OwnedColumn::Decimal75`.
/// Will panic if trying to convert `OwnedColumn::Scalar`, as this conversion is not implemented
impl<S: Scalar> From<OwnedColumn<S>> for ArrayRef {
fn from(value: OwnedColumn<S>) -> Self {
match value {
OwnedColumn::Boolean(col) => Arc::new(BooleanArray::from(col)),
OwnedColumn::TinyInt(col) => Arc::new(Int8Array::from(col)),
OwnedColumn::SmallInt(col) => Arc::new(Int16Array::from(col)),
OwnedColumn::Int(col) => Arc::new(Int32Array::from(col)),
OwnedColumn::BigInt(col) => Arc::new(Int64Array::from(col)),
OwnedColumn::Int128(col) => Arc::new(
Decimal128Array::from(col)
.with_precision_and_scale(38, 0)
.unwrap(),
),
OwnedColumn::Decimal75(precision, scale, col) => {
let converted_col: Vec<i256> = col.iter().map(convert_scalar_to_i256).collect();
Arc::new(
Decimal256Array::from(converted_col)
.with_precision_and_scale(precision.value(), scale)
.unwrap(),
)
}
OwnedColumn::Scalar(_) => unimplemented!("Cannot convert Scalar type to arrow type"),
OwnedColumn::VarChar(col) => Arc::new(StringArray::from(col)),
OwnedColumn::TimestampTZ(time_unit, _, col) => match time_unit {
PoSQLTimeUnit::Second => Arc::new(TimestampSecondArray::from(col)),
PoSQLTimeUnit::Millisecond => Arc::new(TimestampMillisecondArray::from(col)),
PoSQLTimeUnit::Microsecond => Arc::new(TimestampMicrosecondArray::from(col)),
PoSQLTimeUnit::Nanosecond => Arc::new(TimestampNanosecondArray::from(col)),
},
}
}
}
impl<S: Scalar> TryFrom<OwnedTable<S>> for RecordBatch {
type Error = ArrowError;
fn try_from(value: OwnedTable<S>) -> Result<Self, Self::Error> {
if value.is_empty() {
Ok(RecordBatch::new_empty(SchemaRef::new(Schema::empty())))
} else {
RecordBatch::try_from_iter(
value
.into_inner()
.into_iter()
.map(|(identifier, owned_column)| (identifier, ArrayRef::from(owned_column))),
)
}
}
}
impl<S: Scalar> TryFrom<ArrayRef> for OwnedColumn<S> {
type Error = OwnedArrowConversionError;
fn try_from(value: ArrayRef) -> Result<Self, Self::Error> {
Self::try_from(&value)
}
}
impl<S: Scalar> TryFrom<&ArrayRef> for OwnedColumn<S> {
type Error = OwnedArrowConversionError;
#[allow(clippy::too_many_lines)]
/// # Panics
///
/// Will panic if downcasting fails for the following types:
/// - `BooleanArray` when converting from `DataType::Boolean`.
/// - `Int16Array` when converting from `DataType::Int16`.
/// - `Int32Array` when converting from `DataType::Int32`.
/// - `Int64Array` when converting from `DataType::Int64`.
/// - `Decimal128Array` when converting from `DataType::Decimal128(38, 0)`.
/// - `Decimal256Array` when converting from `DataType::Decimal256` if precision is less than or equal to 75.
/// - `StringArray` when converting from `DataType::Utf8`.
fn try_from(value: &ArrayRef) -> Result<Self, Self::Error> {
match &value.data_type() {
// Arrow uses a bit-packed representation for booleans.
// Hence we need to unpack the bits to get the actual boolean values.
DataType::Boolean => Ok(Self::Boolean(
value
.as_any()
.downcast_ref::<BooleanArray>()
.unwrap()
.iter()
.collect::<Option<Vec<bool>>>()
.ok_or(OwnedArrowConversionError::NullNotSupportedYet)?,
)),
DataType::Int8 => Ok(Self::TinyInt(
value
.as_any()
.downcast_ref::<Int8Array>()
.unwrap()
.values()
.to_vec(),
)),
DataType::Int16 => Ok(Self::SmallInt(
value
.as_any()
.downcast_ref::<Int16Array>()
.unwrap()
.values()
.to_vec(),
)),
DataType::Int32 => Ok(Self::Int(
value
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.values()
.to_vec(),
)),
DataType::Int64 => Ok(Self::BigInt(
value
.as_any()
.downcast_ref::<Int64Array>()
.unwrap()
.values()
.to_vec(),
)),
DataType::Decimal128(38, 0) => Ok(Self::Int128(
value
.as_any()
.downcast_ref::<Decimal128Array>()
.unwrap()
.values()
.to_vec(),
)),
DataType::Decimal256(precision, scale) if *precision <= 75 => Ok(Self::Decimal75(
Precision::new(*precision).expect("precision is less than 76"),
*scale,
value
.as_any()
.downcast_ref::<Decimal256Array>()
.unwrap()
.values()
.iter()
.map(convert_i256_to_scalar)
.map(Option::unwrap)
.collect(),
)),
DataType::Utf8 => Ok(Self::VarChar(
value
.as_any()
.downcast_ref::<StringArray>()
.unwrap()
.iter()
.map(|s| s.unwrap().to_string())
.collect(),
)),
DataType::Timestamp(time_unit, timezone) => match time_unit {
ArrowTimeUnit::Second => {
let array = value
.as_any()
.downcast_ref::<TimestampSecondArray>()
.expect(
"This cannot fail, all Arrow TimeUnits are mapped to PoSQL TimeUnits",
);
let timestamps = array.values().iter().copied().collect::<Vec<i64>>();
Ok(OwnedColumn::TimestampTZ(
PoSQLTimeUnit::Second,
PoSQLTimeZone::try_from(timezone)?,
timestamps,
))
}
ArrowTimeUnit::Millisecond => {
let array = value
.as_any()
.downcast_ref::<TimestampMillisecondArray>()
.expect(
"This cannot fail, all Arrow TimeUnits are mapped to PoSQL TimeUnits",
);
let timestamps = array.values().iter().copied().collect::<Vec<i64>>();
Ok(OwnedColumn::TimestampTZ(
PoSQLTimeUnit::Millisecond,
PoSQLTimeZone::try_from(timezone)?,
timestamps,
))
}
ArrowTimeUnit::Microsecond => {
let array = value
.as_any()
.downcast_ref::<TimestampMicrosecondArray>()
.expect(
"This cannot fail, all Arrow TimeUnits are mapped to PoSQL TimeUnits",
);
let timestamps = array.values().iter().copied().collect::<Vec<i64>>();
Ok(OwnedColumn::TimestampTZ(
PoSQLTimeUnit::Microsecond,
PoSQLTimeZone::try_from(timezone)?,
timestamps,
))
}
ArrowTimeUnit::Nanosecond => {
let array = value
.as_any()
.downcast_ref::<TimestampNanosecondArray>()
.expect(
"This cannot fail, all Arrow TimeUnits are mapped to PoSQL TimeUnits",
);
let timestamps = array.values().iter().copied().collect::<Vec<i64>>();
Ok(OwnedColumn::TimestampTZ(
PoSQLTimeUnit::Nanosecond,
PoSQLTimeZone::try_from(timezone)?,
timestamps,
))
}
},
&data_type => Err(OwnedArrowConversionError::UnsupportedType {
datatype: data_type.clone(),
}),
}
}
}
impl<S: Scalar> TryFrom<RecordBatch> for OwnedTable<S> {
type Error = OwnedArrowConversionError;
fn try_from(value: RecordBatch) -> Result<Self, Self::Error> {
let num_columns = value.num_columns();
let table: Result<IndexMap<_, _>, Self::Error> = value
.schema()
.fields()
.iter()
.zip(value.columns())
.map(|(field, array_ref)| {
let owned_column = OwnedColumn::try_from(array_ref)?;
let identifier = Identifier::try_new(field.name())?; //This may always succeed.
Ok((identifier, owned_column))
})
.collect();
let owned_table = Self::try_new(table?)?;
if num_columns == owned_table.num_columns() {
Ok(owned_table)
} else {
Err(OwnedArrowConversionError::DuplicateIdentifiers)
}
}
}