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


##########
tests/sqlparser_postgres.rs:
##########
@@ -14530,3 +14530,66 @@ fn parse_alter_table_set_schema() {
         "sql parser error: Expected: identifier, found: EOF"
     );
 }
+
+#[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",
+    );
+    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",
+        "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);
+    }
+
+    let error = pg_and_generic()
+        .parse_sql_statements("CREATE STATISTICS s ON a, b")
+        .expect_err("expected missing FROM clause");
+    assert!(error.to_string().contains("Expected: FROM"), "{error}");
+
+    for invalid_sql in [
+        "CREATE STATISTICS s ON a + 1 FROM t",
+        "CREATE STATISTICS s ON a::INT FROM t",
+        "CREATE STATISTICS s ON row_number() OVER () FROM t",
+        "CREATE STATISTICS IF NOT EXISTS ON a FROM t",
+    ] {
+        pg_and_generic()

Review Comment:
   Actually assert against the expected error type so we avoid these strings 
failing on the wrong errors.



-- 
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