|
| 1 | +# cardinality |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +Returns the number of elements in a MAP value, it is an alias of [map_size()](map_size.md). |
| 6 | + |
| 7 | +From version 2.5, StarRocks supports querying complex data types MAP and STRUCT from data lakes. MAP is an unordered collection of key-value pairs, for example, `{"a":1, "b":2}`. One key-value pair constitutes one element, for example, `{"a":1, "b":2}` contains two elements. |
| 8 | + |
| 9 | +You can use external catalogs provided by StarRocks to query MAP and STRUCT data from Apache Hive™, Apache Hudi, and Apache Iceberg. You can only query data from ORC and Parquet files. For more information about how to use external catalogs to query external data sources, see [Overview of catalogs](../../../data_source/catalog/catalog_overview.md) and topics related to the required catalog type. |
| 10 | + |
| 11 | +## Syntax |
| 12 | + |
| 13 | +```Haskell |
| 14 | +INT cardinality(any_map) |
| 15 | +``` |
| 16 | + |
| 17 | +## Parameters |
| 18 | + |
| 19 | +`any_map`: the MAP value from which you want to retrieve the number of elements. |
| 20 | + |
| 21 | +## Return value |
| 22 | + |
| 23 | +Returns a value of the INT value. |
| 24 | + |
| 25 | +If the input is NULL, NULL is returned. |
| 26 | + |
| 27 | +If a key or value in the MAP value is NULL, NULL is processed as a normal value. |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +This example uses the Hive table `hive_map`, which contains the following data: |
| 32 | + |
| 33 | +```Plain |
| 34 | +select * from hive_map order by col_int; |
| 35 | ++---------+---------------+ |
| 36 | +| col_int | col_map | |
| 37 | ++---------+---------------+ |
| 38 | +| 1 | {"a":1,"b":2} | |
| 39 | +| 2 | {"c":3} | |
| 40 | +| 3 | {"d":4,"e":5} | |
| 41 | ++---------+---------------+ |
| 42 | +3 rows in set (0.05 sec) |
| 43 | +``` |
| 44 | + |
| 45 | +After a Hive catalog is created in your database, you can use this catalog and the cardinality() function to obtain the number of elements in each row of the `cardinality` column. |
| 46 | + |
| 47 | +```Plaintext |
| 48 | +select cardinality(col_map) from hive_map order by col_int; |
| 49 | ++----------------------+ |
| 50 | +| cardinality(col_map) | |
| 51 | ++----------------------+ |
| 52 | +| 2 | |
| 53 | +| 1 | |
| 54 | +| 2 | |
| 55 | ++----------------------+ |
| 56 | +3 rows in set (0.05 sec) |
| 57 | +``` |
| 58 | + |
| 59 | +## keyword |
| 60 | + |
| 61 | +CARDINALITY,MAP_LENGTH,MAP |
0 commit comments