Skip to content

Commit ca259e0

Browse files
committed
support cast/try_cast for decimal: signed numeric to decimal
1 parent 4b454d0 commit ca259e0

File tree

3 files changed

+188
-2
lines changed

3 files changed

+188
-2
lines changed

datafusion/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ avro = ["avro-rs", "num-traits"]
5252
[dependencies]
5353
ahash = "0.7"
5454
hashbrown = { version = "0.11", features = ["raw"] }
55-
arrow = { version = "6.3.0", features = ["prettyprint"] }
56-
parquet = { version = "6.3.0", features = ["arrow"] }
55+
#arrow = { version = "6.3.0", features = ["prettyprint"] }
56+
#parquet = { version = "6.3.0", features = ["arrow"] }
57+
arrow = { path = "/Users/kliu3/Documents/github/arrow-rs/arrow", features = ["prettyprint"] }
58+
parquet = { path = "/Users/kliu3/Documents/github/arrow-rs/parquet", features = ["arrow"] }
5759
sqlparser = "0.13"
5860
paste = "^1.0"
5961
num_cpus = "1.13.0"

datafusion/src/physical_plan/expressions/cast.rs

+92
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ pub fn cast_with_options(
125125
cast_type: DataType,
126126
cast_options: CastOptions,
127127
) -> Result<Arc<dyn PhysicalExpr>> {
128+
// TODO
129+
// support numeric data type to decimal
130+
// support one type decimal to another type decimal
128131
let expr_type = expr.data_type(input_schema)?;
129132
if expr_type == cast_type {
130133
Ok(expr.clone())
@@ -217,6 +220,95 @@ mod tests {
217220
}};
218221
}
219222

