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