Skip to content

Commit

Permalink
sqllogictests & type inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
korowa committed Dec 26, 2022
1 parent ac75ffe commit 71ee82c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 3 deletions.
98 changes: 98 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/nullif.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

statement ok
CREATE TABLE test(
int_field INT,
bool_field BOOLEAN,
text_field TEXT,
more_ints INT
) as VALUES
(1, true, 'abc', 2),
(2, false, 'def', 2),
(3, NULL, 'ghij', 3),
(NULL, NULL, NULL, 4),
(4, false, 'zxc', 5),
(NULL, true, NULL, 6)
;

# Basic arrays tests
query T
SELECT NULLIF(int_field, 2) FROM test;
----
1
NULL
3
NULL
4
NULL

query T
SELECT NULLIF(bool_field, false) FROM test;
----
true
NULL
NULL
NULL
NULL
true

query T
SELECT NULLIF(text_field, 'zxc') FROM test;
----
abc
def
ghij
NULL
NULL
NULL

query T
SELECT NULLIF(int_field, more_ints) FROM test;
----
1
NULL
NULL
NULL
4
NULL

query T
SELECT NULLIF(3, int_field) FROM test;
----
3
3
NULL
3
3
3

# Scalar values tests
query T
SELECT NULLIF(1, 1);
----
NULL

query T
SELECT NULLIF(1, 3);
----
1

query T
SELECT NULLIF(NULL, NULL);
----
NULL
6 changes: 3 additions & 3 deletions datafusion/physical-expr/src/expressions/nullif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub fn nullif_func(args: &[ColumnarValue]) -> Result<ColumnarValue> {
Ok(ColumnarValue::Array(array))
}
(ColumnarValue::Scalar(lhs), ColumnarValue::Scalar(rhs)) => {
let val = match lhs.eq(rhs) {
true => ScalarValue::Null,
let val: ScalarValue = match lhs.eq(rhs) {
true => lhs.get_datatype().try_into()?,
false => lhs.clone(),
};

Expand Down Expand Up @@ -210,7 +210,7 @@ mod tests {
let result_eq = nullif_func(&[a_eq, b_eq])?;
let result_eq = result_eq.into_array(1);

let expected_eq = Arc::new(NullArray::new(1)) as ArrayRef;
let expected_eq = Arc::new(Int32Array::from(vec![None])) as ArrayRef;

assert_eq!(expected_eq.as_ref(), result_eq.as_ref());

Expand Down

0 comments on commit 71ee82c

Please sign in to comment.