Skip to content

Commit 1bf37ce

Browse files
authored
feat(function): Implement map_size function (#15444)
* map_size * test
1 parent ff9b1fc commit 1bf37ce

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

src/query/functions/src/scalars/map.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use databend_common_expression::types::GenericType;
2222
use databend_common_expression::types::MapType;
2323
use databend_common_expression::types::NullType;
2424
use databend_common_expression::types::NullableType;
25+
use databend_common_expression::types::NumberType;
26+
use databend_common_expression::types::SimpleDomain;
2527
use databend_common_expression::vectorize_1_arg;
2628
use databend_common_expression::vectorize_with_builder_2_arg;
2729
use databend_common_expression::FunctionDomain;
@@ -156,4 +158,16 @@ pub fn register(registry: &mut FunctionRegistry) {
156158
|map, _| map.values
157159
),
158160
);
161+
162+
registry.register_1_arg_core::<EmptyMapType, NumberType<u8>, _, _>(
163+
"map_size",
164+
|_, _| FunctionDomain::Domain(SimpleDomain { min: 0, max: 0 }),
165+
|_, _| Value::Scalar(0u8),
166+
);
167+
168+
registry.register_1_arg::<MapType<GenericType<0>, GenericType<1>>, NumberType<u64>, _, _>(
169+
"map_size",
170+
|_, _| FunctionDomain::Full,
171+
|map, _| map.len() as u64,
172+
);
159173
}

src/query/functions/tests/it/scalars/map.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ fn test_map() {
2929
test_get(file);
3030
test_map_keys(file);
3131
test_map_values(file);
32+
test_map_size(file);
3233
}
3334

3435
fn test_create(file: &mut impl Write) {
@@ -144,3 +145,32 @@ fn test_map_values(file: &mut impl Write) {
144145
&columns,
145146
);
146147
}
148+
149+
fn test_map_size(file: &mut impl Write) {
150+
run_ast(file, "map_size({})", &[]);
151+
run_ast(file, "map_size({'a':1,'b':2,'c':3})", &[]);
152+
run_ast(file, "map_size({'a':NULL,'b':2,'c':NULL})", &[]);
153+
154+
let columns = [
155+
("a_col", StringType::from_data(vec!["a", "b", "c"])),
156+
("b_col", StringType::from_data(vec!["d", "e", "f"])),
157+
("c_col", StringType::from_data(vec!["x", "y", "z"])),
158+
(
159+
"d_col",
160+
StringType::from_data_with_validity(vec!["v1", "v2", "v3"], vec![true, true, true]),
161+
),
162+
(
163+
"e_col",
164+
StringType::from_data_with_validity(vec!["v4", "v5", ""], vec![true, true, false]),
165+
),
166+
(
167+
"f_col",
168+
StringType::from_data_with_validity(vec!["v6", "", "v7"], vec![true, false, true]),
169+
),
170+
];
171+
run_ast(
172+
file,
173+
"map_size(map([a_col, b_col, c_col], [d_col, e_col, f_col]))",
174+
&columns,
175+
);
176+
}

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,9 @@ Functions overloads:
24382438
0 map_keys(Map(Nothing)) :: Array(Nothing)
24392439
1 map_keys(Map(T0, T1)) :: Array(T0)
24402440
2 map_keys(Map(T0, T1) NULL) :: Array(T0) NULL
2441+
0 map_size(Map(Nothing)) :: UInt8
2442+
1 map_size(Map(T0, T1)) :: UInt64
2443+
2 map_size(Map(T0, T1) NULL) :: UInt64 NULL
24412444
0 map_values(Map(Nothing)) :: Array(Nothing)
24422445
1 map_values(Map(T0, T1)) :: Array(T1)
24432446
2 map_values(Map(T0, T1) NULL) :: Array(T1) NULL

src/query/functions/tests/it/scalars/testdata/map.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,57 @@ evaluation (internal):
338338
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
339339

340340

