nom-sql icon indicating copy to clipboard operation
nom-sql copied to clipboard

Select expressions must have at least one table

Open glittershark opened this issue 4 years ago • 5 comments
trafficstars

Without this, select * from; parses successfully, even though it shouldn't.

glittershark avatar Dec 01 '20 22:12 glittershark

SELECT 1; is valid SQL, should this only apply after a FROM token is seen? (or whatever the SQL grammar we’re using says)

trevyn avatar Dec 01 '20 23:12 trevyn

Ah, yes, definitely

glittershark avatar Dec 02 '20 02:12 glittershark

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

glittershark avatar Dec 02 '20 17:12 glittershark

cadac57 is the more minimal version of the change which leaves the AST untouched

glittershark avatar Dec 02 '20 17:12 glittershark

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?

ms705 avatar Dec 07 '20 23:12 ms705