risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_indexes.rs

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