Skip to content

Commit 5d61fdd

Browse files
authored
Hive: Support MAP<K, V> column types (#2346)
1 parent 182eae8 commit 5d61fdd

6 files changed

Lines changed: 39 additions & 7 deletions

File tree

src/ast/data_type.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,11 @@ pub enum DataType {
439439
Custom(ObjectName, Vec<String>),
440440
/// Arrays.
441441
Array(ArrayElemTypeDef),
442-
/// Map, see [ClickHouse].
442+
/// Map, see [ClickHouse], [Hive].
443443
///
444444
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/map
445-
Map(Box<DataType>, Box<DataType>),
445+
/// [Hive]: https://hive.apache.org/docs/latest/language/languagemanual-types/
446+
Map(Box<DataType>, Box<DataType>, MapBracketKind),
446447
/// Tuple, see [ClickHouse].
447448
///
448449
/// [ClickHouse]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
@@ -785,9 +786,14 @@ impl fmt::Display for DataType {
785786
DataType::LowCardinality(data_type) => {
786787
write!(f, "LowCardinality({data_type})")
787788
}
788-
DataType::Map(key_data_type, value_data_type) => {
789-
write!(f, "Map({key_data_type}, {value_data_type})")
790-
}
789+
DataType::Map(key_data_type, value_data_type, bracket) => match bracket {
790+
MapBracketKind::Parentheses => {
791+
write!(f, "Map({key_data_type}, {value_data_type})")
792+
}
793+
MapBracketKind::AngleBrackets => {
794+
write!(f, "MAP<{key_data_type}, {value_data_type}>")
795+
}
796+
},
791797
DataType::Tuple(fields) => {
792798
write!(f, "Tuple({})", display_comma_separated(fields))
793799
}
@@ -904,6 +910,17 @@ pub enum StructBracketKind {
904910
AngleBrackets,
905911
}
906912

913+
/// Type of brackets used for `MAP` types.
914+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
915+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
916+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
917+
pub enum MapBracketKind {
918+
/// Example: `Map(String, UInt16)`
919+
Parentheses,
920+
/// Example: `MAP<STRING, INT>`
921+
AngleBrackets,
922+
}
923+
907924
/// Timestamp and Time data types information about TimeZone formatting.
908925
///
909926
/// This is more related to a display information than real differences between each variant. To

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::{
5353

5454
pub use self::data_type::{
5555
ArrayElemTypeDef, BinaryLength, CharLengthUnits, CharacterLength, DataType, EnumMember,
56-
ExactNumberInfo, IntervalFields, StructBracketKind, TimezoneInfo,
56+
ExactNumberInfo, IntervalFields, MapBracketKind, StructBracketKind, TimezoneInfo,
5757
};
5858
pub use self::dcl::{
5959
AlterRoleOperation, CreateRole, Grant, ResetConfig, Revoke, RoleOption, SecondaryRoles,

src/dialect/hive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ impl Dialect for HiveDialect {
7979
fn supports_from_first_insert(&self) -> bool {
8080
true
8181
}
82+
83+
/// See <https://hive.apache.org/docs/latest/language/languagemanual-types/>
84+
fn supports_map_literal_with_angle_brackets(&self) -> bool {
85+
true
86+
}
8287
}

src/parser/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12748,6 +12748,7 @@ impl<'a> Parser<'a> {
1274812748
Ok(DataType::Map(
1274912749
Box::new(key_data_type),
1275012750
Box::new(value_data_type),
12751+
MapBracketKind::AngleBrackets,
1275112752
))
1275212753
}
1275312754
Keyword::MAP if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {
@@ -12756,6 +12757,7 @@ impl<'a> Parser<'a> {
1275612757
Ok(DataType::Map(
1275712758
Box::new(key_data_type),
1275812759
Box::new(value_data_type),
12760+
MapBracketKind::Parentheses,
1275912761
))
1276012762
}
1276112763
Keyword::NESTED if dialect_is!(dialect is ClickHouseDialect | GenericDialect) => {

tests/sqlparser_clickhouse.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ fn parse_create_table_with_nested_data_types() {
767767
name: Ident::new("m"),
768768
data_type: DataType::Map(
769769
Box::new(DataType::String(None)),
770-
Box::new(DataType::UInt16)
770+
Box::new(DataType::UInt16),
771+
MapBracketKind::Parentheses
771772
),
772773
options: vec![],
773774
},

tests/sqlparser_hive.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@ fn test_tample_sample() {
562562
hive().verified_stmt("SELECT * FROM source TABLESAMPLE (10 ROWS)");
563563
}
564564

565+
#[test]
566+
fn parse_create_table_with_map_column_comment() {
567+
hive().verified_stmt(
568+
"CREATE TABLE target (kv_map MAP<STRING, STRING> COMMENT 'kv col comment') COMMENT 'this is table comment'",
569+
);
570+
}
571+
565572
fn hive() -> TestedDialects {
566573
TestedDialects::new(vec![Box::new(HiveDialect {})])
567574
}

0 commit comments

Comments
 (0)