Cast to MAP Types
MAP type is used to store and process key-value pair data, which can contain various basic types of keys and values, and can also nest other complex types.
Cast to MAP
FROM String
Warning:
Historical Apache Doris behavior change
Before Apache Doris 4.0, some strings that didn't meet the MAP format might be converted normally (for example, '{1:1,2}'). Starting from Apache Doris 4.0, strings that don't meet the MAP format will report an error in strict mode and return NULL in non-strict mode.
Strict Mode
BNF Definition
<map> ::= "{" <map-content>? "}" | <empty-map>
<empty-map> ::= "{}"
<map-content> ::= <key-token> <map_key_delimiter> <value-token>
(<collection-delim> <key-token> <map_key_delimiter> <value-token>)*
<key-token> ::= <whitespace>* "\"" <inner-sequence> "\"" <whitespace>*
| <whitespace>* "'" <inner-sequence> "'" <whitespace>*
| <whitespace>* <inner-sequence> <whitespace>*
<value-token> ::= <key-token>
<inner-sequence> ::= .*
<collection-delim> ::= ","
<map_key_delimiter> ::= ":"
Rule Description
- The textual representation of a MAP must begin with a left brace
{and end with a right brace}. - Empty MAPs are directly represented as
{}. - Key-value pairs within the MAP are separated by commas
,. - Each key-value pair consists of a key, a colon
:, and a value, in the order "key:value". - Keys and values can optionally be enclosed in matching single quotes (
') or double quotes ("). The content inside the quotes is treated as a single entity. - Whitespace is allowed before and after elements within the MAP.
- During parsing, parts that match
<key-token>continue to apply the parsing rules of type K; parts that match<value-token>continue to apply the parsing rules of type V. These applied BNF rules and parsing logic are still considered part of the current MAP<K, V>'s BNF and parsing logic, with corresponding error handling and result transfer to the current MAP<K, V>'s behavior and results. - Elements can use "null" to represent a null value.
If the MAP format is not met, or if the key/value in key-value pairs does not meet the format of the corresponding type, an error is reported.
Examples
| Input String | Conversion Result | Comment |
|---|---|---|
| "{}" | {} | Valid empty MAP |
| " {}" | Error | Does not start with a brace, parsing fails |
| '{123:456}' | Cast to MAP<int,int>: {123:456} | Valid MAP |
| '{123:null}' | Cast to MAP<int,int>: {123:null} | Valid MAP containing null value |
| '{ 123 : 456 }' | Cast to MAP<int,int>: {123:456} | Valid MAP with whitespace |
| '{"123":"456"}' | Cast to MAP<int,int>: {123:456} | Valid MAP using quotes |
| '{ "123":"abc" }' | Error | "abc" cannot be converted to int type |
| '{ 1:2 ,34, 5:6}' | Error | Does not meet MAP format |
Non-Strict Mode
BNF Definition
<map> ::= "{" <map-content>? "}" | <empty-map>
<empty-map> ::= "{}"
<map-content> ::= <key-token> <map_key_delimiter> <value-token>
(<collection-delim> <key-token> <map_key_delimiter> <value-token>)*
<key-token> ::= <whitespace>* "\"" <inner-sequence> "\"" <whitespace>*
| <whitespace>* "'" <inner-sequence> "'" <whitespace>*
| <whitespace>* <inner-sequence> <whitespace>*
<value-token> ::= <key-token>
<inner-sequence> ::= .*
<collection-delim> ::= ","
<map_key_delimiter> ::= ":"
Rule Description
- The textual representation of a MAP must begin with a left brace
{and end with a right brace}. - Empty MAPs are directly represented as
{}. - Key-value pairs within the MAP are separated by commas
,. - Each key-value pair consists of a key, a colon
:, and a value, in the order "key:value". - Keys and values can optionally be enclosed in matching single quotes (
') or double quotes ("). The content inside the quotes is treated as a single entity. - Whitespace is allowed before and after elements within the MAP.
- During parsing, parts that match
<key-token>continue to apply the parsing rules of type K; parts that match<value-token>continue to apply the parsing rules of type V. These applied BNF rules and parsing logic are still considered part of the current MAP<K, V>'s BNF and parsing logic, with corresponding error handling and result transfer to the current MAP<K, V>'s behavior and results. - Elements can use "null" to represent a null value.
If the MAP format does not meet the BNF format above, NULL is returned. If key/value in key-value pairs does not meet the format of the corresponding type, the corresponding position is set to null.
Examples
| Input String | Conversion Result | Comment |
|---|---|---|
| "{}" | {} | Valid empty MAP |
| " {}" | NULL | Does not start with a brace, parsing fails |
| '{123:456}' | Cast to MAP<int,int>: {123:456} | Valid MAP |
| '{123:null}' | Cast to MAP<int,int>: {123:null} | Valid MAP containing null value |
| '{ 123 : 456 }' | Cast to MAP<int,int>: {123:456} | Valid MAP with whitespace |
| '{"123":"456"}' | Cast to MAP<int,int>: {123:456} | Valid MAP using quotes |
| '{ "123":"abc" }' | Cast to MAP<int,int>: {123:null} | "abc" cannot be converted to int type, converted to null |
| '{ 1:2 ,34, 5:6}' | NULL | Does not meet MAP format |
Duplicate Keys in Nested MAPs
When a string is cast to a MAP, ARRAY, or STRUCT, The query engine removes duplicate keys from every MAP in the converted result, including MAPs nested in an ARRAY, a STRUCT, or a MAP value. When a key occurs more than once, the last value is retained.
SELECT CAST('{"outer":[{"a":1,"a":2}]}' AS MAP<STRING, ARRAY<MAP<STRING, INT>>>);
SELECT CAST('[{"a":1,"a":2}]' AS ARRAY<MAP<STRING, INT>>);
SELECT CAST('{"m":{"a":1,"a":2}}' AS STRUCT<m:MAP<STRING, INT>>);
{"outer":[{"a":2}]}
[{"a":2}]
{"m":{"a":2}}
FROM MAP<Other Type>
Strict Mode
Rule Description
For each element in the MAP, a Cast from Other Type To Type is performed. The Cast is also in strict mode.
Examples
| Input MAP | Conversion Result | Comment |
|---|---|---|
| {"123":"456"} | Cast to MAP<int,int>: {123:456} | "123" and "456" can be converted to Int |
| {"abc":"123"} | Error | "abc" cannot be converted to Int |
| {"123":null} | Cast to MAP<int,int>: {123:null} | The Cast result of null is still null |
Non-Strict Mode
Rule Description
For each element in the MAP, a Cast from Other Type To Type is performed. The Cast is also in non-strict mode.
Examples
| Input MAP | Conversion Result | Comment |
|---|---|---|
| {"123":"456"} | Cast to MAP<int,int>: {123:456} | "123" and "456" can be converted to Int |
| {"abc":"123"} | Cast to MAP<int,int>: {null:123} | "abc" cannot be converted to Int, converted to null |
| {"123":null} | Cast to MAP<int,int>: {123:null} | The Cast result of null is still null |