Skip to content
Closed
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
2 changes: 1 addition & 1 deletion datafusion-testing
2 changes: 1 addition & 1 deletion datafusion/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ config_namespace! {
/// [`SessionConfig`]: https://docs.rs/datafusion/latest/datafusion/prelude/struct.SessionConfig.html
pub struct SqlParserOptions {
/// When set to true, SQL parser will parse float as decimal type
pub parse_float_as_decimal: bool, default = false
pub parse_float_as_decimal: bool, default = true

/// When set to true, SQL parser will normalize ident (convert ident to lowercase when not quoted)
pub enable_ident_normalization: bool, default = true
Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/tests/parquet/page_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ async fn prune_f64_scalar_fun_and_gt() {
Scenario::Float64,
"SELECT * FROM t where abs(f - 1) <= 0.000001 and f >= 0.1",
Some(0),
Some(10),
Some(0),
1,
5,
)
Expand Down
4 changes: 2 additions & 2 deletions datafusion/core/tests/parquet/row_group_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ async fn prune_f64_scalar_fun_and_gt() {
.with_scenario(Scenario::Float64)
.with_query("SELECT * FROM t where abs(f - 1) <= 0.000001 and f >= 0.1")
.with_expected_errors(Some(0))
.with_matched_by_stats(Some(2))
.with_pruned_by_stats(Some(2))
.with_matched_by_stats(Some(0))
.with_pruned_by_stats(Some(0))
.with_matched_by_bloom_filter(Some(0))
.with_pruned_by_bloom_filter(Some(0))
.with_expected_rows(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,11 +1071,11 @@ async fn create_scalar_function_from_sql_statement() -> Result<()> {

assert_batches_eq!(
&[
"+-----------------------------------+",
"| better_add(Float64(2),Float64(2)) |",
"+-----------------------------------+",
"| 4.0 |",
"+-----------------------------------+",
"+---------------------------------------------------------------+",
"| better_add(Decimal128(Some(20),2,1),Decimal128(Some(20),2,1)) |",
"+---------------------------------------------------------------+",
"| 4.0 |",
"+---------------------------------------------------------------+",
],
&result
);
Expand Down
36 changes: 28 additions & 8 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,25 @@ fn math_decimal_coercion(
}
// Unlike with comparison we don't coerce to a decimal in the case of floating point
// numbers, instead falling back to floating point arithmetic instead
(Decimal128(_, _), Int8 | Int16 | Int32 | Int64) => {
Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?))
}
(Int8 | Int16 | Int32 | Int64, Decimal128(_, _)) => {
Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone()))
}
(Decimal256(_, _), Int8 | Int16 | Int32 | Int64) => Some((
(
Decimal128(_, _),
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
) => Some((lhs_type.clone(), coerce_numeric_type_to_decimal(rhs_type)?)),
(
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
Decimal128(_, _),
) => Some((coerce_numeric_type_to_decimal(lhs_type)?, rhs_type.clone())),
(
Decimal256(_, _),
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
) => Some((
lhs_type.clone(),
coerce_numeric_type_to_decimal256(rhs_type)?,
)),
(Int8 | Int16 | Int32 | Int64, Decimal256(_, _)) => Some((
(
Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64,
Decimal256(_, _),
) => Some((
coerce_numeric_type_to_decimal256(lhs_type)?,
rhs_type.clone(),
)),
Expand Down Expand Up @@ -2156,6 +2164,18 @@ mod tests {
DataType::Decimal128(10, 0),
DataType::Decimal128(10, 2),
);
test_math_decimal_coercion_rule(
DataType::UInt32,
DataType::Decimal128(10, 2),
DataType::Decimal128(10, 0),
DataType::Decimal128(10, 2),
);
test_math_decimal_coercion_rule(
DataType::Decimal128(10, 2),
DataType::UInt32,
DataType::Decimal128(10, 2),
DataType::Decimal128(10, 0),
);

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/arrow_typeof.slt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Int32
query T
SELECT arrow_typeof(1.0)
----
Float64
Decimal128(2, 1)

# arrow_typeof_f32
query T
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/coalesce.slt
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ select
arrow_typeof(coalesce(1, 2.0))
;
----
1 Float64
1 Decimal128(21, 1)

query RT
select
coalesce(2.0, 1),
arrow_typeof(coalesce(2.0, 1))
;
----
2 Float64
2 Decimal128(21, 1)

query RT
select
Expand Down Expand Up @@ -322,7 +322,7 @@ select coalesce(null, 34, arrow_cast(123, 'Dictionary(Int32, Int8)'));
query RT
select coalesce(2.0, 1, '3'), arrow_typeof(coalesce(2.0, 1, '3'));
----
2 Float64
2 Decimal128(21,1)

# explicitly cast to Int8, and it will implicitly cast to Int64
query IT
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/decimal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ Decimal128(12, 2) 400420638.54
query TR
select arrow_typeof(cast(400420638.54 as decimal(12,2)) * 1.0), cast(400420638.54 as decimal(12,2)) * 1.0;
----
Float64 400420638.54
Decimal128(15, 3) 400420638.54


query TB
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/explain.slt
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ query TT
explain select struct(1, 2.3, 'abc');
----
logical_plan
01)Projection: Struct({c0:1,c1:2.3,c2:abc}) AS struct(Int64(1),Float64(2.3),Utf8("abc"))
01)Projection: Struct({c0:1,c1:2.3,c2:abc}) AS struct(Int64(1),Decimal128(Some(23),2,1),Utf8("abc"))
02)--EmptyRelation
physical_plan
01)ProjectionExec: expr=[{c0:1,c1:2.3,c2:abc} as struct(Int64(1),Float64(2.3),Utf8("abc"))]
01)ProjectionExec: expr=[{c0:1,c1:2.3,c2:abc} as struct(Int64(1),Decimal128(Some(23),2,1),Utf8("abc"))]
02)--PlaceholderRowExec


Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ select repeat('-1.2', arrow_cast(3, 'Int32'));
----
-1.2-1.2-1.2

