LucaCappelletti94 commented on code in PR #3040:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/3040#discussion_r4730538651


##########
src/parser/mod.rs:
##########
@@ -7556,6 +7566,60 @@ impl<'a> Parser<'a> {
         Ok(owner)
     }
 
+    fn parse_create_statistics(&mut self) -> Result<Statement, ParserError> {
+        let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, 
Keyword::EXISTS]);
+        let name = if self.peek_keyword(Keyword::ON) || 
self.peek_token_ref().token == Token::LParen
+        {
+            if if_not_exists {
+                return self.expected_ref("identifier", self.peek_token_ref());
+            }
+            None
+        } else {
+            Some(self.parse_object_name(false)?)
+        };
+        let kinds = if self.peek_token_ref().token == Token::LParen {
+            self.parse_parenthesized(|parser| {
+                parser.parse_comma_separated(|parser| 
parser.parse_identifier())
+            })?
+        } else {
+            Vec::new()
+        };
+        self.expect_keyword_is(Keyword::ON)?;
+        let expressions = self.parse_comma_separated(|parser| {
+            let location = parser.peek_token_ref().span.start;
+            let expr = parser.parse_expr()?;
+            match expr {
+                Expr::Identifier(_)
+                | Expr::Nested(_)
+                | Expr::Cast {
+                    kind: CastKind::Cast,
+                    ..
+                }
+                | Expr::Extract { .. }
+                | Expr::Substring { .. }
+                | Expr::Trim { .. }
+                | Expr::Position { .. }
+                | Expr::Overlay { .. }
+                | Expr::Ceil { .. }
+                | Expr::Floor { .. } => Ok(expr),
+                Expr::Function(ref fun) if fun.over.is_none() => Ok(expr),

Review Comment:
   You should reject the other aggregate and window clauses here too, since 
`func_expr_windowless` excludes `FILTER` and `WITHIN GROUP` as well as `OVER`. 
PostgreSQL 18.6 raises a syntax error on `CREATE STATISTICS s ON count(*) 
FILTER (WHERE a > 0), b FROM t` and on `CREATE STATISTICS s ON 
percentile_cont(0.5) WITHIN GROUP (ORDER BY a), b FROM t`, while this branch 
accepts and round-trips both.
   
   ```suggestion
                   Expr::Function(ref fun)
                       if fun.over.is_none()
                           && fun.filter.is_none()
                           && fun.within_group.is_empty()
                           && fun.null_treatment.is_none() =>
                   {
                       Ok(expr)
                   }
   ```



##########
tests/sqlparser_postgres.rs:
##########
@@ -14597,3 +14597,118 @@ fn parse_alter_column_set_storage() {
         );
     }
 }
+
+#[test]
+fn parse_create_statistics() {
+    let statement = pg_and_generic().verified_stmt(
+        "CREATE STATISTICS IF NOT EXISTS public.s (mcv, dependencies) ON a, (b 
+ 1) FROM public.t",
+    );
+    assert_eq!(statement.span(), Span::empty());
+    assert_display_propagates_write_errors(
+        &statement,
+        &["CREATE STATISTICS", " IF NOT EXISTS", "s", " (", " ON "],
+    );
+    let Statement::CreateStatistics(statistics) = statement else {
+        panic!("expected CREATE STATISTICS");
+    };
+    assert!(statistics.if_not_exists);
+    assert_eq!(
+        statistics.name.expect("statistics name").to_string(),
+        "public.s"
+    );
+    assert_eq!(statistics.from[0].relation.to_string(), "public.t");
+    assert_eq!(
+        statistics.kinds,
+        vec![Ident::new("mcv"), Ident::new("dependencies")]
+    );
+    assert!(matches!(
+        statistics.expressions.as_slice(),
+        [Expr::Identifier(_), Expr::Nested(_)]
+    ));
+
+    for sql in [
+        "CREATE STATISTICS s ON a, b FROM t",
+        r#"CREATE STATISTICS IF NOT EXISTS "on" ON a FROM t"#,
+        "CREATE STATISTICS ON (a + b) FROM t",
+        "CREATE STATISTICS ON lower(a) FROM t",
+        "CREATE STATISTICS (ndistinct) ON a, b FROM t",
+        "CREATE STATISTICS s ON CAST(a AS INT), EXTRACT(YEAR FROM b), 
SUBSTRING(c FROM 1 FOR 2), TRIM(d), POSITION('x' IN e), OVERLAY(f PLACING 'x' 
FROM 1), CEIL(g), FLOOR(h) FROM t",
+        "CREATE STATISTICS s ON a, b FROM t alias",
+        "CREATE STATISTICS s ON a, b FROM t AS alias",
+        "CREATE STATISTICS s ON a, b FROM t1, t2",
+        "CREATE STATISTICS s ON a, b FROM t1 JOIN t2 ON t1.id = t2.id",
+        "CREATE STATISTICS s ON a, b FROM (SELECT 1) AS sub",
+        "CREATE STATISTICS s ON a, b FROM generate_series(1, 10) AS s",
+    ] {
+        pg_and_generic().verified_stmt(sql);
+    }
+
+    for sql in [
+        "CREATE STATISTICS s ON a, b FROM ONLY t",
+        "CREATE STATISTICS s ON a, b FROM t *",
+    ] {
+        pg().verified_stmt(sql);
+    }
+
+    for (invalid_sql, expected_error) in [
+        ("CREATE STATISTICS s ON a, b", "Expected: FROM, found: EOF"),
+        (
+            "CREATE STATISTICS s ON a + 1 FROM t",
+            "Expected a column, function call, or parenthesized expression",
+        ),
+        (
+            "CREATE STATISTICS s ON a::INT FROM t",
+            "Expected a column, function call, or parenthesized expression",
+        ),
+        (
+            "CREATE STATISTICS s ON row_number() OVER () FROM t",
+            "Expected a column, function call, or parenthesized expression",
+        ),

Review Comment:
   You could pin both clauses next to the `OVER` case, so that a guard which 
only checks `over` fails this test.
   
   ```suggestion
           ),
           (
               "CREATE STATISTICS s ON count(*) FILTER (WHERE a > 0) FROM t",
               "Expected a column, function call, or parenthesized expression",
           ),
           (
               "CREATE STATISTICS s ON percentile_cont(0.5) WITHIN GROUP (ORDER 
BY a) FROM t",
               "Expected a column, function call, or parenthesized expression",
           ),
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to