Skip to content

Commit 959856b

Browse files
authored
feat: Implement equality = and inequality <> support for BinaryView (#11004)
* feat: Implement equality = and inequality <> support for BinaryView Signed-off-by: Chojan Shang <[email protected]> * chore: make fmt happy Signed-off-by: Chojan Shang <[email protected]> --------- Signed-off-by: Chojan Shang <[email protected]>
1 parent 5b4c365 commit 959856b

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed

datafusion/common/src/scalar/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ impl ScalarValue {
15731573
DataType::Utf8View => build_array_string!(StringViewArray, Utf8View),
15741574
DataType::Utf8 => build_array_string!(StringArray, Utf8),
15751575
DataType::LargeUtf8 => build_array_string!(LargeStringArray, LargeUtf8),
1576+
DataType::BinaryView => build_array_string!(BinaryViewArray, BinaryView),
15761577
DataType::Binary => build_array_string!(BinaryArray, Binary),
15771578
DataType::LargeBinary => build_array_string!(LargeBinaryArray, LargeBinary),
15781579
DataType::Date32 => build_array_primitive!(Date32Array, Date32),
@@ -1727,7 +1728,6 @@ impl ScalarValue {
17271728
| DataType::Time64(TimeUnit::Millisecond)
17281729
| DataType::Map(_, _)
17291730
| DataType::RunEndEncoded(_, _)
1730-
| DataType::BinaryView
17311731
| DataType::ListView(_)
17321732
| DataType::LargeListView(_) => {
17331733
return _internal_err!(

datafusion/expr/src/type_coercion/binary.rs

+3
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,9 @@ fn binary_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>
991991
(Binary | Utf8, Binary) | (Binary, Utf8) => Some(Binary),
992992
(LargeBinary | Binary | Utf8 | LargeUtf8, LargeBinary)
993993
| (LargeBinary, Binary | Utf8 | LargeUtf8) => Some(LargeBinary),
994+
(BinaryView, BinaryView) | (BinaryView, Binary) | (Binary, BinaryView) => {
995+
Some(BinaryView)
996+
}
994997
_ => None,
995998
}
996999
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
########
19+
## Test setup
20+
########
21+
22+
statement ok
23+
create table test_source as values
24+
('Andrew', 'X'),
25+
('Xiangpeng', 'Xiangpeng'),
26+
('Raphael', 'R'),
27+
(NULL, 'R')
28+
;
29+
30+
# Table with the different combination of column types
31+
statement ok
32+
CREATE TABLE test AS
33+
SELECT
34+
arrow_cast(column1, 'Utf8') as column1_utf8,
35+
arrow_cast(column2, 'Utf8') as column2_utf8,
36+
arrow_cast(column1, 'Binary') AS column1_binary,
37+
arrow_cast(column2, 'Binary') AS column2_binary,
38+
arrow_cast(arrow_cast(column1, 'Binary'), 'BinaryView') AS column1_binaryview,
39+
arrow_cast(arrow_cast(column2, 'Binary'), 'BinaryView') AS column2_binaryview,
40+
arrow_cast(column1, 'Dictionary(Int32, Binary)') AS column1_dict,
41+
arrow_cast(column2, 'Dictionary(Int32, Binary)') AS column2_dict
42+
FROM test_source;
43+
44+
statement ok
45+
drop table test_source
46+
47+
########
48+
## BinaryView to BinaryView
49+
########
50+
51+
# BinaryView scalar to BinaryView scalar
52+
53+
query BBBB
54+
SELECT
55+
arrow_cast(arrow_cast('NULL', 'Binary'), 'BinaryView') = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison1,
56+
arrow_cast(arrow_cast('NULL', 'Binary'), 'BinaryView') <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison2,
57+
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison3,
58+
arrow_cast(arrow_cast('Xiangpeng', 'Binary'), 'BinaryView') <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') AS comparison4;
59+
----
60+
false true true true
61+
62+
63+
# BinaryView column to BinaryView column comparison as filters
64+
65+
query TT
66+
select column1_utf8, column2_utf8 from test where column1_binaryview = column2_binaryview;
67+
----
68+
Xiangpeng Xiangpeng
69+
70+
query TT
71+
select column1_utf8, column2_utf8 from test where column1_binaryview <> column2_binaryview;
72+
----
73+
Andrew X
74+
Raphael R
75+
76+
# BinaryView column to BinaryView column
77+
query TTBB
78+
select
79+
column1_utf8, column2_utf8,
80+
column1_binaryview = column2_binaryview,
81+
column1_binaryview <> column2_binaryview
82+
from test;
83+
----
84+
Andrew X false true
85+
Xiangpeng Xiangpeng true false
86+
Raphael R false true
87+
NULL R NULL NULL
88+
89+
# BinaryView column to BinaryView scalar comparison
90+
query TTBBBB
91+
select
92+
column1_utf8, column2_utf8,
93+
column1_binaryview = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
94+
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = column1_binaryview,
95+
column1_binaryview <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
96+
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') <> column1_binaryview
97+
from test;
98+
----
99+
Andrew X true true false false
100+
Xiangpeng Xiangpeng false false true true
101+
Raphael R false false true true
102+
NULL R NULL NULL NULL NULL
103+
104+
########
105+
## BinaryView to Binary
106+
########
107+
108+
# test BinaryViewArray with Binary columns
109+
query TTBBBB
110+
select
111+
column1_utf8, column2_utf8,
112+
column1_binaryview = column2_binary,
113+
column2_binary = column1_binaryview,
114+
column1_binaryview <> column2_binary,
115+
column2_binary <> column1_binaryview
116+
from test;
117+
----
118+
Andrew X false false true true
119+
Xiangpeng Xiangpeng true true false false
120+
Raphael R false false true true
121+
NULL R NULL NULL NULL NULL
122+
123+
# BinaryView column to Binary scalar
124+
query TTBBBB
125+
select
126+
column1_utf8, column2_utf8,
127+
column1_binaryview = arrow_cast('Andrew', 'Binary'),
128+
arrow_cast('Andrew', 'Binary') = column1_binaryview,
129+
column1_binaryview <> arrow_cast('Andrew', 'Binary'),
130+
arrow_cast('Andrew', 'Binary') <> column1_binaryview
131+
from test;
132+
----
133+
Andrew X true true false false
134+
Xiangpeng Xiangpeng false false true true
135+
Raphael R false false true true
136+
NULL R NULL NULL NULL NULL
137+
138+
# Binary column to BinaryView scalar
139+
query TTBBBB
140+
select
141+
column1_utf8, column2_utf8,
142+
column1_binary = arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
143+
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') = column1_binary,
144+
column1_binary <> arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView'),
145+
arrow_cast(arrow_cast('Andrew', 'Binary'), 'BinaryView') <> column1_binary
146+
from test;
147+
----
148+
Andrew X true true false false
149+
Xiangpeng Xiangpeng false false true true
150+
Raphael R false false true true
151+
NULL R NULL NULL NULL NULL
152+
153+
statement ok
154+
drop table test;

0 commit comments

Comments
 (0)