Skip to content

Commit de5347c

Browse files
Merge pull request #2395 from dolthub/nathan/infoschemSeq
information schema sequences
2 parents fd9c0ce + 9430c4c commit de5347c

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

server/tables/information_schema/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ func Init() {
3030
// Postgres-specific tables/views to be added to information_schema database
3131
information_schema.NewInformationSchemaTablesToAdd = map[string]sql.Table{
3232
ConstraintColumnUsageViewName: newConstraintColumnUsageView(),
33+
SequencesTableName: newSequencesTable(),
3334
}
3435
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2026 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package information_schema
16+
17+
import (
18+
"strconv"
19+
20+
"github.com/dolthub/go-mysql-server/sql"
21+
"github.com/dolthub/go-mysql-server/sql/information_schema"
22+
23+
"github.com/dolthub/doltgresql/server/functions"
24+
pgtypes "github.com/dolthub/doltgresql/server/types"
25+
)
26+
27+
const SequencesTableName = "sequences"
28+
29+
// newSequencesTable returns a InformationSchemaTable for MySQL.
30+
func newSequencesTable() *information_schema.InformationSchemaTable {
31+
return &information_schema.InformationSchemaTable{
32+
TableName: SequencesTableName,
33+
TableSchema: sequencesSchema,
34+
Reader: sequencesRowIter,
35+
}
36+
}
37+
38+
// tablesSchema is the schema for the information_schema.TABLES table.
39+
var sequencesSchema = sql.Schema{
40+
{Name: "sequence_catalog", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName},
41+
{Name: "sequence_schema", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName},
42+
{Name: "sequence_name", Type: sql_identifier, Default: nil, Nullable: true, Source: SequencesTableName},
43+
{Name: "data_type", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName},
44+
{Name: "numeric_precision", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName},
45+
{Name: "numeric_precision_radix", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName},
46+
{Name: "numeric_scale", Type: cardinal_number, Default: nil, Nullable: true, Source: SequencesTableName},
47+
{Name: "start_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName},
48+
{Name: "minimum_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName},
49+
{Name: "maximum_value", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName},
50+
{Name: "increment", Type: character_data, Default: nil, Nullable: true, Source: SequencesTableName},
51+
{Name: "cycle_option", Type: yes_or_no, Default: nil, Nullable: true, Source: SequencesTableName},
52+
}
53+
54+
// sequencesRowIter implements the sql.RowIter for the information_schema.Sequences table.
55+
func sequencesRowIter(ctx *sql.Context, _ sql.Catalog) (sql.RowIter, error) {
56+
var rows []sql.Row
57+
58+
err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{
59+
Sequence: func(_ *sql.Context, schema functions.ItemSchema, sequence functions.ItemSequence) (cont bool, err error) {
60+
sequenceType := pgtypes.GetTypeByID(sequence.Item.DataTypeID)
61+
62+
var precision, radix, scale interface{}
63+
if sequenceType != nil {
64+
precision, radix, scale = getColumnPrecisionAndScale(sequenceType)
65+
}
66+
67+
cycleOption := "NO"
68+
if sequence.Item.Cycle {
69+
cycleOption = "YES"
70+
}
71+
72+
rows = append(rows, sql.Row{
73+
schema.Item.Name(), //sequence_catalog
74+
schema.Item.SchemaName(), //sequence_schema
75+
sequence.Item.Id.SequenceName(), //sequence_name
76+
sequenceType.String(), //data_type
77+
precision, //numeric_precision
78+
radix, //numeric_precision_radix
79+
scale, //numeric_scale
80+
strconv.FormatInt(sequence.Item.Start, 10), //start_value
81+
strconv.FormatInt(sequence.Item.Minimum, 10), //minimum_value
82+
strconv.FormatInt(sequence.Item.Maximum, 10), //maximum_value
83+
strconv.FormatInt(sequence.Item.Increment, 10), //increment
84+
cycleOption, //cycle_option
85+
})
86+
return true, nil
87+
},
88+
})
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
return sql.RowsToRowIter(rows...), nil
94+
}

testing/go/information_schema_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,44 @@ func TestInfoSchemaViews(t *testing.T) {
477477
},
478478
})
479479
}
480+
481+
func TestInfoSchemaSequences(t *testing.T) {
482+
RunScripts(t, []ScriptTest{
483+
{
484+
Name: "information_schema.sequences",
485+
SetUpScript: []string{
486+
"create sequence standard as smallint;",
487+
"create schema test_schema",
488+
"create table test_schema.test_table (id serial);",
489+
"create sequence big increment by 3 start with 10 minvalue 1 cycle;",
490+
"create sequence negative increment by -1",
491+
},
492+
Assertions: []ScriptTestAssertion{
493+
{
494+
Query: "select * from information_schema.sequences where sequence_name = 'standard';",
495+
Expected: []sql.Row{
496+
{"postgres", "public", "standard", "smallint", 16, 2, 0, "1", "1", "32767", "1", "NO"},
497+
},
498+
},
499+
{
500+
Query: "select sequence_schema,sequence_name,data_type,numeric_precision from information_schema.sequences where sequence_name = 'test_table_id_seq';",
501+
Expected: []sql.Row{
502+
{"test_schema", "test_table_id_seq", "integer", 32},
503+
},
504+
},
505+
{
506+
Query: "select sequence_name,data_type,numeric_precision,minimum_value,increment,cycle_option from information_schema.sequences where sequence_name = 'big';",
507+
Expected: []sql.Row{
508+
{"big", "bigint", 64, "1", "3", "YES"},
509+
},
510+
},
511+
{
512+
Query: "select sequence_name, increment from information_schema.sequences where sequence_name = 'negative';",
513+
Expected: []sql.Row{
514+
{"negative", "-1"},
515+
},
516+
},
517+
},
518+
},
519+
})
520+
}

0 commit comments

Comments
 (0)