Skip to content

Commit b24dbe5

Browse files
authored
Replace FromStr with normal parser function for FileFormat (#201)
The previous version accepted quoting file format keywords (`STORED AS "TEXTFILE"`) and was inconsistent with the way WindowFrameUnits was parsed.
1 parent 68afa2a commit b24dbe5

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

src/ast/mod.rs

-23
Original file line numberDiff line numberDiff line change
@@ -835,29 +835,6 @@ impl fmt::Display for FileFormat {
835835
}
836836
}
837837

838-
use crate::parser::ParserError;
839-
use std::str::FromStr;
840-
impl FromStr for FileFormat {
841-
type Err = ParserError;
842-
843-
fn from_str(s: &str) -> Result<Self, Self::Err> {
844-
use self::FileFormat::*;
845-
match s {
846-
"TEXTFILE" => Ok(TEXTFILE),
847-
"SEQUENCEFILE" => Ok(SEQUENCEFILE),
848-
"ORC" => Ok(ORC),
849-
"PARQUET" => Ok(PARQUET),
850-
"AVRO" => Ok(AVRO),
851-
"RCFILE" => Ok(RCFILE),
852-
"JSONFILE" => Ok(JSONFILE),
853-
_ => Err(ParserError::ParserError(format!(
854-
"Unexpected file format: {}",
855-
s
856-
))),
857-
}
858-
}
859-
}
860-
861838
/// A `LISTAGG` invocation `LISTAGG( [ DISTINCT ] <expr>[, <separator> ] [ON OVERFLOW <on_overflow>] ) )
862839
/// [ WITHIN GROUP (ORDER BY <within_group1>[, ...] ) ]`
863840
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

src/dialect/keywords.rs

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ define_keywords!(
8484
ATOMIC,
8585
AUTHORIZATION,
8686
AVG,
87+
AVRO,
8788
BEGIN,
8889
BEGIN_FRAME,
8990
BEGIN_PARTITION,
@@ -231,6 +232,7 @@ define_keywords!(
231232
IS,
232233
ISOLATION,
233234
JOIN,
235+
JSONFILE,
234236
KEY,
235237
LAG,
236238
LANGUAGE,
@@ -291,6 +293,7 @@ define_keywords!(
291293
ONLY,
292294
OPEN,
293295
OR,
296+
ORC,
294297
ORDER,
295298
OUT,
296299
OUTER,
@@ -318,6 +321,7 @@ define_keywords!(
318321
PROCEDURE,
319322
RANGE,
320323
RANK,
324+
RCFILE,
321325
READ,
322326
READS,
323327
REAL,
@@ -356,6 +360,7 @@ define_keywords!(
356360
SECOND,
357361
SELECT,
358362
SENSITIVE,
363+
SEQUENCEFILE,
359364
SERIALIZABLE,
360365
SESSION,
361366
SESSION_USER,
@@ -389,6 +394,7 @@ define_keywords!(
389394
TABLE,
390395
TABLESAMPLE,
391396
TEXT,
397+
TEXTFILE,
392398
THEN,
393399
TIES,
394400
TIME,

src/parser.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,7 @@ impl Parser {
10061006
let table_name = self.parse_object_name()?;
10071007
let (columns, constraints) = self.parse_columns()?;
10081008
self.expect_keywords(&[Keyword::STORED, Keyword::AS])?;
1009-
// We probably shouldn't parse the file format as an identifier..
1010-
let file_format = self
1011-
.parse_identifier()?
1012-
.value
1013-
.to_ascii_uppercase()
1014-
.parse::<FileFormat>()?;
1009+
let file_format = self.parse_file_format()?;
10151010

10161011
self.expect_keyword(Keyword::LOCATION)?;
10171012
let location = self.parse_literal_string()?;
@@ -1028,6 +1023,22 @@ impl Parser {
10281023
})
10291024
}
10301025

1026+
pub fn parse_file_format(&mut self) -> Result<FileFormat, ParserError> {
1027+
match self.next_token() {
1028+
Token::Word(w) => match w.keyword {
1029+
Keyword::AVRO => Ok(FileFormat::AVRO),
1030+
Keyword::JSONFILE => Ok(FileFormat::JSONFILE),
1031+
Keyword::ORC => Ok(FileFormat::ORC),
1032+
Keyword::PARQUET => Ok(FileFormat::PARQUET),
1033+
Keyword::RCFILE => Ok(FileFormat::RCFILE),
1034+
Keyword::SEQUENCEFILE => Ok(FileFormat::SEQUENCEFILE),
1035+
Keyword::TEXTFILE => Ok(FileFormat::TEXTFILE),
1036+
_ => self.expected("fileformat", Token::Word(w)),
1037+
},
1038+
unexpected => self.expected("fileformat", unexpected),
1039+
}
1040+
}
1041+
10311042
pub fn parse_create_view(&mut self) -> Result<Statement, ParserError> {
10321043
let materialized = self.parse_keyword(Keyword::MATERIALIZED);
10331044
self.expect_keyword(Keyword::VIEW)?;

0 commit comments

Comments
 (0)