Skip to content

Commit fe23559

Browse files
authored
Merge pull request ClickHouse#78171 from UnamedRus/mapContainsValuesLike
Add mapContainsValuesLike/mapContainsValues/mapExtractValuesLike functions
2 parents b7c0c11 + ab302b8 commit fe23559

14 files changed

+490
-136
lines changed

ci/jobs/scripts/check_style/aspell-ignore/en/aspell-dict.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,9 +2209,13 @@ mapAll
22092209
mapApply
22102210
mapConcat
22112211
mapContains
2212+
mapContainsKey
22122213
mapContainsKeyLike
2214+
mapContainsValue
2215+
mapContainsValueLike
22132216
mapExists
22142217
mapExtractKeyLike
2218+
mapExtractValueLike
22152219
mapFilter
22162220
mapFromArrays
22172221
mapKeys

docs/en/sql-reference/functions/tuple-map-functions.md

Lines changed: 155 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,50 @@ Result:
398398
└──────────────────────────────┴───────────────────────────────────┘
399399
```
400400

401+
402+
## mapKeys {#mapkeys}
403+
404+
Returns the keys of a given map.
405+
406+
This function can be optimized by enabling setting [optimize_functions_to_subcolumns](/operations/settings/settings#optimize_functions_to_subcolumns).
407+
With enabled setting, the function only reads the [keys](/sql-reference/data-types/map#reading-subcolumns-of-map) subcolumn instead the whole map.
408+
The query `SELECT mapKeys(m) FROM table` is transformed to `SELECT m.keys FROM table`.
409+
410+
**Syntax**
411+
412+
```sql
413+
mapKeys(map)
414+
```
415+
416+
**Arguments**
417+
418+
- `map` — Map. [Map](../data-types/map.md).
419+
420+
**Returned value**
421+
422+
- Array containing all keys from the `map`. [Array](../data-types/array.md).
423+
424+
**Example**
425+
426+
Query:
427+
428+
```sql
429+
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
430+
431+
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
432+
433+
SELECT mapKeys(a) FROM tab;
434+
```
435+
436+
Result:
437+
438+
```text
439+
┌─mapKeys(a)────────────┐
440+
│ ['name','age'] │
441+
│ ['number','position'] │
442+
└───────────────────────┘
443+
```
444+
401445
## mapContains {#mapcontains}
402446

403447
Returns if a given key is contained in a given map.
@@ -408,6 +452,8 @@ Returns if a given key is contained in a given map.
408452
mapContains(map, key)
409453
```
410454

455+
Alias: `mapContainsKey(map, key)`
456+
411457
**Arguments**
412458

413459
- `map` — Map. [Map](../data-types/map.md).
@@ -439,27 +485,62 @@ Result:
439485
└────────────────────────┘
440486
```
441487

442-
## mapKeys {#mapkeys}
443488

444-
Returns the keys of a given map.
489+
## mapContainsKeyLike {#mapcontainskeylike}
445490

446-
This function can be optimized by enabling setting [optimize_functions_to_subcolumns](/operations/settings/settings#optimize_functions_to_subcolumns).
447-
With enabled setting, the function only reads the [keys](/sql-reference/data-types/map#reading-subcolumns-of-map) subcolumn instead the whole map.
448-
The query `SELECT mapKeys(m) FROM table` is transformed to `SELECT m.keys FROM table`.
491+
**Syntax**
492+
493+
```sql
494+
mapContainsKeyLike(map, pattern)
495+
```
496+
497+
**Arguments**
498+
- `map` — Map. [Map](../data-types/map.md).
499+
- `pattern` - String pattern to match.
500+
501+
**Returned value**
502+
503+
- `1` if `map` contains `key` like specified pattern, `0` if not.
504+
505+
**Example**
506+
507+
Query:
508+
509+
```sql
510+
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
511+
512+
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
513+
514+
SELECT mapContainsKeyLike(a, 'a%') FROM tab;
515+
```
516+
517+
Result:
518+
519+
```text
520+
┌─mapContainsKeyLike(a, 'a%')─┐
521+
│ 1 │
522+
│ 0 │
523+
└─────────────────────────────┘
524+
```
525+
526+
## mapExtractKeyLike {#mapextractkeylike}
527+
528+
Give a map with string keys and a LIKE pattern, this function returns a map with elements where the key matches the pattern.
449529

450530
**Syntax**
451531

452532
```sql
453-
mapKeys(map)
533+
mapExtractKeyLike(map, pattern)
454534
```
455535

456536
**Arguments**
457537

458538
- `map` — Map. [Map](../data-types/map.md).
539+
- `pattern` - String pattern to match.
459540

460541
**Returned value**
461542

462-
- Array containing all keys from the `map`. [Array](../data-types/array.md).
543+
- A map containing elements the key matching the specified pattern. If no elements match the pattern, an empty map is returned.
463544

464545
**Example**
465546

@@ -468,20 +549,21 @@ Query:
468549
```sql
469550
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
470551

471-
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
552+
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
472553

473-
SELECT mapKeys(a) FROM tab;
554+
SELECT mapExtractKeyLike(a, 'a%') FROM tab;
474555
```
475556

476557
Result:
477558

478559
```text
479-
┌─mapKeys(a)────────────┐
480-
['name','age']
481-
['number','position']
482-
└───────────────────────┘
560+
┌─mapExtractKeyLike(a, 'a%')─┐
561+
{'abc':'abc'}
562+
{}
563+
└────────────────────────────
483564
```
484565

566+
485567
## mapValues {#mapvalues}
486568

487569
Returns the values of a given map.
@@ -525,12 +607,55 @@ Result:
525607
└──────────────────┘
526608
```
527609

