Skip to content

Commit

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

Needed for: cockroachdb#109603
Epic: none

Release note (sql change): The `pg_encoding_max_length`
builtin function is now supported, which improves compatibility with
PostgreSQL.
  • Loading branch information
annrpom committed Oct 24, 2023
1 parent 229a8d3 commit a7ae0c8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -952,4 +952,38 @@ attname typname information_schema._pg_interval_type
ubar interval NULL
ufoo interval NULL

statement ok
DROP TYPE u;

subtest end

subtest information_schema._pg_char_octet_length

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

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

statement ok
CREATE TYPE u AS (ufoo char, ubar int);

query TTI colnames
SELECT a.attname,
t.typname,
information_schema._pg_char_octet_length(a.atttypid,a.atttypmod)
FROM pg_attribute a
JOIN pg_type t
ON a.atttypid = t.oid
WHERE a.attname = 'ufoo'
ORDER BY a.attname
----
attname typname information_schema._pg_char_octet_length
ufoo bpchar 4

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 @@ -2489,6 +2489,7 @@ var builtinOidsArray = []string{
2518: `information_schema._pg_char_octet_length(typid: oid, typmod: int4) -> int`,
2519: `information_schema._pg_datetime_precision(typid: oid, typmod: int4) -> int`,
2520: `information_schema._pg_interval_type(typid: oid, typmod: int4) -> string`,
2521: `pg_encoding_max_length(encoding: int) -> int`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,27 @@ var pgBuiltins = map[string]builtinDefinition{
// Note that this function was removed from Postgres in version 10.
"pg_is_xlog_replay_paused": makeNotUsableFalseBuiltin(),

// pg_encoding_max_length returns the maximum length of a given encoding. For CRDB's use case,
// we only support UTF8; so, this will return the max_length of UTF8 - which is 4.
// https://github.com/postgres/postgres/blob/master/src/common/wchar.c
"pg_encoding_max_length": makeBuiltin(
tree.FunctionProperties{},
tree.Overload{
Types: tree.ParamTypes{{Name: "encoding", Typ: types.Int}},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
if cmp, err := args[0].CompareError(evalCtx, DatEncodingUTFId); err != nil {
return tree.DNull, err
} else if cmp == 0 {
return tree.NewDInt(4), nil
}
return tree.DNull, nil
},
Info: notUsableInfo,
Volatility: volatility.Immutable,
},
),

// Access Privilege Inquiry Functions allow users to query object access
// privileges programmatically. Each function has a number of variants,
// which differ based on their function signatures. These signatures have
Expand Down

0 comments on commit a7ae0c8

Please sign in to comment.