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: add pg_sequence_parameters builtin #21069

Merged
merged 1 commit into from
Jan 2, 2018
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
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/sequences
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ SELECT currval('foo')
----
2

query T
SELECT pg_sequence_parameters('foo'::regclass::oid)
----
(1,1,9223372036854775807,1,f,1,20)

# You can create a sequence with different increment.

statement ok
Expand All @@ -144,6 +149,11 @@ SELECT nextval('bar')
----
6

query T
SELECT pg_sequence_parameters('bar'::regclass::oid)
----
(1,1,9223372036854775807,5,f,1,20)

# You can create a sequence with different start and increment.

statement ok
Expand All @@ -159,6 +169,11 @@ SELECT nextval('baz')
----
7

query T
SELECT pg_sequence_parameters('baz'::regclass::oid)
----
(2,1,9223372036854775807,5,f,1,20)

# You can create a sequence that goes down.

statement ok
Expand All @@ -174,6 +189,11 @@ SELECT nextval('down_test')
----
9

query T
SELECT pg_sequence_parameters('down_test'::regclass::oid)
----
(10,-9223372036854775808,-1,-1,f,1,20)


# You can create and use a sequence with special characters.

Expand Down
31 changes: 31 additions & 0 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,37 @@ var pgBuiltins = map[string][]tree.Builtin{
Info: notUsableInfo,
},
},
"pg_sequence_parameters": {
// pg_sequence_parameters is an undocumented Postgres builtin that returns
// information about a sequence given its OID. It's nevertheless used by
// at least one UI tool, so we provide an implementation for compatibility.
// The real implementation returns a record; we fake it by returning a
// comma-delimited string enclosed by parentheses.
// TODO(jordan): convert this to return a record type once we support that.
tree.Builtin{
Types: tree.ArgTypes{{"sequence_oid", types.Oid}},
DistsqlBlacklist: true,
ReturnType: tree.FixedReturnType(types.String),
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
r, err := ctx.Planner.QueryRow(
ctx.Ctx(), `SELECT seqstart, seqmin, seqmax, seqincrement, seqcycle, seqcache, seqtypid
FROM pg_catalog.pg_sequence WHERE seqrelid=$1`, args[0])
if err != nil {
return nil, err
}
if len(r) == 0 {
return nil, pgerror.NewErrorf(pgerror.CodeUndefinedTableError, "unknown sequence (OID=%s)", args[0])
}
seqstart, seqmin, seqmax, seqincrement, seqcycle, seqcache, seqtypid := r[0], r[1], r[2], r[3], r[4], r[5], r[6]
seqcycleStr := "t"
if seqcycle.(*tree.DBool) == tree.DBoolFalse {
seqcycleStr = "f"
}
return tree.NewDString(fmt.Sprintf("(%s,%s,%s,%s,%s,%s,%s)", seqstart, seqmin, seqmax, seqincrement, seqcycleStr, seqcache, seqtypid)), nil
},
Info: notUsableInfo,
},
},
"format_type": {
tree.Builtin{
Types: tree.ArgTypes{{"type_oid", types.Oid}, {"typemod", types.Int}},
Expand Down