Skip to content

Commit

Permalink
sql: add the getdatabaseencoding() builtin function
Browse files Browse the repository at this point in the history
Resolves cockroachdb#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.
  • Loading branch information
abhishek20123g committed Feb 16, 2020
1 parent 3c2dcbc commit e17cd68
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,8 @@ SELECT * FROM crdb_internal.check_consistency(true, ‘\x02’, ‘\x04’)</p>
</span></td></tr>
<tr><td><a name="current_user"></a><code>current_user() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the current user. This function is provided for compatibility with PostgreSQL.</p>
</span></td></tr>
<tr><td><a name="getdatabaseencoding"></a><code>getdatabaseencoding() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the current encoding name used by the database.</p>
</span></td></tr>
<tr><td><a name="version"></a><code>version() &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the node’s version of CockroachDB.</p>
</span></td></tr></tbody>
</table>
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit e17cd68

Please sign in to comment.