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: make float as number encoding #1171

Merged
merged 1 commit into from
Aug 24, 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
7 changes: 7 additions & 0 deletions common_types/src/datum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,13 @@ impl<'a> DatumView<'a> {
}
}

pub fn as_date_i32(&self) -> Option<i32> {
match self {
DatumView::Date(v) => Some(*v),
_ => None,
}
}

pub fn as_i8(&self) -> Option<i8> {
match self {
DatumView::Int8(v) => Some(*v),
Expand Down
48 changes: 0 additions & 48 deletions components/codec/src/columnar/float.rs

This file was deleted.

37 changes: 29 additions & 8 deletions components/codec/src/columnar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ use crate::varint;

mod bool;
mod bytes;
mod float;
mod int;
mod number;
mod timestamp;

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -267,7 +266,9 @@ impl ColumnarEncoder {
DatumKind::Double => {
enc.estimated_encoded_size(datums.clone().filter_map(|v| v.as_f64()))
}
DatumKind::Float => todo!(),
DatumKind::Float => {
enc.estimated_encoded_size(datums.clone().filter_map(|v| v.as_f32()))
}
DatumKind::Varbinary => {
enc.estimated_encoded_size(datums.clone().filter_map(|v| v.into_bytes()))
}
Expand Down Expand Up @@ -301,7 +302,9 @@ impl ColumnarEncoder {
DatumKind::Boolean => {
enc.estimated_encoded_size(datums.clone().filter_map(|v| v.as_bool()))
}
DatumKind::Date => todo!(),
DatumKind::Date => {
enc.estimated_encoded_size(datums.clone().filter_map(|v| v.as_date_i32()))
}
DatumKind::Time => todo!(),
};

Expand All @@ -323,7 +326,7 @@ impl ColumnarEncoder {
datums.filter_map(|v| v.as_timestamp().map(|v| v.as_i64())),
),
DatumKind::Double => enc.encode(buf, datums.filter_map(|v| v.as_f64())),
DatumKind::Float => todo!(),
DatumKind::Float => enc.encode(buf, datums.filter_map(|v| v.as_f32())),
DatumKind::Varbinary => enc.encode(buf, datums.filter_map(|v| v.into_bytes())),
DatumKind::String => enc.encode(
buf,
Expand All @@ -338,7 +341,7 @@ impl ColumnarEncoder {
DatumKind::Int16 => enc.encode(buf, datums.filter_map(|v| v.as_i16())),
DatumKind::Int8 => enc.encode(buf, datums.filter_map(|v| v.as_i8())),
DatumKind::Boolean => enc.encode(buf, datums.filter_map(|v| v.as_bool())),
DatumKind::Date => todo!(),
DatumKind::Date => enc.encode(buf, datums.filter_map(|v| v.as_date_i32())),
DatumKind::Time => todo!(),
}
}
Expand Down Expand Up @@ -449,7 +452,10 @@ impl ColumnarDecoder {
let with_float = |v: f64| f(Datum::from(v));
ValuesDecoderImpl.decode(ctx, buf, with_float)
}
DatumKind::Float => todo!(),
DatumKind::Float => {
let with_float = |v: f32| f(Datum::from(v));
ValuesDecoderImpl.decode(ctx, buf, with_float)
}
DatumKind::Varbinary => {
let with_bytes = |v: Bytes| f(Datum::from(v));
ValuesDecoderImpl.decode(ctx, buf, with_bytes)
Expand Down Expand Up @@ -518,7 +524,13 @@ impl ColumnarDecoder {
let with_bool = |v: bool| f(Datum::from(v));
ValuesDecoderImpl.decode(ctx, buf, with_bool)
}
DatumKind::Date => todo!(),
DatumKind::Date => {
let with_i32 = |value: i32| {
let datum = Datum::Date(value);
f(datum)
};
ValuesDecoderImpl.decode(ctx, buf, with_i32)
}
DatumKind::Time => todo!(),
}
}
Expand Down Expand Up @@ -642,6 +654,15 @@ mod tests {
check_encode_end_decode(10, datums, DatumKind::Null);
}

#[test]
fn test_float() {
let datums = vec![Datum::from(10.0f32), Datum::from(9.0f32)];
check_encode_end_decode(10, datums, DatumKind::Float);

let datums = vec![Datum::from(10.0f64), Datum::from(1.0f64)];
check_encode_end_decode(10, datums, DatumKind::Double);
}

#[test]
fn test_i64() {
let datums = vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ const MAX_NUM_BYTES_OF_64VARINT: usize = 10;
const VERSION: u8 = 0;
const VERSION_SIZE: usize = 1;

macro_rules! impl_int_encoding {
($int_type: ty, $write_method: ident, $read_method: ident) => {
impl ValuesEncoder<$int_type> for ValuesEncoderImpl {
macro_rules! impl_number_encoding {
($num_type: ty, $write_method: ident, $read_method: ident) => {
impl ValuesEncoder<$num_type> for ValuesEncoderImpl {
fn encode<B, I>(&self, buf: &mut B, values: I) -> Result<()>
where
B: BufMut,
I: Iterator<Item = $int_type>,
I: Iterator<Item = $num_type>,
{
for v in values {
buf.$write_method(v);
Expand All @@ -44,11 +44,11 @@ macro_rules! impl_int_encoding {
}
}

impl ValuesDecoder<$int_type> for ValuesDecoderImpl {
impl ValuesDecoder<$num_type> for ValuesDecoderImpl {
fn decode<B, F>(&self, _ctx: DecodeContext<'_>, buf: &mut B, mut f: F) -> Result<()>
where
B: Buf,
F: FnMut($int_type) -> Result<()>,
F: FnMut($num_type) -> Result<()>,
{
while buf.remaining() > 0 {
let v = buf.$read_method();
Expand All @@ -61,12 +61,14 @@ macro_rules! impl_int_encoding {
};
}

impl_int_encoding!(i8, put_i8, get_i8);
impl_int_encoding!(u8, put_u8, get_u8);
impl_int_encoding!(u16, put_u16, get_u16);
impl_int_encoding!(i16, put_i16, get_i16);
impl_int_encoding!(u32, put_u32, get_u32);
impl_int_encoding!(i32, put_i32, get_i32);
impl_number_encoding!(i8, put_i8, get_i8);
impl_number_encoding!(u8, put_u8, get_u8);
impl_number_encoding!(u16, put_u16, get_u16);
impl_number_encoding!(i16, put_i16, get_i16);
impl_number_encoding!(u32, put_u32, get_u32);
impl_number_encoding!(i32, put_i32, get_i32);
impl_number_encoding!(f32, put_f32, get_f32);
impl_number_encoding!(f64, put_f64, get_f64);

impl ValuesEncoder<i64> for ValuesEncoderImpl {
fn encode<B, I>(&self, buf: &mut B, values: I) -> Result<()>
Expand Down