Skip to content
Closed
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/hive_2589_digit_prefixed_field.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT [dbo]. 1te_sk = 1
11 changes: 10 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,10 +1503,19 @@ const fn is_number_expr(expr: &Expr) -> bool {
matches!(expr, Expr::Value(v) if matches!(v.value, Value::Number(_, _)))
}

/// Whether a leading `.` would merge into `expr` when the displayed SQL is
/// re-lexed: `.1` lexes as a number in front of a numeric literal or an
/// unquoted identifier starting with a digit (Hive allows such words).
fn dot_merges_with_expr(expr: &Expr) -> bool {
is_number_expr(expr)
|| matches!(expr, Expr::Identifier(ident) if ident.quote_style.is_none()
&& ident.value.starts_with(|c: char| c.is_ascii_digit()))
}

impl fmt::Display for AccessExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AccessExpr::Dot(expr) if is_number_expr(expr) => write!(f, " . {expr}"),
AccessExpr::Dot(expr) if dot_merges_with_expr(expr) => write!(f, " . {expr}"),
AccessExpr::Dot(expr) => write!(f, ".{expr}"),
AccessExpr::Subscript(subscript) => write!(f, "[{subscript}]"),
}
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_hive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,16 @@ fn parse_create_table_with_map_column_comment() {
);
}

#[test]
fn test_field_access_on_digit_prefixed_identifier() {
// The space before `1te_sk` keeps `.1` from being lexed as a number,
// which previously reparsed as a different statement. See
// https://github.com/apache/datafusion-sqlparser-rs/issues/2589
hive().one_statement_parses_to("SELECT [dbo]. 1te_sk = 1", "SELECT [dbo] . 1te_sk = 1");
hive().verified_stmt("SELECT [dbo] . 1te_sk");
hive().verified_stmt("SELECT dbo.1te_sk");
}

fn hive() -> TestedDialects {
TestedDialects::new(vec![Box::new(HiveDialect {})])
}
Expand Down
Loading