risingwave_frontend/binder/relation/table_function.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::str::FromStr;
use itertools::Itertools;
use risingwave_common::bail_not_implemented;
use risingwave_common::catalog::{Field, Schema, RW_INTERNAL_TABLE_FUNCTION_NAME};
use risingwave_common::types::DataType;
use risingwave_sqlparser::ast::{Function, FunctionArg, FunctionArgList, ObjectName, TableAlias};
use super::watermark::is_watermark_func;
use super::{Binder, Relation, Result, WindowTableFunctionKind};
use crate::binder::bind_context::Clause;
use crate::error::ErrorCode;
use crate::expr::{Expr, ExprImpl};
impl Binder {
/// Binds a table function AST, which is a function call in a relation position.
///
/// Besides [`crate::expr::TableFunction`] expr, it can also be other things like window table
/// functions, or scalar functions.
///
/// `with_ordinality` is only supported for the `TableFunction` case now.
pub(super) fn bind_table_function(
&mut self,
name: ObjectName,
alias: Option<TableAlias>,
args: Vec<FunctionArg>,
with_ordinality: bool,
) -> Result<Relation> {
let func_name = &name.0[0].real_value();
// internal/system table functions
{
if func_name.eq_ignore_ascii_case(RW_INTERNAL_TABLE_FUNCTION_NAME) {
if with_ordinality {
bail_not_implemented!(
"WITH ORDINALITY for internal/system table function {}",
func_name
);
}
return self.bind_internal_table(args, alias);
}
}
// window table functions (tumble/hop)
if let Ok(kind) = WindowTableFunctionKind::from_str(func_name) {
if with_ordinality {
return Err(ErrorCode::InvalidInputSyntax(format!(
"WITH ORDINALITY for window table function {}",
func_name
))
.into());
}
return Ok(Relation::WindowTableFunction(Box::new(
self.bind_window_table_function(alias, kind, args)?,
)));
}
// watermark
if is_watermark_func(func_name) {
if with_ordinality {
return Err(ErrorCode::InvalidInputSyntax(
"WITH ORDINALITY for watermark".to_string(),
)
.into());
}
return Ok(Relation::Watermark(Box::new(
self.bind_watermark(alias, args)?,
)));
};
self.push_context();
let mut clause = Some(Clause::From);
std::mem::swap(&mut self.context.clause, &mut clause);
let func = self.bind_function(Function {
scalar_as_agg: false,
name,
arg_list: FunctionArgList::args_only(args),
over: None,
filter: None,
within_group: None,
});
self.context.clause = clause;
self.pop_context()?;
let func = func?;
if let ExprImpl::TableFunction(func) = &func {
if func.args.iter().any(|arg| arg.has_subquery()) {
// Same error reports as DuckDB.
return Err(ErrorCode::InvalidInputSyntax(
format!("Only table-in-out functions can have subquery parameters. The table function has subquery parameters is {}", func.name()),
)
.into());
}
}
// bool indicates if the field is hidden
let mut columns = if let DataType::Struct(s) = func.return_type() {
// If the table function returns a struct, it will be flattened into multiple columns.
let schema = Schema::from(&s);
schema.fields.into_iter().map(|f| (false, f)).collect_vec()
} else {
// If there is an table alias (and it doesn't return a struct),
// we should use the alias as the table function's
// column name. If column aliases are also provided, they
// are handled in bind_table_to_context.
//
// Note: named return value should take precedence over table alias.
// But we don't support it yet.
// e.g.,
// ```sql
// > create function foo(ret out int) language sql as 'select 1';
// > select t.ret from foo() as t;
// ```
let col_name = if let Some(alias) = &alias {
alias.name.real_value()
} else {
func_name.clone()
};
vec![(false, Field::with_name(func.return_type(), col_name))]
};
if with_ordinality {
columns.push((false, Field::with_name(DataType::Int64, "ordinality")));
}
self.bind_table_to_context(columns, func_name.clone(), alias)?;
Ok(Relation::TableFunction {
expr: func,
with_ordinality,
})
}
}