Skip to content

Commit 1eded64

Browse files
authored
add information_schema.constraint_column_usage view (#2270)
1 parent 532eb62 commit 1eded64

6 files changed

Lines changed: 127 additions & 1 deletion

File tree

server/functions/regtype.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ var regtypein = framework.Function1{
8080
if typeName == "char" && schema == "" {
8181
return id.NewType("pg_catalog", "bpchar").AsId(), nil
8282
}
83+
if typeName == "int" {
84+
typeName = "int4"
85+
}
8386
if internalID, ok := pgtypes.NameToInternalID[typeName]; ok && (internalID.SchemaName() == schema || schema == "") {
8487
return internalID.AsId(), nil
8588
}

server/functions/setval.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func parseRelationName(ctx *sql.Context, name string) (schema string, relation s
8989
return "", "", errors.Errorf(`cannot parse relation: %s`, name)
9090
}
9191

92-
// Trim any quotes from the relation name
92+
// Trim any quotes from the schema and the relation name
93+
schema = strings.Trim(schema, `"`)
9394
relation = strings.Trim(relation, `"`)
9495

9596
return schema, relation, nil
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
"github.com/dolthub/go-mysql-server/sql"
19+
"github.com/dolthub/go-mysql-server/sql/information_schema"
20+
21+
"github.com/dolthub/doltgresql/server/functions"
22+
)
23+
24+
// ConstraintColumnUsageViewName is the name of the CONSTRAINT_COLUMN_USAGE view.
25+
const ConstraintColumnUsageViewName = "constraint_column_usage"
26+
27+
// newConstraintColumnUsageView creates a new information_schema.CONSTRAINT_COLUMN_USAGE view.
28+
func newConstraintColumnUsageView() *information_schema.InformationSchemaTable {
29+
return &information_schema.InformationSchemaTable{
30+
TableName: ConstraintColumnUsageViewName,
31+
TableSchema: constraintColumnUsageSchema,
32+
Reader: constraintColumnUsageRowIter,
33+
}
34+
}
35+
36+
// constraintColumnUsage is the schema for the information_schema.CONSTRAINT_COLUMN_USAGE view.
37+
var constraintColumnUsageSchema = sql.Schema{
38+
{Name: "table_catalog", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
39+
{Name: "table_schema", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
40+
{Name: "table_name", Type: sql_identifier, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
41+
{Name: "column_name", Type: character_data, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
42+
{Name: "constraint_catalog", Type: character_data, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
43+
{Name: "constraint_schema", Type: yes_or_no, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
44+
{Name: "constraint_name", Type: yes_or_no, Default: nil, Nullable: true, Source: ConstraintColumnUsageViewName},
45+
}
46+
47+
// constraintColumnUsageRowIter implements the sql.RowIter for the information_schema.CONSTRAINT_COLUMN_USAGE view.
48+
func constraintColumnUsageRowIter(ctx *sql.Context, catalog sql.Catalog) (sql.RowIter, error) {
49+
var rows []sql.Row
50+
51+
err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{
52+
Check: func(ctx *sql.Context, schema functions.ItemSchema, table functions.ItemTable, check functions.ItemCheck) (cont bool, err error) {
53+
54+
// TODO: Fill out the rest of the columns.
55+
rows = append(rows, sql.Row{
56+
schema.Item.Name(), // table_catalog
57+
schema.Item.SchemaName(), // table_schema
58+
table.Item.Name(), // table_name
59+
nil, // column_name
60+
nil, // constraint_catalog
61+
nil, // constraint_schema
62+
check.Item.Name, // constraint_name
63+
})
64+
return true, nil
65+
},
66+
})
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
return sql.RowsToRowIter(rows...), nil
72+
}

server/tables/information_schema/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package information_schema
1616

1717
import (
18+
"github.com/dolthub/go-mysql-server/sql"
1819
"github.com/dolthub/go-mysql-server/sql/information_schema"
1920
)
2021

@@ -25,4 +26,9 @@ func Init() {
2526
information_schema.NewSchemataTable = newSchemataTable
2627
information_schema.NewTablesTable = newTablesTable
2728
information_schema.NewViewsTable = newViewsTable
29+
30+
// Postgres-specific tables/views to be added to information_schema database
31+
information_schema.NewInformationSchemaTablesToAdd = map[string]sql.Table{
32+
ConstraintColumnUsageViewName: newConstraintColumnUsageView(),
33+
}
2834
}

testing/go/functions_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,12 @@ func TestFunctionsOID(t *testing.T) {
780780
{"integer"},
781781
},
782782
},
783+
{
784+
Query: `SELECT to_regtype(('int'::regtype)::text);`,
785+
Expected: []sql.Row{
786+
{"integer"},
787+
},
788+
},
783789
{
784790
Query: `SELECT to_regtype((('integer'::regtype)::oid)::text);`,
785791
Expected: []sql.Row{
@@ -1393,6 +1399,11 @@ func TestSystemInformationFunctions(t *testing.T) {
13931399
ExpectedColNames: []string{"pg_get_serial_sequence"},
13941400
Expected: []sql.Row{{"public.t1_id_seq"}},
13951401
},
1402+
{
1403+
Query: `SELECT pg_get_serial_sequence('"public"."t1"', 'id');`,
1404+
ExpectedColNames: []string{"pg_get_serial_sequence"},
1405+
Expected: []sql.Row{{"public.t1_id_seq"}},
1406+
},
13961407
{
13971408
// Test with no schema specified
13981409
Query: `SELECT pg_get_serial_sequence('t1', 'id');`,

testing/go/session_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,36 @@ func TestDiscard(t *testing.T) {
6969
},
7070
})
7171
}
72+
73+
func TestRollback(t *testing.T) {
74+
RunScripts(t, []ScriptTest{
75+
{
76+
Name: "Test rollback transaction",
77+
SetUpScript: []string{
78+
`BEGIN`,
79+
`CREATE temporary TABLE test (a INT)`,
80+
`insert into test values (1)`,
81+
},
82+
Assertions: []ScriptTestAssertion{
83+
{
84+
Query: "select * from test",
85+
Expected: []sql.Row{{1}},
86+
},
87+
{
88+
Query: "ROLLBACK",
89+
Expected: []sql.Row{},
90+
},
91+
{
92+
Query: "select * from test",
93+
ExpectedErr: "table not found",
94+
Skip: true, // temp table should be dropped after ROLLBACK
95+
},
96+
{
97+
Query: "create temp table test (b int)",
98+
Expected: []sql.Row{},
99+
Skip: true, // temp table should be dropped after ROLLBACK
100+
},
101+
},
102+
},
103+
})
104+
}

0 commit comments

Comments
 (0)