Skip to content

Commit

Permalink
sql: add information_schema._pg_interval_type builtin
Browse files Browse the repository at this point in the history
This commit adds an implementation for the
`information_schema._pg_interval_type` builtin.

The builtin is implemented as a user-defined function in Postgres
[here](https://github.com/postgres/postgres/blob/master/src/backend/catalog/information_schema.sql)

Needed for: #109603
Epic: none

Release note (sql change): The `information_schema._pg_interval_type`
builtin function is now supported, which improves compatibility with
PostgreSQL.
  • Loading branch information
annrpom committed Nov 6, 2023
1 parent 7eb96a8 commit 6581b36
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,29 @@ select information_schema._pg_datetime_precision(1186, NULL);
NULL

subtest end

subtest information_schema._pg_interval_type

query T
SELECT information_schema._pg_interval_type(1, 0);
----
NULL

statement ok
CREATE TYPE u AS (ufoo interval, ubar interval HOUR TO MINUTE);

query TTT colnames
SELECT a.attname,
t.typname,
information_schema._pg_interval_type(a.atttypid, a.atttypmod)
FROM pg_attribute a
JOIN pg_type t
ON a.atttypid = t.oid
WHERE a.attname IN ('ufoo', 'ubar')
ORDER BY a.attname
----
attname typname information_schema._pg_interval_type
ubar interval NULL
ufoo interval NULL

subtest end
1 change: 1 addition & 0 deletions pkg/sql/sem/builtins/fixed_oids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,7 @@ var builtinOidsArray = []string{
2538: `information_schema._pg_char_octet_length(typid: oid, typmod: int4) -> int`,
2539: `pg_encoding_max_length(encoding: int) -> int`,
2540: `information_schema._pg_datetime_precision(typid: oid, typmod: int4) -> int`,
2541: `information_schema._pg_interval_type(typid: oid, typmod: int4) -> string`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,26 @@ var pgBuiltins = map[string]builtinDefinition{
},
),

// https://github.com/postgres/postgres/blob/master/src/backend/catalog/information_schema.sql
"information_schema._pg_interval_type": makeBuiltin(tree.FunctionProperties{Category: builtinconstants.CategorySystemInfo},
tree.Overload{
Types: tree.ParamTypes{
{Name: "typid", Typ: types.Oid},
{Name: "typmod", Typ: types.Int4},
},
ReturnType: tree.FixedReturnType(types.String),
Body: `SELECT
CASE WHEN $1 IN (1186) /* interval */
THEN pg_catalog.upper(substring(pg_catalog.format_type($1, $2), 'interval[()0-9]* #"%#"', '#'))
ELSE null
END`,
Info: notUsableInfo,
Volatility: volatility.Immutable,
CalledOnNullInput: true,
Language: tree.RoutineLangSQL,
},
),

"nameconcatoid": makeBuiltin(
tree.FunctionProperties{
Category: builtinconstants.CategorySystemInfo,
Expand Down

0 comments on commit 6581b36

Please sign in to comment.