common_function/admin/
build_index_table.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use arrow::datatypes::DataType as ArrowDataType;
16use common_error::ext::BoxedError;
17use common_macro::admin_fn;
18use common_query::error::{
19    InvalidFuncArgsSnafu, MissingTableMutationHandlerSnafu, Result, TableMutationSnafu,
20    UnsupportedInputDataTypeSnafu,
21};
22use datafusion_expr::{Signature, Volatility};
23use datatypes::prelude::*;
24use session::context::QueryContextRef;
25use session::table_name::table_name_to_full_name;
26use snafu::{ResultExt, ensure};
27use table::requests::BuildIndexTableRequest;
28
29use crate::handlers::TableMutationHandlerRef;
30
31#[admin_fn(
32    name = BuildIndexFunction,
33    display_name = build_index,
34    sig_fn = build_index_signature,
35    ret = uint64
36)]
37pub(crate) async fn build_index(
38    table_mutation_handler: &TableMutationHandlerRef,
39    query_ctx: &QueryContextRef,
40    params: &[ValueRef<'_>],
41) -> Result<Value> {
42    ensure!(
43        params.len() == 1,
44        InvalidFuncArgsSnafu {
45            err_msg: format!(
46                "The length of the args is not correct, expect 1, have: {}",
47                params.len()
48            ),
49        }
50    );
51
52    let ValueRef::String(table_name) = params[0] else {
53        return UnsupportedInputDataTypeSnafu {
54            function: "build_index",
55            datatypes: params.iter().map(|v| v.data_type()).collect::<Vec<_>>(),
56        }
57        .fail();
58    };
59
60    let (catalog_name, schema_name, table_name) = table_name_to_full_name(table_name, query_ctx)
61        .map_err(BoxedError::new)
62        .context(TableMutationSnafu)?;
63
64    let affected_rows = table_mutation_handler
65        .build_index(
66            BuildIndexTableRequest {
67                catalog_name,
68                schema_name,
69                table_name,
70            },
71            query_ctx.clone(),
72        )
73        .await?;
74
75    Ok(Value::from(affected_rows as u64))
76}
77
78fn build_index_signature() -> Signature {
79    Signature::uniform(1, vec![ArrowDataType::Utf8], Volatility::Immutable)
80}