223+
#[test]
224+
fn test_cast_numeric_to_decimal() -> Result<()> {
225+
// int32
226+
generic_test_cast!(
227+
Int32Array,
228+
DataType::Int32,
229+
vec![1, 2, 3, 4, 5],
230+
// TODO
231+
UInt32Array,
232+
DataType::UInt32,
233+
vec![
234+
Some(1_u32),
235+
Some(2_u32),
236+
Some(3_u32),
237+
Some(4_u32),
238+
Some(5_u32)
239+
],
240+
DEFAULT_DATAFUSION_CAST_OPTIONS
241+
);
242+
// int64
243+
generic_test_cast!(
244+
Int32Array,
245+
DataType::Int64,
246+
vec![1, 2, 3, 4, 5],
247+
// TODO
248+
UInt32Array,
249+
DataType::UInt32,
250+
vec![
251+
Some(1_u32),
252+
Some(2_u32),
253+
Some(3_u32),
254+
Some(4_u32),
255+
Some(5_u32)
256+
],
257+
DEFAULT_DATAFUSION_CAST_OPTIONS
258+
);
259+
// float32
260+
generic_test_cast!(
261+
Int32Array,
262+
DataType::Float32,
263+
vec![1, 2, 3, 4, 5],
264+
// TODO
265+
UInt32Array,
266+
DataType::UInt32,
267+
vec![
268+
Some(1_u32),
269+
Some(2_u32),
270+
Some(3_u32),
271+
Some(4_u32),
272+
Some(5_u32)
273+
],
274+
DEFAULT_DATAFUSION_CAST_OPTIONS
275+
);
276+
// float64
277+
generic_test_cast!(
278+
Int32Array,
279+
DataType::Float64,
280+
vec![1, 2, 3, 4, 5],
281+
// TODO
282+
UInt32Array,
283+
DataType::UInt32,
284+
vec![
285+
Some(1_u32),
286+
Some(2_u32),
287+
Some(3_u32),
288+
Some(4_u32),
289+
Some(5_u32)
290+
],
291+
DEFAULT_DATAFUSION_CAST_OPTIONS
292+
);
293+
generic_test_cast!(
294+
Int32Array,
295+
DataType::Decimal(10, 4),
296+
vec![1, 2, 3, 4, 5],
297+
// TODO
298+
UInt32Array,
299+
DataType::UInt32,
300+
vec![
301+
Some(1_u32),
302+
Some(2_u32),
303+
Some(3_u32),
304+
Some(4_u32),
305+
Some(5_u32)
306+
],
307+
DEFAULT_DATAFUSION_CAST_OPTIONS
308+
);
309+
Ok(())
310+
}
311+
220312
#[test]
221313
fn test_cast_i32_u32() -> Result<()> {
222314
generic_test_cast!(

datafusion/src/physical_plan/expressions/try_cast.rs

+92
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ pub fn try_cast(
101101
input_schema: &Schema,
102102
cast_type: DataType,
103103
) -> Result<Arc<dyn PhysicalExpr>> {
104+
// TODO
105+
// support numeric data type to decimal
106+
// support one type decimal to another type decimal
104107
let expr_type = expr.data_type(input_schema)?;
105108
if expr_type == cast_type {
106109
Ok(expr.clone())
@@ -175,6 +178,95 @@ mod tests {
175178
}};
176179
}
177180

181+
#[test]
182+
fn test_try_cast_numeric_to_decimal() -> Result<()> {
183+
// int32
184+
generic_test_cast!(
185+
Int32Array,
186+
DataType::Int32,
187+
vec![1, 2, 3, 4, 5],
188+
// TODO
189+
UInt32Array,
190+
DataType::UInt32,
191+
vec![
192+
Some(1_u32),
193+
Some(2_u32),
194+
Some(3_u32),
195+
Some(4_u32),
196+
Some(5_u32)
197+
],
198+
DEFAULT_DATAFUSION_CAST_OPTIONS
199+
);
200+
// int64
201+
generic_test_cast!(
202+
Int32Array,
203+
DataType::Int64,
204+
vec![1, 2, 3, 4, 5],
205+
// TODO
206+
UInt32Array,
207+
DataType::UInt32,
208+
vec![
209+
Some(1_u32),
210+
Some(2_u32),
211+
Some(3_u32),
212+
Some(4_u32),
213+
Some(5_u32)
214+
],
215+
DEFAULT_DATAFUSION_CAST_OPTIONS
216+
);
217+
// float32
218+
generic_test_cast!(
219+
Int32Array,
220+
DataType::Float32,
221+
vec![1, 2, 3, 4, 5],
222+
// TODO
223+
UInt32Array,
224+
DataType::UInt32,
225+
vec![
226+
Some(1_u32),
227+
Some(2_u32),
228+
Some(3_u32),
229+
Some(4_u32),
230+
Some(5_u32)
231+
],
232+
DEFAULT_DATAFUSION_CAST_OPTIONS
233+
);
234+
// float64
235+
generic_test_cast!(
236+
Int32Array,
237+
DataType::Float64,
238+
vec![1, 2, 3, 4, 5],
239+
// TODO
240+
UInt32Array,
241+
DataType::UInt32,
242+
vec![
243+
Some(1_u32),
244+
Some(2_u32),
245+
Some(3_u32),
246+
Some(4_u32),
247+
Some(5_u32)
248+
],
249+
DEFAULT_DATAFUSION_CAST_OPTIONS
250+
);
251+
generic_test_cast!(
252+
Int32Array,
253+
DataType::Decimal(10, 4),
254+
vec![1, 2, 3, 4, 5],
255+
// TODO
256+
UInt32Array,
257+
DataType::UInt32,
258+
vec![
259+
Some(1_u32),
260+
Some(2_u32),
261+
Some(3_u32),
262+
Some(4_u32),
263+
Some(5_u32)
264+
],
265+
DEFAULT_DATAFUSION_CAST_OPTIONS
266+
);
267+
Ok(())
268+
}
269+
178270
#[test]
179271
fn test_cast_i32_u32() -> Result<()> {
180272
generic_test_cast!(

0 commit comments

Comments
 (0)