risingwave_frontend/expr/function_impl/
pg_get_indexdef.rs

1// Copyright 2025 RisingWave Labs
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 risingwave_expr::{ExprError, Result, capture_context, function};
16use thiserror_ext::AsReport;
17
18use super::context::{CATALOG_READER, DB_NAME};
19use crate::catalog::CatalogReader;
20
21#[function("pg_get_indexdef(int4) -> varchar")]
22fn pg_get_indexdef(oid: i32, writer: &mut impl std::fmt::Write) -> Result<()> {
23    pg_get_indexdef_impl_captured(oid, 0, writer)
24}
25
26#[function("pg_get_indexdef(int4, int4, boolean) -> varchar")]
27fn pg_get_indexdef_col(
28    oid: i32,
29    column_no: i32,
30    _pretty_bool: bool,
31    writer: &mut impl std::fmt::Write,
32) -> Result<()> {
33    pg_get_indexdef_impl_captured(oid, column_no, writer)
34}
35
36#[capture_context(CATALOG_READER, DB_NAME)]
37fn pg_get_indexdef_impl(
38    catalog: &CatalogReader,
39    db_name: &str,
40    oid: i32,
41    column_no: i32,
42    writer: &mut impl std::fmt::Write,
43) -> Result<()> {
44    let ans = if column_no == 0 {
45        catalog
46            .read_guard()
47            .get_index_by_id(db_name, oid as u32)
48            .map_err(|e| ExprError::InvalidParam {
49                name: "oid",
50                reason: e.to_report_string().into(),
51            })?
52            .index_table()
53            .create_sql()
54    } else {
55        catalog
56            .read_guard()
57            .get_index_by_id(db_name, oid as u32)
58            .map_err(|e| ExprError::InvalidParam {
59                name: "oid",
60                reason: e.to_report_string().into(),
61            })?
62            .get_column_def(column_no as usize - 1)
63            .unwrap_or_default()
64    };
65    write!(writer, "{}", ans).unwrap();
66    Ok(())
67}