Skip to content

Commit

Permalink
feat: support information_schema.table_constraints (#18716)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzl25 authored Sep 26, 2024
1 parent b3765ce commit 375e933
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
28 changes: 14 additions & 14 deletions e2e_test/batch/catalog/pg_class.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ SELECT oid,relname,relowner,relkind FROM pg_catalog.pg_class ORDER BY oid limit
----
2147478647 columns 1 v
2147478648 schemata 1 v
2147478649 tables 1 v
2147478650 views 1 v
2147478651 pg_am 1 v
2147478652 pg_attrdef 1 v
2147478653 pg_attribute 1 v
2147478654 pg_auth_members 1 v
2147478655 pg_cast 1 r
2147478656 pg_class 1 v
2147478657 pg_collation 1 v
2147478658 pg_constraint 1 r
2147478659 pg_conversion 1 v
2147478660 pg_database 1 v
2147478661 pg_depend 1 v
2147478649 table_constraints 1 v
2147478650 tables 1 v
2147478651 views 1 v
2147478652 pg_am 1 v
2147478653 pg_attrdef 1 v
2147478654 pg_attribute 1 v
2147478655 pg_auth_members 1 v
2147478656 pg_cast 1 r
2147478657 pg_class 1 v
2147478658 pg_collation 1 v
2147478659 pg_constraint 1 r
2147478660 pg_conversion 1 v
2147478661 pg_database 1 v

query ITIT
SELECT oid,relname,relowner,relkind FROM pg_catalog.pg_class WHERE oid = 'pg_namespace'::regclass;
----
2147478671 pg_namespace 1 v
2147478672 pg_namespace 1 v
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@

mod columns;
mod schemata;
mod table_constraints;
mod tables;
mod views;
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.

use risingwave_common::types::Fields;
use risingwave_frontend_macro::system_catalog;

/// The view `table_constraints` contains all constraints belonging to tables that the current user owns or has some privilege other than SELECT on.
/// Ref: [`https://www.postgresql.org/docs/current/infoschema-table-constraints.html`]
/// Limitation:
/// This view assume the constraint schema is the same as the table schema, since `pg_clatalog`.`pg_constraint` only support primrary key.
#[system_catalog(
view,
"information_schema.table_constraints",
"SELECT CURRENT_DATABASE() AS constraint_catalog,
pg_namespace.nspname AS constraint_schema,
pg_constraint.conname AS constraint_name,
CURRENT_DATABASE() AS table_catalog,
pg_namespace.nspname AS table_schema,
pg_class.relname AS table_name,
CASE
WHEN contype = 'p' THEN 'PRIMARY KEY'
WHEN contype = 'u' THEN 'UNIQUE'
WHEN contype = 'c' THEN 'CHECK'
WHEN contype = 'x' THEN 'EXCLUDE'
ELSE contype
END AS constraint_type,
CASE
WHEN condeferrable THEN 'YES'
ELSE 'NO'
END AS is_deferrable,
'NO' AS initially_deferred,
CASE
WHEN convalidated THEN 'YES'
ELSE 'NO'
END AS enforced
FROM pg_catalog.pg_constraint
JOIN pg_catalog.pg_class ON pg_constraint.conrelid = pg_class.oid
JOIN rw_catalog.rw_relations ON rw_relations.id = pg_class.oid
JOIN pg_catalog.pg_namespace ON pg_class.relnamespace = pg_namespace.oid
WHERE rw_relations.relation_type != 'table' or (rw_relations.relation_type = 'table' and has_table_privilege(pg_constraint.conrelid, 'INSERT, UPDATE, DELETE'))
ORDER BY constraint_catalog, constraint_schema, constraint_name"
)]
#[derive(Fields)]
struct TableConstraints {
constraint_catalog: String,
constraint_schema: String,
constraint_name: String,
table_catalog: String,
table_schema: String,
table_name: String,
constraint_type: String,
is_deferrable: String,
initially_deferred: String,
enforced: String,
}

0 comments on commit 375e933

Please sign in to comment.