341+
ast : map_size({})
342+
raw expr : map_size(map(array(), array()))
343+
checked expr : map_size<Map(Nothing)>(map<Array(Nothing), Array(Nothing)>(array<>(), array<>()))
344+
optimized expr : 0_u8
345+
output type : UInt8
346+
output domain : {0..=0}
347+
output : 0
348+
349+
350+
ast : map_size({'a':1,'b':2,'c':3})
351+
raw expr : map_size(map(array('a', 'b', 'c'), array(1, 2, 3)))
352+
checked expr : map_size<T0=String, T1=UInt8><Map(T0, T1)>(map<T0=String, T1=UInt8><Array(T0), Array(T1)>(array<T0=String><T0, T0, T0>("a", "b", "c"), array<T0=UInt8><T0, T0, T0>(1_u8, 2_u8, 3_u8)))
353+
optimized expr : 3_u64
354+
output type : UInt64
355+
output domain : {3..=3}
356+
output : 3
357+
358+
359+
ast : map_size({'a':NULL,'b':2,'c':NULL})
360+
raw expr : map_size(map(array('a', 'b', 'c'), array(NULL, 2, NULL)))
361+
checked expr : map_size<T0=String, T1=UInt8 NULL><Map(T0, T1)>(map<T0=String, T1=UInt8 NULL><Array(T0), Array(T1)>(array<T0=String><T0, T0, T0>("a", "b", "c"), array<T0=UInt8 NULL><T0, T0, T0>(CAST(NULL AS UInt8 NULL), CAST(2_u8 AS UInt8 NULL), CAST(NULL AS UInt8 NULL))))
362+
optimized expr : 3_u64
363+
output type : UInt64
364+
output domain : {3..=3}
365+
output : 3
366+
367+
368+
ast : map_size(map([a_col, b_col, c_col], [d_col, e_col, f_col]))
369+
raw expr : map_size(map(array(a_col::String, b_col::String, c_col::String), array(d_col::String NULL, e_col::String NULL, f_col::String NULL)))
370+
checked expr : map_size<T0=String, T1=String NULL><Map(T0, T1)>(map<T0=String, T1=String NULL><Array(T0), Array(T1)>(array<T0=String><T0, T0, T0>(a_col, b_col, c_col), array<T0=String NULL><T0, T0, T0>(d_col, e_col, f_col)))
371+
evaluation:
372+
+--------+-------------+-------------+-------------+---------------+----------------------+----------------------+---------+
373+
| | a_col | b_col | c_col | d_col | e_col | f_col | Output |
374+
+--------+-------------+-------------+-------------+---------------+----------------------+----------------------+---------+
375+
| Type | String | String | String | String NULL | String NULL | String NULL | UInt64 |
376+
| Domain | {"a"..="c"} | {"d"..="f"} | {"x"..="z"} | {"v1"..="v3"} | {""..="v5"} ∪ {NULL} | {""..="v7"} ∪ {NULL} | Unknown |
377+
| Row 0 | 'a' | 'd' | 'x' | 'v1' | 'v4' | 'v6' | 3 |
378+
| Row 1 | 'b' | 'e' | 'y' | 'v2' | 'v5' | NULL | 3 |
379+
| Row 2 | 'c' | 'f' | 'z' | 'v3' | NULL | 'v7' | 3 |
380+
+--------+-------------+-------------+-------------+---------------+----------------------+----------------------+---------+
381+
evaluation (internal):
382+
+--------+-----------------------------------------------------------------------------------------------------------------+
383+
| Column | Data |
384+
+--------+-----------------------------------------------------------------------------------------------------------------+
385+
| a_col | StringColumn { data: 0x616263, offsets: [0, 1, 2, 3] } |
386+
| b_col | StringColumn { data: 0x646566, offsets: [0, 1, 2, 3] } |
387+
| c_col | StringColumn { data: 0x78797a, offsets: [0, 1, 2, 3] } |
388+
| d_col | NullableColumn { column: StringColumn { data: 0x763176327633, offsets: [0, 2, 4, 6] }, validity: [0b_____111] } |
389+
| e_col | NullableColumn { column: StringColumn { data: 0x76347635, offsets: [0, 2, 4, 4] }, validity: [0b_____011] } |
390+
| f_col | NullableColumn { column: StringColumn { data: 0x76367637, offsets: [0, 2, 2, 4] }, validity: [0b_____101] } |
391+
| Output | UInt64([3, 3, 3]) |
392+
+--------+-----------------------------------------------------------------------------------------------------------------+
393+
394+

tests/sqllogictests/suites/query/functions/02_0074_function_map.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ select map_keys({}), map_values({})
1212
----
1313
[] []
1414

15+
query TT
16+
select map_size({})
17+
----
18+
0
19+
1520
query TT
1621
select map_keys({'k1':1,'k2':2,'k3':null}), map_values({'k1':1,'k2':2,'k3':null})
1722
----
@@ -30,6 +35,13 @@ select map_keys(col1), map_keys(col2) from t
3035
['k5','k6'] ['d','e','f']
3136
[] NULL
3237

38+
query TT
39+
select map_size(col1), map_size(col2) from t
40+
----
41+
3 2
42+
2 3
43+
0 NULL
44+
3345
query TT
3446
select map_values(col1), map_values(col2) from t
3547
----

0 commit comments

Comments
 (0)