Skip to content

Commit 09513f3

Browse files
committed
implement FromStr for FileType
1 parent 002ca5d commit 09513f3

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

datafusion/src/sql/parser.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use sqlparser::{
2525
parser::{Parser, ParserError},
2626
tokenizer::{Token, Tokenizer},
2727
};
28+
use std::str::FromStr;
2829

2930
// Use `Parser::expected` instead, if possible
3031
macro_rules! parser_err {
@@ -44,6 +45,22 @@ pub enum FileType {
4445
CSV,
4546
}
4647

48+
impl FromStr for FileType {
49+
type Err = ParserError;
50+
51+
fn from_str(s: &str) -> Result<Self, Self::Err> {
52+
match s.to_uppercase().as_str() {
53+
"PARQUET" => Ok(Self::Parquet),
54+
"NDJSON" => Ok(Self::NdJson),
55+
"CSV" => Ok(Self::CSV),
56+
other => Err(ParserError::ParserError(format!(
57+
"expect one of PARQUET, NDJSON, or CSV, found: {}",
58+
other
59+
))),
60+
}
61+
}
62+
}
63+
4764
/// DataFusion extension DDL for `CREATE EXTERNAL TABLE`
4865
#[derive(Debug, Clone, PartialEq)]
4966
pub struct CreateExternalTable {
@@ -268,12 +285,7 @@ impl<'a> DFParser<'a> {
268285
/// Parses the set of valid formats
269286
fn parse_file_format(&mut self) -> Result<FileType, ParserError> {
270287
match self.parser.next_token() {
271-
Token::Word(w) => match &*w.value {
272-
"PARQUET" => Ok(FileType::Parquet),
273-
"NDJSON" => Ok(FileType::NdJson),
274-
"CSV" => Ok(FileType::CSV),
275-
_ => self.expected("one of PARQUET, NDJSON, or CSV", Token::Word(w)),
276-
},
288+
Token::Word(w) => w.value.parse(),
277289
unexpected => self.expected("one of PARQUET, NDJSON, or CSV", unexpected),
278290
}
279291
}
@@ -367,12 +379,23 @@ mod tests {
367379
});
368380
expect_parse_ok(sql, expected)?;
369381

382+
// positive case: it is ok for parquet files to be other than upper case
383+
let sql = "CREATE EXTERNAL TABLE t STORED AS parqueT LOCATION 'foo.parquet'";
384+
let expected = Statement::CreateExternalTable(CreateExternalTable {
385+
name: "t".into(),
386+
columns: vec![],
387+
file_type: FileType::Parquet,
388+
has_header: false,
389+
location: "foo.parquet".into(),
390+
});
391+
expect_parse_ok(sql, expected)?;
392+
370393
// Error cases: Invalid type
371394
let sql =
372395
"CREATE EXTERNAL TABLE t(c1 int) STORED AS UNKNOWN_TYPE LOCATION 'foo.csv'";
373396
expect_parse_error(
374397
sql,
375-
"Expected one of PARQUET, NDJSON, or CSV, found: UNKNOWN_TYPE",
398+
"expect one of PARQUET, NDJSON, or CSV",
376399
);
377400

378401
Ok(())

0 commit comments

Comments
 (0)