From 92c5a9f5f4959189da5e1c0cee1ffd7329319763 Mon Sep 17 00:00:00 2001 From: "Lei, HUANG" <6406592+v0y4g3r@users.noreply.github.com> Date: Thu, 5 Jun 2025 10:16:53 +0800 Subject: [PATCH] chore: allow numberic values in alter statements (#6252) chore/allow-numberic-values-in-alter: ### Commit Message Enhance `alter_parser.rs` to Support Numeric Values - Updated `parse_string_options` function in `alter_parser.rs` to handle numeric literals in addition to string literals and `NULL` for alter table statements. - Added a new test `test_parse_alter_with_numeric_value` in `alter_parser.rs` to verify the parsing of numeric values in alter table options. --- src/sql/src/parsers/alter_parser.rs | 49 +++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/sql/src/parsers/alter_parser.rs b/src/sql/src/parsers/alter_parser.rs index 67ab86f2ca..0cfd4b0964 100644 --- a/src/sql/src/parsers/alter_parser.rs +++ b/src/sql/src/parsers/alter_parser.rs @@ -391,13 +391,16 @@ fn parse_string_options(parser: &mut Parser) -> std::result::Result<(String, Str parser.expect_token(&Token::Eq)?; let value = if parser.parse_keyword(Keyword::NULL) { "".to_string() - } else if let Ok(v) = parser.parse_literal_string() { - v } else { - return Err(ParserError::ParserError(format!( - "Unexpected option value for alter table statements, expect string literal or NULL, got: `{}`", - parser.next_token() - ))); + let next_token = parser.peek_token(); + if let Token::Number(number_as_string, _) = next_token.token { + parser.advance_token(); + number_as_string + } else { + parser.parse_literal_string().map_err(|_|{ + ParserError::ParserError(format!("Unexpected option value for alter table statements, expect string literal, numeric literal or NULL, got: `{}`", next_token)) + })? + } }; Ok((name, value)) } @@ -1088,4 +1091,38 @@ mod tests { ) .unwrap_err(); } + + #[test] + fn test_parse_alter_with_numeric_value() { + for sql in [ + "ALTER TABLE test SET 'compaction.twcs.trigger_file_num'=8;", + "ALTER TABLE test SET 'compaction.twcs.trigger_file_num'='8';", + ] { + let mut result = ParserContext::create_with_dialect( + sql, + &GreptimeDbDialect {}, + ParseOptions::default(), + ) + .unwrap(); + assert_eq!(1, result.len()); + + let statement = result.remove(0); + assert_matches!(statement, Statement::AlterTable { .. }); + match statement { + Statement::AlterTable(alter_table) => { + let alter_operation = alter_table.alter_operation(); + assert_matches!(alter_operation, AlterTableOperation::SetTableOptions { .. }); + match alter_operation { + AlterTableOperation::SetTableOptions { options } => { + assert_eq!(options.len(), 1); + assert_eq!(options[0].key, "compaction.twcs.trigger_file_num"); + assert_eq!(options[0].value, "8"); + } + _ => unreachable!(), + } + } + _ => unreachable!(), + } + } + } }