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                };
55                RwIndex {
56                    id: index.id.index_id as i32,
57                    name: index.name.clone(),
58                    primary_table_id: index.primary_table.id().table_id as i32,
59                    key_columns: index
60                        .index_item
61                        .iter()
62                        .take(index_columns_len as usize)
63                        .map(|index| {
64                            let ind = if let Some(input_ref) = index.as_input_ref() {
65                                input_ref.index() + 1
66                            } else {
67                                0
68                            };
69                            ind as i16
70                        })
71                        .collect(),
72                    include_columns: index
73                        .index_item
74                        .iter()
75                        .skip(index_columns_len as usize)
76                        .map(|index| {
77                            let ind = if let Some(input_ref) = index.as_input_ref() {
78                                input_ref.index() + 1
79                            } else {
80                                0
81                            };
82                            ind as i16
83                        })
84                        .collect(),
85                    schema_id: schema.id() as i32,
86                    owner: index.index_table().owner as i32,
87                    definition: index_table.create_sql(),
88                    acl: vec![],
89                    initialized_at: index.initialized_at_epoch.map(|e| e.as_timestamptz()),
90                    created_at: index.created_at_epoch.map(|e| e.as_timestamptz()),
91                    initialized_at_cluster_version: index.initialized_at_cluster_version.clone(),
92                    created_at_cluster_version: index.created_at_cluster_version.clone(),
93                }
94            })
95        })
96        .collect())
97}