Skip to content

Commit 3a8369a

Browse files
authored
Parse true and false as identifiers in mssql (#1510)
1 parent 9082448 commit 3a8369a

File tree

6 files changed

+97
-60
lines changed

6 files changed

+97
-60
lines changed

src/dialect/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,12 @@ pub trait Dialect: Debug + Any {
616616
fn supports_top_before_distinct(&self) -> bool {
617617
false
618618
}
619+
620+
/// Returns true if the dialect supports boolean literals (`true` and `false`).
621+
/// For example, in MSSQL these are treated as identifiers rather than boolean literals.
622+
fn supports_boolean_literals(&self) -> bool {
623+
true
624+
}
619625
}
620626

621627
/// This represents the operators for which precedence must be defined

src/dialect/mssql.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ impl Dialect for MsSqlDialect {
5757
fn supports_try_convert(&self) -> bool {
5858
true
5959
}
60+
61+
/// In MSSQL, there is no boolean type, and `true` and `false` are valid column names
62+
fn supports_boolean_literals(&self) -> bool {
63+
false
64+
}
6065
}

src/parser/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,11 @@ impl<'a> Parser<'a> {
10141014
let next_token = self.next_token();
10151015
let expr = match next_token.token {
10161016
Token::Word(w) => match w.keyword {
1017-
Keyword::TRUE | Keyword::FALSE | Keyword::NULL => {
1017+
Keyword::TRUE | Keyword::FALSE if self.dialect.supports_boolean_literals() => {
1018+
self.prev_token();
1019+
Ok(Expr::Value(self.parse_value()?))
1020+
}
1021+
Keyword::NULL => {
10181022
self.prev_token();
10191023
Ok(Expr::Value(self.parse_value()?))
10201024
}
@@ -7577,8 +7581,12 @@ impl<'a> Parser<'a> {
75777581
let location = next_token.location;
75787582
match next_token.token {
75797583
Token::Word(w) => match w.keyword {
7580-
Keyword::TRUE => Ok(Value::Boolean(true)),
7581-
Keyword::FALSE => Ok(Value::Boolean(false)),
7584+
Keyword::TRUE if self.dialect.supports_boolean_literals() => {
7585+
Ok(Value::Boolean(true))
7586+
}
7587+
Keyword::FALSE if self.dialect.supports_boolean_literals() => {
7588+
Ok(Value::Boolean(false))
7589+
}
75827590
Keyword::NULL => Ok(Value::Null),
75837591
Keyword::NoKeyword if w.quote_style.is_some() => match w.quote_style {
75847592
Some('"') => Ok(Value::DoubleQuotedString(w.value)),

tests/sqlparser_common.rs

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ fn parse_top_level() {
820820
verified_stmt("(SELECT 1)");
821821
verified_stmt("((SELECT 1))");
822822
verified_stmt("VALUES (1)");
823-
verified_stmt("VALUES ROW(1, true, 'a'), ROW(2, false, 'b')");
823+
verified_stmt("VALUES ROW(1, NULL, 'a'), ROW(2, NULL, 'b')");
824824
}
825825

826826
#[test]
@@ -1499,7 +1499,7 @@ fn parse_is_not_distinct_from() {
14991499
#[test]
15001500
fn parse_not_precedence() {
15011501
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
1502-
let sql = "NOT true OR true";
1502+
let sql = "NOT 1 OR 1";
15031503
assert_matches!(
15041504
verified_expr(sql),
15051505
Expr::BinaryOp {
@@ -1919,44 +1919,6 @@ fn parse_binary_all() {
19191919
);
19201920
}
19211921

1922-
#[test]
1923-
fn parse_logical_xor() {
1924-
let sql = "SELECT true XOR true, false XOR false, true XOR false, false XOR true";
1925-
let select = verified_only_select(sql);
1926-
assert_eq!(
1927-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1928-
left: Box::new(Expr::Value(Value::Boolean(true))),
1929-
op: BinaryOperator::Xor,
1930-
right: Box::new(Expr::Value(Value::Boolean(true))),
1931-
}),
1932-
select.projection[0]
1933-
);
1934-
assert_eq!(
1935-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1936-
left: Box::new(Expr::Value(Value::Boolean(false))),
1937-
op: BinaryOperator::Xor,
1938-
right: Box::new(Expr::Value(Value::Boolean(false))),
1939-
}),
1940-
select.projection[1]
1941-
);
1942-
assert_eq!(
1943-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1944-
left: Box::new(Expr::Value(Value::Boolean(true))),
1945-
op: BinaryOperator::Xor,
1946-
right: Box::new(Expr::Value(Value::Boolean(false))),
1947-
}),
1948-
select.projection[2]
1949-
);
1950-
assert_eq!(
1951-
SelectItem::UnnamedExpr(Expr::BinaryOp {
1952-
left: Box::new(Expr::Value(Value::Boolean(false))),
1953-
op: BinaryOperator::Xor,
1954-
right: Box::new(Expr::Value(Value::Boolean(true))),
1955-
}),
1956-
select.projection[3]
1957-
);
1958-
}
1959-
19601922
#[test]
19611923
fn parse_between() {
19621924
fn chk(negated: bool) {
@@ -4113,14 +4075,14 @@ fn parse_alter_table_alter_column() {
41134075
);
41144076

41154077
match alter_table_op(verified_stmt(&format!(
4116-
"{alter_stmt} ALTER COLUMN is_active SET DEFAULT false"
4078+
"{alter_stmt} ALTER COLUMN is_active SET DEFAULT 0"
41174079
))) {
41184080
AlterTableOperation::AlterColumn { column_name, op } => {
41194081
assert_eq!("is_active", column_name.to_string());
41204082
assert_eq!(
41214083
op,
41224084
AlterColumnOperation::SetDefault {
4123-
value: Expr::Value(Value::Boolean(false))
4085+
value: Expr::Value(test_utils::number("0"))
41244086
}
41254087
);
41264088
}
@@ -6502,7 +6464,7 @@ fn parse_values() {
65026464
verified_stmt("SELECT * FROM (VALUES (1), (2), (3))");
65036465
verified_stmt("SELECT * FROM (VALUES (1), (2), (3)), (VALUES (1, 2, 3))");
65046466
verified_stmt("SELECT * FROM (VALUES (1)) UNION VALUES (1)");
6505-
verified_stmt("SELECT * FROM (VALUES ROW(1, true, 'a'), ROW(2, false, 'b')) AS t (a, b, c)");
6467+
verified_stmt("SELECT * FROM (VALUES ROW(1, NULL, 'a'), ROW(2, NULL, 'b')) AS t (a, b, c)");
65066468
}
65076469

65086470
#[test]
@@ -7321,15 +7283,15 @@ fn lateral_derived() {
73217283
let lateral_str = if lateral_in { "LATERAL " } else { "" };
73227284
let sql = format!(
73237285
"SELECT * FROM customer LEFT JOIN {lateral_str}\
7324-
(SELECT * FROM order WHERE order.customer = customer.id LIMIT 3) AS order ON true"
7286+
(SELECT * FROM orders WHERE orders.customer = customer.id LIMIT 3) AS orders ON 1"
73257287
);
73267288
let select = verified_only_select(&sql);
73277289
let from = only(select.from);
73287290
assert_eq!(from.joins.len(), 1);
73297291
let join = &from.joins[0];
73307292
assert_eq!(
73317293
join.join_operator,
7332-
JoinOperator::LeftOuter(JoinConstraint::On(Expr::Value(Value::Boolean(true))))
7294+
JoinOperator::LeftOuter(JoinConstraint::On(Expr::Value(test_utils::number("1"))))
73337295
);
73347296
if let TableFactor::Derived {
73357297
lateral,
@@ -7338,10 +7300,10 @@ fn lateral_derived() {
73387300
} = join.relation
73397301
{
73407302
assert_eq!(lateral_in, lateral);
7341-
assert_eq!(Ident::new("order"), alias.name);
7303+
assert_eq!(Ident::new("orders"), alias.name);
73427304
assert_eq!(
73437305
subquery.to_string(),
7344-
"SELECT * FROM order WHERE order.customer = customer.id LIMIT 3"
7306+
"SELECT * FROM orders WHERE orders.customer = customer.id LIMIT 3"
73457307
);
73467308
} else {
73477309
unreachable!()
@@ -8381,7 +8343,7 @@ fn parse_merge() {
83818343
_ => unreachable!(),
83828344
};
83838345

8384-
let sql = "MERGE INTO s.bar AS dest USING newArrivals AS S ON false WHEN NOT MATCHED THEN INSERT VALUES (stg.A, stg.B, stg.C)";
8346+
let sql = "MERGE INTO s.bar AS dest USING newArrivals AS S ON (1 > 1) WHEN NOT MATCHED THEN INSERT VALUES (stg.A, stg.B, stg.C)";
83858347
verified_stmt(sql);
83868348
}
83878349

@@ -11160,13 +11122,11 @@ fn parse_explain_with_option_list() {
1116011122

1116111123
#[test]
1116211124
fn test_create_policy() {
11163-
let sql = concat!(
11164-
"CREATE POLICY my_policy ON my_table ",
11165-
"AS PERMISSIVE FOR SELECT ",
11166-
"TO my_role, CURRENT_USER ",
11167-
"USING (c0 = 1) ",
11168-
"WITH CHECK (true)"
11169-
);
11125+
let sql: &str = "CREATE POLICY my_policy ON my_table \
11126+
AS PERMISSIVE FOR SELECT \
11127+
TO my_role, CURRENT_USER \
11128+
USING (c0 = 1) \
11129+
WITH CHECK (1 = 1)";
1117011130

1117111131
match all_dialects().verified_stmt(sql) {
1117211132
Statement::CreatePolicy {
@@ -11194,7 +11154,14 @@ fn test_create_policy() {
1119411154
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
1119511155
})
1119611156
);
11197-
assert_eq!(with_check, Some(Expr::Value(Value::Boolean(true))));
11157+
assert_eq!(
11158+
with_check,
11159+
Some(Expr::BinaryOp {
11160+
left: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
11161+
op: BinaryOperator::Eq,
11162+
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
11163+
})
11164+
);
1119811165
}
1119911166
_ => unreachable!(),
1120011167
}
@@ -11205,7 +11172,7 @@ fn test_create_policy() {
1120511172
"AS PERMISSIVE FOR SELECT ",
1120611173
"TO my_role, CURRENT_USER ",
1120711174
"USING (c0 IN (SELECT column FROM t0)) ",
11208-
"WITH CHECK (true)"
11175+
"WITH CHECK (1 = 1)"
1120911176
));
1121011177
// omit AS / FOR / TO / USING / WITH CHECK clauses is allowed
1121111178
all_dialects().verified_stmt("CREATE POLICY my_policy ON my_table");

tests/sqlparser_mssql.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,18 @@ fn parse_create_table_with_identity_column() {
13661366
}
13671367
}
13681368

1369+
#[test]
1370+
fn parse_true_false_as_identifiers() {
1371+
assert_eq!(
1372+
ms().verified_expr("true"),
1373+
Expr::Identifier(Ident::new("true"))
1374+
);
1375+
assert_eq!(
1376+
ms().verified_expr("false"),
1377+
Expr::Identifier(Ident::new("false"))
1378+
);
1379+
}
1380+
13691381
fn ms() -> TestedDialects {
13701382
TestedDialects::new(vec![Box::new(MsSqlDialect {})])
13711383
}

tests/sqlparser_mysql.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,3 +2817,42 @@ fn test_group_concat() {
28172817
mysql_and_generic()
28182818
.verified_expr("GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')");
28192819
}
2820+
2821+
/// The XOR binary operator is only supported in MySQL
2822+
#[test]
2823+
fn parse_logical_xor() {
2824+
let sql = "SELECT true XOR true, false XOR false, true XOR false, false XOR true";
2825+
let select = mysql_and_generic().verified_only_select(sql);
2826+
assert_eq!(
2827+
SelectItem::UnnamedExpr(Expr::BinaryOp {
2828+
left: Box::new(Expr::Value(Value::Boolean(true))),
2829+
op: BinaryOperator::Xor,
2830+
right: Box::new(Expr::Value(Value::Boolean(true))),
2831+
}),
2832+
select.projection[0]
2833+
);
2834+
assert_eq!(
2835+
SelectItem::UnnamedExpr(Expr::BinaryOp {
2836+
left: Box::new(Expr::Value(Value::Boolean(false))),
2837+
op: BinaryOperator::Xor,
2838+
right: Box::new(Expr::Value(Value::Boolean(false))),
2839+
}),
2840+
select.projection[1]
2841+
);
2842+
assert_eq!(
2843+
SelectItem::UnnamedExpr(Expr::BinaryOp {
2844+
left: Box::new(Expr::Value(Value::Boolean(true))),
2845+
op: BinaryOperator::Xor,
2846+
right: Box::new(Expr::Value(Value::Boolean(false))),
2847+
}),
2848+
select.projection[2]
2849+
);
2850+
assert_eq!(
2851+
SelectItem::UnnamedExpr(Expr::BinaryOp {
2852+
left: Box::new(Expr::Value(Value::Boolean(false))),
2853+
op: BinaryOperator::Xor,
2854+
right: Box::new(Expr::Value(Value::Boolean(true))),
2855+
}),
2856+
select.projection[3]
2857+
);
2858+
}

0 commit comments

Comments
 (0)