Skip to content
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
50 changes: 46 additions & 4 deletions datafusion/core/tests/sqllogictests/test_files/scalar.slt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
statement ok
CREATE TABLE t1(
a INT,
b INT
b INT,
c INT,
d INT
) as VALUES
(1, 100),
(2, 1000),
(3, 10000)
(1, 100, 567, 1024),
(2, 1000, 123, 256),
(3, 10000, 978, 2048)
;

# log scalar function
Expand Down Expand Up @@ -68,3 +70,43 @@ query RR rowsort
select log(0) a, log(1, 64) b
----
-Infinity Infinity

# bitwise and with column and scalar
query I rowsort
select c & 856 from t1;
----
528
848
88

# bitwise or with column and scalar
query I rowsort
select c | 856 from t1;
----
891
895
986

# bitwise xor with column and scalar
query I rowsort
select c ^ 856 from t1;
----
138
367
803

# right shift with column and scalar
query I rowsort
select d >> 2 from t1;
----
256
512
64

# left shift with column and scalar
query I rowsort
select d << 2 from t1;
----
1024
4096
8192
59 changes: 55 additions & 4 deletions datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,27 @@ fn bitwise_coercion(left_type: &DataType, right_type: &DataType) -> Option<DataT
return Some(left_type.clone());
}

// TODO support other data type
match (left_type, right_type) {
(Int64, _) | (_, Int64) => Some(Int64),
(Int32, _) | (_, Int32) => Some(Int32),
(Int16, _) | (_, Int16) => Some(Int16),
(UInt64, _) | (_, UInt64) => Some(UInt64),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 these rules look reasonable to me.

(Int64, _)
| (_, Int64)
| (UInt32, Int8)
| (Int8, UInt32)
| (UInt32, Int16)
| (Int16, UInt32)
| (UInt32, Int32)
| (Int32, UInt32) => Some(Int64),
(Int32, _)
| (_, Int32)
| (UInt16, Int16)
| (Int16, UInt16)
| (UInt16, Int8)
| (Int8, UInt16) => Some(Int32),
(UInt32, _) | (_, UInt32) => Some(UInt32),
(Int16, _) | (_, Int16) | (Int8, UInt8) | (UInt8, Int8) => Some(Int16),
(UInt16, _) | (_, UInt16) => Some(UInt16),
(Int8, _) | (_, Int8) => Some(Int8),
(UInt8, _) | (_, UInt8) => Some(UInt8),
_ => None,
}
}
Expand Down Expand Up @@ -1035,6 +1050,42 @@ mod tests {
Operator::BitwiseAnd,
DataType::Int64
);
test_coercion_binary_rule!(
DataType::UInt64,
DataType::UInt64,
Operator::BitwiseAnd,
DataType::UInt64
);
test_coercion_binary_rule!(
DataType::Int8,
DataType::UInt32,
Operator::BitwiseAnd,
DataType::Int64
);
test_coercion_binary_rule!(
DataType::UInt32,
DataType::Int32,
Operator::BitwiseAnd,
DataType::Int64
);
test_coercion_binary_rule!(
DataType::UInt16,
DataType::Int16,
Operator::BitwiseAnd,
DataType::Int32
);
test_coercion_binary_rule!(
DataType::UInt32,
DataType::UInt32,
Operator::BitwiseAnd,
DataType::UInt32
);
test_coercion_binary_rule!(
DataType::UInt16,
DataType::UInt32,
Operator::BitwiseAnd,
DataType::UInt32
);
Ok(())
}

Expand Down
99 changes: 90 additions & 9 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1664,16 +1664,28 @@ mod tests {
vec![0i64, 0i64, 1i64],
);
test_coercion!(
Int16Array,
DataType::Int16,
vec![1i16, 2i16, 3i16],
Int64Array,
DataType::Int64,
vec![10i64, 4i64, 5i64],
UInt16Array,
DataType::UInt16,
vec![1u16, 2u16, 3u16],
UInt64Array,
DataType::UInt64,
vec![10u64, 4u64, 5u64],
Operator::BitwiseAnd,
UInt64Array,
DataType::UInt64,
vec![0u64, 0u64, 1u64],
);
test_coercion!(
UInt16Array,
DataType::UInt16,
vec![1u16, 2u16, 3u16],
UInt64Array,
DataType::UInt64,
vec![10u64, 4u64, 5u64],
Operator::BitwiseOr,
Int64Array,
DataType::Int64,
vec![11i64, 6i64, 7i64],
UInt64Array,
DataType::UInt64,
vec![11u64, 6u64, 7u64],
);
test_coercion!(
Int16Array,
Expand All @@ -1687,6 +1699,18 @@ mod tests {
DataType::Int64,
vec![9i64, 4i64, 6i64],
);
test_coercion!(
UInt16Array,
DataType::UInt16,
vec![3u16, 2u16, 3u16],
UInt64Array,
DataType::UInt64,
vec![10u64, 6u64, 5u64],
Operator::BitwiseXor,
UInt64Array,
DataType::UInt64,
vec![9u64, 4u64, 6u64],
);
Ok(())
}

