Skip to content

Commit af79db9

Browse files
authored
fix: udf args recursion crash and binding not found on table (#19091)
1 parent f9c16aa commit af79db9

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/query/sql/src/planner/semantic/type_check.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5757,13 +5757,13 @@ impl<'a> TypeChecker<'a> {
57575757
let mut visitor = UDFArgVisitor::new(&arg_types, arguments);
57585758
udf_expr.drive_mut(&mut visitor);
57595759

5760-
// independent context
5760+
// Use current binding context so column references inside arguments can be resolved.
57615761
let box (expr, _) = TypeChecker::try_create(
5762-
&mut BindContext::new(),
5762+
self.bind_context,
57635763
self.ctx.clone(),
5764-
&NameResolutionContext::default(),
5765-
MetadataRef::default(),
5766-
&[],
5764+
self.name_resolution_ctx,
5765+
self.metadata.clone(),
5766+
self.aliases,
57675767
self.forbid_udf,
57685768
)?
57695769
.resolve(&udf_expr)?;

src/query/sql/src/planner/semantic/udf_rewriter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a> VisitorMut<'a> for UdfRewriter {
243243
}
244244

245245
#[derive(StatementVisitorMut)]
246-
#[visitor(Expr(enter))]
246+
#[visitor(Expr(exit))]
247247
pub struct UDFArgVisitor<'a> {
248248
arg_types: &'a [(String, DataType)],
249249
args: &'a [Expr],
@@ -254,7 +254,7 @@ impl<'a> UDFArgVisitor<'a> {
254254
Self { arg_types, args }
255255
}
256256

257-
fn enter_expr(&mut self, expr: &mut Expr) {
257+
fn exit_expr(&mut self, expr: &mut Expr) {
258258
if let Expr::ColumnRef { span, column } = expr {
259259
if column.database.is_some() || column.table.is_some() {
260260
return;

tests/sqllogictests/suites/base/03_common/03_0013_select_udf.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,18 @@ SELECT bool_not(1 = 0);
317317
----
318318
1
319319

320+
statement ok
321+
CREATE OR REPLACE FUNCTION substring_index(str STRING, delim STRING, count INT) RETURNS STRING AS $$ ARRAY_TO_STRING(ARRAY_SLICE(SPLIT(str, delim), 1, count), delim) $$;
322+
323+
statement ok
324+
CREATE OR REPLACE TABLE str_test ( str VARCHAR NULL ) ENGINE=FUSE;
325+
326+
statement ok
327+
INSERT INTO str_test VALUES('a-b-c-d');
328+
329+
query T
330+
SELECT substring_index(str, '-', 2) FROM str_test;
331+
----
332+
a-b
333+
320334
# TODO: UDF Timestamp Timezone

0 commit comments

Comments
 (0)