Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement equality = and inequality <> support for BinaryView #11004

Merged
merged 2 commits into from
Jun 20, 2024
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
2 changes: 1 addition & 1 deletion datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ impl ScalarValue {
DataType::Utf8View => build_array_string!(StringViewArray, Utf8View),
DataType::Utf8 => build_array_string!(StringArray, Utf8),
DataType::LargeUtf8 => build_array_string!(LargeStringArray, LargeUtf8),
DataType::BinaryView => build_array_string!(BinaryViewArray, BinaryView),
DataType::Binary => build_array_string!(BinaryArray, Binary),
DataType::LargeBinary => build_array_string!(LargeBinaryArray, LargeBinary),
DataType::Date32 => build_array_primitive!(Date32Array, Date32),
Expand Down Expand Up @@ -1727,7 +1728,6 @@ impl ScalarValue {
| DataType::Time64(TimeUnit::Millisecond)
| DataType::Map(_, _)
| DataType::RunEndEncoded(_, _)
| DataType::BinaryView
| DataType::ListView(_)
| DataType::LargeListView(_) => {
return _internal_err!(
Expand Down
3 changes: 3 additions & 0 deletions datafusion/expr/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ fn binary_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>
(Binary | Utf8, Binary) | (Binary, Utf8) => Some(Binary),
(LargeBinary | Binary | Utf8 | LargeUtf8, LargeBinary)
| (LargeBinary, Binary | Utf8 | LargeUtf8) => Some(LargeBinary),
(BinaryView, BinaryView) | (BinaryView, Binary) | (Binary, BinaryView) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 I wonder if we should also handle LargeBinary as well

Copy link
Member Author

Choose a reason for hiding this comment

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

We also don't handle LargeUtf8. 👀

Copy link
Contributor

Choose a reason for hiding this comment

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

Good call -- I filed #11023 to track

Some(BinaryView)
}
_ => None,
}
}
Expand Down
154 changes: 154 additions & 0 deletions datafusion/sqllogictest/test_files/binary_view.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# 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.

########
## Test setup
########

statement ok
create table test_source as values
('Andrew', 'X'),
('Xiangpeng', 'Xiangpeng'),
('Raphael', 'R'),
(NULL, 'R')
;

# Table with the different combination of column types
statement ok
CREATE TABLE test AS
SELECT
arrow_cast(column1, 'Utf8') as column1_utf8,
arrow_cast(column2, 'Utf8') as column2_utf8,
arrow_cast(column1, 'Binary') AS column1_binary,
arrow_cast(column2, 'Binary') AS column2_binary,
arrow_cast(arrow_cast(column1, 'Binary'), 'BinaryView') AS column1_binaryview,
arrow_cast(arrow_cast(column2, 'Binary'), 'BinaryView') AS column2_binaryview,
arrow_cast(column1, 'Dictionary(Int32, Binary)') AS column1_dict,
arrow_cast(column2, 'Dictionary(Int32, Binary)') AS column2_dict
FROM test_source;

statement ok
drop table test_source

########
## BinaryView to BinaryView
########

# BinaryView scalar to BinaryView scalar

query BBBB
SELECT
arrow_cast(arrow_cast('NULL', 'Binary'), 'BinaryView') = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison1,
arrow_cast(arrow_cast('NULL', 'Binary'), 'BinaryView') <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison2,
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison3,
arrow_cast(arrow_cast('Xiangpeng', 'Binary'), 'BinaryView') <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison4;
----
false true true true


# BinaryView column to BinaryView column comparison as filters

query TT
select column1_utf8, column2_utf8 from test where column1_binaryview = column2_binaryview;
----
Xiangpeng Xiangpeng

query TT
select column1_utf8, column2_utf8 from test where column1_binaryview <> column2_binaryview;
----
Andrew X
Raphael R

# BinaryView column to BinaryView column
query TTBB
select
column1_utf8, column2_utf8,
column1_binaryview = column2_binaryview,
column1_binaryview <> column2_binaryview
from test;
----
Andrew X false true
Xiangpeng Xiangpeng true false
Raphael R false true
NULL R NULL NULL

# BinaryView column to BinaryView scalar comparison
query TTBBBB
select
column1_utf8, column2_utf8,
column1_binaryview = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = column1_binaryview,
column1_binaryview <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') <> column1_binaryview
from test;
----
Andrew X true true false false
Xiangpeng Xiangpeng false false true true
Raphael R false false true true
NULL R NULL NULL NULL NULL

########
## BinaryView to Binary
########

# test BinaryViewArray with Binary columns
query TTBBBB
select
column1_utf8, column2_utf8,
column1_binaryview = column2_binary,
column2_binary = column1_binaryview,
column1_binaryview <> column2_binary,
column2_binary <> column1_binaryview
from test;
----
Andrew X false false true true
Xiangpeng Xiangpeng true true false false
Raphael R false false true true
NULL R NULL NULL NULL NULL

# BinaryView column to Binary scalar
query TTBBBB
select
column1_utf8, column2_utf8,
column1_binaryview = arrow_cast('Andrew', 'Binary'),
arrow_cast('Andrew', 'Binary') = column1_binaryview,
column1_binaryview <> arrow_cast('Andrew', 'Binary'),
arrow_cast('Andrew', 'Binary') <> column1_binaryview
from test;
----
Andrew X true true false false
Xiangpeng Xiangpeng false false true true
Raphael R false false true true
NULL R NULL NULL NULL NULL

# Binary column to BinaryView scalar
query TTBBBB
select
column1_utf8, column2_utf8,
column1_binary = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = column1_binary,
column1_binary <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') <> column1_binary
from test;
----
Andrew X true true false false
Xiangpeng Xiangpeng false false true true
Raphael R false false true true
NULL R NULL NULL NULL NULL

statement ok
drop table test;