Skip to content

Commit d8f824c

Browse files
committed
merge CreateExternalTable & CreateTable.
1 parent 3555659 commit d8f824c

File tree

4 files changed

+59
-78
lines changed

4 files changed

+59
-78
lines changed

src/sqlast/mod.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,9 @@ pub enum SQLStatement {
249249
name: SQLObjectName,
250250
/// Optional schema
251251
columns: Vec<SQLColumnDef>,
252-
},
253-
/// CREATE EXTERNAL TABLE
254-
SQLCreateExternalTable {
255-
/// Table name
256-
name: SQLObjectName,
257-
/// Optional schema
258-
columns: Vec<SQLColumnDef>,
259-
file_format: FileFormat,
260-
location: String,
252+
external: bool,
253+
file_format: Option<FileFormat>,
254+
location: Option<String>,
261255
},
262256
/// ALTER TABLE
263257
SQLAlterTable {
@@ -370,30 +364,37 @@ impl ToString for SQLStatement {
370364
query.to_string()
371365
)
372366
}
373-
SQLStatement::SQLCreateTable { name, columns } => format!(
374-
"CREATE TABLE {} ({})",
367+
SQLStatement::SQLCreateTable {
368+
name,
369+
columns,
370+
external,
371+
file_format,
372+
location,
373+
} if *external => format!(
374+
"CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'",
375375
name.to_string(),
376376
columns
377377
.iter()
378378
.map(|c| c.to_string())
379379
.collect::<Vec<String>>()
380-
.join(", ")
380+
.join(", "),
381+
file_format.as_ref().map(|f| f.to_string()).unwrap(),
382+
location.as_ref().unwrap()
381383
),
382-
SQLStatement::SQLCreateExternalTable {
384+
SQLStatement::SQLCreateTable {
383385
name,
384386
columns,
385-
file_format,
386-
location,
387+
external: _,
388+
file_format: _,
389+
location: _,
387390
} => format!(
388-
"CREATE EXTERNAL TABLE {} ({}) STORED AS {} LOCATION '{}'",
391+
"CREATE TABLE {} ({})",
389392
name.to_string(),
390393
columns
391394
.iter()
392395
.map(|c| c.to_string())
393396
.collect::<Vec<String>>()
394-
.join(", "),
395-
file_format.to_string(),
396-
location
397+
.join(", ")
397398
),
398399
SQLStatement::SQLAlterTable { name, operation } => {
399400
format!("ALTER TABLE {} {}", name.to_string(), operation.to_string())

src/sqlparser.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,12 @@ impl Parser {
641641
self.expect_keyword("LOCATION")?;
642642
let location = self.parse_literal_string()?;
643643

644-
Ok(SQLStatement::SQLCreateExternalTable {
644+
Ok(SQLStatement::SQLCreateTable {
645645
name: table_name,
646646
columns,
647-
file_format,
648-
location,
647+
external: true,
648+
file_format: Some(file_format),
649+
location: Some(location),
649650
})
650651
}
651652

@@ -676,6 +677,9 @@ impl Parser {
676677
Ok(SQLStatement::SQLCreateTable {
677678
name: table_name,
678679
columns,
680+
external: false,
681+
file_format: None,
682+
location: None,
679683
})
680684
}
681685

tests/sqlparser_generic.rs

+11-53
Original file line numberDiff line numberDiff line change
@@ -434,52 +434,12 @@ fn parse_create_table() {
434434
lng double)",
435435
);
436436
match ast {
437-
SQLStatement::SQLCreateTable { name, columns } => {
438-
assert_eq!("uk_cities", name.to_string());
439-
assert_eq!(3, columns.len());
440-
441-
let c_name = &columns[0];
442-
assert_eq!("name", c_name.name);
443-
assert_eq!(SQLType::Varchar(Some(100)), c_name.data_type);
444-
assert_eq!(false, c_name.allow_null);
445-
446-
let c_lat = &columns[1];
447-
assert_eq!("lat", c_lat.name);
448-
assert_eq!(SQLType::Double, c_lat.data_type);
449-
assert_eq!(true, c_lat.allow_null);
450-
451-
let c_lng = &columns[2];
452-
assert_eq!("lng", c_lng.name);
453-
assert_eq!(SQLType::Double, c_lng.data_type);
454-
assert_eq!(true, c_lng.allow_null);
455-
}
456-
_ => assert!(false),
457-
}
458-
}
459-
460-
#[test]
461-
fn parse_create_external_table() {
462-
let sql = String::from(
463-
"CREATE EXTERNAL TABLE uk_cities (\
464-
name VARCHAR(100) NOT NULL,\
465-
lat DOUBLE NULL,\
466-
lng DOUBLE NULL)\
467-
STORED AS TEXTFILE LOCATION '/tmp/example.csv",
468-
);
469-
let ast = one_statement_parses_to(
470-
&sql,
471-
"CREATE EXTERNAL TABLE uk_cities (\
472-
name character varying(100) NOT NULL, \
473-
lat double, \
474-
lng double) \
475-
STORED AS TEXTFILE LOCATION '/tmp/example.csv'",
476-
);
477-
match ast {
478-
SQLStatement::SQLCreateExternalTable {
437+
SQLStatement::SQLCreateTable {
479438
name,
480439
columns,
481-
file_format,
482-
location,
440+
external: _,
441+
file_format: _,
442+
location: _,
483443
} => {
484444
assert_eq!("uk_cities", name.to_string());
485445
assert_eq!(3, columns.len());
@@ -498,23 +458,19 @@ fn parse_create_external_table() {
498458
assert_eq!("lng", c_lng.name);
499459
assert_eq!(SQLType::Double, c_lng.data_type);
500460
assert_eq!(true, c_lng.allow_null);
501-
502-
assert_eq!(FileFormat::TEXTFILE, file_format);
503-
assert_eq!("/tmp/example.csv", location);
504461
}
505462
_ => assert!(false),
506463
}
507464
}
508465

509466
#[test]
510-
fn parse_create_external_table_newline() {
467+
fn parse_create_external_table() {
511468
let sql = String::from(
512469
"CREATE EXTERNAL TABLE uk_cities (\
513470
name VARCHAR(100) NOT NULL,\
514471
lat DOUBLE NULL,\
515472
lng DOUBLE NULL)\
516-
STORED AS TEXTFILE
517-
LOCATION '/tmp/example.csv",
473+
STORED AS TEXTFILE LOCATION '/tmp/example.csv",
518474
);
519475
let ast = one_statement_parses_to(
520476
&sql,
@@ -525,9 +481,10 @@ fn parse_create_external_table_newline() {
525481
STORED AS TEXTFILE LOCATION '/tmp/example.csv'",
526482
);
527483
match ast {
528-
SQLStatement::SQLCreateExternalTable {
484+
SQLStatement::SQLCreateTable {
529485
name,
530486
columns,
487+
external,
531488
file_format,
532489
location,
533490
} => {
@@ -549,8 +506,9 @@ fn parse_create_external_table_newline() {
549506
assert_eq!(SQLType::Double, c_lng.data_type);
550507
assert_eq!(true, c_lng.allow_null);
551508

552-
assert_eq!(FileFormat::TEXTFILE, file_format);
553-
assert_eq!("/tmp/example.csv", location);
509+
assert!(external);
510+
assert_eq!(FileFormat::TEXTFILE, file_format.unwrap());
511+
assert_eq!("/tmp/example.csv", location.unwrap());
554512
}
555513
_ => assert!(false),
556514
}

tests/sqlparser_postgres.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,13 @@ fn parse_create_table_with_defaults() {
163163
active integer NOT NULL)",
164164
);
165165
match one_statement_parses_to(&sql, "") {
166-
SQLStatement::SQLCreateTable { name, columns } => {
166+
SQLStatement::SQLCreateTable {
167+
name,
168+
columns,
169+
external: _,
170+
file_format: _,
171+
location: _,
172+
} => {
167173
assert_eq!("public.customer", name.to_string());
168174
assert_eq!(10, columns.len());
169175

@@ -204,7 +210,13 @@ fn parse_create_table_from_pg_dump() {
204210
active integer
205211
)");
206212
match one_statement_parses_to(&sql, "") {
207-
SQLStatement::SQLCreateTable { name, columns } => {
213+
SQLStatement::SQLCreateTable {
214+
name,
215+
columns,
216+
external: _,
217+
file_format: _,
218+
location: _,
219+
} => {
208220
assert_eq!("public.customer", name.to_string());
209221

210222
let c_customer_id = &columns[0];
@@ -261,7 +273,13 @@ fn parse_create_table_with_inherit() {
261273
)",
262274
);
263275
match verified_stmt(&sql) {
264-
SQLStatement::SQLCreateTable { name, columns } => {
276+
SQLStatement::SQLCreateTable {
277+
name,
278+
columns,
279+
external: _,
280+
file_format: _,
281+
location: _,
282+
} => {
265283
assert_eq!("bazaar.settings", name.to_string());
266284

267285
let c_name = &columns[0];

0 commit comments

Comments
 (0)