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 Int256/Uint256 as Scalar and ScalarRef #8984

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ derivative = "2"
easy-ext = "1"
either = "1"
enum-as-inner = "0.5"
ethnum = { version = "1", features = ["serde"] }
fixedbitset = { version = "0.4", features = ["std"] }
futures = { version = "0.3", default-features = false, features = ["alloc"] }
futures-async-stream = "0.2"
Expand Down
6 changes: 6 additions & 0 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod struct_type;
pub mod to_binary;
pub mod to_text;

mod num256;
mod ordered_float;

use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike};
Expand All @@ -69,6 +70,7 @@ use crate::array::{
StructValue,
};
use crate::error::Result as RwResult;
use crate::types::num256::{Int256, Int256Ref, Uint256, Uint256Ref};

pub type F32 = ordered_float::OrderedFloat<f32>;
pub type F64 = ordered_float::OrderedFloat<f64>;
Expand Down Expand Up @@ -499,6 +501,8 @@ macro_rules! for_all_scalar_variants {
{ Int16, int16, i16, i16 },
{ Int32, int32, i32, i32 },
{ Int64, int64, i64, i64 },
{ Int256, int256, Int256, Int256Ref<'scalar> },
{ Uint256, uint256, Uint256, Uint256Ref<'scalar> },
{ Serial, serial, Serial, Serial },
{ Float32, float32, F32, F32 },
{ Float64, float64, F64, F64 },
Expand Down Expand Up @@ -1029,6 +1033,8 @@ impl ScalarRefImpl<'_> {
Self::Int16(v) => v.serialize(ser)?,
Self::Int32(v) => v.serialize(ser)?,
Self::Int64(v) => v.serialize(ser)?,
Self::Int256(v) => v.serialize(ser)?,
Self::Uint256(v) => v.serialize(ser)?,
Self::Serial(v) => v.serialize(ser)?,
Self::Float32(v) => v.serialize(ser)?,
Self::Float64(v) => v.serialize(ser)?,
Expand Down
102 changes: 102 additions & 0 deletions src/common/src/types/num256.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 std::fmt::Write;
use std::hash::Hasher;
use std::mem;

use bytes::Bytes;
use ethnum::{I256, U256};
use postgres_types::{ToSql, Type};
use serde::{Serialize, Serializer};
use to_text::ToText;

use crate::types::to_binary::ToBinary;
use crate::types::{to_text, DataType, Scalar, ScalarRef};

#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Default, Hash)]
pub struct Uint256(Box<U256>);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct Uint256Ref<'a>(&'a U256);

#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Default, Hash)]
pub struct Int256(Box<I256>);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct Int256Ref<'a>(&'a I256);

macro_rules! impl_common_for_num256 {
($scalar:ident, $scalar_ref:ident < $gen:tt > , $inner:ty) => {
impl Scalar for $scalar {
type ScalarRefType<$gen> = $scalar_ref<$gen>;

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

impl<$gen> ScalarRef<$gen> for $scalar_ref<$gen> {
type ScalarType = $scalar;

fn to_owned_scalar(&self) -> Self::ScalarType {
$scalar((*self.0).into())
}

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

impl Serialize for $scalar_ref<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}

impl $scalar_ref<'_> {
#[inline]
pub fn to_le_bytes(self) -> [u8; mem::size_of::<$inner>()] {
self.0.to_le_bytes()
}
}

impl ToText for $scalar_ref<'_> {
fn write<W: Write>(&self, f: &mut W) -> std::fmt::Result {
write!(f, "{}", self.0)
}

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

impl ToBinary for $scalar_ref<'_> {
fn to_binary_with_type(&self, _ty: &DataType) -> crate::error::Result<Option<Bytes>> {
let mut output = bytes::BytesMut::new();
self.0
.to_be_bytes()
.as_ref()
.to_sql(&Type::ANY, &mut output)
.unwrap();
Ok(Some(output.freeze()))
}
}
};
}

impl_common_for_num256!(Uint256, Uint256Ref<'a>, U256);
impl_common_for_num256!(Int256, Int256Ref<'a>, I256);
2 changes: 2 additions & 0 deletions src/common/src/types/to_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ 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::Int256(v) => v.to_binary_with_type(ty),
ScalarRefImpl::Uint256(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),
Expand Down
4 changes: 4 additions & 0 deletions src/common/src/util/value_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ 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::Int256(v) => buf.put_slice(&v.to_le_bytes()),
ScalarRefImpl::Uint256(v) => buf.put_slice(&v.to_le_bytes()),
ScalarRefImpl::Serial(v) => buf.put_i64_le(v.into_inner()),
ScalarRefImpl::Float32(v) => buf.put_f32_le(v.into_inner()),
ScalarRefImpl::Float64(v) => buf.put_f64_le(v.into_inner()),
Expand All @@ -238,6 +240,8 @@ fn estimate_serialize_scalar_size(value: ScalarRefImpl<'_>) -> usize {
ScalarRefImpl::Int16(_) => 2,
ScalarRefImpl::Int32(_) => 4,
ScalarRefImpl::Int64(_) => 8,
ScalarRefImpl::Int256(_) => 32,
ScalarRefImpl::Uint256(_) => 32,
ScalarRefImpl::Serial(_) => 8,
ScalarRefImpl::Float32(_) => 4,
ScalarRefImpl::Float64(_) => 8,
Expand Down