Skip to content

Add support for introspecting domain types #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [Unreleased]

### Added

- Support for introspecting domain types.
([#380](https://github.com/hasura/ndc-postgres/pull/380))

### Changed

### Fixed

## [v0.5.1] - 2024-03-21

### Added
Expand Down
47 changes: 44 additions & 3 deletions crates/configuration/src/version3/version3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-- query with arguments set.

-- DEALLOCATE ALL; -- Or use 'DEALLOCATE configuration' between reloads
-- PREPARE configuration(varchar[], varchar[], jsonb, varchar[]) AS
-- PREPARE configuration(varchar[], varchar[], varchar[], jsonb, varchar[]) AS

WITH
-- The overall structure of this query is a CTE (i.e. 'WITH .. SELECT')
Expand Down Expand Up @@ -215,12 +215,15 @@ WITH
-- query execution. If we export them as opaque scalar types, adding
-- proper support later becomes a breaking change, which we'd like to
-- avoid.
--
-- We intentionally do not filter out domain types, see the relation
-- `domain_types` below.
t.typtype NOT IN
(
-- Interesting t.typtype 'types of types':
-- 'b' for base type
'c', --for composite type
-- 'd' for domain (a predicate-restricted version of a type)
'c', -- for composite type
-- 'd', for domain (a predicate-restricted version of a type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird comma.

-- 'e' for enum
'p' -- for pseudo-type (anyelement etc)
-- 'r' for range
Expand Down Expand Up @@ -263,6 +266,36 @@ WITH
'xid8'
)
),
-- Domain types are scalar types that have been adorned with a CHECK
-- expression that any instance of the type must satisfy.
--
-- While the `scalar_types` relation above does pick up on domain types as
-- well we also need to keep track of them separately in order to be able to
-- infer comparison operators and aggregation functions.
--
-- Domain types are created using the `CREATE DOMAIN` statement (see
-- https://www.postgresql.org/docs/current/sql-createdomain.html).
domain_types AS
(
SELECT
t.oid AS type_id,
t.typnamespace AS schema_id,
t.typname AS type_name,
base_type.type_name AS base_type
FROM
pg_catalog.pg_type AS t
INNER JOIN
-- Until the schema is made part of our model of types we only consider
-- those defined in the public schema.
unqualified_schemas_for_types_and_procedures as q
ON (t.typnamespace = q.schema_id)
INNER JOIN
scalar_types
AS base_type
ON (base_type.type_id = t.typbasetype)
WHERE
t.typtype = 'd'
),
array_types AS
(
SELECT
Expand Down Expand Up @@ -505,6 +538,14 @@ WITH
('geometry', 'text'),
('text', 'geometry')
)
UNION
-- Any domain type may be implicitly cast to its base type, even though these casts
-- are not declared in `pg_cast`.
SELECT
domain_types.type_name as from_type,
domain_types.base_type as to_type
FROM
domain_types
),

-- Some comparison operators are not defined explicitly for every type they would be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,52 @@ expression: result
}
}
},
"even_number": {
"aggregate_functions": {},
"comparison_operators": {
"_eq": {
"type": "equal"
},
"_gt": {
"type": "custom",
"argument_type": {
"type": "named",
"name": "even_number"
}
},
"_gte": {
"type": "custom",
"argument_type": {
"type": "named",
"name": "even_number"
}
},
"_in": {
"type": "in"
},
"_lt": {
"type": "custom",
"argument_type": {
"type": "named",
"name": "even_number"
}
},
"_lte": {
"type": "custom",
"argument_type": {
"type": "named",
"name": "even_number"
}
},
"_neq": {
"type": "custom",
"argument_type": {
"type": "named",
"name": "even_number"
}
}
}
},
"float4": {
"aggregate_functions": {
"avg": {
Expand Down Expand Up @@ -2198,6 +2244,16 @@ expression: result
}
}
},
"even_numbers": {
"fields": {
"the_number": {
"type": {
"type": "named",
"name": "even_number"
}
}
}
},
"insert_album": {
"fields": {
"AlbumId": {
Expand Down Expand Up @@ -3338,6 +3394,38 @@ expression: result
}
}
},
"v1_insert_even_numbers_object": {
"fields": {
"the_number": {
"type": {
"type": "named",
"name": "even_number"
}
}
}
},
"v1_insert_even_numbers_response": {
"description": "Responses from the 'v1_insert_even_numbers' procedure",
"fields": {
"affected_rows": {
"description": "The number of rows affected by the mutation",
"type": {
"type": "named",
"name": "int4"
}
},
"returning": {
"description": "Data from rows affected by the mutation",
"type": {
"type": "array",
"element_type": {
"type": "named",
"name": "even_numbers"
}
}
}
}
},
"value_types": {
"fields": {
"bool": {
Expand Down Expand Up @@ -3708,6 +3796,13 @@ expression: result
}
}
},
{
"name": "even_numbers",
"arguments": {},
"type": "even_numbers",
"uniqueness_constraints": {},
"foreign_keys": {}
},
{
"name": "address_identity_function",
"description": "A native query used to test support for composite types",
Expand Down Expand Up @@ -4465,6 +4560,22 @@ expression: result
"type": "named",
"name": "v1_insert_Track_response"
}
},
{
"name": "v1_insert_even_numbers",
"description": "Insert into the even_numbers table",
"arguments": {
"_object": {
"type": {
"type": "named",
"name": "v1_insert_even_numbers_object"
}
}
},
"result_type": {
"type": "named",
"name": "v1_insert_even_numbers_response"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,23 @@ expression: default_configuration
"foreignRelations": {},
"description": null
},
"even_numbers": {
"schemaName": "public",
"tableName": "even_numbers",
"columns": {
"the_number": {
"name": "the_number",
"type": {
"scalarType": "even_number"
},
"nullable": "nonNullable",
"description": null
}
},
"uniquenessConstraints": {},
"foreignRelations": {},
"description": null
},
"spatial_ref_sys": {
"schemaName": "public",
"tableName": "spatial_ref_sys",
Expand Down Expand Up @@ -1301,6 +1318,50 @@ expression: default_configuration
"isInfix": true
}
},
"even_number": {
"_eq": {
"operatorName": "=",
"operatorKind": "equal",
"argumentType": "even_number",
"isInfix": true
},
"_gt": {
"operatorName": ">",
"operatorKind": "custom",
"argumentType": "even_number",
"isInfix": true
},
"_gte": {
"operatorName": ">=",
"operatorKind": "custom",
"argumentType": "even_number",
"isInfix": true
},
"_in": {
"operatorName": "IN",
"operatorKind": "in",
"argumentType": "even_number",
"isInfix": true
},
"_lt": {
"operatorName": "<",
"operatorKind": "custom",
"argumentType": "even_number",
"isInfix": true
},
"_lte": {
"operatorName": "<=",
"operatorKind": "custom",
"argumentType": "even_number",
"isInfix": true
},
"_neq": {
"operatorName": "<>",
"operatorKind": "custom",
"argumentType": "even_number",
"isInfix": true
}
},
"float8": {
"_eq": {
"operatorName": "=",
Expand Down
Loading
Loading