Skip to content

Commit

Permalink
fix: typos and bit operations (#2944)
Browse files Browse the repository at this point in the history
* fix: typos and bit operation

* fix: helper

* chore: add tests in decimal128.rs and interval.rs

* chore: test

* chore: change proto version

* chore: clippy
  • Loading branch information
QuenKar authored Dec 18, 2023
1 parent e35a494 commit 5dc7ce1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ etcd-client = "0.12"
fst = "0.4.7"
futures = "0.3"
futures-util = "0.3"
greptime-proto = { git = "https://github.com/GreptimeTeam/greptime-proto.git", rev = "b1d403088f02136bcebde53d604f491c260ca8e2" }
greptime-proto = { git = "https://github.com/GreptimeTeam/greptime-proto.git", rev = "a31ea166fc015ea7ff111ac94e26c3a5d64364d2" }
humantime-serde = "1.1"
itertools = "0.10"
lazy_static = "1.4"
Expand Down
45 changes: 18 additions & 27 deletions src/api/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,8 @@ pub fn convert_i128_to_interval(v: i128) -> v1::IntervalMonthDayNano {

/// Convert common decimal128 to grpc decimal128 without precision and scale.
pub fn convert_to_pb_decimal128(v: Decimal128) -> v1::Decimal128 {
let value = v.val();
v1::Decimal128 {
hi: (value >> 64) as i64,
lo: value as i64,
}
let (hi, lo) = v.split_value();
v1::Decimal128 { hi, lo }
}

pub fn pb_value_to_value_ref<'a>(
Expand Down Expand Up @@ -580,9 +577,9 @@ pub fn pb_value_to_value_ref<'a>(
ValueData::TimeMillisecondValue(t) => ValueRef::Time(Time::new_millisecond(*t)),
ValueData::TimeMicrosecondValue(t) => ValueRef::Time(Time::new_microsecond(*t)),
ValueData::TimeNanosecondValue(t) => ValueRef::Time(Time::new_nanosecond(*t)),
ValueData::IntervalYearMonthValues(v) => ValueRef::Interval(Interval::from_i32(*v)),
ValueData::IntervalDayTimeValues(v) => ValueRef::Interval(Interval::from_i64(*v)),
ValueData::IntervalMonthDayNanoValues(v) => {
ValueData::IntervalYearMonthValue(v) => ValueRef::Interval(Interval::from_i32(*v)),
ValueData::IntervalDayTimeValue(v) => ValueRef::Interval(Interval::from_i64(*v)),
ValueData::IntervalMonthDayNanoValue(v) => {
let interval = Interval::from_month_day_nano(v.months, v.days, v.nanoseconds);
ValueRef::Interval(interval)
}
Expand Down Expand Up @@ -986,13 +983,13 @@ pub fn to_proto_value(value: Value) -> Option<v1::Value> {
},
Value::Interval(v) => match v.unit() {
IntervalUnit::YearMonth => v1::Value {
value_data: Some(ValueData::IntervalYearMonthValues(v.to_i32())),
value_data: Some(ValueData::IntervalYearMonthValue(v.to_i32())),
},
IntervalUnit::DayTime => v1::Value {
value_data: Some(ValueData::IntervalDayTimeValues(v.to_i64())),
value_data: Some(ValueData::IntervalDayTimeValue(v.to_i64())),
},
IntervalUnit::MonthDayNano => v1::Value {
value_data: Some(ValueData::IntervalMonthDayNanoValues(
value_data: Some(ValueData::IntervalMonthDayNanoValue(
convert_i128_to_interval(v.to_i128()),
)),
},
Expand All @@ -1011,12 +1008,9 @@ pub fn to_proto_value(value: Value) -> Option<v1::Value> {
value_data: Some(ValueData::DurationNanosecondValue(v.value())),
},
},
Value::Decimal128(v) => {
let (hi, lo) = v.split_value();
v1::Value {
value_data: Some(ValueData::Decimal128Value(v1::Decimal128 { hi, lo })),
}
}
Value::Decimal128(v) => v1::Value {
value_data: Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
},
Value::List(_) => return None,
};

Expand Down Expand Up @@ -1051,9 +1045,9 @@ pub fn proto_value_type(value: &v1::Value) -> Option<ColumnDataType> {
ValueData::TimeMillisecondValue(_) => ColumnDataType::TimeMillisecond,
ValueData::TimeMicrosecondValue(_) => ColumnDataType::TimeMicrosecond,
ValueData::TimeNanosecondValue(_) => ColumnDataType::TimeNanosecond,
ValueData::IntervalYearMonthValues(_) => ColumnDataType::IntervalYearMonth,
ValueData::IntervalDayTimeValues(_) => ColumnDataType::IntervalDayTime,
ValueData::IntervalMonthDayNanoValues(_) => ColumnDataType::IntervalMonthDayNano,
ValueData::IntervalYearMonthValue(_) => ColumnDataType::IntervalYearMonth,
ValueData::IntervalDayTimeValue(_) => ColumnDataType::IntervalDayTime,
ValueData::IntervalMonthDayNanoValue(_) => ColumnDataType::IntervalMonthDayNano,
ValueData::DurationSecondValue(_) => ColumnDataType::DurationSecond,
ValueData::DurationMillisecondValue(_) => ColumnDataType::DurationMillisecond,
ValueData::DurationMicrosecondValue(_) => ColumnDataType::DurationMicrosecond,
Expand Down Expand Up @@ -1109,10 +1103,10 @@ pub fn value_to_grpc_value(value: Value) -> GrpcValue {
TimeUnit::Nanosecond => ValueData::TimeNanosecondValue(v.value()),
}),
Value::Interval(v) => Some(match v.unit() {
IntervalUnit::YearMonth => ValueData::IntervalYearMonthValues(v.to_i32()),
IntervalUnit::DayTime => ValueData::IntervalDayTimeValues(v.to_i64()),
IntervalUnit::YearMonth => ValueData::IntervalYearMonthValue(v.to_i32()),
IntervalUnit::DayTime => ValueData::IntervalDayTimeValue(v.to_i64()),
IntervalUnit::MonthDayNano => {
ValueData::IntervalMonthDayNanoValues(convert_i128_to_interval(v.to_i128()))
ValueData::IntervalMonthDayNanoValue(convert_i128_to_interval(v.to_i128()))
}
}),
Value::Duration(v) => Some(match v.unit() {
Expand All @@ -1121,10 +1115,7 @@ pub fn value_to_grpc_value(value: Value) -> GrpcValue {
TimeUnit::Microsecond => ValueData::DurationMicrosecondValue(v.value()),
TimeUnit::Nanosecond => ValueData::DurationNanosecondValue(v.value()),
}),
Value::Decimal128(v) => {
let (hi, lo) = v.split_value();
Some(ValueData::Decimal128Value(v1::Decimal128 { hi, lo }))
}
Value::Decimal128(v) => Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
Value::List(_) => unreachable!(),
},
}
Expand Down
36 changes: 34 additions & 2 deletions src/common/decimal/src/decimal128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ impl Decimal128 {
}

/// Convert from precision, scale, a i128 value which
/// represents by two i64 value(high-64 bit, low-64 bit).
/// represents by i64 + i64 value(high-64 bit, low-64 bit).
pub fn from_value_precision_scale(hi: i64, lo: i64, precision: u8, scale: i8) -> Self {
let value = (hi as i128) << 64 | lo as i128;
// 128 64 0
// +-------+-------+-------+-------+-------+-------+-------+-------+
// | hi | lo |
// +-------+-------+-------+-------+-------+-------+-------+-------+
let hi = (hi as u128 & u64::MAX as u128) << 64;
let lo = lo as u128 & u64::MAX as u128;
let value = (hi | lo) as i128;
Self::new(value, precision, scale)
}
}
Expand Down Expand Up @@ -429,4 +435,30 @@ mod tests {
let decimal2 = Decimal128::from_str("1234567890.123").unwrap();
assert_eq!(decimal1.partial_cmp(&decimal2), None);
}

#[test]
fn test_convert_with_i128() {
let test_decimal128_eq = |value| {
let decimal1 =
Decimal128::new(value, DECIMAL128_MAX_PRECISION, DECIMAL128_DEFAULT_SCALE);
let (hi, lo) = decimal1.split_value();
let decimal2 = Decimal128::from_value_precision_scale(
hi,
lo,
DECIMAL128_MAX_PRECISION,
DECIMAL128_DEFAULT_SCALE,
);
assert_eq!(decimal1, decimal2);
};

test_decimal128_eq(1 << 63);

test_decimal128_eq(0);
test_decimal128_eq(1234567890);
test_decimal128_eq(-1234567890);
test_decimal128_eq(32781372819372817382183218i128);
test_decimal128_eq(-32781372819372817382183218i128);
test_decimal128_eq(i128::MAX);
test_decimal128_eq(i128::MIN);
}
}
49 changes: 34 additions & 15 deletions src/common/time/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,24 @@ impl Interval {
}

pub fn to_i128(&self) -> i128 {
let mut result = 0;
result |= self.months as i128;
result <<= 32;
result |= self.days as i128;
result <<= 64;
result |= self.nsecs as i128;
result
// 128 96 64 0
// +-------+-------+-------+-------+-------+-------+-------+-------+
// | months | days | nanoseconds |
// +-------+-------+-------+-------+-------+-------+-------+-------+
let months = (self.months as u128 & u32::MAX as u128) << 96;
let days = (self.days as u128 & u32::MAX as u128) << 64;
let nsecs = self.nsecs as u128 & u64::MAX as u128;
(months | days | nsecs) as i128
}

pub fn to_i64(&self) -> i64 {
let mut result = 0;
result |= self.days as i64;
result <<= 32;
result |= self.nsecs / NANOS_PER_MILLI;
result
// 64 32 0
// +-------+-------+-------+-------+-------+-------+-------+-------+
// | days | milliseconds |
// +-------+-------+-------+-------+-------+-------+-------+-------+
let days = (self.days as u64 & u32::MAX as u64) << 32;
let milliseconds = (self.nsecs / NANOS_PER_MILLI) as u64 & u32::MAX as u64;
(days | milliseconds) as i64
}

pub fn to_i32(&self) -> i32 {
Expand Down Expand Up @@ -635,9 +638,25 @@ mod tests {

#[test]
fn test_interval_i128_convert() {
let interval = Interval::from_month_day_nano(1, 1, 1);
let interval_i128 = interval.to_i128();
assert_eq!(interval_i128, 79228162532711081667253501953);
let test_interval_eq = |month, day, nano| {
let interval = Interval::from_month_day_nano(month, day, nano);
let interval_i128 = interval.to_i128();
let interval2 = Interval::from_i128(interval_i128);
assert_eq!(interval, interval2);
};

test_interval_eq(1, 2, 3);
test_interval_eq(1, -2, 3);
test_interval_eq(1, -2, -3);
test_interval_eq(-1, -2, -3);
test_interval_eq(i32::MAX, i32::MAX, i64::MAX);
test_interval_eq(i32::MIN, i32::MAX, i64::MAX);
test_interval_eq(i32::MAX, i32::MIN, i64::MAX);
test_interval_eq(i32::MAX, i32::MAX, i64::MIN);
test_interval_eq(i32::MIN, i32::MIN, i64::MAX);
test_interval_eq(i32::MAX, i32::MIN, i64::MIN);
test_interval_eq(i32::MIN, i32::MAX, i64::MIN);
test_interval_eq(i32::MIN, i32::MIN, i64::MIN);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/operator/src/req_convert/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ fn push_column_to_rows(column: Column, rows: &mut [Row]) -> Result<()> {
(TimeNanosecond, TimeNanosecondValue, time_nanosecond_values),
(
IntervalYearMonth,
IntervalYearMonthValues,
IntervalYearMonthValue,
interval_year_month_values
),
(
IntervalDayTime,
IntervalDayTimeValues,
IntervalDayTimeValue,
interval_day_time_values
),
(
IntervalMonthDayNano,
IntervalMonthDayNanoValues,
IntervalMonthDayNanoValue,
interval_month_day_nano_values
),
(DurationSecond, DurationSecondValue, duration_second_values),
Expand Down

0 comments on commit 5dc7ce1

Please sign in to comment.