risingwave_frontend/catalog/system_catalog/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod information_schema;
pub mod pg_catalog;
pub mod rw_catalog;

use std::collections::HashMap;
use std::sync::{Arc, LazyLock};

use futures::stream::BoxStream;
use itertools::Itertools;
use parking_lot::RwLock;
use risingwave_common::acl::AclMode;
use risingwave_common::array::DataChunk;
use risingwave_common::catalog::{
    ColumnCatalog, ColumnDesc, Field, SysCatalogReader, TableDesc, TableId, DEFAULT_SUPER_USER_ID,
    MAX_SYS_CATALOG_NUM, SYS_CATALOG_START_ID,
};
use risingwave_common::error::BoxedError;
use risingwave_common::session_config::SessionConfig;
use risingwave_common::system_param::local_manager::SystemParamsReaderRef;
use risingwave_common::types::DataType;
use risingwave_pb::meta::list_table_fragment_states_response::TableFragmentState;
use risingwave_pb::meta::table_parallelism::{PbFixedParallelism, PbParallelism};
use risingwave_pb::user::grant_privilege::Object;

use crate::catalog::catalog_service::CatalogReader;
use crate::catalog::view_catalog::ViewCatalog;
use crate::meta_client::FrontendMetaClient;
use crate::session::AuthContext;
use crate::user::user_catalog::UserCatalog;
use crate::user::user_privilege::available_prost_privilege;
use crate::user::user_service::UserInfoReader;
use crate::user::UserId;

#[derive(Clone, Debug, PartialEq)]
pub struct SystemTableCatalog {
    pub id: TableId,

    pub name: String,

    // All columns in this table.
    pub columns: Vec<ColumnCatalog>,

    /// Primary key columns indices.
    pub pk: Vec<usize>,

    // owner of table, should always be default super user, keep it for compatibility.
    pub owner: u32,

    /// description of table, set by `comment on`.
    pub description: Option<String>,
}

impl SystemTableCatalog {
    /// Get a reference to the system catalog's table id.
    pub fn id(&self) -> TableId {
        self.id
    }

    pub fn with_id(mut self, id: TableId) -> Self {
        self.id = id;
        self
    }

    /// Get a reference to the system catalog's columns.
    pub fn columns(&self) -> &[ColumnCatalog] {
        &self.columns
    }

    /// Get a [`TableDesc`] of the system table.
    pub fn table_desc(&self) -> TableDesc {
        TableDesc {
            table_id: self.id,
            columns: self.columns.iter().map(|c| c.column_desc.clone()).collect(),
            stream_key: self.pk.clone(),
            ..Default::default()
        }
    }

    /// Get a reference to the system catalog's name.
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }
}

pub struct SysCatalogReaderImpl {
    // Read catalog info: database/schema/source/table.
    catalog_reader: CatalogReader,
    // Read user info.
    user_info_reader: UserInfoReader,
    // Read from meta.
    meta_client: Arc<dyn FrontendMetaClient>,
    // Read auth context.
    auth_context: Arc<AuthContext>,
    // Read config.
    config: Arc<RwLock<SessionConfig>>,
    // Read system params.
    system_params: SystemParamsReaderRef,
}

impl SysCatalogReaderImpl {
    pub fn new(
        catalog_reader: CatalogReader,
        user_info_reader: UserInfoReader,
        meta_client: Arc<dyn FrontendMetaClient>,
        auth_context: Arc<AuthContext>,
        config: Arc<RwLock<SessionConfig>>,
        system_params: SystemParamsReaderRef,
    ) -> Self {
        Self {
            catalog_reader,
            user_info_reader,
            meta_client,
            auth_context,
            config,
            system_params,
        }
    }
}

pub struct BuiltinTable {
    name: &'static str,
    schema: &'static str,
    columns: Vec<SystemCatalogColumnsDef<'static>>,
    pk: &'static [usize],
    function: for<'a> fn(&'a SysCatalogReaderImpl) -> BoxStream<'a, Result<DataChunk, BoxedError>>,
}

pub struct BuiltinView {
    name: &'static str,
    schema: &'static str,
    columns: Vec<SystemCatalogColumnsDef<'static>>,
    sql: String,
}

pub enum BuiltinCatalog {
    Table(BuiltinTable),
    View(BuiltinView),
}

impl BuiltinCatalog {
    fn full_name(&self) -> String {
        match self {
            BuiltinCatalog::Table(t) => format!("{}.{}", t.schema, t.name),
            BuiltinCatalog::View(t) => format!("{}.{}", t.schema, t.name),
        }
    }
}

impl From<&BuiltinTable> for SystemTableCatalog {
    fn from(val: &BuiltinTable) -> Self {
        SystemTableCatalog {
            id: TableId::placeholder(),
            name: val.name.to_string(),
            columns: val
                .columns
                .iter()
                .enumerate()
                .map(|(idx, (name, ty))| ColumnCatalog {
                    column_desc: ColumnDesc::new_atomic(ty.clone(), name, idx as i32),
                    is_hidden: false,
                })
                .collect(),
            pk: val.pk.to_vec(),
            owner: DEFAULT_SUPER_USER_ID,
            description: None,
        }
    }
}

impl From<&BuiltinView> for ViewCatalog {
    fn from(val: &BuiltinView) -> Self {
        ViewCatalog {
            id: 0,
            name: val.name.to_string(),
            columns: val
                .columns
                .iter()
                .map(|(name, ty)| Field::with_name(ty.clone(), name.to_string()))
                .collect(),
            sql: val.sql.clone(),
            owner: DEFAULT_SUPER_USER_ID,
            properties: Default::default(),
        }
    }
}

// TODO: support struct column and type name when necessary.
pub(super) type SystemCatalogColumnsDef<'a> = (&'a str, DataType);

