Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing functions for show functions #13250

Merged
merged 2 commits into from
Oct 13, 2023
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/query/ast/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,21 @@ impl Expr {
| Expr::DateTrunc { span, .. } => *span,
}
}

pub fn all_function_like_syntaxes() -> &'static [&'static str] {
&[
"CAST",
"TRY_CAST",
"EXTRACT",
"DATE_PART",
"POSITION",
"SUBSTRING",
"TRIM",
"DATE_ADD",
"DATE_SUB",
"DATE_TRUNC",
]
}
}

impl Display for IntervalKind {
Expand Down
10 changes: 5 additions & 5 deletions src/query/sql/src/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl<'a> TypeChecker<'a> {
let func_name = normalize_identifier(name, self.name_resolution_ctx).to_string();
let func_name = func_name.as_str();
if !is_builtin_function(func_name)
&& !Self::all_rewritable_scalar_function().contains(&func_name)
&& !Self::all_sugar_functions().contains(&func_name)
{
if let Some(udf) = self.resolve_udf(*span, func_name, args).await? {
return Ok(udf);
Expand All @@ -686,7 +686,7 @@ impl<'a> TypeChecker<'a> {
.chain(GENERAL_WINDOW_FUNCTIONS.iter().cloned().map(str::to_string))
.chain(GENERAL_LAMBDA_FUNCTIONS.iter().cloned().map(str::to_string))
.chain(
Self::all_rewritable_scalar_function()
Self::all_sugar_functions()
.iter()
.cloned()
.map(str::to_string),
Expand Down Expand Up @@ -1742,7 +1742,7 @@ impl<'a> TypeChecker<'a> {
) -> Result<Box<(ScalarExpr, DataType)>> {
// Check if current function is a virtual function, e.g. `database`, `version`
if let Some(rewritten_func_result) = self
.try_rewrite_scalar_function(span, func_name, arguments)
.try_rewrite_sugar_function(span, func_name, arguments)
.await
{
return rewritten_func_result;
Expand Down Expand Up @@ -2133,7 +2133,7 @@ impl<'a> TypeChecker<'a> {
Ok(Box::new((subquery_expr.into(), data_type)))
}

pub fn all_rewritable_scalar_function() -> &'static [&'static str] {
pub fn all_sugar_functions() -> &'static [&'static str] {
&[
"database",
"currentdatabase",
Expand Down Expand Up @@ -2162,7 +2162,7 @@ impl<'a> TypeChecker<'a> {

#[async_recursion::async_recursion]
#[async_backtrace::framed]
async fn try_rewrite_scalar_function(
async fn try_rewrite_sugar_function(
&mut self,
span: Span,
func_name: &str,
Expand Down
1 change: 1 addition & 0 deletions src/query/storages/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test = false
enable-histogram-metrics = ["common-metrics/enable-histogram"]

[dependencies]
common-ast = { path = "../../ast" }
common-base = { path = "../../../common/base" }
common-catalog = { path = "../../catalog" }
common-config = { path = "../../config" }
Expand Down
23 changes: 17 additions & 6 deletions src/query/storages/system/src/functions_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use common_meta_app::principal::UserDefinedFunction;
use common_meta_app::schema::TableIdent;
use common_meta_app::schema::TableInfo;
use common_meta_app::schema::TableMeta;
use common_sql::TypeChecker;
use common_users::UserApiProvider;

use crate::table::AsyncOneBlockSystemTable;
Expand All @@ -54,27 +55,37 @@ impl AsyncSystemTable for FunctionsTable {
ctx: Arc<dyn TableContext>,
_push_downs: Option<PushDownInfo>,
) -> Result<DataBlock> {
// TODO(andylokandy): add rewritable function names, e.g. database()
let func_names = BUILTIN_FUNCTIONS.registered_names();
let mut scalar_func_names: Vec<String> = BUILTIN_FUNCTIONS.registered_names();
scalar_func_names.extend(
TypeChecker::all_sugar_functions()
.iter()
.map(|name| name.to_string()),
);
scalar_func_names.extend(
common_ast::ast::Expr::all_function_like_syntaxes()
.iter()
.map(|name| name.to_lowercase()),
);
scalar_func_names.sort();
let aggregate_function_factory = AggregateFunctionFactory::instance();
let aggr_func_names = aggregate_function_factory.registered_names();
let udfs = FunctionsTable::get_udfs(ctx).await?;

let names: Vec<&str> = func_names
let names: Vec<&str> = scalar_func_names
.iter()
.chain(aggr_func_names.iter())
.chain(&aggr_func_names)
.chain(udfs.iter().map(|udf| &udf.name))
.map(|x| x.as_str())
.collect();

let builtin_func_len = func_names.len() + aggr_func_names.len();
let builtin_func_len = scalar_func_names.len() + aggr_func_names.len();

let is_builtin = (0..names.len())
.map(|i| i < builtin_func_len)
.collect::<Vec<bool>>();

let is_aggregate = (0..names.len())
.map(|i| i >= func_names.len() && i < builtin_func_len)
.map(|i| i >= scalar_func_names.len() && i < builtin_func_len)
.collect::<Vec<bool>>();

let definitions = (0..names.len())
Expand Down
Loading