Expand Down Expand Up @@ -4003,6 +4027,22 @@ mod tests {
let expected = Int32Array::from(vec![Some(13), None, Some(12)]);
assert_eq!(result.as_ref(), &expected);

let left =
Arc::new(UInt32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
let right =
Arc::new(UInt32Array::from(vec![Some(1), Some(3), Some(7)])) as ArrayRef;
let mut result = bitwise_and(left.clone(), right.clone())?;
let expected = UInt32Array::from(vec![Some(0), None, Some(3)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_or(left.clone(), right.clone())?;
let expected = UInt32Array::from(vec![Some(13), None, Some(15)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_xor(left.clone(), right.clone())?;
let expected = UInt32Array::from(vec![Some(13), None, Some(12)]);
assert_eq!(result.as_ref(), &expected);

Ok(())
}

Expand All @@ -4019,6 +4059,17 @@ mod tests {
result = bitwise_shift_right(result.clone(), modules.clone())?;
assert_eq!(result.as_ref(), &input);

let input =
Arc::new(UInt32Array::from(vec![Some(2), None, Some(10)])) as ArrayRef;
let modules =
Arc::new(UInt32Array::from(vec![Some(2), Some(4), Some(8)])) as ArrayRef;
let mut result = bitwise_shift_left(input.clone(), modules.clone())?;

let expected = UInt32Array::from(vec![Some(8), None, Some(2560)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_shift_right(result.clone(), modules.clone())?;
assert_eq!(result.as_ref(), &input);
Ok(())
}

Expand All @@ -4031,6 +4082,12 @@ mod tests {
let expected = Int32Array::from(vec![Some(32)]);
assert_eq!(result.as_ref(), &expected);

let input = Arc::new(UInt32Array::from(vec![Some(2)])) as ArrayRef;
let modules = Arc::new(UInt32Array::from(vec![Some(100)])) as ArrayRef;
let result = bitwise_shift_left(input.clone(), modules.clone())?;

let expected = UInt32Array::from(vec![Some(32)]);
assert_eq!(result.as_ref(), &expected);
Ok(())
}

Expand All @@ -4049,6 +4106,21 @@ mod tests {
result = bitwise_xor_scalar(&left, right).unwrap()?;
let expected = Int32Array::from(vec![Some(15), None, Some(8)]);
assert_eq!(result.as_ref(), &expected);

let left =
Arc::new(UInt32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
let right = ScalarValue::from(3u32);
let mut result = bitwise_and_scalar(&left, right.clone()).unwrap()?;
let expected = UInt32Array::from(vec![Some(0), None, Some(3)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_or_scalar(&left, right.clone()).unwrap()?;
let expected = UInt32Array::from(vec![Some(15), None, Some(11)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_xor_scalar(&left, right).unwrap()?;
let expected = UInt32Array::from(vec![Some(15), None, Some(8)]);
assert_eq!(result.as_ref(), &expected);
Ok(())
}

Expand All @@ -4064,6 +4136,15 @@ mod tests {
result = bitwise_shift_right_scalar(&result, module).unwrap()?;
assert_eq!(result.as_ref(), &input);

let input = Arc::new(UInt32Array::from(vec![Some(2), None, Some(4)])) as ArrayRef;
let module = ScalarValue::from(10u32);
let mut result = bitwise_shift_left_scalar(&input, module.clone()).unwrap()?;

let expected = UInt32Array::from(vec![Some(2048), None, Some(4096)]);
assert_eq!(result.as_ref(), &expected);

result = bitwise_shift_right_scalar(&result, module).unwrap()?;
assert_eq!(result.as_ref(), &input);
Ok(())
}

Expand Down
Loading