/// `infer_dummy_view_sql` returns a dummy SQL statement for a view with the given columns that
/// returns no rows. For example, with columns `a` and `b`, it returns `SELECT NULL::integer AS a,
/// NULL::varchar AS b WHERE 1 != 1`.
// FIXME(noel): Tracked by <https://github.com/risingwavelabs/risingwave/issues/3431#issuecomment-1164160988>
#[inline(always)]
pub fn infer_dummy_view_sql(columns: &[SystemCatalogColumnsDef<'_>]) -> String {
    format!(
        "SELECT {} WHERE 1 != 1",
        columns
            .iter()
            .map(|(name, ty)| format!("NULL::{} AS {}", ty, name))
            .join(", ")
    )
}

fn extract_parallelism_from_table_state(state: &TableFragmentState) -> String {
    match state
        .parallelism
        .as_ref()
        .and_then(|parallelism| parallelism.parallelism.as_ref())
    {
        Some(PbParallelism::Auto(_)) | Some(PbParallelism::Adaptive(_)) => "adaptive".to_string(),
        Some(PbParallelism::Fixed(PbFixedParallelism { parallelism })) => {
            format!("fixed({parallelism})")
        }
        Some(PbParallelism::Custom(_)) => "custom".to_string(),
        None => "unknown".to_string(),
    }
}

/// get acl items of `object` in string, ignore public.
fn get_acl_items(
    object: &Object,
    for_dml_table: bool,
    users: &Vec<UserCatalog>,
    username_map: &HashMap<UserId, String>,
) -> Vec<String> {
    let mut res = vec![];
    let super_privilege = available_prost_privilege(*object, for_dml_table);
    for user in users {
        let privileges = if user.is_super {
            vec![&super_privilege]
        } else {
            user.grant_privileges
                .iter()
                .filter(|&privilege| privilege.object.as_ref().unwrap() == object)
                .collect_vec()
        };
        if privileges.is_empty() {
            continue;
        };
        let mut grantor_map = HashMap::new();
        privileges.iter().for_each(|&privilege| {
            privilege.action_with_opts.iter().for_each(|ao| {
                grantor_map
                    .entry(ao.granted_by)
                    .or_insert_with(Vec::new)
                    .push((ao.get_action().unwrap(), ao.with_grant_option));
            })
        });
        for (granted_by, actions) in grantor_map {
            let mut aclitem = String::new();
            aclitem.push_str(&user.name);
            aclitem.push('=');
            for (action, option) in actions {
                aclitem.push_str(&AclMode::from(action).to_string());
                if option {
                    aclitem.push('*');
                }
            }
            aclitem.push('/');
            // should be able to query grantor's name
            aclitem.push_str(username_map.get(&granted_by).unwrap());
            res.push(aclitem);
        }
    }
    res
}

pub struct SystemCatalog {
    // table id = index + SYS_CATALOG_START_ID
    catalogs: Vec<BuiltinCatalog>,
}

pub fn get_sys_tables_in_schema(schema_name: &str) -> Vec<Arc<SystemTableCatalog>> {
    SYS_CATALOGS
        .catalogs
        .iter()
        .enumerate()
        .filter_map(|(idx, c)| match c {
            BuiltinCatalog::Table(t) if t.schema == schema_name => Some(Arc::new(
                SystemTableCatalog::from(t)
                    .with_id((idx as u32 + SYS_CATALOG_START_ID as u32).into()),
            )),
            _ => None,
        })
        .collect()
}

pub fn get_sys_views_in_schema(schema_name: &str) -> Vec<Arc<ViewCatalog>> {
    SYS_CATALOGS
        .catalogs
        .iter()
        .enumerate()
        .filter_map(|(idx, c)| match c {
            BuiltinCatalog::View(v) if v.schema == schema_name => Some(Arc::new(
                ViewCatalog::from(v).with_id(idx as u32 + SYS_CATALOG_START_ID as u32),
            )),
            _ => None,
        })
        .collect()
}

/// The global registry of all builtin catalogs.
pub static SYS_CATALOGS: LazyLock<SystemCatalog> = LazyLock::new(|| {
    tracing::info!("found {} catalogs", SYS_CATALOGS_SLICE.len());
    assert!(SYS_CATALOGS_SLICE.len() <= MAX_SYS_CATALOG_NUM as usize);
    let catalogs = SYS_CATALOGS_SLICE
        .iter()
        .map(|f| f())
        .sorted_by_key(|c| c.full_name())
        .collect();
    SystemCatalog { catalogs }
});

#[linkme::distributed_slice]
pub static SYS_CATALOGS_SLICE: [fn() -> BuiltinCatalog];

impl SysCatalogReader for SysCatalogReaderImpl {
    fn read_table(&self, table_id: TableId) -> BoxStream<'_, Result<DataChunk, BoxedError>> {
        let table_name = SYS_CATALOGS
            .catalogs
            .get((table_id.table_id - SYS_CATALOG_START_ID as u32) as usize)
            .unwrap();
        match table_name {
            BuiltinCatalog::Table(t) => (t.function)(self),
            BuiltinCatalog::View(_) => panic!("read_table should not be called on a view"),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::catalog::system_catalog::SYS_CATALOGS;
    use crate::test_utils::LocalFrontend;

    #[tokio::test]
    async fn test_builtin_view_definition() {
        let frontend = LocalFrontend::new(Default::default()).await;
        let sqls = SYS_CATALOGS.catalogs.iter().filter_map(|c| match c {
            super::BuiltinCatalog::View(v) => Some(v.sql.clone()),
            _ => None,
        });
        for sql in sqls {
            frontend.query_formatted_result(sql).await;
        }
    }
}