-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11801 from lluki/platform_expr_proto_datetime
- Loading branch information
Showing
10 changed files
with
444 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
#![cfg(feature = "test-utils")] | ||
|
||
//! Custom [`proptest::strategy::Strategy`] implementations for the | ||
//! [`chrono`] fields used inthe codebase. | ||
//! | ||
//! See the [`proptest`] docs[^1] for an example. | ||
//! | ||
//! [^1]: <https://altsysrq.github.io/proptest-book/proptest-derive/modifiers.html#strategy> | ||
use chrono::{DateTime, Duration, FixedOffset, NaiveDateTime, Timelike, Utc}; | ||
use chrono_tz::{Tz, TZ_VARIANTS}; | ||
use proptest::prelude::Strategy; | ||
|
||
pub fn any_naive_datetime() -> impl Strategy<Value = NaiveDateTime> { | ||
use ::chrono::naive::{MAX_DATETIME, MIN_DATETIME}; | ||
(0..(MAX_DATETIME.nanosecond() - MIN_DATETIME.nanosecond())) | ||
.prop_map(|x| MIN_DATETIME + Duration::nanoseconds(x as i64)) | ||
} | ||
|
||
pub fn any_datetime() -> impl Strategy<Value = DateTime<Utc>> { | ||
any_naive_datetime().prop_map(|x| DateTime::from_utc(x, Utc)) | ||
} | ||
|
||
pub fn any_fixed_offset() -> impl Strategy<Value = FixedOffset> { | ||
(-86_401..86_400).prop_map(FixedOffset::east) | ||
} | ||
|
||
pub fn any_timezone() -> impl Strategy<Value = Tz> { | ||
(0..TZ_VARIANTS.len()).prop_map(|idx| *TZ_VARIANTS.get(idx).unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
syntax = "proto3"; | ||
|
||
package adt.datetime; | ||
|
||
import "chrono.proto"; | ||
import "google/protobuf/empty.proto"; | ||
|
||
message ProtoTimezone { | ||
oneof kind { | ||
chrono.ProtoFixedOffset fixed_offset = 1; | ||
chrono.ProtoTz tz = 2; | ||
} | ||
} | ||
|
||
message ProtoDateTimeUnits { | ||
oneof kind { | ||
google.protobuf.Empty epoch = 1; | ||
google.protobuf.Empty millennium = 2; | ||
google.protobuf.Empty century = 3; | ||
google.protobuf.Empty decade = 4; | ||
google.protobuf.Empty year = 5; | ||
google.protobuf.Empty quarter = 6; | ||
google.protobuf.Empty week = 7; | ||
google.protobuf.Empty month = 8; | ||
google.protobuf.Empty hour = 9; | ||
google.protobuf.Empty day = 10; | ||
google.protobuf.Empty day_of_week = 11; | ||
google.protobuf.Empty day_of_year = 12; | ||
google.protobuf.Empty iso_day_of_week = 13; | ||
google.protobuf.Empty iso_day_of_year = 14; | ||
google.protobuf.Empty minute = 15; | ||
google.protobuf.Empty second = 16; | ||
google.protobuf.Empty milliseconds = 17; | ||
google.protobuf.Empty microseconds = 18; | ||
google.protobuf.Empty timezone = 19; | ||
google.protobuf.Empty timezone_hour = 20; | ||
google.protobuf.Empty timezone_minute = 21; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
//! Protobuf structs mirroring [`crate::adt::datetime`]. | ||
include!(concat!(env!("OUT_DIR"), "/adt.datetime.rs")); | ||
|
||
use super::super::{ProtoRepr, TryFromProtoError}; | ||
use crate::adt::datetime::Timezone::*; | ||
use crate::adt::datetime::{DateTimeUnits, Timezone}; | ||
use chrono::FixedOffset; | ||
use chrono_tz::Tz; | ||
|
||
impl From<&Timezone> for ProtoTimezone { | ||
fn from(value: &Timezone) -> Self { | ||
use proto_timezone::Kind; | ||
ProtoTimezone { | ||
kind: Some(match value { | ||
FixedOffset(fo) => Kind::FixedOffset(fo.into_proto()), | ||
Tz(tz) => Kind::Tz(tz.into_proto()), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<ProtoTimezone> for Timezone { | ||
type Error = TryFromProtoError; | ||
|
||
fn try_from(value: ProtoTimezone) -> Result<Self, Self::Error> { | ||
use proto_timezone::Kind; | ||
let kind = value | ||
.kind | ||
.ok_or_else(|| TryFromProtoError::MissingField("ProtoTimezone::kind".into()))?; | ||
Ok(match kind { | ||
Kind::FixedOffset(pof) => Timezone::FixedOffset(FixedOffset::from_proto(pof)?), | ||
Kind::Tz(ptz) => Timezone::Tz(Tz::from_proto(ptz)?), | ||
}) | ||
} | ||
} | ||
|
||
impl From<&DateTimeUnits> for ProtoDateTimeUnits { | ||
fn from(value: &DateTimeUnits) -> Self { | ||
use proto_date_time_units::Kind; | ||
ProtoDateTimeUnits { | ||
kind: Some(match value { | ||
DateTimeUnits::Epoch => Kind::Epoch(()), | ||
DateTimeUnits::Millennium => Kind::Millennium(()), | ||
DateTimeUnits::Century => Kind::Century(()), | ||
DateTimeUnits::Decade => Kind::Decade(()), | ||
DateTimeUnits::Year => Kind::Year(()), | ||
DateTimeUnits::Quarter => Kind::Quarter(()), | ||
DateTimeUnits::Week => Kind::Week(()), | ||
DateTimeUnits::Month => Kind::Month(()), | ||
DateTimeUnits::Hour => Kind::Hour(()), | ||
DateTimeUnits::Day => Kind::Day(()), | ||
DateTimeUnits::DayOfWeek => Kind::DayOfWeek(()), | ||
DateTimeUnits::DayOfYear => Kind::DayOfYear(()), | ||
DateTimeUnits::IsoDayOfWeek => Kind::IsoDayOfWeek(()), | ||
DateTimeUnits::IsoDayOfYear => Kind::IsoDayOfYear(()), | ||
DateTimeUnits::Minute => Kind::Minute(()), | ||
DateTimeUnits::Second => Kind::Second(()), | ||
DateTimeUnits::Milliseconds => Kind::Milliseconds(()), | ||
DateTimeUnits::Microseconds => Kind::Microseconds(()), | ||
DateTimeUnits::Timezone => Kind::Timezone(()), | ||
DateTimeUnits::TimezoneHour => Kind::TimezoneHour(()), | ||
DateTimeUnits::TimezoneMinute => Kind::TimezoneMinute(()), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<ProtoDateTimeUnits> for DateTimeUnits { | ||
type Error = TryFromProtoError; | ||
|
||
fn try_from(value: ProtoDateTimeUnits) -> Result<Self, Self::Error> { | ||
use proto_date_time_units::Kind; | ||
let kind = value | ||
.kind | ||
.ok_or_else(|| TryFromProtoError::MissingField("ProtoDateTimeUnits.kind".into()))?; | ||
Ok(match kind { | ||
Kind::Epoch(_) => DateTimeUnits::Epoch, | ||
Kind::Millennium(_) => DateTimeUnits::Millennium, | ||
Kind::Century(_) => DateTimeUnits::Century, | ||
Kind::Decade(_) => DateTimeUnits::Decade, | ||
Kind::Year(_) => DateTimeUnits::Year, | ||
Kind::Quarter(_) => DateTimeUnits::Quarter, | ||
Kind::Week(_) => DateTimeUnits::Week, | ||
Kind::Month(_) => DateTimeUnits::Month, | ||
Kind::Hour(_) => DateTimeUnits::Hour, | ||
Kind::Day(_) => DateTimeUnits::Day, | ||
Kind::DayOfWeek(_) => DateTimeUnits::DayOfWeek, | ||
Kind::DayOfYear(_) => DateTimeUnits::DayOfYear, | ||
Kind::IsoDayOfWeek(_) => DateTimeUnits::IsoDayOfWeek, | ||
Kind::IsoDayOfYear(_) => DateTimeUnits::IsoDayOfYear, | ||
Kind::Minute(_) => DateTimeUnits::Minute, | ||
Kind::Second(_) => DateTimeUnits::Second, | ||
Kind::Milliseconds(_) => DateTimeUnits::Milliseconds, | ||
Kind::Microseconds(_) => DateTimeUnits::Microseconds, | ||
Kind::Timezone(_) => DateTimeUnits::Timezone, | ||
Kind::TimezoneHour(_) => DateTimeUnits::TimezoneHour, | ||
Kind::TimezoneMinute(_) => DateTimeUnits::TimezoneMinute, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::proto::protobuf_roundtrip; | ||
use proptest::prelude::*; | ||
|
||
proptest! { | ||
#[test] | ||
fn datetimeunits_serialization_roundtrip(expect in any::<DateTimeUnits>() ) { | ||
let actual = protobuf_roundtrip::<_, ProtoDateTimeUnits>(&expect); | ||
assert!(actual.is_ok()); | ||
assert_eq!(actual.unwrap(), expect); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ | |
pub mod array; | ||
pub mod char; | ||
pub mod datetime; | ||
pub mod numeric; | ||
pub mod varchar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
|
||
syntax = "proto3"; | ||
|
||
package chrono; | ||
|
||
message ProtoTz { | ||
string name = 1; | ||
} | ||
|
||
message ProtoNaiveDate { | ||
int32 year = 1; | ||
uint32 ordinal = 2; | ||
} | ||
|
||
message ProtoNaiveTime { | ||
uint32 secs = 1; | ||
uint32 frac = 2; | ||
} | ||
|
||
message ProtoNaiveDateTime { | ||
ProtoNaiveDate date = 1; | ||
ProtoNaiveTime time = 2; | ||
} | ||
|
||
message ProtoFixedOffset { | ||
int32 local_minus_utc = 1; | ||
} |
Oops, something went wrong.