iffyio commented on code in PR #1580:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/1580#discussion_r1874713209


##########
src/parser/mod.rs:
##########
@@ -10584,6 +10602,114 @@ impl<'a> Parser<'a> {
         }
     }
 
+    fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSample>>, 
ParserError> {
+        if self
+            .parse_one_of_keywords(&[Keyword::SAMPLE, Keyword::TABLESAMPLE])
+            .is_none()
+        {
+            return Ok(None);
+        }
+
+        let sample = if self
+            .parse_one_of_keywords(&[Keyword::BERNOULLI, Keyword::ROW])
+            .is_some()
+        {
+            self.expect_token(&Token::LParen)?;
+            let expr = self.parse_expr()?;
+
+            let (probability, value, unit) = if 
self.parse_keyword(Keyword::ROWS) {
+                (None, Some(expr), Some(TableSampleUnit::Rows))
+            } else if self.parse_keyword(Keyword::PERCENT) {
+                (None, Some(expr), Some(TableSampleUnit::Percent))
+            } else {
+                (Some(expr), None, None)
+            };
+            self.expect_token(&Token::RParen)?;
+            TableSample::Bernoulli(TableSampleBernoulli {
+                probability,
+                value,
+                unit,
+            })
+        } else if self
+            .parse_one_of_keywords(&[Keyword::SYSTEM, Keyword::BLOCK])
+            .is_some()
+        {
+            self.expect_token(&Token::LParen)?;
+            let probability = self.parse_expr()?;
+            self.expect_token(&Token::RParen)?;
+            let seed = if self
+                .parse_one_of_keywords(&[Keyword::REPEATABLE, Keyword::SEED])
+                .is_some()
+            {
+                self.expect_token(&Token::LParen)?;
+                let seed = self.parse_expr()?;
+                self.expect_token(&Token::RParen)?;
+                Some(seed)
+            } else {
+                None
+            };
+            TableSample::System(TableSampleSystem {
+                probability,
+                repeatable: seed,
+            })
+        } else if self.peek_token().token == Token::LParen {
+            self.expect_token(&Token::LParen)?;
+            if self.parse_keyword(Keyword::BUCKET) {
+                let bucket = self.parse_number_value()?;
+                self.expect_keywords(&[Keyword::OUT, Keyword::OF])?;
+                let total = self.parse_number_value()?;
+                let on = if self.parse_keyword(Keyword::ON) {
+                    Some(self.parse_expr()?)
+                } else {
+                    None
+                };
+                self.expect_token(&Token::RParen)?;
+                TableSample::Bucket(TableSampleBucket { bucket, total, on })
+            } else {
+                let value = match self.try_parse(|p| p.parse_number_value()) {

Review Comment:
   Should the try_parse rather be maybe_parse (we seem to be ignoring the 
returned error otherwise)?



##########
src/ast/query.rs:
##########
@@ -1002,6 +1002,12 @@ pub enum TableFactor {
         partitions: Vec<Ident>,
         /// Optional PartiQL JsonPath: <https://partiql.org/dql/from.html>
         json_path: Option<JsonPath>,
+        /// Optional table sample modifier
+        /// See: 
<https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
+        sample: Option<Box<TableSample>>,
+        /// Position of the table sample modifier in the table factor. Default 
is after the table alias
+        /// e.g. `SELECT * FROM tbl t TABLESAMPLE (10 ROWS)`. See 
`Dialect::supports_table_sample_before_alias`.
+        sample_before_alias: bool,

Review Comment:
   Would it make sense to use an enum here? e.g
   ```rust
   enum TableSampleKind {
       BeforeTableAlias(Box<TableSample>)
       // ...
   }
   sample: Option<TableSampleKind>
   ```
   thinking that avoids the surplus flag on the table factor and would lend 
itself to be extensible if required later on



##########
src/dialect/mod.rs:
##########
@@ -707,6 +707,19 @@ pub trait Dialect: Debug + Any {
     fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
         keywords::RESERVED_FOR_IDENTIFIER.contains(&kw)
     }
+
+    /// Returns true if this dialect supports the `TABLESAMPLE` option
+    /// before the table alias option.
+    /// 
<https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#_7_6_table_reference>

Review Comment:
   For this and the `supports_implicit_table_sample` method can we include an 
example statement to illustrate the dialect method? thinking that could be 
useful here to let the reader avoid having to infer from the spec if they don't 
have to



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