From e17cd689557e7253dfa191a9738cb8f1467977db Mon Sep 17 00:00:00 2001 From: abhishek20123g Date: Sat, 15 Feb 2020 06:48:16 +0530 Subject: [PATCH] sql: add the getdatabaseencoding() builtin function Resolves https://github.com/cockroachdb/cockroach/issues/41771. This commit adds builtin function, getdatabaseencoding(), and the unit-test case for it. Release note (sql change): This PR is introduced to add builtin function, getdatabaseencoding(), which returns the current encoding name used by the database. --- docs/generated/sql/functions.md | 2 ++ .../testdata/logic_test/builtin_function | 5 +++++ pkg/sql/sem/builtins/builtins.go | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/docs/generated/sql/functions.md b/docs/generated/sql/functions.md index 385c9bb4e5e2..111cd8b2576b 100644 --- a/docs/generated/sql/functions.md +++ b/docs/generated/sql/functions.md @@ -1123,6 +1123,8 @@ SELECT * FROM crdb_internal.check_consistency(true, ‘\x02’, ‘\x04’)

current_user() → string

Returns the current user. This function is provided for compatibility with PostgreSQL.

+getdatabaseencoding() → string

Returns the current encoding name used by the database.

+
version() → string

Returns the node’s version of CockroachDB.

diff --git a/pkg/sql/logictest/testdata/logic_test/builtin_function b/pkg/sql/logictest/testdata/logic_test/builtin_function index 283c3e62b9e8..d5fc283e3d27 100644 --- a/pkg/sql/logictest/testdata/logic_test/builtin_function +++ b/pkg/sql/logictest/testdata/logic_test/builtin_function @@ -2556,3 +2556,8 @@ SELECT timezone('1970-01-01 01:00'::timestamptz, 'UTC+6') statement ok SET TIME ZONE +0 + +query T +SELECT getdatabaseencoding() +---- +UTF8 diff --git a/pkg/sql/sem/builtins/builtins.go b/pkg/sql/sem/builtins/builtins.go index 04a8ea0000bd..1eee757c7765 100644 --- a/pkg/sql/sem/builtins/builtins.go +++ b/pkg/sql/sem/builtins/builtins.go @@ -3064,6 +3064,28 @@ may increase either contention or retry errors, or both.`, }, ), + "getdatabaseencoding": makeBuiltin( + tree.FunctionProperties{Category: categorySystemInfo}, + tree.Overload{ + Types: tree.ArgTypes{}, + ReturnType: tree.FixedReturnType(types.String), + Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) { + // computing encoding name bypassing encoding_id which is + // extracted from pg_database to pg_encoding_to_char function. + row, err := ctx.InternalExecutor.QueryRow( + ctx.Ctx(), "getdatabaseencoding", + ctx.Txn, + "SELECT pg_encoding_to_char(encoding) "+ + "FROM pg_database WHERE datname = $1", ctx.SessionData.Database) + if err != nil { + return nil, err + } + return row[0], nil + }, + Info: "Returns the current encoding name used by the database.", + }, + ), + // https://www.postgresql.org/docs/10/static/functions-info.html // // Note that in addition to what the pg doc says ("current_schema =