Skip to content

Apache DataFusion SQL Query Engine

Serialize timezone in timestamp scalar values (#2120)

Dan Harris91673b3f0969
diff --git a/datafusion/proto/proto/datafusion.proto b/datafusion/proto/proto/datafusion.proto
index 2e934b86ce..8c74c4a9ba 100644
--- a/datafusion/proto/proto/datafusion.proto
+++ b/datafusion/proto/proto/datafusion.proto
@@ -401,6 +401,16 @@ message ScalarListValue{
repeated ScalarValue values = 2;
}
+message ScalarTimestampValue {
+ oneof value {
+ int64 time_microsecond_value = 1;
+ int64 time_nanosecond_value = 2;
+ int64 time_second_value = 3;
+ int64 time_millisecond_value = 4;
+ };
+ string timezone = 5;
+}
+
message ScalarValue{
oneof value {
bool bool_value = 1;
@@ -418,18 +428,15 @@ message ScalarValue{
double float64_value = 13;
//Literal Date32 value always has a unit of day
int32 date_32_value = 14;
- int64 time_microsecond_value = 15;
- int64 time_nanosecond_value = 16;
ScalarListValue list_value = 17;
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;
+ ScalarTimestampValue timestamp_value = 26;
}
}
diff --git a/datafusion/proto/src/from_proto.rs b/datafusion/proto/src/from_proto.rs
index dd8b13547d..c0c40dee88 100644
--- a/datafusion/proto/src/from_proto.rs
+++ b/datafusion/proto/src/from_proto.rs
@@ -560,12 +560,6 @@ impl TryFrom<&protobuf::scalar_value::Value> for ScalarValue {
Value::Float32Value(v) => ScalarValue::Float32(Some(*v)),
Value::Float64Value(v) => ScalarValue::Float64(Some(*v)),
Value::Date32Value(v) => ScalarValue::Date32(Some(*v)),
- Value::TimeMicrosecondValue(v) => {
- ScalarValue::TimestampMicrosecond(Some(*v), None)
- }
- Value::TimeNanosecondValue(v) => {
- ScalarValue::TimestampNanosecond(Some(*v), None)
- }
Value::ListValue(v) => v.try_into()?,
Value::NullListValue(v) => ScalarValue::List(None, Box::new(v.try_into()?)),
Value::NullValue(null_enum) => {
@@ -581,12 +575,31 @@ impl TryFrom<&protobuf::scalar_value::Value> for ScalarValue {
)
}
Value::Date64Value(v) => ScalarValue::Date64(Some(*v)),
- Value::TimeSecondValue(v) => ScalarValue::TimestampSecond(Some(*v), None),
- Value::TimeMillisecondValue(v) => {
- ScalarValue::TimestampMillisecond(Some(*v), None)
- }
Value::IntervalYearmonthValue(v) => ScalarValue::IntervalYearMonth(Some(*v)),
Value::IntervalDaytimeValue(v) => ScalarValue::IntervalDayTime(Some(*v)),
+ Value::TimestampValue(v) => {
+ let ts_value =
+ v.value.as_ref().ok_or_else(|| Error::required("value"))?;
+ let timezone = if v.timezone.is_empty() {
+ None
+ } else {
+ Some(v.timezone.clone())
+ };
+ match ts_value {
+ protobuf::scalar_timestamp_value::Value::TimeMicrosecondValue(t) => {
+ ScalarValue::TimestampMicrosecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeNanosecondValue(t) => {
+ ScalarValue::TimestampNanosecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeSecondValue(t) => {
+ ScalarValue::TimestampSecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeMillisecondValue(t) => {
+ ScalarValue::TimestampMillisecond(Some(*t), timezone)
+ }
+ }
+ }
};
Ok(scalar)
}
@@ -794,8 +807,6 @@ impl TryFrom<&protobuf::ScalarValue> for ScalarValue {
Value::Float32Value(v) => Self::Float32(Some(*v)),
Value::Float64Value(v) => Self::Float64(Some(*v)),
Value::Date32Value(v) => Self::Date32(Some(*v)),
- Value::TimeMicrosecondValue(v) => Self::TimestampMicrosecond(Some(*v), None),
- Value::TimeNanosecondValue(v) => Self::TimestampNanosecond(Some(*v), None),
Value::ListValue(scalar_list) => {
let protobuf::ScalarListValue {
values,
@@ -829,10 +840,33 @@ impl TryFrom<&protobuf::ScalarValue> for ScalarValue {
)
}
Value::Date64Value(v) => Self::Date64(Some(*v)),
- Value::TimeSecondValue(v) => Self::TimestampSecond(Some(*v), None),
- Value::TimeMillisecondValue(v) => Self::TimestampMillisecond(Some(*v), None),
Value::IntervalYearmonthValue(v) => Self::IntervalYearMonth(Some(*v)),
Value::IntervalDaytimeValue(v) => Self::IntervalDayTime(Some(*v)),
+ Value::TimestampValue(v) => {
+ let timezone = if v.timezone.is_empty() {
+ None
+ } else {
+ Some(v.timezone.clone())
+ };
+
+ let ts_value =
+ v.value.as_ref().ok_or_else(|| Error::required("value"))?;
+
+ match ts_value {
+ protobuf::scalar_timestamp_value::Value::TimeMicrosecondValue(t) => {
+ Self::TimestampMicrosecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeNanosecondValue(t) => {
+ Self::TimestampNanosecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeSecondValue(t) => {
+ Self::TimestampSecond(Some(*t), timezone)
+ }
+ protobuf::scalar_timestamp_value::Value::TimeMillisecondValue(t) => {
+ Self::TimestampMillisecond(Some(*t), timezone)
+ }
+ }
+ }
})
}
}
@@ -1343,12 +1377,37 @@ fn typechecked_scalar_value_conversion(
(Value::Date32Value(v), PrimitiveScalarType::Date32) => {
ScalarValue::Date32(Some(*v))
}
- (Value::TimeMicrosecondValue(v), PrimitiveScalarType::TimeMicrosecond) => {
- ScalarValue::TimestampMicrosecond(Some(*v), None)
- }
- (Value::TimeNanosecondValue(v), PrimitiveScalarType::TimeMicrosecond) => {
- ScalarValue::TimestampNanosecond(Some(*v), None)
- }
+ (
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone,
+ value:
+ Some(protobuf::scalar_timestamp_value::Value::TimeMicrosecondValue(v)),
+ }),
+ PrimitiveScalarType::TimeMicrosecond,
+ ) => ScalarValue::TimestampMicrosecond(Some(*v), unwrap_timezone(timezone)),
+ (
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone,
+ value:
+ Some(protobuf::scalar_timestamp_value::Value::TimeNanosecondValue(v)),
+ }),
+ PrimitiveScalarType::TimeNanosecond,
+ ) => ScalarValue::TimestampNanosecond(Some(*v), unwrap_timezone(timezone)),
+ (
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone,
+ value: Some(protobuf::scalar_timestamp_value::Value::TimeSecondValue(v)),
+ }),
+ PrimitiveScalarType::TimeSecond,
+ ) => ScalarValue::TimestampSecond(Some(*v), unwrap_timezone(timezone)),
+ (
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone,
+ value:
+ Some(protobuf::scalar_timestamp_value::Value::TimeMillisecondValue(v)),
+ }),
+ PrimitiveScalarType::TimeMillisecond,
+ ) => ScalarValue::TimestampMillisecond(Some(*v), unwrap_timezone(timezone)),
(Value::Utf8Value(v), PrimitiveScalarType::Utf8) => {
ScalarValue::Utf8(Some(v.to_owned()))
}
@@ -1418,12 +1477,6 @@ fn typechecked_scalar_value_conversion(
(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))
}
@@ -1434,6 +1487,14 @@ fn typechecked_scalar_value_conversion(
})
}
+fn unwrap_timezone(proto_value: &str) -> Option<String> {
+ if proto_value.is_empty() {
+ None
+ } else {
+ Some(proto_value.to_string())
+ }
+}
+
fn from_proto_binary_op(op: &str) -> Result<Operator, Error> {
match op {
"And" => Ok(Operator::And),
diff --git a/datafusion/proto/src/lib.rs b/datafusion/proto/src/lib.rs
index a45dfb08ab..17e148fb94 100644
--- a/datafusion/proto/src/lib.rs
+++ b/datafusion/proto/src/lib.rs
@@ -150,8 +150,6 @@ mod roundtrip_tests {
ScalarValue::LargeUtf8(None),
ScalarValue::List(None, Box::new(DataType::Boolean)),
ScalarValue::Date32(None),
- ScalarValue::TimestampMicrosecond(None, None),
- ScalarValue::TimestampNanosecond(None, None),
ScalarValue::Boolean(Some(true)),
ScalarValue::Boolean(Some(false)),
ScalarValue::Float32(Some(1.0)),
@@ -192,9 +190,20 @@ mod roundtrip_tests {
ScalarValue::Date32(Some(i32::MAX)),
ScalarValue::TimestampNanosecond(Some(0), None),
ScalarValue::TimestampNanosecond(Some(i64::MAX), None),
+ ScalarValue::TimestampNanosecond(Some(0), Some("UTC".to_string())),
+ ScalarValue::TimestampNanosecond(None, None),
ScalarValue::TimestampMicrosecond(Some(0), None),
ScalarValue::TimestampMicrosecond(Some(i64::MAX), None),
+ ScalarValue::TimestampMicrosecond(Some(0), Some("UTC".to_string())),
ScalarValue::TimestampMicrosecond(None, None),
+ ScalarValue::TimestampMillisecond(Some(0), None),
+ ScalarValue::TimestampMillisecond(Some(i64::MAX), None),
+ ScalarValue::TimestampMillisecond(Some(0), Some("UTC".to_string())),
+ ScalarValue::TimestampMillisecond(None, None),
+ ScalarValue::TimestampSecond(Some(0), None),
+ ScalarValue::TimestampSecond(Some(i64::MAX), None),
+ ScalarValue::TimestampSecond(Some(0), Some("UTC".to_string())),
+ ScalarValue::TimestampSecond(None, None),
ScalarValue::List(
Some(Box::new(vec![
ScalarValue::Float32(Some(-213.1)),
diff --git a/datafusion/proto/src/to_proto.rs b/datafusion/proto/src/to_proto.rs
index df91e22f02..5db24bdc54 100644
--- a/datafusion/proto/src/to_proto.rs
+++ b/datafusion/proto/src/to_proto.rs
@@ -872,14 +872,28 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
Value::Date32Value(*s)
})
}
- datafusion::scalar::ScalarValue::TimestampMicrosecond(val, _) => {
+ datafusion::scalar::ScalarValue::TimestampMicrosecond(val, tz) => {
create_proto_scalar(val, PrimitiveScalarType::TimeMicrosecond, |s| {
- Value::TimeMicrosecondValue(*s)
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
+ value: Some(
+ protobuf::scalar_timestamp_value::Value::TimeMicrosecondValue(
+ *s,
+ ),
+ ),
+ })
})
}
- datafusion::scalar::ScalarValue::TimestampNanosecond(val, _) => {
+ datafusion::scalar::ScalarValue::TimestampNanosecond(val, tz) => {
create_proto_scalar(val, PrimitiveScalarType::TimeNanosecond, |s| {
- Value::TimeNanosecondValue(*s)
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
+ value: Some(
+ protobuf::scalar_timestamp_value::Value::TimeNanosecondValue(
+ *s,
+ ),
+ ),
+ })
})
}
datafusion::scalar::ScalarValue::Decimal128(val, p, s) => match *val {
@@ -905,14 +919,26 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
Value::Date64Value(*s)
})
}
- datafusion::scalar::ScalarValue::TimestampSecond(val, _) => {
+ datafusion::scalar::ScalarValue::TimestampSecond(val, tz) => {
create_proto_scalar(val, PrimitiveScalarType::TimeSecond, |s| {
- Value::TimeSecondValue(*s)
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
+ value: Some(
+ protobuf::scalar_timestamp_value::Value::TimeSecondValue(*s),
+ ),
+ })
})
}
- datafusion::scalar::ScalarValue::TimestampMillisecond(val, _) => {
+ datafusion::scalar::ScalarValue::TimestampMillisecond(val, tz) => {
create_proto_scalar(val, PrimitiveScalarType::TimeMillisecond, |s| {
- Value::TimeMillisecondValue(*s)
+ Value::TimestampValue(protobuf::ScalarTimestampValue {
+ timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
+ value: Some(
+ protobuf::scalar_timestamp_value::Value::TimeMillisecondValue(
+ *s,
+ ),
+ ),
+ })
})
}
datafusion::scalar::ScalarValue::IntervalYearMonth(val) => {