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

feat(common): introduce Serial as Scalar and ScalarRef #8327

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/common/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod jsonb_array;
pub mod list_array;
mod macros;
mod primitive_array;
pub(crate) mod serial_array;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: mod serial_array and pub use those needed.

pub mod stream_chunk;
mod stream_chunk_iter;
pub mod struct_array;
Expand Down
43 changes: 43 additions & 0 deletions src/common/src/array/serial_array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use postgres_types::{ToSql as _, Type};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct Serial(pub i64);

impl crate::types::to_text::ToText for Serial {
fn write<W: std::fmt::Write>(&self, f: &mut W) -> std::fmt::Result {
write!(f, "{}", self.0)
}

fn write_with_type<W: std::fmt::Write>(
&self,
_ty: &crate::types::DataType,
f: &mut W,
) -> std::fmt::Result {
self.write(f)
}
}

impl crate::types::to_binary::ToBinary for Serial {
fn to_binary_with_type(
&self,
_ty: &crate::types::DataType,
) -> crate::error::Result<Option<bytes::Bytes>> {
let mut output = bytes::BytesMut::new();
self.0.to_sql(&Type::ANY, &mut output).unwrap();
Ok(Some(output.freeze()))
}
}
3 changes: 3 additions & 0 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use strum_macros::EnumDiscriminants;
use self::struct_type::StructType;
use self::to_binary::ToBinary;
use self::to_text::ToText;
use crate::array::serial_array::Serial;
use crate::array::{
read_interval_unit, ArrayBuilderImpl, JsonbRef, JsonbVal, ListRef, ListValue,
PrimitiveArrayItemType, StructRef, StructValue,
Expand Down Expand Up @@ -457,6 +458,7 @@ macro_rules! for_all_scalar_variants {
{ Int16, int16, i16, i16 },
{ Int32, int32, i32, i32 },
{ Int64, int64, i64, i64 },
{ Serial, serial, Serial, Serial },
{ Float32, float32, OrderedF32, OrderedF32 },
{ Float64, float64, OrderedF64, OrderedF64 },
{ Utf8, utf8, Box<str>, &'scalar str },
Expand Down Expand Up @@ -836,6 +838,7 @@ impl ScalarRefImpl<'_> {
Self::Int16(v) => v.serialize(ser)?,
Self::Int32(v) => v.serialize(ser)?,
Self::Int64(v) => v.serialize(ser)?,
Self::Serial(v) => v.0.serialize(ser)?,
Self::Float32(v) => v.serialize(ser)?,
Self::Float64(v) => v.serialize(ser)?,
Self::Utf8(v) => v.serialize(ser)?,
Expand Down
23 changes: 23 additions & 0 deletions src/common/src/types/scalar_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::hash::Hasher;

use super::*;
use crate::array::list_array::{ListRef, ListValue};
use crate::array::serial_array::Serial;
use crate::array::struct_array::{StructRef, StructValue};
use crate::{for_all_native_types, for_all_scalar_variants};

Expand Down Expand Up @@ -159,6 +160,28 @@ impl<'a> ScalarRef<'a> for bool {
}
}

/// Implement `Scalar` for `Serial`.
impl Scalar for Serial {
type ScalarRefType<'a> = Serial;

fn as_scalar_ref(&self) -> Self::ScalarRefType<'_> {
Serial(self.0)
}
}

/// Implement `ScalarRef` for `Serial`.
impl<'a> ScalarRef<'a> for Serial {
type ScalarType = Serial;

fn to_owned_scalar(&self) -> Serial {
*self
}

fn hash_scalar<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}

/// Implement `Scalar` for `Decimal`.
impl Scalar for Decimal {
type ScalarRefType<'a> = Decimal;
Expand Down
1 change: 1 addition & 0 deletions src/common/src/types/to_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl ToBinary for ScalarRefImpl<'_> {
ScalarRefImpl::Int16(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Int32(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Int64(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Serial(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Float32(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Float64(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Utf8(v) => v.to_binary_with_type(ty),
Expand Down
1 change: 1 addition & 0 deletions src/common/src/util/value_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn serialize_scalar(value: ScalarRefImpl<'_>, buf: &mut impl BufMut) {
ScalarRefImpl::Int16(v) => buf.put_i16_le(v),
ScalarRefImpl::Int32(v) => buf.put_i32_le(v),
ScalarRefImpl::Int64(v) => buf.put_i64_le(v),
ScalarRefImpl::Serial(v) => buf.put_i64_le(v.0),
ScalarRefImpl::Float32(v) => buf.put_f32_le(v.into_inner()),
ScalarRefImpl::Float64(v) => buf.put_f64_le(v.into_inner()),
ScalarRefImpl::Utf8(v) => serialize_str(v.as_bytes(), buf),
Expand Down