Skip to content

Commit

Permalink
sql: add information_schema._pg_datetime_precision builtin
Browse files Browse the repository at this point in the history
This commit adds an implementation for the
`information_schema._pg_datetime_precision` 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: cockroachdb#109603
Epic: none

Release note (sql change): The
`information_schema._pg_datetime_precision` builtin function is
now supported, which improves compatibility with PostgreSQL.
  • Loading branch information
annrpom committed Nov 6, 2023
1 parent 30de3e1 commit 7eb96a8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
47 changes: 43 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -892,14 +892,14 @@ subtest end
subtest information_schema._pg_char_octet_length

query I
SELECT information_schema._pg_char_octet_length(25, NULL)
SELECT information_schema._pg_char_octet_length(25, -1)
----
NULL
1073741824

query I
SELECT information_schema._pg_char_octet_length(25, -1)
SELECT information_schema._pg_char_octet_length(25, NULL)
----
1073741824
NULL

statement ok
CREATE TYPE u AS (ufoo char, ubar int);
Expand Down Expand Up @@ -935,3 +935,42 @@ SELECT pg_encoding_max_length(1)
NULL

subtest end

subtest information_schema._pg_datetime_precision

query I
select information_schema._pg_datetime_precision(1082, -1);
----
0

query I
select information_schema._pg_datetime_precision(1083, -1);
----
6

query I
select information_schema._pg_datetime_precision(1083, 5);
----
5

query I
select information_schema._pg_datetime_precision(1186, -1);
----
6

query I
select information_schema._pg_datetime_precision(1186, 5);
----
5

query I
select information_schema._pg_datetime_precision(1086, 5);
----
NULL

query I
select information_schema._pg_datetime_precision(1186, NULL);
----
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 @@ -2508,6 +2508,7 @@ var builtinOidsArray = []string{
2537: `percentile_disc_impl(arg1: float[], arg2: refcursor) -> refcursor[]`,
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`,
}

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

// NOTE: this could be defined as a user-defined function, like
// it is in Postgres:
// https://github.com/postgres/postgres/blob/master/src/backend/catalog/information_schema.sql
// CREATE FUNCTION _pg_datetime_precision(typid oid, typmod int4) RETURNS integer
// LANGUAGE sql
// IMMUTABLE
// PARALLEL SAFE
// RETURNS NULL ON NULL INPUT
// RETURN
// CASE WHEN $1 IN (1082) /* date */
// THEN 0
// WHEN $1 IN (1083, 1114, 1184, 1266) /* time, timestamp, same + tz */
// THEN CASE WHEN $2 < 0 THEN 6 ELSE $2 END
// WHEN $1 IN (1186) /* interval */
// THEN CASE WHEN $2 < 0 OR $2 & 0xFFFF = 0xFFFF THEN 6 ELSE $2 & 0xFFFF END
// ELSE null
// END;
"information_schema._pg_datetime_precision": 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.Int),
Fn: func(_ context.Context, _ *eval.Context, args tree.Datums) (tree.Datum, error) {
typid := args[0].(*tree.DOid).Oid
typmod := *args[1].(*tree.DInt)
if typid == oid.T_date {
return tree.DZero, nil
} else if typid == oid.T_time || typid == oid.T_timestamp || typid == oid.T_timestamptz || typid == oid.T_timetz {
if typmod < 0 {
return tree.NewDInt(6), nil
}
return tree.NewDInt(typmod), nil
} else if typid == oid.T_interval {
if typmod < 0 || (typmod&0xFFFF) == 0xFFFF {
return tree.NewDInt(6), nil
}
return tree.NewDInt(typmod & 0xFFFF), nil
}
return tree.DNull, nil
},
Info: notUsableInfo,
Volatility: volatility.Immutable,
},
),

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

0 comments on commit 7eb96a8

Please sign in to comment.