Apache DataFusion SQL Query Engine
[Ballista] Add Decimal128, Date64, TimestampSecond, TimestampMillisecond, Interv… (#1659)
gaojun2048bf68073b5a53
diff --git a/ballista/rust/core/proto/ballista.proto b/ballista/rust/core/proto/ballista.protoindex b3adb36685..15a7342d7b 100644--- a/ballista/rust/core/proto/ballista.proto+++ b/ballista/rust/core/proto/ballista.proto@@ -1179,9 +1179,21 @@ message ScalarValue{ScalarType null_list_value = 18;PrimitiveScalarType null_value = 19;+ Decimal128 decimal128_value = 20;+ int64 date_64_value = 21;+ int64 time_second_value = 22;+ int64 time_millisecond_value = 23;+ int32 interval_yearmonth_value = 24;+ int64 interval_daytime_value = 25;}}+message Decimal128{+ bytes value = 1;+ int64 p = 2;+ int64 s = 3;+}+// Contains all valid datafusion scalar type except for// Listenum PrimitiveScalarType{@@ -1203,6 +1215,13 @@ enum PrimitiveScalarType{TIME_MICROSECOND = 14;TIME_NANOSECOND = 15;NULL = 16;++ DECIMAL128 = 17;+ DATE64 = 20;+ TIME_SECOND = 21;+ TIME_MILLISECOND = 22;+ INTERVAL_YEARMONTH = 23;+ INTERVAL_DAYTIME = 24;}message ScalarType{diff --git a/ballista/rust/core/src/serde/logical_plan/from_proto.rs b/ballista/rust/core/src/serde/logical_plan/from_proto.rsindex 427e6bf713..5684855914 100644--- a/ballista/rust/core/src/serde/logical_plan/from_proto.rs+++ b/ballista/rust/core/src/serde/logical_plan/from_proto.rs@@ -18,7 +18,9 @@//! Serde code to convert from protocol buffers to Rust data structures.use crate::error::BallistaError;-use crate::serde::{from_proto_binary_op, proto_error, protobuf, str_to_byte};+use crate::serde::{+ from_proto_binary_op, proto_error, protobuf, str_to_byte, vec_to_array,+};use crate::{convert_box_required, convert_required};use datafusion::arrow::datatypes::{DataType, Field, Schema, TimeUnit};use datafusion::datasource::file_format::avro::AvroFormat;@@ -540,12 +542,51 @@ fn typechecked_scalar_value_conversion("Untyped scalar null is not a valid scalar value",))}+ PrimitiveScalarType::Decimal128 => {+ ScalarValue::Decimal128(None, 0, 0)+ }+ PrimitiveScalarType::Date64 => ScalarValue::Date64(None),+ PrimitiveScalarType::TimeSecond => {+ ScalarValue::TimestampSecond(None, None)+ }+ PrimitiveScalarType::TimeMillisecond => {+ ScalarValue::TimestampMillisecond(None, None)+ }+ PrimitiveScalarType::IntervalYearmonth => {+ ScalarValue::IntervalYearMonth(None)+ }+ PrimitiveScalarType::IntervalDaytime => {+ ScalarValue::IntervalDayTime(None)+ }};scalar_value} else {return Err(proto_error("Could not convert to the proper type"));}}+ (Value::Decimal128Value(val), PrimitiveScalarType::Decimal128) => {+ let array = vec_to_array(val.value.clone());+ ScalarValue::Decimal128(+ Some(i128::from_be_bytes(array)),+ val.p as usize,+ val.s as usize,+ )+ }+ (Value::Date64Value(v), PrimitiveScalarType::Date64) => {+ ScalarValue::Date64(Some(*v))+ }+ (Value::TimeSecondValue(v), PrimitiveScalarType::TimeSecond) => {+ ScalarValue::TimestampSecond(Some(*v), None)+ }+ (Value::TimeMillisecondValue(v), PrimitiveScalarType::TimeMillisecond) => {+ ScalarValue::TimestampMillisecond(Some(*v), None)+ }+ (Value::IntervalYearmonthValue(v), PrimitiveScalarType::IntervalYearmonth) => {+ ScalarValue::IntervalYearMonth(Some(*v))+ }+ (Value::IntervalDaytimeValue(v), PrimitiveScalarType::IntervalDaytime) => {+ ScalarValue::IntervalDayTime(Some(*v))+ }_ => return Err(proto_error("Could not convert to the proper type")),})}@@ -607,6 +648,29 @@ impl TryInto<datafusion::scalar::ScalarValue> for &protobuf::scalar_value::Value.ok_or_else(|| proto_error("Invalid scalar type"))?.try_into()?}+ protobuf::scalar_value::Value::Decimal128Value(val) => {+ let array = vec_to_array(val.value.clone());+ ScalarValue::Decimal128(+ Some(i128::from_be_bytes(array)),+ val.p as usize,+ val.s as usize,+ )+ }+ protobuf::scalar_value::Value::Date64Value(v) => {+ ScalarValue::Date64(Some(*v))+ }+ protobuf::scalar_value::Value::TimeSecondValue(v) => {+ ScalarValue::TimestampSecond(Some(*v), None)+ }+ protobuf::scalar_value::Value::TimeMillisecondValue(v) => {+ ScalarValue::TimestampMillisecond(Some(*v), None)+ }+ protobuf::scalar_value::Value::IntervalYearmonthValue(v) => {+ ScalarValue::IntervalYearMonth(Some(*v))+ }+ protobuf::scalar_value::Value::IntervalDaytimeValue(v) => {+ ScalarValue::IntervalDayTime(Some(*v))+ }};Ok(scalar)}@@ -763,6 +827,22 @@ impl TryInto<datafusion::scalar::ScalarValue> for protobuf::PrimitiveScalarTypeprotobuf::PrimitiveScalarType::TimeNanosecond => {ScalarValue::TimestampNanosecond(None, None)}+ protobuf::PrimitiveScalarType::Decimal128 => {+ ScalarValue::Decimal128(None, 0, 0)+ }+ protobuf::PrimitiveScalarType::Date64 => ScalarValue::Date64(None),+ protobuf::PrimitiveScalarType::TimeSecond => {+ ScalarValue::TimestampSecond(None, None)+ }+ protobuf::PrimitiveScalarType::TimeMillisecond => {+ ScalarValue::TimestampMillisecond(None, None)+ }+ protobuf::PrimitiveScalarType::IntervalYearmonth => {+ ScalarValue::IntervalYearMonth(None)+ }+ protobuf::PrimitiveScalarType::IntervalDaytime => {+ ScalarValue::IntervalDayTime(None)+ }})}}@@ -845,6 +925,29 @@ impl TryInto<datafusion::scalar::ScalarValue> for &protobuf::ScalarValue {.ok_or_else(|| proto_error("Protobuf deserialization error found invalid enum variant for DatafusionScalar"))?;null_type_enum.try_into()?}+ protobuf::scalar_value::Value::Decimal128Value(val) => {+ let array = vec_to_array(val.value.clone());+ ScalarValue::Decimal128(+ Some(i128::from_be_bytes(array)),+ val.p as usize,+ val.s as usize,+ )+ }+ protobuf::scalar_value::Value::Date64Value(v) => {+ ScalarValue::Date64(Some(*v))+ }+ protobuf::scalar_value::Value::TimeSecondValue(v) => {+ ScalarValue::TimestampSecond(Some(*v), None)+ }+ protobuf::scalar_value::Value::TimeMillisecondValue(v) => {+ ScalarValue::TimestampMillisecond(Some(*v), None)+ }+ protobuf::scalar_value::Value::IntervalYearmonthValue(v) => {+ ScalarValue::IntervalYearMonth(Some(*v))+ }+ protobuf::scalar_value::Value::IntervalDaytimeValue(v) => {+ ScalarValue::IntervalDayTime(Some(*v))+ }})}}diff --git a/ballista/rust/core/src/serde/logical_plan/to_proto.rs b/ballista/rust/core/src/serde/logical_plan/to_proto.rsindex 5d77a97383..eb5d8102de 100644--- a/ballista/rust/core/src/serde/logical_plan/to_proto.rs+++ b/ballista/rust/core/src/serde/logical_plan/to_proto.rs@@ -563,6 +563,51 @@ impl TryFrom<&datafusion::scalar::ScalarValue> for protobuf::ScalarValue {Value::TimeNanosecondValue(*s)})}+ datafusion::scalar::ScalarValue::Decimal128(val, p, s) => {+ match *val {+ Some(v) => {+ let array = v.to_be_bytes();+ let vec_val: Vec<u8> = array.to_vec();+ protobuf::ScalarValue {+ value: Some(Value::Decimal128Value(protobuf::Decimal128 {+ value: vec_val,+ p: *p as i64,+ s: *s as i64,+ })),+ }+ }+ None => {+ protobuf::ScalarValue {+ value: Some(protobuf::scalar_value::Value::NullValue(PrimitiveScalarType::Decimal128 as i32))+ }+ }+ }+ }+ datafusion::scalar::ScalarValue::Date64(val) => {+ create_proto_scalar(val, PrimitiveScalarType::Date64, |s| {+ Value::Date64Value(*s)+ })+ }+ datafusion::scalar::ScalarValue::TimestampSecond(val, _) => {+ create_proto_scalar(val, PrimitiveScalarType::TimeSecond, |s| {+ Value::TimeSecondValue(*s)+ })+ }+ datafusion::scalar::ScalarValue::TimestampMillisecond(val, _) => {+ create_proto_scalar(val, PrimitiveScalarType::TimeMillisecond, |s| {+ Value::TimeMillisecondValue(*s)+ })+ }+ datafusion::scalar::ScalarValue::IntervalYearMonth(val) => {+ create_proto_scalar(val, PrimitiveScalarType::IntervalYearmonth, |s| {+ Value::IntervalYearmonthValue(*s)+ })+ }+ datafusion::scalar::ScalarValue::IntervalDayTime(val) => {+ create_proto_scalar(val, PrimitiveScalarType::IntervalDaytime, |s| {+ Value::IntervalDaytimeValue(*s)+ })+ }_ => {return Err(proto_error(format!("Error converting to Datatype to scalar type, {:?} is invalid as a datafusion scalar.",diff --git a/ballista/rust/core/src/serde/mod.rs b/ballista/rust/core/src/serde/mod.rsindex 5f9950d9ad..4026273a9e 100644--- a/ballista/rust/core/src/serde/mod.rs+++ b/ballista/rust/core/src/serde/mod.rs@@ -20,7 +20,7 @@use std::{convert::TryInto, io::Cursor};-use datafusion::arrow::datatypes::UnionMode;+use datafusion::arrow::datatypes::{IntervalUnit, UnionMode};use datafusion::logical_plan::{JoinConstraint, JoinType, Operator};use datafusion::physical_plan::aggregates::AggregateFunction;use datafusion::physical_plan::window_functions::BuiltInWindowFunction;@@ -315,6 +315,20 @@ impl Into<datafusion::arrow::datatypes::DataType> for protobuf::PrimitiveScalarTDataType::Time64(TimeUnit::Nanosecond)}protobuf::PrimitiveScalarType::Null => DataType::Null,+ protobuf::PrimitiveScalarType::Decimal128 => DataType::Decimal(0, 0),+ protobuf::PrimitiveScalarType::Date64 => DataType::Date64,+ protobuf::PrimitiveScalarType::TimeSecond => {+ DataType::Timestamp(TimeUnit::Second, None)+ }+ protobuf::PrimitiveScalarType::TimeMillisecond => {+ DataType::Timestamp(TimeUnit::Millisecond, None)+ }+ protobuf::PrimitiveScalarType::IntervalYearmonth => {+ DataType::Interval(IntervalUnit::YearMonth)+ }+ protobuf::PrimitiveScalarType::IntervalDaytime => {+ DataType::Interval(IntervalUnit::DayTime)+ }}}}@@ -376,3 +390,9 @@ fn str_to_byte(s: &str) -> Result<u8, BallistaError> {}Ok(s.as_bytes()[0])}++fn vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {+ v.try_into().unwrap_or_else(|v: Vec<T>| {+ panic!("Expected a Vec of length {} but it was {}", N, v.len())+ })+}