Skip to content

Commit 902f6c2

Browse files
committed
sql_parser: select parser tests refactoring (8)
1 parent c175f1a commit 902f6c2

File tree

7 files changed

+102
-124
lines changed

7 files changed

+102
-124
lines changed

src/ast/alter.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ pub enum AlterTableStatementType {
3030
#[derive(Debug, PartialEq, Clone)]
3131
pub struct ColumnDefinition {
3232
pub column_name: Identifier,
33+
3334
pub column_type: Option<DataType>,
35+
3436
pub column_constraints: Vec<ColumnConstraint>,
3537
}
3638

src/ast/create.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct CreateVirtualTableStatement {
1616

1717
pub module_name: Identifier,
1818

19-
pub module_arguments: Vec<Expression>,
19+
pub module_arguments: Vec<ColumnDefinition>,
2020
}
2121

2222
/// An AST for [CREATE INDEX](https://www.sqlite.org/lang_createindex.html) SQL statement.

src/parser/create/create_table.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ mod create_table_tests {
197197
expression::test_utils::identifier_expr, parser::test_utils::run_sunny_day_test,
198198
ColumnConstraint, ColumnConstraintType, ColumnDefinition, ConflictClause,
199199
CreateTableColumnDef, CreateTableOption, DataType, Identifier, IndexedColumn, Ordering,
200-
Statement, TableConstraint, TableConstraintType, TableOption,
200+
PrimaryKeyConstraint, Statement, TableConstraint, TableConstraintType, TableOption,
201201
};
202202

203203
use super::test_utils::create_table_statement;
@@ -308,6 +308,53 @@ mod create_table_tests {
308308
);
309309
}
310310

