From f12851c29378454d63a9c3c5477a5665331b46a7 Mon Sep 17 00:00:00 2001 From: Annie Pompa Date: Mon, 23 Oct 2023 18:36:39 -0400 Subject: [PATCH] sql: add information_schema._pg_char_octet_length builtin 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: #109603 Epic: none Release note (sql change): The `information_schema._pg_char_octet_length` builtin function is now supported, which improves compatibility with PostgreSQL. --- pkg/sql/sem/builtins/fixed_oids.go | 1 + pkg/sql/sem/builtins/pg_builtins.go | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/sql/sem/builtins/fixed_oids.go b/pkg/sql/sem/builtins/fixed_oids.go index 02e0e2c2d7a0..c9c7d7df313f 100644 --- a/pkg/sql/sem/builtins/fixed_oids.go +++ b/pkg/sql/sem/builtins/fixed_oids.go @@ -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 diff --git a/pkg/sql/sem/builtins/pg_builtins.go b/pkg/sql/sem/builtins/pg_builtins.go index c070d50ec998..4bf164e36889 100644 --- a/pkg/sql/sem/builtins/pg_builtins.go +++ b/pkg/sql/sem/builtins/pg_builtins.go @@ -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 @@ -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,