528-
## mapContainsKeyLike {#mapcontainskeylike}
610+
## mapContainsValue {#mapcontainsvalue}
611+
612+
Returns if a given key is contained in a given map.
529613

530614
**Syntax**
531615

532616
```sql
533-
mapContainsKeyLike(map, pattern)
617+
mapContainsValue(map, value)
618+
```
619+
620+
Alias: `mapContainsValue(map, value)`
621+
622+
**Arguments**
623+
624+
- `map` — Map. [Map](../data-types/map.md).
625+
- `value` — Value. Type must match the value type of `map`.
626+
627+
**Returned value**
628+
629+
- `1` if `map` contains `value`, `0` if not. [UInt8](../data-types/int-uint.md).
630+
631+
**Example**
632+
633+
Query:
634+
635+
```sql
636+
CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
637+
638+
INSERT INTO tab VALUES ({'name':'eleven','age':'11'}), ({'number':'twelve','position':'6.0'});
639+
640+
SELECT mapContainsValue(a, '11') FROM tab;
641+
642+
```
643+
644+
Result:
645+
646+
```text
647+
┌─mapContainsValue(a, '11')─┐
648+
│ 1 │
649+
│ 0 │
650+
└───────────────────────────┘
651+
```
652+
653+
## mapContainsValueLike {#mapcontainsvaluelike}
654+
655+
**Syntax**
656+
657+
```sql
658+
mapContainsValueLike(map, pattern)
534659
```
535660

536661
**Arguments**
@@ -539,7 +664,7 @@ mapContainsKeyLike(map, pattern)
539664

540665
**Returned value**
541666

542-
- `1` if `map` contains `key` like specified pattern, `0` if not.
667+
- `1` if `map` contains `value` like specified pattern, `0` if not.
543668

544669
**Example**
545670

@@ -550,26 +675,26 @@ CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
550675

551676
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
552677

553-
SELECT mapContainsKeyLike(a, 'a%') FROM tab;
678+
SELECT mapContainsValueLike(a, 'a%') FROM tab;
554679
```
555680

556681
Result:
557682

558683
```text
559-
┌─mapContainsKeyLike(a, 'a%')─┐
560-
1 │
561-
0 │
562-
└─────────────────────────────
684+
┌─mapContainsV⋯ke(a, 'a%')─┐
685+
│ 1 │
686+
│ 0 │
687+
└──────────────────────────┘
563688
```
564689

565-
## mapExtractKeyLike {#mapextractkeylike}
690+
## mapExtractValueLike {#mapextractvaluelike}
566691

567-
Give a map with string keys and a LIKE pattern, this function returns a map with elements where the key matches the pattern.
692+
Give a map with string values and a LIKE pattern, this function returns a map with elements where the value matches the pattern.
568693

569694
**Syntax**
570695

571696
```sql
572-
mapExtractKeyLike(map, pattern)
697+
mapExtractValueLike(map, pattern)
573698
```
574699

575700
**Arguments**
@@ -579,7 +704,7 @@ mapExtractKeyLike(map, pattern)
579704

580705
**Returned value**
581706

582-
- A map containing elements the key matching the specified pattern. If no elements match the pattern, an empty map is returned.
707+
- A map containing elements the value matching the specified pattern. If no elements match the pattern, an empty map is returned.
583708

584709
**Example**
585710

@@ -590,16 +715,16 @@ CREATE TABLE tab (a Map(String, String)) ENGINE = Memory;
590715

591716
INSERT INTO tab VALUES ({'abc':'abc','def':'def'}), ({'hij':'hij','klm':'klm'});
592717

593-
SELECT mapExtractKeyLike(a, 'a%') FROM tab;
718+
SELECT mapExtractValueLike(a, 'a%') FROM tab;
594719
```
595720

596721
Result:
597722

598723
```text
599-
┌─mapExtractKeyLike(a, 'a%')─┐
600-
│ {'abc':'abc'} │
601-
│ {} │
602-
└────────────────────────────┘
724+
┌─mapExtractValueLike(a, 'a%')─┐
725+
│ {'abc':'abc'}
726+
│ {}
727+
└──────────────────────────────
603728
```
604729

605730
## mapApply {#mapapply}

src/Analyzer/Passes/FunctionToSubcolumnsPass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ std::map<std::pair<TypeIndex, String>, NodeToSubcolumnTransformer> node_transfor
185185
},
186186
},
187187
{
188-
{TypeIndex::Map, "mapContains"},
188+
{TypeIndex::Map, "mapContainsKey"},
189189
[](QueryTreeNodePtr &, FunctionNode & function_node, ColumnContext & ctx)
190190
{
191-
/// Replace `mapContains(map_argument, argument)` with `has(map_argument.keys, argument)`
191+
/// Replace `mapContainsKey(map_argument, argument)` with `has(map_argument.keys, argument)`
192192
const auto & data_type_map = assert_cast<const DataTypeMap &>(*ctx.column.type);
193193

194194
NameAndTypePair column{ctx.column.name + ".keys", std::make_shared<DataTypeArray>(data_type_map.getKeyType())};

0 commit comments

Comments
 (0)