sqlparser/dialect/hive.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::dialect::Dialect;
19use crate::keywords::{Keyword, RESERVED_FOR_TABLE_FACTOR};
20use crate::parser::Parser;
21
22/// A [`Dialect`] for [Hive](https://hive.apache.org/).
23#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct HiveDialect {}
26
27impl Dialect for HiveDialect {
28 fn is_delimited_identifier_start(&self, ch: char) -> bool {
29 (ch == '"') || (ch == '`')
30 }
31
32 fn supports_hash_comment(&self) -> bool {
33 true
34 }
35
36 /// The alias is an `identifier`, see `selectItem` in
37 /// <https://github.com/apache/hive/blob/master/parser/src/java/org/apache/hadoop/hive/ql/parse/SelectClauseParser.g>
38 fn supports_string_literal_column_alias(&self, _explicit: bool) -> bool {
39 false
40 }
41
42 /// The alias is an `identifier`, see `tableSource` in
43 /// <https://github.com/apache/hive/blob/master/parser/src/java/org/apache/hadoop/hive/ql/parse/FromClauseParser.g>
44 fn supports_string_literal_table_alias(&self, _explicit: bool) -> bool {
45 false
46 }
47
48 /// See <https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-RulesforColumnNames>
49 fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
50 Some('`')
51 }
52
53 fn is_identifier_start(&self, ch: char) -> bool {
54 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '$'
55 }
56
57 fn is_identifier_part(&self, ch: char) -> bool {
58 ch.is_ascii_lowercase()
59 || ch.is_ascii_uppercase()
60 || ch.is_ascii_digit()
61 || ch == '_'
62 || ch == '$'
63 || ch == '{'
64 || ch == '}'
65 }
66
67 /// LIMIT is not reserved in Hive; identifiers may be nonReserved keywords.
68 /// See <https://github.com/apache/hive/blob/4e9caba7c02a947a39c60de427acb7189c8c1fe8/parser/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g#L945>
69 fn is_table_factor(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
70 kw == &Keyword::LIMIT || !RESERVED_FOR_TABLE_FACTOR.contains(kw)
71 }
72
73 fn supports_filter_during_aggregation(&self) -> bool {
74 true
75 }
76
77 fn supports_numeric_prefix(&self) -> bool {
78 true
79 }
80
81 fn require_interval_qualifier(&self) -> bool {
82 true
83 }
84
85 /// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362061#Tutorial-BuiltInOperators>
86 fn supports_bang_not_operator(&self) -> bool {
87 true
88 }
89
90 /// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362036#LanguageManualDML-Loadingfilesintotables>
91 fn supports_load_data(&self) -> bool {
92 true
93 }
94
95 /// See <https://cwiki.apache.org/confluence/display/hive/languagemanual+sampling>
96 fn supports_table_sample_before_alias(&self) -> bool {
97 true
98 }
99
100 /// See <https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=30151323#EnhancedAggregation,Cube,GroupingandRollup-CubesandRollupsr>
101 fn supports_group_by_with_modifier(&self) -> bool {
102 true
103 }
104
105 // TODO: The parsing of the FROM keyword seems wrong, as it happens within the CTE.
106 // See https://github.com/apache/datafusion-sqlparser-rs/issues/2236 for more details.
107 /// See <https://hive.apache.org/docs/latest/language/common-table-expression/>
108 fn supports_from_first_insert(&self) -> bool {
109 true
110 }
111
112 /// See <https://hive.apache.org/docs/latest/language/languagemanual-types/>
113 fn supports_map_literal_with_angle_brackets(&self) -> bool {
114 true
115 }
116}