Skip to content

Commit

Permalink
builtins: implement _pg_index_position as UDF
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
jordanlewis committed Jan 5, 2023
1 parent acf5006 commit d363b40
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2056,43 +2056,20 @@ SELECT description
// _pg_index_position return the column's position in the index
// (or NULL if not there).
//
// NOTE: this could be defined as a user-defined function, like
// it is in Postgres:
// NOTE: this is defined as a UDF, same as in Postgres:
// https://github.com/postgres/postgres/blob/master/src/backend/catalog/information_schema.sql
//
// CREATE FUNCTION _pg_index_position(oid, smallint) RETURNS int
// LANGUAGE sql STRICT STABLE
// BEGIN ATOMIC
// SELECT (ss.a).n FROM
// (SELECT information_schema._pg_expandarray(indkey) AS a
// FROM pg_catalog.pg_index WHERE indexrelid = $1) ss
// WHERE (ss.a).x = $2;
// END;
//
"information_schema._pg_index_position": makeBuiltin(defProps(),
tree.Overload{
IsUDF: true,
Types: tree.ParamTypes{
{Name: "oid", Typ: types.Oid},
{Name: "col", Typ: types.Int2},
},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
r, err := evalCtx.Planner.QueryRowEx(
ctx, "information_schema._pg_index_position",
sessiondata.NoSessionDataOverride,
`SELECT (ss.a).n FROM
(SELECT information_schema._pg_expandarray(indkey) AS a
FROM pg_catalog.pg_index WHERE indexrelid = $1) ss
WHERE (ss.a).x = $2`,
args[0], args[1])
if err != nil {
return nil, err
}
if len(r) == 0 {
return tree.DNull, nil
}
return r[0], nil
},
Body: `SELECT (ss.a).n FROM
(SELECT information_schema._pg_expandarray(indkey) AS a
FROM pg_catalog.pg_index WHERE indexrelid = $1) ss
WHERE (ss.a).x = $2`,
Info: notUsableInfo,
Volatility: volatility.Stable,
},
Expand Down

0 comments on commit d363b40

Please sign in to comment.