Skip to content

feat(query): Introducing Decimal64 #18010

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

Merged
merged 21 commits into from
Jun 4, 2025
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
125 changes: 73 additions & 52 deletions src/common/io/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,86 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Write;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;

use ethnum::i256;

pub fn display_decimal_128(num: i128, scale: u8) -> String {
let mut buf = String::new();
if scale == 0 {
write!(buf, "{}", num).unwrap();
} else {
let pow_scale = 10_i128.pow(scale as u32);
if num >= 0 {
write!(
buf,
"{}.{:0>width$}",
num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
} else {
write!(
buf,
"-{}.{:0>width$}",
-num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
pub fn display_decimal_128(num: i128, scale: u8) -> impl Display + Debug {
struct Decimal {
num: i128,
scale: u8,
}

impl Display for Decimal {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if self.scale == 0 {
write!(f, "{}", self.num)
} else {
let pow_scale = 10_i128.pow(self.scale as u32);
let sign = if self.num.is_negative() { "-" } else { "" };
let num = self.num.abs();
write!(
f,
"{sign}{}.{:0>width$}",
num / pow_scale,
num % pow_scale,
width = self.scale as usize
)
}
}
}

impl Debug for Decimal {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(&self.to_string())
}
}
buf

Decimal { num, scale }
}

pub fn display_decimal_256(num: i256, scale: u8) -> String {
let mut buf = String::new();
if scale == 0 {
write!(buf, "{}", num).unwrap();
} else {
let pow_scale = i256::from(10).pow(scale as u32);
// -1/10 = 0
if num >= 0 {
write!(
buf,
"{}.{:0>width$}",
num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
} else {
write!(
buf,
"-{}.{:0>width$}",
-num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
pub fn display_decimal_256(num: i256, scale: u8) -> impl Display + Debug {
struct Decimal256 {
num: i256,
scale: u8,
}

impl Display for Decimal256 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if self.scale == 0 {
write!(f, "{}", self.num)
} else {
let pow_scale = i256::from(10).pow(self.scale as u32);
// -1/10 = 0
if self.num >= 0 {
write!(
f,
"{}.{:0>width$}",
self.num / pow_scale,
(self.num % pow_scale).abs(),
width = self.scale as usize
)
} else {
write!(
f,
"-{}.{:0>width$}",
-self.num / pow_scale,
(self.num % pow_scale).abs(),
width = self.scale as usize
)
}
}
}
}

impl Debug for Decimal256 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(&self.to_string())
}
}
buf

Decimal256 { num, scale }
}
2 changes: 1 addition & 1 deletion src/common/native/src/read/array/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::io::Cursor;
use std::marker::PhantomData;

use databend_common_column::buffer::Buffer;
use databend_common_expression::types::AccessType;
use databend_common_expression::types::Number;
use databend_common_expression::types::NumberType;
use databend_common_expression::types::ValueType;
use databend_common_expression::Column;
use databend_common_expression::TableDataType;

Expand Down
2 changes: 1 addition & 1 deletion src/common/native/src/read/array/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::io::Cursor;

use databend_common_column::buffer::Buffer;
use databend_common_column::types::months_days_micros;
use databend_common_expression::types::AccessType;
use databend_common_expression::types::IntervalType;
use databend_common_expression::types::ValueType;
use databend_common_expression::Column;
use databend_common_expression::TableDataType;

Expand Down
3 changes: 3 additions & 0 deletions src/meta/proto-conv/src/schema_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ impl FromToProto for ex::types::DecimalDataType {

fn to_pb(&self) -> Result<pb::Decimal, Incompatible> {
let x = match self {
ex::types::DecimalDataType::Decimal64(x) => {
pb::decimal::Decimal::Decimal128(ex::types::decimal::DecimalSize::to_pb(x)?)
}
ex::types::DecimalDataType::Decimal128(x) => {
pb::decimal::Decimal::Decimal128(ex::types::decimal::DecimalSize::to_pb(x)?)
}
Expand Down
60 changes: 33 additions & 27 deletions src/query/ast/src/ast/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Write as _;

use derive_visitor::Drive;
use derive_visitor::DriveMut;
Expand Down Expand Up @@ -291,32 +290,39 @@ pub(crate) fn write_space_separated_string_map(
Ok(())
}

pub fn display_decimal_256(num: i256, scale: u8) -> String {
let mut buf = String::new();
if scale == 0 {
write!(buf, "{}", num).unwrap();
} else {
let pow_scale = i256::from(10).pow(scale as u32);
// -1/10 = 0
if num >= 0 {
write!(
buf,
"{}.{:0>width$}",
num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
} else {
write!(
buf,
"-{}.{:0>width$}",
-num / pow_scale,
(num % pow_scale).abs(),
width = scale as usize
)
.unwrap();
pub(crate) fn display_decimal_256(num: i256, scale: u8) -> impl Display {
struct Decimal256 {
num: i256,
scale: u8,
}

impl Display for Decimal256 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.scale == 0 {
write!(f, "{}", self.num)
} else {
let pow_scale = i256::from(10).pow(self.scale as u32);
// -1/10 = 0
if self.num >= 0 {
write!(
f,
"{}.{:0>width$}",
self.num / pow_scale,
(self.num % pow_scale).abs(),
width = self.scale as usize
)
} else {
write!(
f,
"-{}.{:0>width$}",
-self.num / pow_scale,
(self.num % pow_scale).abs(),
width = self.scale as usize
)
}
}
}
}
buf

Decimal256 { num, scale }
}
9 changes: 7 additions & 2 deletions src/query/expression/src/aggregate/group_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use databend_common_exception::Result;

use crate::types::i256;
use crate::types::number::Number;
use crate::types::AccessType;
use crate::types::AnyType;
use crate::types::BinaryColumn;
use crate::types::BinaryType;
use crate::types::BitmapType;
use crate::types::BooleanType;
use crate::types::DateType;
use crate::types::Decimal64As128Type;
use crate::types::DecimalColumn;
use crate::types::DecimalScalar;
use crate::types::DecimalType;
Expand Down Expand Up @@ -100,6 +102,9 @@ impl<const IS_FIRST: bool> ValueVisitor for HashVisitor<'_, IS_FIRST> {

fn visit_any_decimal(&mut self, column: DecimalColumn) -> Result<()> {
match column {
DecimalColumn::Decimal64(buffer, _) => {
self.combine_group_hash_type_column::<Decimal64As128Type>(&buffer);
}
DecimalColumn::Decimal128(buffer, _) => {
self.combine_group_hash_type_column::<DecimalType<i128>>(&buffer);
}
Expand Down Expand Up @@ -198,7 +203,7 @@ impl<const IS_FIRST: bool> ValueVisitor for HashVisitor<'_, IS_FIRST> {
impl<const IS_FIRST: bool> HashVisitor<'_, IS_FIRST> {
fn combine_group_hash_type_column<T>(&mut self, col: &T::Column)
where
T: ValueType,
T: AccessType,
for<'a> T::ScalarRef<'a>: AggHash,
{
if IS_FIRST {
Expand Down Expand Up @@ -270,6 +275,7 @@ where I: Index
NumberScalar::NUM_TYPE(v) => v.agg_hash(),
}),
Scalar::Decimal(v) => match v {
DecimalScalar::Decimal64(v, _) => (v as i128).agg_hash(),
DecimalScalar::Decimal128(v, _) => v.agg_hash(),
DecimalScalar::Decimal256(v, _) => v.agg_hash(),
},
Expand Down Expand Up @@ -539,7 +545,6 @@ mod tests {
use databend_common_column::bitmap::Bitmap;

use super::*;
use crate::types::AccessType;
use crate::types::ArgType;
use crate::types::Int32Type;
use crate::types::NullableColumn;
Expand Down
2 changes: 2 additions & 0 deletions src/query/expression/src/aggregate/payload_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub unsafe fn serialize_column_to_rowformat(
}
}),
Column::Decimal(v) => match v {
DecimalColumn::Decimal64(_, _) => unimplemented!(),
DecimalColumn::Decimal128(buffer, size) => {
if size.can_carried_by_128() {
for index in select_vector.iter().take(rows).copied() {
Expand Down Expand Up @@ -264,6 +265,7 @@ pub unsafe fn row_match_column(
}
}),
Column::Decimal(v) => match v {
DecimalColumn::Decimal64(_, _) => unreachable!(),
DecimalColumn::Decimal128(_, _) => row_match_column_type::<DecimalType<i128>>(
col,
validity,
Expand Down
5 changes: 4 additions & 1 deletion src/query/expression/src/converts/arrow/to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ impl From<&TableField> for Field {
TableDataType::Number(ty) => with_number_type!(|TYPE| match ty {
NumberDataType::TYPE => ArrowDataType::TYPE,
}),
TableDataType::Decimal(DecimalDataType::Decimal64(size)) => {
ArrowDataType::Decimal128(size.precision(), size.scale() as i8)
}
TableDataType::Decimal(DecimalDataType::Decimal128(size)) => {
ArrowDataType::Decimal128(size.precision(), size.scale() as i8)
}
Expand Down Expand Up @@ -315,7 +318,7 @@ impl From<&Column> for ArrayData {
Column::Decimal(c) => {
let c = c.clone().strict_decimal_data_type();
let arrow_type = match c {
DecimalColumn::Decimal128(_, size) => {
DecimalColumn::Decimal64(_, size) | DecimalColumn::Decimal128(_, size) => {
ArrowDataType::Decimal128(size.precision(), size.scale() as _)
}
DecimalColumn::Decimal256(_, size) => {
Expand Down
Loading
Loading