risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_indexes.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_common::types::{Fields, Timestamptz};
16use risingwave_frontend_macro::system_catalog;
17
18use crate::catalog::index_catalog::IndexType;
19use crate::catalog::system_catalog::SysCatalogReaderImpl;
20use crate::error::Result;
21
22#[derive(Fields)]
23struct RwIndex {
24    #[primary_key]
25    id: i32,
26    name: String,
27    primary_table_id: i32,
28    key_columns: Vec<i16>,
29    include_columns: Vec<i16>,
30    schema_id: i32,
31    owner: i32,
32    definition: String,
33    acl: Vec<String>,
34    initialized_at: Option<Timestamptz>,
35    created_at: Option<Timestamptz>,
36    initialized_at_cluster_version: Option<String>,
37    created_at_cluster_version: Option<String>,
38}
39
40#[system_catalog(table, "rw_catalog.rw_indexes")]
41fn read_rw_indexes(reader: &SysCatalogReaderImpl) -> Result<Vec<RwIndex>> {
42    let catalog_reader = reader.catalog_reader.read_guard();
43    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
44    let user_reader = reader.user_info_reader.read_guard();
45    let current_user = user_reader
46        .get_user_by_name(&reader.auth_context.user_name)
47        .expect("user not found");
48
49    Ok(schemas
50        .flat_map(|schema| {
51            schema.iter_index_with_acl(current_user).map(|index| {
52                let (index_table, index_columns_len) = match &index.index_type {
53                    IndexType::Table(index) => (&index.index_table, index.index_columns_len),
54                    IndexType::Vector(index) => (&index.index_table, 1),
55                };
56                RwIndex {
57                    id: index.id.index_id as i32,
58                    name: index.name.clone(),
59                    primary_table_id: index.primary_table.id().table_id as i32,
60                    key_columns: index
61                        .index_item
62                        .iter()
63                        .take(index_columns_len as usize)
64                        .map(|index| {
65                            let ind = if let Some(input_ref) = index.as_input_ref() {
66                                input_ref.index() + 1
67                            } else {
68                                0
69                            };
70                            ind as i16
71                        })
72                        .collect(),
73                    include_columns: index
74                        .index_item
75                        .iter()
76                        .skip(index_columns_len as usize)
77                        .map(|index| {
78                            let ind = if let Some(input_ref) = index.as_input_ref() {
79                                input_ref.index() + 1
80                            } else {
81                                0
82                            };
83                            ind as i16
84                        })
85                        .collect(),
86                    schema_id: schema.id() as i32,
87                    owner: index.index_table().owner as i32,
88                    definition: index_table.create_sql(),
89                    acl: vec![],
90                    initialized_at: index.initialized_at_epoch.map(|e| e.as_timestamptz()),
91                    created_at: index.created_at_epoch.map(|e| e.as_timestamptz()),
92                    initialized_at_cluster_version: index.initialized_at_cluster_version.clone(),
93                    created_at_cluster_version: index.created_at_cluster_version.clone(),
94                }
95            })
96        })
97        .collect())
98}