Skip to content

Commit 5d7e38b

Browse files
authored
Merge pull request #7553 from andylokandy/fixxxxx
2 parents e89dfe5 + 9ba1abf commit 5d7e38b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3673
-3357
lines changed

Cargo.lock

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/query/codegen/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ path = "src/bin/codegen.rs"
1818
common-datavalues = { path = "../datavalues" }
1919
common-expression = { path = "../expression" }
2020

21+
# TODO(andylokandy): Use the version from crates.io once
22+
# https://github.com/reem/rust-ordered-float/pull/110 is released.
2123
itertools = "0.10"
24+
ordered-float = { git = "https://github.com/andylokandy/rust-ordered-float.git", branch = "as", features = ["serde"] }
2225
serde = { version = "1.0.137", features = ["derive"] }
2326
serde_json = "1.0.81"

src/query/codegen/src/writes/arithmetics_type_v2.rs

Lines changed: 60 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use std::fs::File;
1616
use std::io::Write;
1717
use std::path::Path;
1818

19-
use common_expression::types::DataType;
20-
use common_expression::types::NumberTypeInfo;
19+
use common_expression::types::NumberDataType;
2120

2221
pub enum OP {
2322
Plus,
@@ -37,7 +36,7 @@ pub fn codegen_arithmetic_type_v2() {
3736
let mut file = File::create(&path).expect("open");
3837

3938
// Write the head.
40-
let codegen_src = file!();
39+
let codegen_src_path = file!();
4140
writeln!(
4241
file,
4342
"// Copyright 2021 Datafuse Labs.
@@ -54,7 +53,10 @@ pub fn codegen_arithmetic_type_v2() {
5453
// See the License for the specific language governing permissions and
5554
// limitations under the License.
5655
57-
// This code is generated by {codegen_src}. DO NOT EDIT.
56+
// This code is generated by {codegen_src_path}. DO NOT EDIT.
57+
58+
use ordered_float::OrderedFloat;
59+
5860
use super::number::Number;
5961
6062
pub trait ResultTypeOfBinary: Sized {{
@@ -81,30 +83,26 @@ pub trait ResultTypeOfUnary: Sized {{
8183
)
8284
.unwrap();
8385

84-
let lhs = vec![
85-
DataType::UInt8,
86-
DataType::UInt16,
87-
DataType::UInt32,
88-
DataType::UInt64,
89-
DataType::Int8,
90-
DataType::Int16,
91-
DataType::Int32,
92-
DataType::Int64,
93-
DataType::Float32,
94-
DataType::Float64,
86+
let number_types = vec![
87+
NumberDataType::UInt8,
88+
NumberDataType::UInt16,
89+
NumberDataType::UInt32,
90+
NumberDataType::UInt64,
91+
NumberDataType::Int8,
92+
NumberDataType::Int16,
93+
NumberDataType::Int32,
94+
NumberDataType::Int64,
95+
NumberDataType::Float32,
96+
NumberDataType::Float64,
9597
];
96-
let rhs = lhs.clone();
9798

98-
for a in &lhs {
99-
for b in &rhs {
100-
let left = a.number_type_info().unwrap();
101-
let right = b.number_type_info().unwrap();
102-
103-
let add_mul = arithmetic_coercion(left, right, OP::Plus);
104-
let minus = arithmetic_coercion(left, right, OP::Minus);
105-
let intdiv = arithmetic_coercion(left, right, OP::IntDiv);
106-
let modulo = arithmetic_coercion(left, right, OP::Modulo);
107-
let least_super = arithmetic_coercion(left, right, OP::Super);
99+
for lhs in &number_types {
100+
for rhs in &number_types {
101+
let add_mul = arithmetic_coercion(*lhs, *rhs, OP::Plus);
102+
let minus = arithmetic_coercion(*lhs, *rhs, OP::Minus);
103+
let intdiv = arithmetic_coercion(*lhs, *rhs, OP::IntDiv);
104+
let modulo = arithmetic_coercion(*lhs, *rhs, OP::Modulo);
105+
let least_super = arithmetic_coercion(*lhs, *rhs, OP::Super);
108106

109107
writeln!(
110108
file,
@@ -116,8 +114,8 @@ impl ResultTypeOfBinary for ({}, {}) {{
116114
type Modulo = {};
117115
type LeastSuper = {};
118116
}}",
119-
to_primitive_str(a.clone()),
120-
to_primitive_str(b.clone()),
117+
to_primitive_str(*lhs),
118+
to_primitive_str(*rhs),
121119
to_primitive_str(add_mul),
122120
to_primitive_str(minus),
123121
to_primitive_str(intdiv),
@@ -128,11 +126,11 @@ impl ResultTypeOfBinary for ({}, {}) {{
128126
}
129127
}
130128

131-
for arg in &lhs {
132-
let negate = neg_coercion(arg.number_type_info().unwrap());
129+
for arg in &number_types {
130+
let negate = neg_coercion(*arg);
133131

134132
match negate {
135-
DataType::Float32 | DataType::Float64 => {
133+
NumberDataType::Float32 | NumberDataType::Float64 => {
136134
writeln!(
137135
file,
138136
"
@@ -159,7 +157,7 @@ impl ResultTypeOfUnary for {} {{
159157
Some(self % rhs)
160158
}}
161159
}}",
162-
to_primitive_str(arg.clone()),
160+
to_primitive_str(*arg),
163161
to_primitive_str(negate),
164162
)
165163
.unwrap();
@@ -192,7 +190,7 @@ impl ResultTypeOfUnary for {} {{
192190
self.checked_rem(rhs)
193191
}}
194192
}}",
195-
to_primitive_str(arg.clone()),
193+
to_primitive_str(*arg),
196194
to_primitive_str(negate),
197195
)
198196
.unwrap();
@@ -202,97 +200,57 @@ impl ResultTypeOfUnary for {} {{
202200
file.flush().unwrap();
203201
}
204202

205-
fn to_primitive_str(dt: DataType) -> &'static str {
203+
fn to_primitive_str(dt: NumberDataType) -> &'static str {
206204
match dt {
207-
DataType::UInt8 => "u8",
208-
DataType::UInt16 => "u16",
209-
DataType::UInt32 => "u32",
210-
DataType::UInt64 => "u64",
211-
DataType::Int8 => "i8",
212-
DataType::Int16 => "i16",
213-
DataType::Int32 => "i32",
214-
DataType::Int64 => "i64",
215-
DataType::Float32 => "f32",
216-
DataType::Float64 => "f64",
217-
_ => panic!("unsupported data type"),
205+
NumberDataType::UInt8 => "u8",
206+
NumberDataType::UInt16 => "u16",
207+
NumberDataType::UInt32 => "u32",
208+
NumberDataType::UInt64 => "u64",
209+
NumberDataType::Int8 => "i8",
210+
NumberDataType::Int16 => "i16",
211+
NumberDataType::Int32 => "i32",
212+
NumberDataType::Int64 => "i64",
213+
NumberDataType::Float32 => "OrderedFloat<f32>",
214+
NumberDataType::Float64 => "OrderedFloat<f64>",
218215
}
219216
}
220217

221-
fn arithmetic_coercion(a: NumberTypeInfo, b: NumberTypeInfo, op: OP) -> DataType {
222-
let is_signed = a.is_signed || b.is_signed;
223-
let is_float = a.is_float || b.is_float;
224-
let bit_width = a.bit_width.max(b.bit_width);
218+
fn arithmetic_coercion(a: NumberDataType, b: NumberDataType, op: OP) -> NumberDataType {
219+
let is_signed = a.is_signed() || b.is_signed();
220+
let is_float = a.is_float() || b.is_float();
221+
let bit_width = a.bit_width().max(b.bit_width());
225222

226223
match op {
227-
OP::Plus | OP::Mul => {
228-
let info = NumberTypeInfo {
229-
is_signed,
230-
is_float,
231-
bit_width: next_bit_width(bit_width),
232-
};
233-
234-
DataType::new_number(info)
235-
}
224+
OP::Plus | OP::Mul => NumberDataType::new(next_bit_width(bit_width), is_signed, is_float),
236225
OP::Modulo => {
237226
if is_float {
238-
return DataType::Float64;
227+
return NumberDataType::Float64;
239228
}
240-
let result_is_signed = a.is_signed;
241-
let right_size = b.bit_width;
229+
let result_is_signed = a.is_signed();
230+
let right_size = b.bit_width();
242231
let size_of_result = if result_is_signed {
243232
next_bit_width(right_size)
244233
} else {
245234
right_size
246235
};
247236

248-
let info = NumberTypeInfo {
249-
is_signed: result_is_signed,
250-
is_float: false,
251-
bit_width: size_of_result,
252-
};
253-
DataType::new_number(info)
254-
}
255-
OP::Minus => {
256-
let info = NumberTypeInfo {
257-
is_signed: true,
258-
is_float,
259-
bit_width: next_bit_width(bit_width),
260-
};
261-
DataType::new_number(info)
262-
}
263-
OP::Div => DataType::Float64,
264-
OP::IntDiv => {
265-
let info = NumberTypeInfo {
266-
is_signed,
267-
is_float: false,
268-
bit_width,
269-
};
270-
DataType::new_number(info)
271-
}
272-
OP::Super => {
273-
let info = NumberTypeInfo {
274-
is_signed,
275-
is_float,
276-
bit_width,
277-
};
278-
DataType::new_number(info)
237+
NumberDataType::new(size_of_result, result_is_signed, false)
279238
}
239+
OP::Minus => NumberDataType::new(next_bit_width(bit_width), true, is_float),
240+
OP::Div => NumberDataType::Float64,
241+
OP::IntDiv => NumberDataType::new(bit_width, is_signed, false),
242+
OP::Super => NumberDataType::new(bit_width, is_signed, is_float),
280243
}
281244
}
282245

283-
fn neg_coercion(a: NumberTypeInfo) -> DataType {
284-
let bit_width = if a.is_signed {
285-
a.bit_width
246+
fn neg_coercion(a: NumberDataType) -> NumberDataType {
247+
let bit_width = if a.is_signed() {
248+
a.bit_width()
286249
} else {
287-
next_bit_width(a.bit_width)
250+
next_bit_width(a.bit_width())
288251
};
289252

290-
let info = NumberTypeInfo {
291-
is_float: a.is_float,
292-
is_signed: true,
293-
bit_width,
294-
};
295-
DataType::new_number(info)
253+
NumberDataType::new(bit_width, true, a.is_float())
296254
}
297255

298256
const fn next_bit_width(width: u8) -> u8 {

src/query/expression/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ enum-as-inner = "0.4"
2525
itertools = "0.10"
2626
match-template = "0.0.1"
2727
num-traits = "0.2.15"
28-
ordered-float = { version = "3.0", features = ["serde"] }
28+
# TODO(andylokandy): Use the version from crates.io once
29+
# https://github.com/reem/rust-ordered-float/pull/110 is released.
30+
ordered-float = { git = "https://github.com/andylokandy/rust-ordered-float.git", branch = "as", features = ["serde"] }
2931
serde = "1.0"
3032

3133
[dev-dependencies]

src/query/expression/src/column_from.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use std::iter::Iterator;
1616

17+
use ordered_float::OrderedFloat;
18+
1719
use crate::types::nullable::NullableColumn;
1820
use crate::types::number::*;
1921
use crate::types::*;
@@ -122,6 +124,28 @@ impl<'a, D: AsRef<[&'a str]>> ColumnFrom<D, [Vec<u8>; 2]> for Column {
122124
}
123125
}
124126

127+
impl<D: AsRef<[f32]>> ColumnFrom<D, [Vec<f32>; 0]> for Column {
128+
fn from_data(d: D) -> Column {
129+
NumberType::<OrderedFloat<f32>>::upcast_column(
130+
NumberType::<OrderedFloat<f32>>::column_from_iter(
131+
d.as_ref().iter().map(|f| OrderedFloat(*f)),
132+
&[],
133+
),
134+
)
135+
}
136+
}
137+
138+
impl<D: AsRef<[f64]>> ColumnFrom<D, [Vec<f64>; 0]> for Column {
139+
fn from_data(d: D) -> Column {
140+
NumberType::<OrderedFloat<f64>>::upcast_column(
141+
NumberType::<OrderedFloat<f64>>::column_from_iter(
142+
d.as_ref().iter().map(|f| OrderedFloat(*f)),
143+
&[],
144+
),
145+
)
146+
}
147+
}
148+
125149
for_common_scalar_values! { impl_from_iterator }
126150
for_common_scalar_values! { impl_from_opt_iterator }
127151
for_common_scalar_values! { impl_from_vec }

0 commit comments

Comments
 (0)