311+
#[test]
312+
fn create_table_with_primary_column() {
313+
let mut expected_stmt = create_table_statement();
314+
315+
expected_stmt.create_table_option =
316+
CreateTableOption::ColumnDefinitions(CreateTableColumnDef {
317+
columns: vec![
318+
ColumnDefinition {
319+
column_name: Identifier::from("id"),
320+
column_type: Some(DataType::PlainDataType("int".into())),
321+
column_constraints: vec![ColumnConstraint {
322+
constraint_type: ColumnConstraintType::PrimaryKey(
323+
PrimaryKeyConstraint {
324+
ordering: None,
325+
conflict_clause: ConflictClause::None,
326+
auto_increment: false,
327+
},
328+
),
329+
name: None,
330+
}],
331+
},
332+
ColumnDefinition {
333+
column_name: Identifier::from("name"),
334+
column_type: Some(DataType::SizedDataType("varchar".into(), "50".into())),
335+
column_constraints: vec![],
336+
},
337+
ColumnDefinition {
338+
column_name: Identifier::from("category"),
339+
column_type: Some(DataType::SizedDataType("varchar".into(), "15".into())),
340+
column_constraints: vec![],
341+
},
342+
ColumnDefinition {
343+
column_name: Identifier::from("cost"),
344+
column_type: Some(DataType::PlainDataType("int".into())),
345+
column_constraints: vec![],
346+
},
347+
],
348+
table_constraints: vec![],
349+
table_options: vec![],
350+
});
351+
352+
run_sunny_day_test(
353+
"CREATE TABLE table_name (id int PRIMARY KEY, name varchar(50), category varchar(15), cost int)",
354+
Statement::CreateTable(expected_stmt.clone()),
355+
);
356+
}
357+
311358
#[test]
312359
fn create_table_with_table_options() {
313360
let table_options = vec![TableOption::WithoutRowId, TableOption::Strict];

src/parser/create/create_virtual_table.rs

+48-19
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
use super::create_table::CreateTableStatementParser;
12
use super::CreateStatementParser;
2-
use crate::expression::ExpressionParser;
33
use crate::parser::errors::ParsingError;
44
use crate::{
5-
CreateVirtualTableStatement, Expression, IdentifierParser, Keyword, Parser, TokenType,
5+
ColumnDefinition, CreateVirtualTableStatement, IdentifierParser, Keyword, Parser, TokenType,
66
};
77

88
pub trait CreateVirtualTableStatementParser {
99
fn parse_create_virtual_table_statement(
1010
&mut self,
1111
) -> Result<CreateVirtualTableStatement, ParsingError>;
1212

13-
fn parse_module_arguments(&mut self) -> Result<Vec<Expression>, ParsingError>;
13+
fn parse_module_arguments(&mut self) -> Result<Vec<ColumnDefinition>, ParsingError>;
1414
}
1515

1616
impl<'a> CreateVirtualTableStatementParser for Parser<'a> {
@@ -26,6 +26,7 @@ impl<'a> CreateVirtualTableStatementParser for Parser<'a> {
2626
self.consume_as_keyword(Keyword::Using)?;
2727

2828
let module_name = self.parse_identifier()?;
29+
2930
let module_arguments = self.parse_module_arguments()?;
3031

3132
Ok(CreateVirtualTableStatement {
@@ -36,22 +37,16 @@ impl<'a> CreateVirtualTableStatementParser for Parser<'a> {
3637
})
3738
}
3839

39-
fn parse_module_arguments(&mut self) -> Result<Vec<Expression>, ParsingError> {
40-
let mut arguments = vec![];
41-
40+
fn parse_module_arguments(&mut self) -> Result<Vec<ColumnDefinition>, ParsingError> {
4241
if self.consume_as(TokenType::LeftParen).is_ok() {
43-
loop {
44-
arguments.push(self.parse_expression()?);
45-
46-
if self.consume_as(TokenType::Comma).is_err() {
47-
break;
48-
}
49-
}
42+
let args = self.parse_column_definitions()?;
5043

5144
self.consume_as(TokenType::RightParen)?;
52-
}
5345

54-
Ok(arguments)
46+
Ok(args)
47+
} else {
48+
Ok(vec![])
49+
}
5550
}
5651
}
5752

@@ -74,8 +69,8 @@ mod create_virtual_table_tests {
7469
use test_utils::create_virtual_table_statement;
7570

7671
use crate::{
77-
expression::test_utils::string_expr, parser::test_utils::run_sunny_day_test, Identifier,
78-
Statement,
72+
parser::test_utils::run_sunny_day_test, ColumnConstraint, ColumnConstraintType,
73+
ConflictClause, DataType, Identifier, PrimaryKeyConstraint, Statement,
7974
};
8075

8176
use super::*;
@@ -114,10 +109,44 @@ mod create_virtual_table_tests {
114109
#[test]
115110
fn create_virtual_table_with_module_arguments() {
116111
let mut stmt = create_virtual_table_statement();
117-
stmt.module_arguments = vec![string_expr("'arg1'"), string_expr("'arg2'")];
112+
stmt.module_arguments = vec![
113+
ColumnDefinition {
114+
column_name: Identifier::from("id"),
115+
column_type: Some(DataType::PlainDataType("int".into())),
116+
column_constraints: vec![ColumnConstraint {
117+
name: None,
118+
constraint_type: ColumnConstraintType::PrimaryKey(PrimaryKeyConstraint {
119+
auto_increment: false,
120+
conflict_clause: ConflictClause::None,
121+
ordering: None,
122+
}),
123+
}],
124+
},
125+
ColumnDefinition {
126+
column_name: Identifier::from("name"),
127+
column_type: Some(DataType::SizedDataType("varchar".into(), "50".into())),
128+
column_constraints: vec![],
129+
},
130+
ColumnDefinition {
131+
column_name: Identifier::from("category"),
132+
column_type: Some(DataType::SizedDataType("varchar".into(), "15".into())),
133+
column_constraints: vec![],
134+
},
135+
ColumnDefinition {
136+
column_name: Identifier::from("cost"),
137+
column_type: Some(DataType::PlainDataType("int".into())),
138+
column_constraints: vec![],
139+
},
140+
];
118141

119142
run_sunny_day_test(
120-
"CREATE VIRTUAL TABLE test_table USING test_module('arg1', 'arg2')",
143+
"CREATE VIRTUAL TABLE test_table
144+
USING test_module(
145+
id int PRIMARY KEY,
146+
name varchar(50),
147+
category varchar(15),
148+
cost int
149+
)",
121150
Statement::CreateVirtualTable(stmt),
122151
);
123152
}

tests/bart-riers-sqlite-dataset/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The test dataset is taken from the [Bart Kiers' SQLite ANTLR parser](https://github.com/bkiers/sqlite-parser/tree/master).
44

5-
In the current implementation, there is 64.27% of the tests that are passing.
5+
In the current implementation, there is 64.26% of the tests that are passing.
66
By passing tests, I mean that the parser successfully parses the SQL query and returns a valid `Expression` AST.
77
The structure of the parsed AST is not checked, as there are no expected ASTs list
88

tests/codeschool-sqlite-parser-test-files/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
The queries under `sql/` folder are taken from the [Codeschool SQLite Parser](https://github.com/codeschool/sqlite-parser/tree/master) repository.
44

55

6-
Current test pass rate is 90.85%
6+
Current test pass rate is 91.24%

tests/codeschool-sqlite-parser-test-files/main.rs

+1-101
Original file line numberDiff line numberDiff line change
@@ -54,108 +54,8 @@ fn run_test(sql_content: &str) -> Result<Statement, ParsingError> {
5454

5555
#[test]
5656
pub fn test() {
57-
/*
58-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-20.sql"
59-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/vtab1-2.sql"
60-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts4aa-1.sql"
61-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/rowid-2.sql"
62-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-21.sql"
63-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-23.sql"
64-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-22.sql"
65-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-26.sql"
66-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-9.sql"
67-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-8.sql"
68-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts1d-1.sql"
69-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-25.sql"
70-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-8.sql"
71-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-8.sql"
72-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-9.sql"
73-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-9.sql"
74-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-18.sql"
75-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/triggerD-1.sql"
76-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-24.sql"
77-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts1c-1.sql"
78-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/pager1-1.sql"
79-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/triggerC-1.sql"
80-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-19.sql"
81-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/alter3-1.sql"
82-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/pager1-3.sql"
83-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-18.sql"
84-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/schema5-1.sql"
85-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/vacuum4-1.sql"
86-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/avtrans-1.sql"
87-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/pagerfault-1.sql"
88-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/jrnlmode-1.sql"
89-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-16.sql"
90-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-13.sql"
91-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts3ac-1.sql"
92-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/tkt-b1d3a2e531-1.sql"
93-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/with3-1.sql"
94-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/shared9-1.sql"
95-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-15.sql"
96-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-10.sql"
97-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-11.sql"
98-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts2d-1.sql"
99-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-14.sql"
100-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-10.sql"
101-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-15.sql"
102-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/selectA-2.sql"
103-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/selectA-3.sql"
104-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-14.sql"
105-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-11.sql"
106-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-13.sql"
107-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-16.sql"
108-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fkey3-1.sql"
109-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/e_fkey-1.sql"
110-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-17.sql"
111-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-12.sql"
112-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-15.sql"
113-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-4.sql"
114-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-4.sql"
115-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-6.sql"
116-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-7.sql"
117-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-5.sql"
118-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fkey4-1.sql"
119-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-5.sql"
120-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-14.sql"
121-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-16.sql"
122-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-7.sql"
123-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts1porter-1.sql"
124-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-7.sql"
125-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts-9fd058691-1.sql"
126-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-5.sql"
127-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-4.sql"
128-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/trigger5-1.sql"
129-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-6.sql"
130-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-6.sql"
131-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-17.sql"
132-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-13.sql"
133-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-2.sql"
134-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/without_rowid3-1.sql"
135-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-2.sql"
136-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/tkt2285-1.sql"
137-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary3-3.sql"
138-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fkey2-1.sql"
139-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-3.sql"
140-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts2c-1.sql"
141-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-12.sql"
142-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-10.sql"
143-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-3.sql"
144-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/orderby7-1.sql"
145-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/boundary1-2.sql"
146-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/fts3ad-1.sql"
147-
"tests/codeschool-sqlite-parser-test-files/sql/official-suite/randexpr1-11.sql"
148-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-unary-1.sql"
149-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-grouping-9.sql"
150-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-grouping-8.sql"
151-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-parenthesis-2.sql"
152-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-grouping-6.sql"
153-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-grouping-7.sql"
154-
"tests/codeschool-sqlite-parser-test-files/sql/create-virtual-table/create-virtual-table-alt-syntax.sql"
155-
*/
156-
15757
let sql_content = fs::read_to_string(
158-
"tests/codeschool-sqlite-parser-test-files/sql/expressions/expression-unary-1.sql",
58+
"tests/codeschool-sqlite-parser-test-files/sql/create-virtual-table/create-virtual-table-alt-syntax.sql",
15959
)
16060
.unwrap();
16161
let result = run_test(&sql_content);

0 commit comments

Comments
 (0)