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: #107942

Release note (sql change): The oidvectortypes built-in has been implemented,
which can format oidvector.
  • Loading branch information
fqazi committed Aug 9, 2023
1 parent 66c9e4f commit 99cb5a1
Show file tree
Hide file tree
Showing 4 changed files with 43 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 @@ -3283,6 +3283,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
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 @@ -4037,3 +4037,8 @@ 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 T rowsort
SELECT oidvectortypes(proargtypes) FROM pg_proc WHERE oid=263
----
FLOAT8, BOOL
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.SQLString())
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 @@ -2440,6 +2440,7 @@ var builtinOidsArray = []string{
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: `oidvectortypes(vector: oidvector) -> string`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down

0 comments on commit 99cb5a1

Please sign in to comment.