nom-sql
nom-sql copied to clipboard
Select expressions must have at least one table
Without this, select * from; parses successfully, even though it
shouldn't.
SELECT 1; is valid SQL, should this only apply after a FROM token is seen? (or whatever the SQL grammar we’re using says)
Ah, yes, definitely
the ANSI sql standard grammar has <table-expression>, containing the from, where, group by, having, and window clauses, as its own node, and then makes that entire node optional in the select_statement. Something like this:
diff --git a/src/select.rs b/src/select.rs
index 0740838..0f4f9b0 100644
--- a/src/select.rs
+++ b/src/select.rs
@@ -77,10 +77,8 @@ impl fmt::Display for LimitClause {
}
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
-pub struct SelectStatement {
+pub struct TableExpression {
pub tables: Vec<Table>,
- pub distinct: bool,
- pub fields: Vec<FieldDefinitionExpression>,
pub join: Vec<JoinClause>,
pub where_clause: Option<ConditionExpression>,
pub group_by: Option<GroupByClause>,
@@ -88,6 +86,13 @@ pub struct SelectStatement {
pub limit: Option<LimitClause>,
}
+#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
+pub struct SelectStatement {
+ pub distinct: bool,
+ pub fields: Vec<FieldDefinitionExpression>,
+ pub tables: Option<TableExpression>,
+}
+
impl fmt::Display for SelectStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SELECT ")?;
feels like the right way to go here. I got about halfway through everything necessary to make that compile but figured I'd ask here before going any further since it's a pretty big breaking change. The other option would be to keep the AST as-is and only change the parser
cadac57 is the more minimal version of the change which leaves the AST untouched
Thank you! :+1: This looks good. I actually agree with @glittershark's more extensive proposed change above. We should make that change, so that SELECT queries without tables are more natural. Either here or in a new PR is fine by me -- @glittershark?