Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization of ScalarValue::IntervalMonthDayNano #3535

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions datafusion/proto/proto/datafusion.proto
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,18 @@ message ScalarDictionaryValue {
ScalarValue value = 2;
}

message IntervalMonthDayNanoValue {
int32 months = 1;
int32 days = 2;
int64 nanos = 3;
}


message ScalarValue{
oneof value {
// Null value of any type (type is encoded)
PrimitiveScalarType null_value = 19;
Copy link
Contributor Author

@alamb alamb Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike PrimitiveScalarType and hope to remove it eventually


bool bool_value = 1;
string utf8_value = 2;
string large_utf8_value = 3;
Expand All @@ -754,7 +763,6 @@ message ScalarValue{
ScalarListValue list_value = 17;
ScalarType null_list_value = 18;

PrimitiveScalarType null_value = 19;
Decimal128 decimal128_value = 20;
int64 date_64_value = 21;
int32 interval_yearmonth_value = 24;
Expand All @@ -764,6 +772,7 @@ message ScalarValue{
bytes binary_value = 28;
bytes large_binary_value = 29;
int64 time64_value = 30;
IntervalMonthDayNanoValue interval_month_day_nano = 31;
}
}

Expand Down Expand Up @@ -794,13 +803,13 @@ enum PrimitiveScalarType{
TIMESTAMP_MICROSECOND = 14;
TIMESTAMP_NANOSECOND = 15;
NULL = 16;

DECIMAL128 = 17;
DATE64 = 20;
TIMESTAMP_SECOND = 21;
TIMESTAMP_MILLISECOND = 22;
INTERVAL_YEARMONTH = 23;
INTERVAL_DAYTIME = 24;
INTERVAL_MONTHDAYNANO = 28;

BINARY = 25;
LARGE_BINARY = 26;
Expand All @@ -822,7 +831,7 @@ message ScalarListType{

// Broke out into multiple message types so that type
// metadata did not need to be in separate message
//All types that are of the empty message types contain no additional metadata
// All types that are of the empty message types contain no additional metadata
// about the type
message ArrowType{
oneof arrow_type_enum{
Expand Down
24 changes: 23 additions & 1 deletion datafusion/proto/src/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use crate::protobuf::{
CubeNode, GroupingSetNode, OptimizedLogicalPlanType, OptimizedPhysicalPlanType,
RollupNode,
};
use arrow::datatypes::{DataType, Field, IntervalUnit, Schema, TimeUnit, UnionMode};
use arrow::datatypes::{
DataType, Field, IntervalMonthDayNanoType, IntervalUnit, Schema, TimeUnit, UnionMode,
};
use datafusion::logical_plan::FunctionRegistry;
use datafusion_common::{
Column, DFField, DFSchema, DFSchemaRef, DataFusionError, ScalarValue,
Expand Down Expand Up @@ -245,6 +247,9 @@ impl From<protobuf::PrimitiveScalarType> for DataType {
protobuf::PrimitiveScalarType::IntervalDaytime => {
DataType::Interval(IntervalUnit::DayTime)
}
protobuf::PrimitiveScalarType::IntervalMonthdaynano => {
DataType::Interval(IntervalUnit::MonthDayNano)
}
}
}
}
Expand Down Expand Up @@ -666,6 +671,7 @@ impl TryFrom<&protobuf::PrimitiveScalarType> for ScalarValue {
}
PrimitiveScalarType::IntervalYearmonth => Self::IntervalYearMonth(None),
PrimitiveScalarType::IntervalDaytime => Self::IntervalDayTime(None),
PrimitiveScalarType::IntervalMonthdaynano => Self::IntervalMonthDayNano(None),
})
}
}
Expand Down Expand Up @@ -805,6 +811,9 @@ impl TryFrom<&protobuf::ScalarValue> for ScalarValue {
}
Value::BinaryValue(v) => Self::Binary(Some(v.clone())),
Value::LargeBinaryValue(v) => Self::LargeBinary(Some(v.clone())),
Value::IntervalMonthDayNano(v) => Self::IntervalMonthDayNano(Some(
IntervalMonthDayNanoType::make_value(v.months, v.days, v.nanos),
)),
})
}
}
Expand Down Expand Up @@ -1510,6 +1519,9 @@ fn typechecked_scalar_value_conversion(
PrimitiveScalarType::IntervalDaytime => {
ScalarValue::IntervalDayTime(None)
}
PrimitiveScalarType::IntervalMonthdaynano => {
ScalarValue::IntervalMonthDayNano(None)
}
PrimitiveScalarType::Binary => ScalarValue::Binary(None),
PrimitiveScalarType::LargeBinary => ScalarValue::LargeBinary(None),
};
Expand All @@ -1535,6 +1547,16 @@ fn typechecked_scalar_value_conversion(
(Value::IntervalDaytimeValue(v), PrimitiveScalarType::IntervalDaytime) => {
ScalarValue::IntervalDayTime(Some(*v))
}
(Value::IntervalMonthDayNano(v), PrimitiveScalarType::IntervalMonthdaynano) => {
let protobuf::IntervalMonthDayNanoValue {
months,
days,
nanos,
} = v;
ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNanoType::make_value(
*months, *days, *nanos,
)))
}
_ => return Err(proto_error("Could not convert to the proper type")),
})
}
Expand Down
22 changes: 21 additions & 1 deletion datafusion/proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ mod roundtrip_tests {
use crate::logical_plan::LogicalExtensionCodec;
use arrow::{
array::ArrayRef,
datatypes::{DataType, Field, IntervalUnit, TimeUnit, UnionMode},
datatypes::{
DataType, Field, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalUnit,
TimeUnit, UnionMode,
},
};
use datafusion::logical_plan::create_udaf;
use datafusion::physical_plan::functions::make_scalar_function;
Expand Down Expand Up @@ -422,6 +425,23 @@ mod roundtrip_tests {
ScalarValue::TimestampSecond(Some(i64::MAX), None),
ScalarValue::TimestampSecond(Some(0), Some("UTC".to_string())),
ScalarValue::TimestampSecond(None, None),
ScalarValue::IntervalDayTime(Some(IntervalDayTimeType::make_value(0, 0))),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears there were no round trip tests for IntervalDayTime either, so I added them as well

ScalarValue::IntervalDayTime(Some(IntervalDayTimeType::make_value(1, 2))),
ScalarValue::IntervalDayTime(Some(IntervalDayTimeType::make_value(
i32::MAX,
i32::MAX,
))),
ScalarValue::IntervalDayTime(None),
ScalarValue::IntervalMonthDayNano(Some(
IntervalMonthDayNanoType::make_value(0, 0, 0),
)),
ScalarValue::IntervalMonthDayNano(Some(
IntervalMonthDayNanoType::make_value(1, 2, 3),
)),
ScalarValue::IntervalMonthDayNano(Some(
IntervalMonthDayNanoType::make_value(i32::MAX, i32::MAX, i64::MAX),
)),
ScalarValue::IntervalMonthDayNano(None),
ScalarValue::new_list(
Some(vec![
ScalarValue::Float32(Some(-213.1)),
Expand Down
20 changes: 16 additions & 4 deletions datafusion/proto/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use crate::protobuf::{
OptimizedPhysicalPlanType, RollupNode,
};
use arrow::datatypes::{
DataType, Field, IntervalUnit, Schema, SchemaRef, TimeUnit, UnionMode,
DataType, Field, IntervalMonthDayNanoType, IntervalUnit, Schema, SchemaRef, TimeUnit,
UnionMode,
};
use datafusion_common::{Column, DFField, DFSchemaRef, ScalarValue};
use datafusion_expr::expr::GroupingSet;
Expand Down Expand Up @@ -1197,9 +1198,20 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
})
}

datafusion::scalar::ScalarValue::IntervalMonthDayNano(_) => {
// not yet implemented (TODO file ticket)
return Err(Error::invalid_scalar_value(val));
datafusion::scalar::ScalarValue::IntervalMonthDayNano(v) => {
let value = if let Some(v) = v {
let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(*v);
Value::IntervalMonthDayNano(protobuf::IntervalMonthDayNanoValue {
months,
days,
nanos,
})
} else {
let null_arrow_type = PrimitiveScalarType::IntervalMonthdaynano;
protobuf::scalar_value::Value::NullValue(null_arrow_type as i32)
};

protobuf::ScalarValue { value: Some(value) }
}

datafusion::scalar::ScalarValue::Struct(_, _) => {
Expand Down