Skip to content

Commit

Permalink
sql: implement oidvectortypes builtin
Browse files Browse the repository at this point in the history
Previously, the oidvectortypes builtin in wasn't implemented, causing
a compatibility gap for tools that need to format oidvectors. To
address this, this patch adds the oidvectortypes built in.

Fixes: cockroachdb#107942

Release note (sql change): The oidvectortypes built-in has been implemented,
which can format oidvector.
  • Loading branch information
fqazi committed Aug 30, 2023
1 parent e6e624c commit e058c0d
Show file tree
Hide file tree
Showing 4 changed files with 79 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 @@ -3544,6 +3544,8 @@ table. Returns an error if validation fails.</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 @@ -4035,3 +4035,19 @@ SELECT crdb_internal.merge_aggregated_stmt_metadata(ARRAY[ '{"aMalformedMetadaOb
{"appNames": [], "db": [], "distSQLCount": 0, "failedCount": 0, "fingerprintID": "", "formattedQuery": "", "fullScanCount": 0, "implicitTxn": false, "query": "", "querySummary": "", "stmtType": "", "totalCount": 0, "vecCount": 0}

subtest end

# 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 @@ -3984,6 +3984,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
26 changes: 26 additions & 0 deletions pkg/sql/sem/builtins/fixed_oids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,32 @@ var builtinOidsArray = []string{
2453: `crdb_internal.reset_activity_tables() -> bool`,
2455: `crdb_internal.repair_catalog_corruption(descriptor_id: int, corruption: string) -> bool`,
2456: `crdb_internal.merge_aggregated_stmt_metadata(input: jsonb[]) -> jsonb`,
2457: `crdb_internal.request_job_execution_details(jobID: int) -> bool`,
2458: `pg_sequence_last_value(sequence_oid: oid) -> int`,
2459: `nameconcatoid(name: string, oid: oid) -> name`,
2460: `pg_get_function_arg_default(func_oid: oid, arg_num: int4) -> string`,
2461: `crdb_internal.plpgsql_raise(severity: string, message: string, detail: string, hint: string, code: string) -> int`,
2462: `workload_index_recs() -> string`,
2463: `workload_index_recs(timestamptz: timestamptz) -> string`,
2466: `crdb_internal.setup_span_configs_stream(tenant_name: string) -> bytes`,
2467: `crdb_internal.request_statement_bundle(stmtFingerprint: string, planGist: string, samplingProbability: float, minExecutionLatency: interval, expiresAfter: interval) -> bool`,
2468: `crdb_internal.request_statement_bundle(stmtFingerprint: string, planGist: string, antiPlanGist: bool, samplingProbability: float, minExecutionLatency: interval, expiresAfter: interval) -> bool`,
2469: `crdb_internal.is_system_table_key(raw_key: bytes) -> bool`,
2470: `crdb_internal.scan_storage_internal_keys(node_id: int, store_id: int, start_key: bytes, end_key: bytes) -> tuple{int AS level, int AS node_id, int AS store_id, int AS snapshot_pinned_keys, int AS snapshot_pinned_keys_bytes, int AS point_key_delete_count, int AS point_key_set_count, int AS range_delete_count, int AS range_key_set_count, int AS range_key_delete_count}`,
2471: `crdb_internal.scan_storage_internal_keys(node_id: int, store_id: int, start_key: bytes, end_key: bytes, mb_per_second: int4) -> tuple{int AS level, int AS node_id, int AS store_id, int AS snapshot_pinned_keys, int AS snapshot_pinned_keys_bytes, int AS point_key_delete_count, int AS point_key_set_count, int AS range_delete_count, int AS range_key_set_count, int AS range_key_delete_count}`,
2472: `bitmask_or(a: varbit, b: varbit) -> varbit`,
2473: `bitmask_or(a: string, b: string) -> varbit`,
2474: `bitmask_or(a: varbit, b: string) -> varbit`,
2475: `bitmask_or(a: string, b: varbit) -> varbit`,
2476: `bitmask_and(a: varbit, b: varbit) -> varbit`,
2477: `bitmask_and(a: string, b: string) -> varbit`,
2478: `bitmask_and(a: varbit, b: string) -> varbit`,
2479: `bitmask_and(a: string, b: varbit) -> varbit`,
2480: `bitmask_xor(a: varbit, b: varbit) -> varbit`,
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

0 comments on commit e058c0d

Please sign in to comment.