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

chore(query): refactor decimal scalar functions #14057

Merged
merged 7 commits into from
Dec 19, 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
21 changes: 6 additions & 15 deletions src/query/expression/src/types/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use ethnum::i256;
use ethnum::AsI256;
use itertools::Itertools;
use num_traits::NumCast;
use num_traits::ToPrimitive;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -305,8 +304,8 @@ pub trait Decimal:
fn default_decimal_size() -> DecimalSize;

fn from_float(value: f64) -> Self;
fn from_u64(value: u64) -> Self;
fn from_i64(value: i64) -> Self;
fn from_i128<U: Into<i128>>(value: U) -> Self;

fn de_binary(bytes: &mut &[u8]) -> Self;

fn to_float32(self, scale: u8) -> f32;
Expand Down Expand Up @@ -442,12 +441,8 @@ impl Decimal for i128 {
}
}

fn from_u64(value: u64) -> Self {
value.to_i128().unwrap()
}

fn from_i64(value: i64) -> Self {
value.to_i128().unwrap()
fn from_i128<U: Into<i128>>(value: U) -> Self {
value.into()
}

fn de_binary(bytes: &mut &[u8]) -> Self {
Expand Down Expand Up @@ -611,12 +606,8 @@ impl Decimal for i256 {
value.as_i256()
}

fn from_u64(value: u64) -> Self {
i256::from(value.to_i128().unwrap())
}

fn from_i64(value: i64) -> Self {
i256::from(value.to_i128().unwrap())
fn from_i128<U: Into<i128>>(value: U) -> Self {
i256::from(value.into())
}

fn de_binary(bytes: &mut &[u8]) -> Self {
Expand Down
12 changes: 6 additions & 6 deletions src/query/expression/src/utils/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pub fn read_decimal_with_size<T: Decimal>(
// Checking whether numbers need to be added or subtracted to calculate rounding
if let Some(r) = n.checked_rem(T::e(scale_diff)) {
if let Some(m) = r.checked_div(T::e(scale_diff - 1)) {
if m >= T::from_i64(5i64) {
if m >= T::from_i128(5i64) {
round_val = Some(T::one());
} else if m <= T::from_i64(-5i64) {
} else if m <= T::from_i128(-5i64) {
round_val = Some(T::minus_one());
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn read_decimal<T: Decimal>(
.checked_mul(T::e(zeros + 1))
.ok_or_else(decimal_overflow_error)?;
n = n
.checked_add(T::from_u64((v - b'0') as u64))
.checked_add(T::from_i128((v - b'0') as u64))
.ok_or_else(decimal_overflow_error)?;
zeros = 0;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn read_decimal<T: Decimal>(
.checked_mul(T::e(zeros + 1))
.ok_or_else(decimal_overflow_error)?;
n = n
.checked_add(T::from_u64((v - b'0') as u64))
.checked_add(T::from_i128((v - b'0') as u64))
.ok_or_else(decimal_overflow_error)?;
digits += zeros + 1;
zeros = 0;
Expand Down Expand Up @@ -288,11 +288,11 @@ pub fn read_decimal_from_json<T: Decimal>(
match value {
serde_json::Value::Number(n) => {
if n.is_i64() {
Ok(T::from_i64(n.as_i64().unwrap())
Ok(T::from_i128(n.as_i64().unwrap())
.with_size(size)
.ok_or_else(decimal_overflow_error)?)
} else if n.is_u64() {
Ok(T::from_u64(n.as_u64().unwrap())
Ok(T::from_i128(n.as_u64().unwrap())
.with_size(size)
.ok_or_else(decimal_overflow_error)?)
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/query/expression/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::types::decimal::DecimalColumnBuilder;
use crate::types::decimal::DecimalDataType;
use crate::types::decimal::DecimalScalar;
use crate::types::decimal::DecimalSize;
use crate::types::decimal::DecimalType;
use crate::types::nullable::NullableColumn;
use crate::types::nullable::NullableColumnBuilder;
use crate::types::nullable::NullableColumnVec;
Expand Down Expand Up @@ -284,6 +285,15 @@ impl<T: ArgType> Value<T> {
}
}

impl<T: Decimal> Value<DecimalType<T>> {
pub fn upcast_decimal(self, size: DecimalSize) -> Value<AnyType> {
match self {
Value::Scalar(scalar) => Value::Scalar(T::upcast_scalar(scalar, size)),
Value::Column(col) => Value::Column(T::upcast_column(col, size)),
}
}
}

impl Value<AnyType> {
pub fn convert_to_full_column(&self, ty: &DataType, num_rows: usize) -> Column {
match self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ where T: Decimal
}
let avg_val = match sum
.checked_mul(T::e(scale_add as u32))
.and_then(|v| v.checked_div(T::from_u64(window_size as u64)))
.and_then(|v| v.checked_div(T::from_i128(window_size as u64)))
{
Some(value) => value,
None => {
Expand Down
2 changes: 1 addition & 1 deletion src/query/functions/src/aggregates/aggregate_avg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ where
match self
.value
.checked_mul(T::Scalar::e(decimal_avg_data.scale_add as u32))
.and_then(|v| v.checked_div(T::Scalar::from_u64(self.count)))
.and_then(|v| v.checked_div(T::Scalar::from_i128(self.count)))
{
Some(value) => {
T::push_item(builder, T::to_scalar_ref(&value));
Expand Down
8 changes: 4 additions & 4 deletions src/query/functions/src/aggregates/aggregate_stddev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
self.count += 1;
if self.count > 1 {
let t = match value
.checked_mul(T::Scalar::from_u64(self.count))
.checked_mul(T::Scalar::from_i128(self.count))
.and_then(|v| v.checked_sub(self.sum))
.and_then(|v| v.checked_mul(T::Scalar::e(VARIANCE_PRECISION as u32)))
{
Expand Down Expand Up @@ -204,7 +204,7 @@ where
}
};

let count = T::Scalar::from_u64(self.count * (self.count - 1));
let count = T::Scalar::from_i128(self.count * (self.count - 1));

let add_variance = match t.checked_div(count) {
Some(t) => t,
Expand Down Expand Up @@ -236,8 +236,8 @@ where
return Ok(());
}

let other_count = T::Scalar::from_u64(other.count);
let self_count = T::Scalar::from_u64(self.count);
let other_count = T::Scalar::from_i128(other.count);
let self_count = T::Scalar::from_i128(self.count);
let t = match other_count
.checked_mul(self.sum)
.and_then(|v| v.checked_mul(T::Scalar::e(VARIANCE_PRECISION as u32)))
Expand Down
8 changes: 4 additions & 4 deletions src/query/functions/src/scalars/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use databend_common_expression::types::ALL_INTEGER_TYPES;
use databend_common_expression::types::ALL_NUMBER_CLASSES;
use databend_common_expression::types::ALL_NUMERICS_TYPES;
use databend_common_expression::types::ALL_UNSIGNED_INTEGER_TYPES;
use databend_common_expression::types::F32;
use databend_common_expression::utils::arithmetics_type::ResultTypeOfBinary;
use databend_common_expression::utils::arithmetics_type::ResultTypeOfUnary;
use databend_common_expression::values::Value;
Expand Down Expand Up @@ -72,10 +73,9 @@ use lexical_core::FormattedSize;
use num_traits::AsPrimitive;

use super::arithmetic_modulo::vectorize_modulo;
use super::decimal::register_decimal_to_float32;
use super::decimal::register_decimal_to_float64;
use super::decimal::register_decimal_to_int;
use crate::scalars::decimal::register_decimal_arithmetic;
use crate::scalars::decimal::register_decimal_to_float;

pub fn register(registry: &mut FunctionRegistry) {
registry.register_aliases("plus", &["add"]);
Expand Down Expand Up @@ -717,10 +717,10 @@ pub fn register_number_to_number(registry: &mut FunctionRegistry) {
NumberClass::Decimal128 => {
// todo(youngsofun): add decimal try_cast and decimal to int and float
if matches!(dest_type, NumberDataType::Float32) {
register_decimal_to_float32(registry);
register_decimal_to_float::<F32>(registry);
}
if matches!(dest_type, NumberDataType::Float64) {
register_decimal_to_float64(registry);
register_decimal_to_float::<F64>(registry);
}

with_number_mapped_type!(|DEST_TYPE| match dest_type {
Expand Down
Loading
Loading