Skip to content

Commit

Permalink
refactor(types): remove dead code and minor (#17691)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxchan authored Jul 16, 2024
1 parent fe20942 commit e0be76d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 138 deletions.
5 changes: 4 additions & 1 deletion src/common/src/array/arrow/arrow_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ use crate::types::*;
use crate::util::iter_util::ZipEqFast;

/// Defines how to convert RisingWave arrays to Arrow arrays.
///
/// This trait allows for customized conversion logic for different external systems using Arrow.
/// The default implementation is based on the `From` implemented in this mod.
pub trait ToArrow {
/// Converts RisingWave `DataChunk` to Arrow `RecordBatch` with specified schema.
///
Expand Down Expand Up @@ -777,7 +780,7 @@ converts!(IntervalArray, arrow_array::IntervalMonthDayNanoArray, @map);
converts!(SerialArray, arrow_array::Int64Array, @map);

/// Converts RisingWave value from and into Arrow value.
pub trait FromIntoArrow {
trait FromIntoArrow {
/// The corresponding element type in the Arrow array.
type ArrowType;
fn from_arrow(value: Self::ArrowType) -> Self;
Expand Down
2 changes: 1 addition & 1 deletion src/common/src/array/proto_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl ArrayImpl {
pub fn from_protobuf(array: &PbArray, cardinality: usize) -> ArrayResult<Self> {
use crate::array::value_reader::*;
let array = match array.array_type() {
PbArrayType::Unspecified => unreachable!(),
PbArrayType::Int16 => read_numeric_array::<i16, I16ValueReader>(array, cardinality)?,
PbArrayType::Int32 => read_numeric_array::<i32, I32ValueReader>(array, cardinality)?,
PbArrayType::Int64 => read_numeric_array::<i64, I64ValueReader>(array, cardinality)?,
Expand All @@ -49,7 +50,6 @@ impl ArrayImpl {
PbArrayType::Jsonb => JsonbArray::from_protobuf(array)?,
PbArrayType::Struct => StructArray::from_protobuf(array)?,
PbArrayType::List => ListArray::from_protobuf(array)?,
PbArrayType::Unspecified => unreachable!(),
PbArrayType::Bytea => {
read_string_array::<BytesArrayBuilder, BytesValueReader>(array, cardinality)?
}
Expand Down
1 change: 1 addition & 0 deletions src/common/src/types/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ macro_rules! for_all_variants {
($macro:ident $(, $x:tt)*) => {
$macro! {
$($x, )*
//data_type variant_name suffix_name scalar scalar_ref array builder
{ Int16, Int16, int16, i16, i16, $crate::array::I16Array, $crate::array::I16ArrayBuilder },
{ Int32, Int32, int32, i32, i32, $crate::array::I32Array, $crate::array::I32ArrayBuilder },
{ Int64, Int64, int64, i64, i64, $crate::array::I64Array, $crate::array::I64ArrayBuilder },
Expand Down
202 changes: 68 additions & 134 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,67 +179,39 @@ impl std::str::FromStr for Box<DataType> {

impl ZeroHeapSize for DataType {}

impl DataTypeName {
pub fn is_scalar(&self) -> bool {
match self {
DataTypeName::Boolean
| DataTypeName::Int16
| DataTypeName::Int32
| DataTypeName::Int64
| DataTypeName::Int256
| DataTypeName::Serial
| DataTypeName::Decimal
| DataTypeName::Float32
| DataTypeName::Float64
| DataTypeName::Varchar
| DataTypeName::Date
| DataTypeName::Timestamp
| DataTypeName::Timestamptz
| DataTypeName::Time
| DataTypeName::Bytea
| DataTypeName::Jsonb
| DataTypeName::Interval => true,

DataTypeName::Struct | DataTypeName::List => false,
}
}
impl TryFrom<DataTypeName> for DataType {
type Error = &'static str;

pub fn to_type(self) -> Option<DataType> {
let t = match self {
DataTypeName::Boolean => DataType::Boolean,
DataTypeName::Int16 => DataType::Int16,
DataTypeName::Int32 => DataType::Int32,
DataTypeName::Int64 => DataType::Int64,
DataTypeName::Int256 => DataType::Int256,
DataTypeName::Serial => DataType::Serial,
DataTypeName::Decimal => DataType::Decimal,
DataTypeName::Float32 => DataType::Float32,
DataTypeName::Float64 => DataType::Float64,
DataTypeName::Varchar => DataType::Varchar,
DataTypeName::Bytea => DataType::Bytea,
DataTypeName::Date => DataType::Date,
DataTypeName::Timestamp => DataType::Timestamp,
DataTypeName::Timestamptz => DataType::Timestamptz,
DataTypeName::Time => DataType::Time,
DataTypeName::Interval => DataType::Interval,
DataTypeName::Jsonb => DataType::Jsonb,
fn try_from(type_name: DataTypeName) -> Result<Self, Self::Error> {
match type_name {
DataTypeName::Boolean => Ok(DataType::Boolean),
DataTypeName::Int16 => Ok(DataType::Int16),
DataTypeName::Int32 => Ok(DataType::Int32),
DataTypeName::Int64 => Ok(DataType::Int64),
DataTypeName::Int256 => Ok(DataType::Int256),
DataTypeName::Serial => Ok(DataType::Serial),
DataTypeName::Decimal => Ok(DataType::Decimal),
DataTypeName::Float32 => Ok(DataType::Float32),
DataTypeName::Float64 => Ok(DataType::Float64),
DataTypeName::Varchar => Ok(DataType::Varchar),
DataTypeName::Bytea => Ok(DataType::Bytea),
DataTypeName::Date => Ok(DataType::Date),
DataTypeName::Timestamp => Ok(DataType::Timestamp),
DataTypeName::Timestamptz => Ok(DataType::Timestamptz),
DataTypeName::Time => Ok(DataType::Time),
DataTypeName::Interval => Ok(DataType::Interval),
DataTypeName::Jsonb => Ok(DataType::Jsonb),
DataTypeName::Struct | DataTypeName::List => {
return None;
Err("Functions returning struct or list can not be inferred. Please use `FunctionCall::new_unchecked`.")
}
};
Some(t)
}
}

impl From<DataTypeName> for DataType {
fn from(type_name: DataTypeName) -> Self {
type_name.to_type().unwrap_or_else(|| panic!("Functions returning struct or list can not be inferred. Please use `FunctionCall::new_unchecked`."))
}
}
}

impl From<&PbDataType> for DataType {
fn from(proto: &PbDataType) -> DataType {
match proto.get_type_name().expect("missing type field") {
PbTypeName::TypeUnspecified => unreachable!(),
PbTypeName::Int16 => DataType::Int16,
PbTypeName::Int32 => DataType::Int32,
PbTypeName::Int64 => DataType::Int64,
Expand All @@ -265,7 +237,6 @@ impl From<&PbDataType> for DataType {
// The first (and only) item is the list element type.
Box::new((&proto.field_type[0]).into()),
),
PbTypeName::TypeUnspecified => unreachable!(),
PbTypeName::Int256 => DataType::Int256,
}
}
Expand Down Expand Up @@ -337,27 +308,7 @@ impl DataType {
}

pub fn prost_type_name(&self) -> PbTypeName {
match self {
DataType::Int16 => PbTypeName::Int16,
DataType::Int32 => PbTypeName::Int32,
DataType::Int64 => PbTypeName::Int64,
DataType::Int256 => PbTypeName::Int256,
DataType::Serial => PbTypeName::Serial,
DataType::Float32 => PbTypeName::Float,
DataType::Float64 => PbTypeName::Double,
DataType::Boolean => PbTypeName::Boolean,
DataType::Varchar => PbTypeName::Varchar,
DataType::Date => PbTypeName::Date,
DataType::Time => PbTypeName::Time,
DataType::Timestamp => PbTypeName::Timestamp,
DataType::Timestamptz => PbTypeName::Timestamptz,
DataType::Decimal => PbTypeName::Decimal,
DataType::Interval => PbTypeName::Interval,
DataType::Jsonb => PbTypeName::Jsonb,
DataType::Struct { .. } => PbTypeName::Struct,
DataType::List { .. } => PbTypeName::List,
DataType::Bytea => PbTypeName::Bytea,
}
DataTypeName::from(self).into()
}

pub fn to_protobuf(&self) -> PbDataType {
Expand All @@ -374,7 +325,23 @@ impl DataType {
DataType::List(datatype) => {
pb.field_type = vec![datatype.to_protobuf()];
}
_ => {}
DataType::Boolean
| DataType::Int16
| DataType::Int32
| DataType::Int64
| DataType::Float32
| DataType::Float64
| DataType::Decimal
| DataType::Date
| DataType::Varchar
| DataType::Time
| DataType::Timestamp
| DataType::Timestamptz
| DataType::Interval
| DataType::Bytea
| DataType::Jsonb
| DataType::Serial
| DataType::Int256 => (),
}
pb
}
Expand All @@ -392,10 +359,6 @@ impl DataType {
)
}

pub fn is_scalar(&self) -> bool {
DataTypeName::from(self).is_scalar()
}

pub fn is_array(&self) -> bool {
matches!(self, DataType::List(_))
}
Expand Down Expand Up @@ -440,37 +403,6 @@ impl DataType {
}
}

/// WARNING: Currently this should only be used in `WatermarkFilterExecutor`. Please be careful
/// if you want to use this.
pub fn min_value(&self) -> ScalarImpl {
match self {
DataType::Int16 => ScalarImpl::Int16(i16::MIN),
DataType::Int32 => ScalarImpl::Int32(i32::MIN),
DataType::Int64 => ScalarImpl::Int64(i64::MIN),
DataType::Int256 => ScalarImpl::Int256(Int256::min_value()),
DataType::Serial => ScalarImpl::Serial(Serial::from(i64::MIN)),
DataType::Float32 => ScalarImpl::Float32(F32::neg_infinity()),
DataType::Float64 => ScalarImpl::Float64(F64::neg_infinity()),
DataType::Boolean => ScalarImpl::Bool(false),
DataType::Varchar => ScalarImpl::Utf8("".into()),
DataType::Bytea => ScalarImpl::Bytea("".to_string().into_bytes().into()),
DataType::Date => ScalarImpl::Date(Date::MIN),
DataType::Time => ScalarImpl::Time(Time::from_hms_uncheck(0, 0, 0)),
DataType::Timestamp => ScalarImpl::Timestamp(Timestamp::MIN),
DataType::Timestamptz => ScalarImpl::Timestamptz(Timestamptz::MIN),
DataType::Decimal => ScalarImpl::Decimal(Decimal::NegativeInf),
DataType::Interval => ScalarImpl::Interval(Interval::MIN),
DataType::Jsonb => ScalarImpl::Jsonb(JsonbVal::null()), // NOT `min` #7981
DataType::Struct(data_types) => ScalarImpl::Struct(StructValue::new(
data_types
.types()
.map(|data_type| Some(data_type.min_value()))
.collect_vec(),
)),
DataType::List(data_type) => ScalarImpl::List(ListValue::empty(data_type)),
}
}

/// Return a new type that removes the outer list.
///
/// ```
Expand Down Expand Up @@ -513,28 +445,32 @@ impl From<DataType> for PbDataType {
}
}

/// Common trait bounds of scalar and scalar reference types.
///
/// NOTE(rc): `Hash` is not in the trait bound list, it's implemented as [`ScalarRef::hash_scalar`].
pub trait ScalarBounds<Impl> = Debug
+ Send
+ Sync
+ Clone
+ PartialEq
+ Eq
// in default ascending order
+ PartialOrd
+ Ord
+ TryFrom<Impl, Error = ArrayError>
// `ScalarImpl`/`ScalarRefImpl`
+ Into<Impl>;
mod private {
use super::*;

/// Common trait bounds of scalar and scalar reference types.
///
/// NOTE(rc): `Hash` is not in the trait bound list, it's implemented as [`ScalarRef::hash_scalar`].
pub trait ScalarBounds<Impl> = Debug
+ Send
+ Sync
+ Clone
+ PartialEq
+ Eq
// in default ascending order
+ PartialOrd
+ Ord
+ TryFrom<Impl, Error = ArrayError>
// `ScalarImpl`/`ScalarRefImpl`
+ Into<Impl>;
}

/// `Scalar` is a trait over all possible owned types in the evaluation
/// framework.
///
/// `Scalar` is reciprocal to `ScalarRef`. Use `as_scalar_ref` to get a
/// reference which has the same lifetime as `self`.
pub trait Scalar: ScalarBounds<ScalarImpl> + 'static {
pub trait Scalar: private::ScalarBounds<ScalarImpl> + 'static {
/// Type for reference of `Scalar`
type ScalarRefType<'a>: ScalarRef<'a, ScalarType = Self> + 'a
where
Expand All @@ -548,17 +484,12 @@ pub trait Scalar: ScalarBounds<ScalarImpl> + 'static {
}
}

/// Convert an `Option<Scalar>` to corresponding `Option<ScalarRef>`.
pub fn option_as_scalar_ref<S: Scalar>(scalar: &Option<S>) -> Option<S::ScalarRefType<'_>> {
scalar.as_ref().map(|x| x.as_scalar_ref())
}

/// `ScalarRef` is a trait over all possible references in the evaluation
/// framework.
///
/// `ScalarRef` is reciprocal to `Scalar`. Use `to_owned_scalar` to get an
/// owned scalar.
pub trait ScalarRef<'a>: ScalarBounds<ScalarRefImpl<'a>> + 'a + Copy {
pub trait ScalarRef<'a>: private::ScalarBounds<ScalarRefImpl<'a>> + 'a + Copy {
/// `ScalarType` is the owned type of current `ScalarRef`.
type ScalarType: Scalar<ScalarRefType<'a> = Self>;

Expand Down Expand Up @@ -653,6 +584,9 @@ impl ToDatumRef for DatumRef<'_> {
}

/// To make sure there is `as_scalar_ref` for all scalar ref types.
/// See <https://github.com/risingwavelabs/risingwave/pull/9977/files#r1208972881>
///
/// This is used by the expr macro.
pub trait SelfAsScalarRef {
fn as_scalar_ref(&self) -> Self;
}
Expand Down Expand Up @@ -1021,15 +955,15 @@ impl ScalarRefImpl<'_> {
}

impl ScalarImpl {
/// Serialize the scalar.
/// Serialize the scalar into the `memcomparable` format.
pub fn serialize(
&self,
ser: &mut memcomparable::Serializer<impl BufMut>,
) -> memcomparable::Result<()> {
self.as_scalar_ref_impl().serialize(ser)
}

/// Deserialize the scalar.
/// Deserialize the scalar from the `memcomparable` format.
pub fn deserialize(
ty: &DataType,
de: &mut memcomparable::Deserializer<impl Buf>,
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/catalog/system_catalog/pg_catalog/pg_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ fn read_pg_cast(_: &SysCatalogReaderImpl) -> Vec<PgCast> {
.enumerate()
.map(|(idx, (src, target, ctx))| PgCast {
oid: idx as i32,
castsource: DataType::from(*src).to_oid(),
casttarget: DataType::from(*target).to_oid(),
castsource: DataType::try_from(*src).unwrap().to_oid(),
casttarget: DataType::try_from(*target).unwrap().to_oid(),
castcontext: ctx.to_string(),
})
.collect()
Expand Down

0 comments on commit e0be76d

Please sign in to comment.