Skip to content

Commit 9980510

Browse files
authored
fix(query): AsyncFunctionCall::display_name should respect quota (#18412)
During the semantic analysis phase of the `nextval` function, the logic for generating `display_name` might not have fully considered SQL identifier quoting rules. If the sequence name was a reserved keyword or contained special characters, the generated `display_name` might not conform to SQL syntax, leading to inaccurate display or potential issues. This change constructs the `display_name` by leveraging the `to_string()` method of the `ident` object returned by `normalize_identifier`. The `ident.to_string()` method already incorporates the logic for necessary identifier quoting according to SQL rules. In main: ``` root@localhost:8000/default/default> create or replace table mytest (seq int default nextval(`ๆต‹่ฏ•`)); error: APIError: QueryFailed: [1001]fail to parse default expr `CAST(nextval(ๆต‹่ฏ•) AS Int32 NULL)` (string length = 35) of field seq, SyntaxException. Code: 1005, Text = unable to recognize the rest tokens. ```
1 parent 367a17d commit 9980510

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

โ€Žsrc/query/sql/src/planner/semantic/type_check.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,15 +5213,18 @@ impl<'a> TypeChecker<'a> {
52135213
))
52145214
.set_span(span));
52155215
}
5216-
let sequence_name = if let Expr::ColumnRef { column, .. } = arguments[0] {
5216+
let (sequence_name, display_name) = if let Expr::ColumnRef { column, .. } = arguments[0] {
52175217
if column.database.is_some() || column.table.is_some() {
52185218
return Err(ErrorCode::SemanticError(
52195219
"nextval function argument identifier should only contain one part".to_string(),
52205220
)
52215221
.set_span(span));
52225222
}
52235223
match &column.column {
5224-
ColumnID::Name(ident) => normalize_identifier(ident, self.name_resolution_ctx).name,
5224+
ColumnID::Name(ident) => {
5225+
let ident = normalize_identifier(ident, self.name_resolution_ctx);
5226+
(ident.name.to_string(), format!("{}({})", func_name, ident))
5227+
}
52255228
ColumnID::Position(pos) => {
52265229
return Err(ErrorCode::SemanticError(format!(
52275230
"nextval function argument don't support identifier {}",
@@ -5245,7 +5248,6 @@ impl<'a> TypeChecker<'a> {
52455248

52465249
databend_common_base::runtime::block_on(catalog.get_sequence(req))?;
52475250

5248-
let display_name = format!("{}({})", func_name, sequence_name);
52495251
let return_type = DataType::Number(NumberDataType::UInt64);
52505252
let func_arg = AsyncFunctionArgument::SequenceFunction(sequence_name);
52515253

โ€Žtests/sqllogictests/suites/stage/sequence_as_default.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ statement ok
22
create or replace sequence seq
33

44
statement ok
5-
create or replace sequence seq1
5+
create or replace sequence `seq1_ๆต‹่ฏ•`
66

77
statement ok
8-
create or replace table tmp (seq int default nextval(seq), a int, seq1 bigint not null default nextval(seq1));
8+
create or replace table tmp (seq int default nextval(seq), a int, seq1 bigint not null default nextval(`seq1_ๆต‹่ฏ•`));
99

1010
statement ok
1111
set sequence_step_size = 1;

0 commit comments

Comments
ย (0)