Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: implement oidvectortypes builtin #108467

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3303,6 +3303,8 @@ may increase either contention or retry errors, or both.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="obj_description"></a><code>obj_description(object_oid: oid, catalog_name: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the comment for a database object specified by its OID and the name of the containing system catalog. For example, obj_description(123456, ‘pg_class’) would retrieve the comment for the table with OID 123456.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="oidvectortypes"></a><code>oidvectortypes(vector: oidvector) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Generates a comma seperated string of type names from an oidvector.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="pg_backend_pid"></a><code>pg_backend_pid() &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Returns a numerical ID attached to this session. This ID is part of the query cancellation key used by the wire protocol. This function was only added for compatibility, and unlike in Postgres, the returned value does not correspond to a real process ID.</p>
</span></td><td>Stable</td></tr>
<tr><td><a name="pg_collation_for"></a><code>pg_collation_for(str: anyelement) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns the collation of the argument</p>
Expand Down
16 changes: 16 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -4197,3 +4197,19 @@ query T
SELECT bitmask_xor(B'001'::VARBIT, '101'::STRING);
----
100

# Query `percentile_disc_impl(arg1: float, arg2: bool) -> bool` from pg_proc
query T rowsort
SELECT oidvectortypes(proargtypes) FROM pg_proc WHERE oid=263
----
float, bool

# Query a custom type and function.
statement ok
CREATE TYPE custom_typ AS ENUM ('good', 'bad');
CREATE OR REPLACE FUNCTION custom_fn(c custom_typ) RETURNS custom_typ AS 'SELECT c' LANGUAGE SQL;

query T rowsort
SELECT oidvectortypes(proargtypes) FROM pg_proc WHERE proname='custom_fn'
----
custom_typ
35 changes: 35 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3993,6 +3993,41 @@ value if you rely on the HLC for accuracy.`,
},
),

"oidvectortypes": makeBuiltin(
tree.FunctionProperties{
Category: builtinconstants.CategoryCompatibility,
},
tree.Overload{
Types: tree.ParamTypes{
{Name: "vector", Typ: types.OidVector},
},
ReturnType: tree.FixedReturnType(types.String),
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
var err error
oidVector := args[0].(*tree.DArray)
result := strings.Builder{}
for idx, datum := range oidVector.Array {
oidDatum := datum.(*tree.DOid)
var typ *types.T
if resolvedTyp, ok := types.OidToType[oidDatum.Oid]; ok {
typ = resolvedTyp
} else {
typ, err = evalCtx.Planner.ResolveTypeByOID(ctx, oidDatum.Oid)
if err != nil {
return nil, err
}
}
result.WriteString(typ.Name())
if idx != len(oidVector.Array)-1 {
result.WriteString(", ")
}
}
return tree.NewDString(result.String()), nil
},
Info: "Generates a comma seperated string of type names from an oidvector.",
Volatility: volatility.Stable,
}),

"crdb_internal.pb_to_json": makeBuiltin(
jsonProps(),
func() []tree.Overload {
Expand Down
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 @@ -2452,6 +2452,7 @@ var builtinOidsArray = []string{
2481: `bitmask_xor(a: string, b: string) -> varbit`,
2482: `bitmask_xor(a: varbit, b: string) -> varbit`,
2483: `bitmask_xor(a: string, b: varbit) -> varbit`,
2484: `oidvectortypes(vector: oidvector) -> string`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down