Skip to content

Commit

Permalink
Merge pull request #11902 from aalexandrov/protobuf_variadic_func
Browse files Browse the repository at this point in the history
  • Loading branch information
aalexandrov authored Apr 20, 2022
2 parents b6c8992 + 86f2a23 commit 10c8467
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/expr/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn main() {
.extern_path(".mz_repr.adt.array", "::mz_repr::adt::array")
.extern_path(".mz_repr.adt.numeric", "::mz_repr::adt::numeric")
.extern_path(".mz_repr.global_id", "::mz_repr::global_id")
.extern_path(".mz_repr.relation_and_scalar", "::mz_repr")
.extern_path(".mz_repr.strconv", "::mz_repr::strconv")
.compile_protos(
&[
Expand Down
23 changes: 11 additions & 12 deletions src/expr/src/scalar/func.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
syntax = "proto3";

import "repr/src/adt/numeric.proto";
import "repr/src/relation_and_scalar.proto";
import "google/protobuf/empty.proto";

package mz_expr.scalar.func;
Expand Down Expand Up @@ -522,28 +523,26 @@ message ProtoBinaryFunc {
}

message ProtoVariadicFunc {
message ProtoRecordCreate {
repeated mz_repr.relation_and_scalar.ProtoColumnName field_names = 1;
}
oneof kind {
google.protobuf.Empty coalesce = 1;
google.protobuf.Empty greatest = 2;
google.protobuf.Empty least = 3;
google.protobuf.Empty concat = 4;
google.protobuf.Empty make_timestamp = 5;
google.protobuf.Empty pad_leading = 6;
google.protobuf.Empty pad_leading = 16;
google.protobuf.Empty substr = 7;
google.protobuf.Empty replace = 8;
google.protobuf.Empty jsonb_build_array = 9;
google.protobuf.Empty jsonb_build_object = 10;
// unsupported: ArrayCreate { elem_type: ScalarType }
google.protobuf.Empty array_create = 11;
// unsupported: ArrayToString { elem_type: ScalarType }
google.protobuf.Empty array_to_string = 12;
// unsupported: ArrayIndex { offset: usize }
google.protobuf.Empty array_index = 13;
// unsupported: ListCreate { elem_type: ScalarType }
google.protobuf.Empty list_create = 14;
// unsupported: RecordCreate { field_names: Vec<ColumnName> }
google.protobuf.Empty record_create = 15;
google.protobuf.Empty list_index = 16;
mz_repr.relation_and_scalar.ProtoScalarType array_create = 11;
mz_repr.relation_and_scalar.ProtoScalarType array_to_string = 12;
uint64 array_index = 13;
mz_repr.relation_and_scalar.ProtoScalarType list_create = 14;
ProtoRecordCreate record_create = 15;
google.protobuf.Empty list_index = 6;
google.protobuf.Empty list_slice_linear = 17;
google.protobuf.Empty split_part = 18;
google.protobuf.Empty regexp_match = 19;
Expand Down
70 changes: 52 additions & 18 deletions src/expr/src/scalar/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use mz_repr::adt::datetime::{DateTimeUnits, Timezone};
use mz_repr::adt::interval::Interval;
use mz_repr::adt::jsonb::JsonbRef;
use mz_repr::adt::numeric::{self, DecimalLike, Numeric, NumericMaxScale};
use mz_repr::proto::TryFromProtoError;
use mz_repr::proto::{ProtoRepr, TryFromProtoError};
use mz_repr::{strconv, ColumnName, ColumnType, Datum, DatumType, Row, RowArena, ScalarType};

use crate::scalar::func::format::DateTimeFormat;
Expand Down Expand Up @@ -2882,6 +2882,12 @@ impl fmt::Display for BinaryFunc {
}
}

/// An explicit [`Arbitrary`] implementation needed here because of a known
/// `proptest` issue.
///
/// Revert to the derive-macro impementation once the issue[^1] is fixed.
///
/// [^1]: <https://github.com/AltSysrq/proptest/issues/152>
impl Arbitrary for BinaryFunc {
type Parameters = ();

Expand Down Expand Up @@ -3663,6 +3669,12 @@ pub enum UnaryFunc {

use proptest::{prelude::*, strategy::*};

/// An explicit [`Arbitrary`] implementation needed here because of a known
/// `proptest` issue.
///
/// Revert to the derive-macro impementation once the issue[^1] is fixed.
///
/// [^1]: <https://github.com/AltSysrq/proptest/issues/152>
impl Arbitrary for UnaryFunc {
type Parameters = ();

Expand Down Expand Up @@ -6941,6 +6953,12 @@ impl fmt::Display for VariadicFunc {
}
}

/// An explicit [`Arbitrary`] implementation needed here because of a known
/// `proptest` issue.
///
/// Revert to the derive-macro impementation once the issue[^1] is fixed.
///
/// [^1]: <https://github.com/AltSysrq/proptest/issues/152>
impl Arbitrary for VariadicFunc {
type Parameters = ();

Expand All @@ -6958,11 +6976,12 @@ impl Arbitrary for VariadicFunc {
Just(VariadicFunc::Replace),
Just(VariadicFunc::JsonbBuildArray),
Just(VariadicFunc::JsonbBuildObject),
// todo: ArrayCreate { elem_type: ScalarType },
// todo: ArrayToString { elem_type: ScalarType },
// todo: ArrayIndex { offset: usize },
// todo: ListCreate { elem_type: ScalarType },
// todo: RecordCreate { field_names: Vec<ColumnName> },
ScalarType::arbitrary().prop_map(|elem_type| VariadicFunc::ArrayCreate { elem_type }),
ScalarType::arbitrary().prop_map(|elem_type| VariadicFunc::ArrayToString { elem_type }),
usize::arbitrary().prop_map(|offset| VariadicFunc::ArrayIndex { offset }),
ScalarType::arbitrary().prop_map(|elem_type| VariadicFunc::ListCreate { elem_type }),
Vec::<ColumnName>::arbitrary()
.prop_map(|field_names| VariadicFunc::RecordCreate { field_names }),
Just(VariadicFunc::ListIndex),
Just(VariadicFunc::ListSliceLinear),
Just(VariadicFunc::SplitPart),
Expand All @@ -6977,9 +6996,9 @@ impl Arbitrary for VariadicFunc {
}

impl From<&VariadicFunc> for ProtoVariadicFunc {
#[allow(clippy::todo)]
fn from(func: &VariadicFunc) -> Self {
use proto_variadic_func::Kind::*;
use proto_variadic_func::ProtoRecordCreate;
let kind = match func {
VariadicFunc::Coalesce => Coalesce(()),
VariadicFunc::Greatest => Greatest(()),
Expand All @@ -6991,11 +7010,13 @@ impl From<&VariadicFunc> for ProtoVariadicFunc {
VariadicFunc::Replace => Replace(()),
VariadicFunc::JsonbBuildArray => JsonbBuildArray(()),
VariadicFunc::JsonbBuildObject => JsonbBuildObject(()),
VariadicFunc::ArrayCreate { .. } => todo!(),
VariadicFunc::ArrayToString { .. } => todo!(),
VariadicFunc::ArrayIndex { .. } => todo!(),
VariadicFunc::ListCreate { .. } => todo!(),
VariadicFunc::RecordCreate { .. } => todo!(),
VariadicFunc::ArrayCreate { elem_type } => ArrayCreate(elem_type.into()),
VariadicFunc::ArrayToString { elem_type } => ArrayToString(elem_type.into()),
VariadicFunc::ArrayIndex { offset } => ArrayIndex(offset.into_proto()),
VariadicFunc::ListCreate { elem_type } => ListCreate(elem_type.into()),
VariadicFunc::RecordCreate { field_names } => RecordCreate(ProtoRecordCreate {
field_names: field_names.iter().map(Into::into).collect(),
}),
VariadicFunc::ListIndex => ListIndex(()),
VariadicFunc::ListSliceLinear => ListSliceLinear(()),
VariadicFunc::SplitPart => SplitPart(()),
Expand All @@ -7013,9 +7034,9 @@ impl From<&VariadicFunc> for ProtoVariadicFunc {
impl TryFrom<ProtoVariadicFunc> for VariadicFunc {
type Error = TryFromProtoError;

#[allow(clippy::todo)]
fn try_from(func: ProtoVariadicFunc) -> Result<Self, Self::Error> {
use proto_variadic_func::Kind::*;
use proto_variadic_func::ProtoRecordCreate;
if let Some(kind) = func.kind {
match kind {
Coalesce(()) => Ok(VariadicFunc::Coalesce),
Expand All @@ -7028,11 +7049,24 @@ impl TryFrom<ProtoVariadicFunc> for VariadicFunc {
Replace(()) => Ok(VariadicFunc::Replace),
JsonbBuildArray(()) => Ok(VariadicFunc::JsonbBuildArray),
JsonbBuildObject(()) => Ok(VariadicFunc::JsonbBuildObject),
ArrayCreate(()) => todo!(),
ArrayToString(()) => todo!(),
ArrayIndex(()) => todo!(),
ListCreate(()) => todo!(),
RecordCreate(()) => todo!(),
ArrayCreate(elem_type) => Ok(VariadicFunc::ArrayCreate {
elem_type: elem_type.try_into()?,
}),
ArrayToString(elem_type) => Ok(VariadicFunc::ArrayToString {
elem_type: elem_type.try_into()?,
}),
ArrayIndex(offset) => Ok(VariadicFunc::ArrayIndex {
offset: usize::from_proto(offset)?,
}),
ListCreate(elem_type) => Ok(VariadicFunc::ListCreate {
elem_type: elem_type.try_into()?,
}),
RecordCreate(ProtoRecordCreate { field_names }) => Ok(VariadicFunc::RecordCreate {
field_names: field_names
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, TryFromProtoError>>()?,
}),
ListIndex(()) => Ok(VariadicFunc::ListIndex),
ListSliceLinear(()) => Ok(VariadicFunc::ListSliceLinear),
SplitPart(()) => Ok(VariadicFunc::SplitPart),
Expand Down
2 changes: 1 addition & 1 deletion src/repr/src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn any_datetime() -> impl Strategy<Value = DateTime<Utc>> {
}

pub fn any_fixed_offset() -> impl Strategy<Value = FixedOffset> {
(-86_401..86_400).prop_map(FixedOffset::east)
(-86_399..86_400).prop_map(FixedOffset::east)
}

pub fn any_timezone() -> impl Strategy<Value = Tz> {
Expand Down
6 changes: 4 additions & 2 deletions src/repr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ pub mod util;

pub use datum_vec::{DatumVec, DatumVecBorrow};
pub use global_id::GlobalId;
pub use relation::{ColumnName, ColumnType, NotNullViolation, RelationDesc, RelationType};
pub use relation::{
ColumnName, ColumnType, NotNullViolation, ProtoColumnName, RelationDesc, RelationType,
};
pub use row::{
datum_list_size, datum_size, datums_size, row_size, DatumList, DatumMap, Row, RowArena,
RowPacker, RowRef,
};
pub use scalar::{AsColumnType, Datum, DatumType, ScalarBaseType, ScalarType};
pub use scalar::{AsColumnType, Datum, DatumType, ProtoScalarType, ScalarBaseType, ScalarType};

// Concrete types used throughout Materialize for the generic parameters in Timely/Differential Dataflow.
/// System-wide timestamp type.
Expand Down

0 comments on commit 10c8467

Please sign in to comment.