Skip to content

Commit 5eb2c21

Browse files
feat: support more map functions (#18073) (#18105)
Co-authored-by: xxchan <[email protected]>
1 parent a4f2803 commit 5eb2c21

File tree

19 files changed

+482
-76
lines changed

19 files changed

+482
-76
lines changed

e2e_test/batch/types/map.slt.part

Lines changed: 116 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,66 @@ create table t (m map (float, float));
88
db error: ERROR: Failed to run the query
99

1010
Caused by:
11-
invalid map key type: double precision
11+
Bind error: invalid map key type: double precision
1212

1313

1414
query error
15-
select map_from_entries(array[1.0,2.0,3.0], array[1,2,3]);
15+
select map_from_key_values(array[1.0,2.0,3.0], array[1,2,3]);
1616
----
1717
db error: ERROR: Failed to run the query
1818

1919
Caused by these errors (recent errors listed first):
20-
1: Failed to bind expression: map_from_entries(ARRAY[1.0, 2.0, 3.0], ARRAY[1, 2, 3])
20+
1: Failed to bind expression: map_from_key_values(ARRAY[1.0, 2.0, 3.0], ARRAY[1, 2, 3])
2121
2: Expr error
2222
3: invalid map key type: numeric
2323

2424

2525
query error
26-
select map_from_entries(array[1,1,3], array[1,2,3]);
26+
select map_from_key_values(array[1,1,3], array[1,2,3]);
2727
----
2828
db error: ERROR: Failed to run the query
2929

3030
Caused by these errors (recent errors listed first):
3131
1: Expr error
32-
2: error while evaluating expression `map_from_entries('{1,1,3}', '{1,2,3}')`
32+
2: error while evaluating expression `map_from_key_values('{1,1,3}', '{1,2,3}')`
3333
3: map keys must be unique
3434

3535

3636
query ?
37-
select map_from_entries(array[1,2,3], array[1,null,3]);
37+
select map_from_key_values(array[1,2,3], array[1,null,3]);
3838
----
3939
{1:1,2:NULL,3:3}
4040

4141

4242
query error
43-
select map_from_entries(array[1,null,3], array[1,2,3]);
43+
select map_from_key_values(array[1,null,3], array[1,2,3]);
4444
----
4545
db error: ERROR: Failed to run the query
4646

4747
Caused by these errors (recent errors listed first):
4848
1: Expr error
49-
2: error while evaluating expression `map_from_entries('{1,NULL,3}', '{1,2,3}')`
49+
2: error while evaluating expression `map_from_key_values('{1,NULL,3}', '{1,2,3}')`
5050
3: map keys must not be NULL
5151

5252

5353
query error
54-
select map_from_entries(array[1,3], array[1,2,3]);
54+
select map_from_key_values(array[1,3], array[1,2,3]);
5555
----
5656
db error: ERROR: Failed to run the query
5757

5858
Caused by these errors (recent errors listed first):
5959
1: Expr error
60-
2: error while evaluating expression `map_from_entries('{1,3}', '{1,2,3}')`
60+
2: error while evaluating expression `map_from_key_values('{1,3}', '{1,2,3}')`
6161
3: map keys and values have different length
6262

6363

6464
query error
65-
select map_from_entries(array[1,2], array[1,2]) = map_from_entries(array[2,1], array[2,1]);
65+
select map_from_key_values(array[1,2], array[1,2]) = map_from_key_values(array[2,1], array[2,1]);
6666
----
6767
db error: ERROR: Failed to run the query
6868

6969
Caused by these errors (recent errors listed first):
70-
1: Failed to bind expression: map_from_entries(ARRAY[1, 2], ARRAY[1, 2]) = map_from_entries(ARRAY[2, 1], ARRAY[2, 1])
70+
1: Failed to bind expression: map_from_key_values(ARRAY[1, 2], ARRAY[1, 2]) = map_from_key_values(ARRAY[2, 1], ARRAY[2, 1])
7171
2: function equal(map(integer,integer), map(integer,integer)) does not exist
7272

7373

@@ -83,32 +83,32 @@ create table t (
8383

8484
statement ok
8585
insert into t values (
86-
map_from_entries(array['a','b','c'], array[1.0,2.0,3.0]::float[]),
87-
map_from_entries(array[1,2,3], array[true,false,true]),
88-
map_from_entries(array['a','b'],
86+
map_from_key_values(array['a','b','c'], array[1.0,2.0,3.0]::float[]),
87+
map_from_key_values(array[1,2,3], array[true,false,true]),
88+
map_from_key_values(array['a','b'],
8989
array[
90-
map_from_entries(array['a1'], array['a2']),
91-
map_from_entries(array['b1'], array['b2'])
90+
map_from_key_values(array['a1'], array['a2']),
91+
map_from_key_values(array['b1'], array['b2'])
9292
]
9393
),
9494
array[
95-
map_from_entries(array['a','b','c'], array[1,2,3]),
96-
map_from_entries(array['d','e','f'], array[4,5,6])
95+
map_from_key_values(array['a','b','c'], array[1,2,3]),
96+
map_from_key_values(array['d','e','f'], array[4,5,6])
9797
],
9898
row(
99-
map_from_entries(array['a','b','c'], array[row(1),row(2),row(3)]::struct<x int>[])
99+
map_from_key_values(array['a','b','c'], array[row(1),row(2),row(3)]::struct<x int>[])
100100
)
101101
);
102102

103103
# cast(map(character varying,integer)) -> map(character varying,double precision)
104104
query ?
105-
select map_from_entries(array['a','b','c'], array[1,2,3])::map(varchar,float);
105+
select map_from_key_values(array['a','b','c'], array[1,2,3])::map(varchar,float);
106106
----
107107
{a:1,b:2,c:3}
108108

109109

110110
statement ok
111-
insert into t(m1) values (map_from_entries(array['a','b','c'], array[1,2,3]));
111+
insert into t(m1) values (map_from_key_values(array['a','b','c'], array[1,2,3]));
112112

113113
query ????? rowsort
114114
select * from t;
@@ -144,7 +144,7 @@ db error: ERROR: Failed to run the query
144144

145145
Caused by these errors (recent errors listed first):
146146
1: Expr error
147-
2: error while evaluating expression `map_from_entries('{a,a}', '{1,2}')`
147+
2: error while evaluating expression `map_from_key_values('{a,a}', '{1,2}')`
148148
3: map keys must be unique
149149

150150

@@ -165,3 +165,96 @@ select
165165
MAP{1:'a',2:'b'}::MAP(VARCHAR,VARCHAR)
166166
----
167167
{} {1:a,2:b}
168+
169+
query error
170+
select map_from_entries(array[]);
171+
----
172+
db error: ERROR: Failed to run the query
173+
174+
Caused by these errors (recent errors listed first):
175+
1: Failed to bind expression: map_from_entries(ARRAY[])
176+
2: Bind error: cannot determine type of empty array
177+
HINT: Explicitly cast to the desired type, for example ARRAY[]::integer[].
178+
179+
180+
query error
181+
select map_from_entries(array[]::int[]);
182+
----
183+
db error: ERROR: Failed to run the query
184+
185+
Caused by these errors (recent errors listed first):
186+
1: Failed to bind expression: map_from_entries(CAST(ARRAY[] AS INT[]))
187+
2: Expr error
188+
3: invalid map entries type, expected struct, got: integer
189+
190+
191+
query error
192+
select map_from_entries(array[]::struct<key float, value int>[]);
193+
----
194+
db error: ERROR: Failed to run the query
195+
196+
Caused by these errors (recent errors listed first):
197+
1: Failed to bind expression: map_from_entries(CAST(ARRAY[] AS STRUCT<key FLOAT, value INT>[]))
198+
2: Expr error
199+
3: invalid map key type: double precision
200+
201+
202+
query ?
203+
select map_from_entries(array[]::struct<key int, value int>[]);
204+
----
205+
{}
206+
207+
208+
query ?
209+
select map_from_entries(array[row('a',1), row('b',2), row('c',3)]);
210+
----
211+
{a:1,b:2,c:3}
212+
213+
214+
query error
215+
select map_from_entries(array[row('a',1), row('a',2), row('c',3)]);
216+
----
217+
db error: ERROR: Failed to run the query
218+
219+
Caused by these errors (recent errors listed first):
220+
1: Expr error
221+
2: error while evaluating expression `map_from_entries('{"(a,1)","(a,2)","(c,3)"}')`
222+
3: map keys must be unique
223+
224+
225+
query error
226+
select map_from_entries(array[row('a',1,2)]);
227+
----
228+
db error: ERROR: Failed to run the query
229+
230+
Caused by these errors (recent errors listed first):
231+
1: Failed to bind expression: map_from_entries(ARRAY[ROW('a', 1, 2)])
232+
2: Expr error
233+
3: the underlying struct for map must have exactly two fields, got: StructType { field_names: [], field_types: [Varchar, Int32, Int32] }
234+
235+
236+
query error
237+
select map_from_entries(array[row(1.0,1)]);
238+
----
239+
db error: ERROR: Failed to run the query
240+
241+
Caused by these errors (recent errors listed first):
242+
1: Failed to bind expression: map_from_entries(ARRAY[ROW(1.0, 1)])
243+
2: Expr error
244+
3: invalid map key type: numeric
245+
246+
247+
query error
248+
select map_from_entries(null);
249+
----
250+
db error: ERROR: Failed to run the query
251+
252+
Caused by these errors (recent errors listed first):
253+
1: Failed to bind expression: map_from_entries(NULL)
254+
2: Bind error: Cannot implicitly cast 'null:Varchar' to polymorphic type AnyArray
255+
256+
257+
query ?
258+
select map_from_entries(null::struct<key int, value int>[]);
259+
----
260+
NULL

proto/expr.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ message ExprNode {
286286
// Map functions
287287
MAP_FROM_ENTRIES = 700;
288288
MAP_ACCESS = 701;
289+
MAP_KEYS = 702;
290+
MAP_VALUES = 703;
291+
MAP_ENTRIES = 704;
292+
MAP_FROM_KEY_VALUES = 705;
293+
MAP_LENGTH = 706;
294+
MAP_CONTAINS = 707;
295+
MAP_CAT = 708;
296+
MAP_INSERT = 709;
297+
MAP_DELETE = 710;
289298

290299
// Non-pure functions below (> 1000)
291300
// ------------------------

src/common/src/array/list_array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ impl ListValue {
372372

373373
/// Creates a new `ListValue` from an iterator of `Datum`.
374374
pub fn from_datum_iter<T: ToDatumRef>(
375-
datatype: &DataType,
375+
elem_datatype: &DataType,
376376
iter: impl IntoIterator<Item = T>,
377377
) -> Self {
378378
let iter = iter.into_iter();
379-
let mut builder = datatype.create_array_builder(iter.size_hint().0);
379+
let mut builder = elem_datatype.create_array_builder(iter.size_hint().0);
380380
for datum in iter {
381381
builder.append(datum);
382382
}

0 commit comments

Comments
 (0)