Skip to content

Commit 4409cd8

Browse files
committed
Revert "support for SELECT TOP on snowflake (#14)"
This reverts commit 3286219.
1 parent 3286219 commit 4409cd8

5 files changed

Lines changed: 10 additions & 51 deletions

File tree

src/ast/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ pub use self::ddl::{
3030
pub use self::operator::{BinaryOperator, UnaryOperator};
3131
pub use self::query::{
3232
Cte, Fetch, Join, JoinConstraint, JoinOperator, Offset, OffsetRows, OrderByExpr, Query, Select,
33-
SelectItem, SetExpr, SetOperator, TableAlias, TableFactor, TableWithJoins, Top, TopQuantity,
34-
Values, With,
33+
SelectItem, SetExpr, SetOperator, TableAlias, TableFactor, TableWithJoins, Top, Values, With,
3534
};
3635
pub use self::value::{DateTimeField, Value};
3736

src/ast/query.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -654,32 +654,17 @@ impl fmt::Display for Fetch {
654654
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
655655
pub struct Top {
656656
/// SQL semantic equivalent of LIMIT but with same structure as FETCH.
657-
/// MSSQL only.
658657
pub with_ties: bool,
659-
/// MSSQL only.
660658
pub percent: bool,
661-
pub quantity: Option<TopQuantity>,
662-
}
663-
664-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
665-
pub enum TopQuantity {
666-
// A parenthesized expression. MSSQL only.
667-
Expr(Expr),
668-
// An unparenthesized integer constant.
669-
Constant(u64),
659+
pub quantity: Option<Expr>,
670660
}
671661

672662
impl fmt::Display for Top {
673663
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
674664
let extension = if self.with_ties { " WITH TIES" } else { "" };
675665
if let Some(ref quantity) = self.quantity {
676666
let percent = if self.percent { " PERCENT" } else { "" };
677-
match quantity {
678-
TopQuantity::Expr(quantity) => write!(f, "TOP ({quantity}){percent}{extension}"),
679-
TopQuantity::Constant(quantity) => {
680-
write!(f, "TOP {quantity}{percent}{extension}")
681-
}
682-
}
667+
write!(f, "TOP ({}){}{}", quantity, percent, extension)
683668
} else {
684669
write!(f, "TOP{}", extension)
685670
}

src/parser.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,14 +2983,9 @@ impl<'a> Parser<'a> {
29832983
let quantity = if self.consume_token(&Token::LParen) {
29842984
let quantity = self.parse_expr()?;
29852985
self.expect_token(&Token::RParen)?;
2986-
Some(TopQuantity::Expr(quantity))
2986+
Some(quantity)
29872987
} else {
2988-
let next_token = self.next_token();
2989-
let quantity = match next_token {
2990-
Token::Number(s) => s.parse::<u64>().expect("literal int"),
2991-
_ => self.expected("literal int", next_token)?,
2992-
};
2993-
Some(TopQuantity::Constant(quantity))
2988+
Some(self.parse_number_value_or_ident()?)
29942989
};
29952990

29962991
let percent = self.parse_keyword(Keyword::PERCENT);

tests/sqlparser_mssql.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ fn parse_mssql_top_paren() {
7676
let sql = "SELECT TOP (5) * FROM foo";
7777
let select = ms_and_generic().verified_only_select(sql);
7878
let top = select.top.unwrap();
79-
assert_eq!(
80-
Some(TopQuantity::Expr(Expr::Value(number("5")))),
81-
top.quantity
82-
);
79+
assert_eq!(Some(Expr::Value(number("5"))), top.quantity);
8380
assert!(!top.percent);
8481
}
8582

@@ -88,10 +85,7 @@ fn parse_mssql_top_percent() {
8885
let sql = "SELECT TOP (5) PERCENT * FROM foo";
8986
let select = ms_and_generic().verified_only_select(sql);
9087
let top = select.top.unwrap();
91-
assert_eq!(
92-
Some(TopQuantity::Expr(Expr::Value(number("5")))),
93-
top.quantity
94-
);
88+
assert_eq!(Some(Expr::Value(number("5"))), top.quantity);
9589
assert!(top.percent);
9690
}
9791

@@ -100,10 +94,7 @@ fn parse_mssql_top_with_ties() {
10094
let sql = "SELECT TOP (5) WITH TIES * FROM foo";
10195
let select = ms_and_generic().verified_only_select(sql);
10296
let top = select.top.unwrap();
103-
assert_eq!(
104-
Some(TopQuantity::Expr(Expr::Value(number("5")))),
105-
top.quantity
106-
);
97+
assert_eq!(Some(Expr::Value(number("5"))), top.quantity);
10798
assert!(top.with_ties);
10899
}
109100

@@ -112,17 +103,14 @@ fn parse_mssql_top_percent_with_ties() {
112103
let sql = "SELECT TOP (10) PERCENT WITH TIES * FROM foo";
113104
let select = ms_and_generic().verified_only_select(sql);
114105
let top = select.top.unwrap();
115-
assert_eq!(
116-
Some(TopQuantity::Expr(Expr::Value(number("10")))),
117-
top.quantity
118-
);
106+
assert_eq!(Some(Expr::Value(number("10"))), top.quantity);
119107
assert!(top.percent);
120108
}
121109

122110
#[test]
123111
fn parse_mssql_top() {
124112
let sql = "SELECT TOP 5 bar, baz FROM foo";
125-
let _ = ms_and_generic().one_statement_parses_to(sql, "SELECT TOP 5 bar, baz FROM foo");
113+
let _ = ms_and_generic().one_statement_parses_to(sql, "SELECT TOP (5) bar, baz FROM foo");
126114
}
127115

128116
fn ms() -> TestedDialects {

tests/sqlparser_snowflake.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,3 @@ fn snowflake_and_generic() -> TestedDialects {
200200
dialects: vec![Box::new(SnowflakeDialect {}), Box::new(GenericDialect {})],
201201
}
202202
}
203-
204-
#[test]
205-
fn parse_top() {
206-
snowflake().one_statement_parses_to(
207-
"SELECT TOP 4 c1 FROM testtable",
208-
"SELECT TOP 4 c1 FROM testtable",
209-
);
210-
}

0 commit comments

Comments
 (0)