Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/sqlite_hex_integer_literals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a FROM t GROUP BY 0X1F
17 changes: 13 additions & 4 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,19 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports integer literals in hexadecimal,
/// octal and binary, e.g. `0x1F`, `0o17` and `0b101`, which then parse as
/// numbers instead of `0x` hexadecimal strings.
fn supports_non_decimal_integer_literals(&self) -> bool {
/// Returns true if the dialect supports hexadecimal integer literals such
/// as `0x1F`, which then parse as numbers instead of `0x` hexadecimal strings.
fn supports_hex_integer_literals(&self) -> bool {
false
}

/// Returns true if the dialect supports octal integer literals such as `0o17`.
fn supports_octal_integer_literals(&self) -> bool {
false
}

/// Returns true if the dialect supports binary integer literals such as `0b101`.
fn supports_binary_integer_literals(&self) -> bool {
false
}

Expand Down
12 changes: 11 additions & 1 deletion src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,17 @@ impl Dialect for PostgreSqlDialect {
}

/// See: <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-NUMERIC>
fn supports_non_decimal_integer_literals(&self) -> bool {
fn supports_hex_integer_literals(&self) -> bool {
true
}

/// See: <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-NUMERIC>
fn supports_octal_integer_literals(&self) -> bool {
true
}

/// See: <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-NUMERIC>
fn supports_binary_integer_literals(&self) -> bool {
true
}

Expand Down
5 changes: 5 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ impl Dialect for SQLiteDialect {
true
}

/// See: <https://sqlite.org/lang_expr.html#hexint>
fn supports_hex_integer_literals(&self) -> bool {
true
}

fn supports_double_eq_assignment(&self) -> bool {
true
}
Expand Down
16 changes: 11 additions & 5 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,11 +1609,17 @@ impl<'a> Tokenizer<'a> {
return Ok(Some(Token::Number(s, false)));
}

if s == "0" && self.dialect.supports_non_decimal_integer_literals() {
if s == "0" {
let radix = match chars.peek() {
Some('x' | 'X') => Some((16, "a hexadecimal")),
Some('o' | 'O') => Some((8, "an octal")),
Some('b' | 'B') => Some((2, "a binary")),
Some('x' | 'X') if self.dialect.supports_hex_integer_literals() => {
Some((16, "a hexadecimal"))
}
Some('o' | 'O') if self.dialect.supports_octal_integer_literals() => {
Some((8, "an octal"))
}
Some('b' | 'B') if self.dialect.supports_binary_integer_literals() => {
Some((2, "a binary"))
}
_ => None,
};
if let Some((radix, name)) = radix {
Expand Down Expand Up @@ -3096,7 +3102,7 @@ mod tests {

let hex_string_dialects = all_dialects_where(|dialect| {
dialect.supports_numeric_literal_underscores()
&& !dialect.supports_non_decimal_integer_literals()
&& !dialect.supports_hex_integer_literals()
});
hex_string_dialects.tokenizes_to(
"0xFF_FF",
Expand Down
47 changes: 47 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2192,3 +2192,50 @@ fn parse_create_virtual_table_module_arguments_unbalanced_err() {
);
}
}

#[test]
fn parse_hex_integer_literals() {
let sql = "SELECT 0x1F, 0X1f, 0xFF_FF";
let canonical = if cfg!(feature = "bigdecimal") {
"SELECT 31, 31, 65535"
} else {
sql
};
let select = sqlite().verified_only_select_with_canonical(sql, canonical);
for item in &select.projection {
assert!(
matches!(
item,
UnnamedExpr(Expr::Value(ValueWithSpan {
value: Value::Number(_, false),
..
}))
),
"{item:?}"
);
}

assert_eq!(
expr_from_projection(&sqlite().verified_only_select("SELECT X'1F'").projection[0]),
&Expr::Value(Value::HexStringLiteral("1F".to_string()).with_empty_span())
);

for (sql, word) in [("0o17", "o17"), ("0b101", "b101")] {
sqlite().tokenizes_to(
sql,
vec![
Token::Number("0".to_string(), false),
Token::make_word(word, None),
],
);
}

for (sql, message) in [
("SELECT 0x", "Expected a hexadecimal digit after '0x'"),
("SELECT 0Xg", "Expected a hexadecimal digit after '0X'"),
("SELECT 0x1_", "Unexpected character '_'"),
] {
let err = sqlite().parse_sql_statements(sql).unwrap_err().to_string();
assert!(err.contains(message), "{sql}: {err}");
}
}
Loading