Skip to content

Commit

Permalink
Use separate configure option for unqualified schemas for types and p…
Browse files Browse the repository at this point in the history
…rocedures
  • Loading branch information
plcplc committed Jan 31, 2024
1 parent fe97bc9 commit 112e55d
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 29 deletions.
23 changes: 17 additions & 6 deletions crates/connectors/ndc-postgres/src/configuration/version2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ impl RawConfiguration {
}
}

pub fn default_unqualified_schemas() -> Vec<String> {
pub fn default_unqualified_schemas_for_tables() -> Vec<String> {
vec!["public".to_string()]
}

pub fn default_unqualified_schemas_for_types_and_procedures() -> Vec<String> {
vec![
// For the sake of having the user's tables to appear unqualified by default.
"public".to_string(),
// For the sake of having types and procedures appear unqualified by default.
"pg_catalog".to_string(),
"tiger".to_string(),
// "topology".to_string(),
]
}

Expand All @@ -71,8 +72,11 @@ pub struct ConfigureOptions {
pub excluded_schemas: Vec<String>,
/// The names of Tables and Views in these schemas will be returned unqualified.
/// The default setting will set the `public` schema as unqualified.
#[serde(default = "default_unqualified_schemas")]
#[serde(default = "default_unqualified_schemas_for_tables")]
pub unqualified_schemas: Vec<String>,
/// The types and procedures in these schemas will be returned unqualified.
#[serde(default = "default_unqualified_schemas_for_types_and_procedures")]
pub unqualified_schemas_for_types_and_procedures: Vec<String>,
/// The mapping of comparison operator names to apply when updating the configuration
#[serde(default = "default_comparison_operator_mapping")]
pub comparison_operator_mapping: Vec<version1::ComparisonOperatorMapping>,
Expand All @@ -93,7 +97,9 @@ impl Default for ConfigureOptions {
fn default() -> ConfigureOptions {
ConfigureOptions {
excluded_schemas: version1::default_excluded_schemas(),
unqualified_schemas: default_unqualified_schemas(),
unqualified_schemas: default_unqualified_schemas_for_tables(),
unqualified_schemas_for_types_and_procedures:
default_unqualified_schemas_for_types_and_procedures(),
comparison_operator_mapping: version1::default_comparison_operator_mapping(),
// we'll change this to `Some(MutationsVersions::V1)` when we
// want to "release" this behaviour
Expand Down Expand Up @@ -268,6 +274,11 @@ pub async fn configure(
let query = sqlx::query(CONFIGURATION_QUERY)
.bind(args.configure_options.excluded_schemas.clone())
.bind(args.configure_options.unqualified_schemas.clone())
.bind(
args.configure_options
.unqualified_schemas_for_types_and_procedures
.clone(),
)
.bind(
serde_json::to_value(args.configure_options.comparison_operator_mapping.clone())
.map_err(|e| connector::UpdateConfigurationError::Other(e.into()))?,
Expand Down
48 changes: 30 additions & 18 deletions crates/connectors/ndc-postgres/src/configuration/version2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,29 @@ WITH
NOT (ns.nspname = ANY ($1))
),

-- These are the schemas of which tables will be unqualified
unqualified_schemas_for_tables AS
(
SELECT DISTINCT
schema_name,
ns.oid as schema_id
FROM
UNNEST($2) AS t(schema_name)
INNER JOIN
pg_namespace
AS ns
ON (ns.nspname = schema_name)
),

-- These are the schemas of which types and procedures will be
-- exported unqualified.
unqualified_schemas AS
unqualified_schemas_for_types_and_procedures AS
(
SELECT DISTINCT
schema_name,
ns.oid as schema_id
FROM
-- Unless 'pg_catalog' (added as a default during version-2) is
-- considered a schema to use unqualified we won't get access to any
-- built-in types or procedures.
-- Therefore it has been put here to preserve behavior. It should be
-- removed once we cut 'version3.sql'.
UNNEST($2 || '{"pg_catalog"}'::text[]) AS t(schema_name)
UNNEST($3) AS t(schema_name)
INNER JOIN
pg_namespace
AS ns
Expand Down Expand Up @@ -195,8 +204,8 @@ WITH
INNER JOIN
-- Until the schema is made part of our model of types we only consider
-- those defined in the public schema.
unqualified_schemas
ON (t.typnamespace = unqualified_schemas.schema_id)
unqualified_schemas_for_types_and_procedures as q
ON (t.typnamespace = q.schema_id)
WHERE
-- We currently filter out pseudo (polymorphic) types, because our schema
-- can only deal with monomorphic types.
Expand Down Expand Up @@ -271,7 +280,7 @@ WITH
INNER JOIN
-- Until the schema is made part of our model of types we only consider
-- types defined in the public schema.
unqualified_schemas
unqualified_schemas_for_types_and_procedures
USING (schema_id)
WHERE
-- See 'scalar_types' above
Expand Down Expand Up @@ -374,8 +383,9 @@ WITH
INNER JOIN
-- Until the schema is made part of our model of types we only consider
-- types defined in the public schema.
unqualified_schemas
on (unqualified_schemas.schema_id = proc.pronamespace)
unqualified_schemas_for_types_and_procedures
AS q
ON (q.schema_id = proc.pronamespace)
WHERE
ret_type.type_id = 'pg_catalog.bool'::regtype
-- We check that we only consider procedures which take two regular
Expand Down Expand Up @@ -409,7 +419,7 @@ WITH
-- Include only procedures that are explicitly selected.
-- This is controlled by the
-- 'introspectPrefixFunctionComparisonOperators' configuration option.
operator_name = ANY ($4)
operator_name = ANY ($5)
),

-- Operators are recorded across 'pg_proc', pg_operator, and 'pg_aggregate', see
Expand Down Expand Up @@ -445,8 +455,9 @@ WITH
INNER JOIN
-- Until the schema is made part of our model of operators we only consider
-- those defined in the public schema.
unqualified_schemas
ON (unqualified_schemas.schema_id = op.oprnamespace)
unqualified_schemas_for_types_and_procedures
AS q
ON (q.schema_id = op.oprnamespace)
WHERE
t_res.type_id = 'pg_catalog.bool'::regtype
ORDER BY op.oprname
Expand Down Expand Up @@ -694,7 +705,7 @@ WITH
v ->> 'operatorName' AS operator_name,
v ->> 'exposedName' AS exposed_name
FROM
jsonb_array_elements($3) AS v
jsonb_array_elements($4) AS v
),

-- Constraints are recorded in 'pg_constraint', see
Expand Down Expand Up @@ -814,7 +825,7 @@ FROM
SELECT
jsonb_object_agg(
CASE
WHEN unqualified_schemas.schema_id IS NOT NULL
WHEN unqualified_schemas_for_tables.schema_id IS NOT NULL
THEN rel.relation_name
ELSE s.schema_name || '_' || rel.relation_name
END,
Expand All @@ -839,7 +850,7 @@ FROM
AS rel

LEFT OUTER JOIN
unqualified_schemas
unqualified_schemas_for_tables
USING (schema_id)

LEFT OUTER JOIN
Expand Down Expand Up @@ -1134,6 +1145,7 @@ FROM
--
-- EXECUTE configuration(
-- '{"information_schema", "tiger", "pg_catalog", "topology"}'::varchar[],
-- '{"public"}'::varchar[],
-- '{"public", "pg_catalog", "tiger"}'::varchar[],
-- '[
-- {"operatorName": "=", "exposedName": "_eq"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ expression: schema
"columnar_internal"
],
"unqualifiedSchemas": [
"public"
],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
Expand Down Expand Up @@ -1326,6 +1329,16 @@ expression: schema
},
"unqualifiedSchemas": {
"description": "The names of Tables and Views in these schemas will be returned unqualified. The default setting will set the `public` schema as unqualified.",
"default": [
"public"
],
"type": "array",
"items": {
"type": "string"
}
},
"unqualifiedSchemasForTypesAndProcedures": {
"description": "The types and procedures in these schemas will be returned unqualified.",
"default": [
"public",
"pg_catalog",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ expression: schema
"columnar_internal"
],
"unqualifiedSchemas": [
"public"
],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
Expand Down Expand Up @@ -820,6 +823,16 @@ expression: schema
},
"unqualifiedSchemas": {
"description": "The names of Tables and Views in these schemas will be returned unqualified. The default setting will set the `public` schema as unqualified.",
"default": [
"public"
],
"type": "array",
"items": {
"type": "string"
}
},
"unqualifiedSchemasForTypesAndProcedures": {
"description": "The types and procedures in these schemas will be returned unqualified.",
"default": [
"public",
"pg_catalog",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,9 @@ expression: default_configuration
"columnar_internal"
],
"unqualifiedSchemas": [
"public"
],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ expression: generated_schema_json
"columnar_internal"
],
"unqualifiedSchemas": [
"public"
],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
Expand Down Expand Up @@ -1292,6 +1295,16 @@ expression: generated_schema_json
},
"unqualifiedSchemas": {
"description": "The names of Tables and Views in these schemas will be returned unqualified. The default setting will set the `public` schema as unqualified.",
"default": [
"public"
],
"type": "array",
"items": {
"type": "string"
}
},
"unqualifiedSchemasForTypesAndProcedures": {
"description": "The types and procedures in these schemas will be returned unqualified.",
"default": [
"public",
"pg_catalog",
Expand Down
7 changes: 6 additions & 1 deletion static/aurora/v2-chinook-ndc-metadata-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,12 @@
"columnar",
"columnar_internal"
],
"unqualifiedSchemas": ["public", "pg_catalog", "tiger"],
"unqualifiedSchemas": ["public"],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
],
"comparisonOperatorMapping": [
{
"operatorName": "=",
Expand Down
7 changes: 6 additions & 1 deletion static/citus/v2-chinook-ndc-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,12 @@
"columnar",
"columnar_internal"
],
"unqualifiedSchemas": ["public", "pg_catalog", "tiger"],
"unqualifiedSchemas": ["public"],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
],
"comparisonOperatorMapping": [
{
"operatorName": "=",
Expand Down
7 changes: 6 additions & 1 deletion static/cockroach/v2-chinook-ndc-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,12 @@
"columnar",
"columnar_internal"
],
"unqualifiedSchemas": ["public", "pg_catalog", "tiger"],
"unqualifiedSchemas": ["public"],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
],
"comparisonOperatorMapping": [
{
"operatorName": "=",
Expand Down
7 changes: 6 additions & 1 deletion static/postgres/v2-chinook-ndc-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2773,7 +2773,12 @@
"columnar",
"columnar_internal"
],
"unqualifiedSchemas": ["public", "pg_catalog", "tiger"],
"unqualifiedSchemas": ["public"],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
],
"comparisonOperatorMapping": [
{
"operatorName": "=",
Expand Down
7 changes: 6 additions & 1 deletion static/yugabyte/v2-chinook-ndc-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,12 @@
"columnar",
"columnar_internal"
],
"unqualifiedSchemas": ["public", "pg_catalog", "tiger"],
"unqualifiedSchemas": ["public"],
"unqualifiedSchemasForTypesAndProcedures": [
"public",
"pg_catalog",
"tiger"
],
"comparisonOperatorMapping": [
{
"operatorName": "=",
Expand Down

0 comments on commit 112e55d

Please sign in to comment.