query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Native\(LogicalType\(Native\(Int64\), Int64\)\) but received NativeType::Float64, DataType: Float64
query error DataFusion error: Error during planning: Internal error: Expect TypeSignatureClass::Native(LogicalType(Native(Int64), Int64)) but received NativeType::Decimal(2, 1), DataType: Decimal128(2, 1).
select repeat('-1.2', 3.2);

query T
Expand Down
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/operator.slt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ select
arrow_typeof(decimal + 2.0)
from numeric_types;
----
Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64
Decimal128(5, 1) Decimal128(7, 1) Decimal128(12, 1) Decimal128(22, 1) Decimal128(5, 1) Decimal128(7, 1) Decimal128(12, 1) Decimal128(22, 1) Float32 Float64 Decimal128(6, 2)

############### Subtraction ###############

Expand Down Expand Up @@ -145,7 +145,7 @@ select
arrow_typeof(decimal - 2.0)
from numeric_types;
----
Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64
Decimal128(5, 1) Decimal128(7, 1) Decimal128(12, 1) Decimal128(22, 1) Decimal128(5, 1) Decimal128(7, 1) Decimal128(12, 1) Decimal128(22, 1) Float32 Float64 Decimal128(6, 2)

############### Multiplication ###############

Expand Down Expand Up @@ -202,7 +202,7 @@ select
arrow_typeof(decimal * 2.0)
from numeric_types;
----
Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64 Float64
Decimal128(6, 1) Decimal128(8, 1) Decimal128(13, 1) Decimal128(23, 1) Decimal128(6, 1) Decimal128(8, 1) Decimal128(13, 1) Decimal128(23, 1) Float32 Float64 Decimal128(8, 3)

############### Division ###############

Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/options.slt
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ drop table a
query RR
select 10000000000000000000.01, -10000000000000000000.01
----
10000000000000000000 -10000000000000000000
10000000000000000000.01 -10000000000000000000.01

query TT
select arrow_typeof(10000000000000000000.01), arrow_typeof(-10000000000000000000.01)
----
Float64 Float64
Decimal128(22, 2) Decimal128(22, 2)

# select 0, i64::MIN, i64::MIN-1, i64::MAX, i64::MAX + 1, u64::MAX, u64::MAX + 1
query IIRIIIR
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ SELECT * FROM (VALUES (1,'a'),(2,NULL)) AS t(c1, c2)
query TT
EXPLAIN VALUES (1, 'a', -1, 1.1),(NULL, 'b', -3, 0.5)
----
logical_plan Values: (Int64(1), Utf8("a"), Int64(-1), Float64(1.1)), (Int64(NULL), Utf8("b"), Int64(-3), Float64(0.5))
logical_plan Values: (Int64(1), Utf8("a"), Int64(-1), Decimal128(Some(11),2,1)), (Int64(NULL), Utf8("b"), Int64(-3), Decimal128(Some(5),2,1) AS Decimal128(Some(5),1,1))
physical_plan DataSourceExec: partitions=1, partition_sizes=[1]

query TT
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/struct.slt
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ select * from t;
query T
select arrow_typeof(column1) from t;
----
Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "c", data_type: Float64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])
Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "c", data_type: Float64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])
Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "c", data_type: Decimal128(21, 1), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])
Struct([Field { name: "r", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "c", data_type: Decimal128(21, 1), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }])

