Skip to content

Commit a4a9bec

Browse files
committed
refactor(expr): Extract number types
1 parent 6024eaf commit a4a9bec

Some content is hidden

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

43 files changed

+3666
-3337
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ common-expression = { path = "../expression" }
2020

2121
serde = { version = "1.0.137", features = ["derive"] }
2222
serde_json = "1.0.81"
23+
ordered-float = { git = "https://github.com/andylokandy/rust-ordered-float.git", branch = "as", features = ["serde"] }
24+

src/query/codegen/src/writes/arithmetics_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn codegen_arithmetic_type() {
2525
use DataValueBinaryOperator::*;
2626
use DataValueUnaryOperator::*;
2727

28-
let dest = Path::new("src/common/datavalues/src/types");
28+
let dest = Path::new("src/query/datavalues/src/types");
2929
let path = dest.join("arithmetics_type.rs");
3030

3131
let mut file = File::create(&path).expect("open");

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

Lines changed: 58 additions & 99 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,
@@ -35,6 +34,7 @@ pub fn codegen_arithmetic_type_v2() {
3534
let path = dest.join("arithmetics_type.rs");
3635

3736
let mut file = File::create(&path).expect("open");
37+
let codegen_src_path = file!();
3838
// Write the head.
3939
writeln!(
4040
file,
@@ -52,7 +52,10 @@ pub fn codegen_arithmetic_type_v2() {
5252
// See the License for the specific language governing permissions and
5353
// limitations under the License.
5454
55-
// This code is generated by common/codegen. DO NOT EDIT.
55+
// This code is generated by {codegen_src_path}. DO NOT EDIT.
56+
57+
use ordered_float::OrderedFloat;
58+
5659
use super::number::Number;
5760
5861
pub trait ResultTypeOfBinary: Sized {{
@@ -79,30 +82,26 @@ pub trait ResultTypeOfUnary: Sized {{
7982
)
8083
.unwrap();
8184

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

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

107106
writeln!(
108107
file,
@@ -114,8 +113,8 @@ impl ResultTypeOfBinary for ({}, {}) {{
114113
type Modulo = {};
115114
type LeastSuper = {};
116115
}}",
117-
to_primitive_str(a.clone()),
118-
to_primitive_str(b.clone()),
116+
to_primitive_str(lhs.clone()),
117+
to_primitive_str(rhs.clone()),
119118
to_primitive_str(add_mul),
120119
to_primitive_str(minus),
121120
to_primitive_str(intdiv),
@@ -126,11 +125,11 @@ impl ResultTypeOfBinary for ({}, {}) {{
126125
}
127126
}
128127

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

132131
match negate {
133-
DataType::Float32 | DataType::Float64 => {
132+
NumberDataType::Float32 | NumberDataType::Float64 => {
134133
writeln!(
135134
file,
136135
"
@@ -200,97 +199,57 @@ impl ResultTypeOfUnary for {} {{
200199
file.flush().unwrap();
201200
}
202201

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

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

224222
match op {
225-
OP::Plus | OP::Mul => {
226-
let info = NumberTypeInfo {
227-
is_signed,
228-
is_float,
229-
bit_width: next_bit_width(bit_width),
230-
};
231-
232-
DataType::new_number(info)
233-
}
223+
OP::Plus | OP::Mul => NumberDataType::new(next_bit_width(bit_width), is_signed, is_float),
234224
OP::Modulo => {
235225
if is_float {
236-
return DataType::Float64;
226+
return NumberDataType::Float64;
237227
}
238-
let result_is_signed = a.is_signed;
239-
let right_size = b.bit_width;
228+
let result_is_signed = a.is_signed();
229+
let right_size = b.bit_width();
240230
let size_of_result = if result_is_signed {
241231
next_bit_width(right_size)
242232
} else {
243233
right_size
244234
};
245235

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

281-
fn neg_coercion(a: NumberTypeInfo) -> DataType {
282-
let bit_width = if a.is_signed {
283-
a.bit_width
245+
fn neg_coercion(a: NumberDataType) -> NumberDataType {
246+
let bit_width = if a.is_signed() {
247+
a.bit_width()
284248
} else {
285-
next_bit_width(a.bit_width)
249+
next_bit_width(a.bit_width())
286250
};
287251

288-
let info = NumberTypeInfo {
289-
is_float: a.is_float,
290-
is_signed: true,
291-
bit_width,
292-
};
293-
DataType::new_number(info)
252+
NumberDataType::new(bit_width, true, a.is_float())
294253
}
295254

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

src/query/expression/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ 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+
ordered-float = { git = "https://github.com/andylokandy/rust-ordered-float.git", branch = "as", features = ["serde"] }
2929
serde = "1.0"
3030

3131
[dev-dependencies]

0 commit comments

Comments
 (0)