Skip to content

Commit

Permalink
sql: add information_schema._pg_char_octet_length builtin
Browse files Browse the repository at this point in the history
This commit adds an implementation for the
`information_schema._pg_char_octet_length` 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_char_octet_length`
builtin function is now supported, which improves compatibility with
PostgreSQL.
  • Loading branch information
annrpom committed Oct 31, 2023
1 parent f94626d commit f12851c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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 @@ -2486,6 +2486,7 @@ var builtinOidsArray = []string{
2515: `crdb_internal.privilege_name(internal_key: string) -> string`,
2516: `crdb_internal.privilege_name(internal_key: string[]) -> string[]`,
2517: `jsonb_array_to_string_array(input: jsonb) -> string[]`,
2518: `information_schema._pg_char_octet_length(typid: oid, typmod: int4) -> int`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down
26 changes: 25 additions & 1 deletion pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@ var pgBuiltins = map[string]builtinDefinition{
return tree.NewDInt(64), nil
case oid.T_numeric:
if typmod != -1 {
// This logics matches the postgres implementation
// This logic matches the postgres implementation
// of how to calculate the precision based on the typmod
// https://github.com/postgres/postgres/blob/d84ffffe582b8e036a14c6bc2378df29167f3a00/src/backend/catalog/information_schema.sql#L109
return tree.NewDInt(((typmod - 4) >> 16) & 65535), nil
Expand Down Expand Up @@ -2183,6 +2183,30 @@ var pgBuiltins = map[string]builtinDefinition{
},
),

// https://github.com/postgres/postgres/blob/master/src/backend/catalog/information_schema.sql
"information_schema._pg_char_octet_length": 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),
Body: `SELECT
CASE WHEN $1 IN (25, 1042, 1043) /* text, char, varchar */
THEN CASE WHEN $2 = -1 /* default typmod */
THEN CAST(2^30 AS integer)
ELSE information_schema._pg_char_max_length($1, $2) *
pg_catalog.pg_encoding_max_length((SELECT encoding FROM pg_catalog.pg_database WHERE datname = pg_catalog.current_database()))
END
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 f12851c

Please sign in to comment.