statement ok
drop table t;
Expand Down
14 changes: 7 additions & 7 deletions datafusion/sqllogictest/test_files/subquery.slt
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,15 @@ query TT
explain SELECT t1_id, (SELECT sum(t2_int * 1.0) + 1 FROM t2 WHERE t2.t2_id = t1.t1_id) as t2_sum from t1
----
logical_plan
01)Projection: t1.t1_id, __scalar_sq_1.sum(t2.t2_int * Float64(1)) + Int64(1) AS t2_sum
01)Projection: t1.t1_id, __scalar_sq_1.sum(t2.t2_int * Decimal128(Some(10),2,1)) + Int64(1) AS t2_sum
02)--Left Join: t1.t1_id = __scalar_sq_1.t2_id
03)----TableScan: t1 projection=[t1_id]
04)----SubqueryAlias: __scalar_sq_1
05)------Projection: sum(t2.t2_int * Float64(1)) + Float64(1) AS sum(t2.t2_int * Float64(1)) + Int64(1), t2.t2_id
06)--------Aggregate: groupBy=[[t2.t2_id]], aggr=[[sum(CAST(t2.t2_int AS Float64)) AS sum(t2.t2_int * Float64(1))]]
05)------Projection: sum(t2.t2_int * Decimal128(Some(10),2,1)) + Decimal128(Some(1),20,0) AS sum(t2.t2_int * Decimal128(Some(10),2,1)) + Int64(1), t2.t2_id
06)--------Aggregate: groupBy=[[t2.t2_id]], aggr=[[sum(CAST(t2.t2_int AS Decimal128(10, 0))) AS sum(t2.t2_int * Decimal128(Some(10),2,1))]]
07)----------TableScan: t2 projection=[t2_id, t2_int]
physical_plan
01)ProjectionExec: expr=[t1_id@1 as t1_id, sum(t2.t2_int * Float64(1)) + Int64(1)@0 as t2_sum]
01)ProjectionExec: expr=[t1_id@0 as t1_id, sum(t2.t2_int * Decimal128(Some(10),2,1)) + Int64(1)@1 as t2_sum]
02)--CoalesceBatchesExec: target_batch_size=2
03)----HashJoinExec: mode=CollectLeft, join_type=Right, on=[(t2_id@1, t1_id@0)], projection=[sum(t2.t2_int * Float64(1)) + Int64(1)@0, t1_id@2]
04)------CoalescePartitionsExec
Expand Down Expand Up @@ -1450,10 +1450,10 @@ query TT
explain SELECT a FROM t1 WHERE EXISTS (SELECT count(*) FROM t2)
----
logical_plan
01)LeftSemi Join:
01)LeftSemi Join:
02)--TableScan: t1 projection=[a]
03)--SubqueryAlias: __correlated_sq_1
04)----Projection:
04)----Projection:
05)------Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]
06)--------TableScan: t2 projection=[]

Expand All @@ -1469,7 +1469,7 @@ statement count 0
create table person(id int, last_name int, state int);

query TT
explain SELECT id FROM person p WHERE EXISTS
explain SELECT id FROM person p WHERE EXISTS
(SELECT * FROM person WHERE last_name = p.last_name AND state = p.state)
----
logical_plan
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/type_coercion.slt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ EXPLAIN SELECT 1 a UNION ALL SELECT 1.1 a
----
logical_plan
01)Union
02)--Projection: CAST(Int64(1) AS Float64) AS a
02)--Projection: CAST(Int64(1) AS Decimal128(21, 1)) AS a
03)----EmptyRelation
04)--Projection: Float64(1.1) AS a
04)--Projection: CAST(Decimal128(Some(11),2,1) AS Decimal128(21, 1)) AS a
05)----EmptyRelation

# union_with_null()
Expand Down
4 changes: 2 additions & 2 deletions datafusion/sqllogictest/test_files/update.slt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ explain update t1 set a=1, b=2, c=3.0, d=NULL;
----
logical_plan
01)Dml: op=[Update] table=[t1]
02)--Projection: CAST(Int64(1) AS Int32) AS a, CAST(Int64(2) AS Utf8) AS b, Float64(3) AS c, CAST(NULL AS Int32) AS d
02)--Projection: CAST(Int64(1) AS Int32) AS a, CAST(Int64(2) AS Utf8) AS b, CAST(Decimal128(Some(30),2,1) AS Float64) AS c, CAST(NULL AS Int32) AS d
03)----TableScan: t1
physical_plan_error This feature is not implemented: Unsupported logical plan: Dml(Update)

Expand All @@ -40,7 +40,7 @@ explain update t1 set a=c+1, b=a, c=c+1.0, d=b;
----
logical_plan
01)Dml: op=[Update] table=[t1]
02)--Projection: CAST(t1.c + CAST(Int64(1) AS Float64) AS Int32) AS a, CAST(t1.a AS Utf8) AS b, t1.c + Float64(1) AS c, CAST(t1.b AS Int32) AS d
02)--Projection: CAST(t1.c + CAST(Int64(1) AS Float64) AS Int32) AS a, CAST(t1.a AS Utf8) AS b, t1.c + CAST(Decimal128(Some(10),2,1) AS Float64) AS c, CAST(t1.b AS Int32) AS d
03)----TableScan: t1
physical_plan_error This feature is not implemented: Unsupported logical plan: Dml(Update)

Expand Down
Loading