Skip to content

Commit 75a258e

Browse files
committed
put pg_catalog implicitly on the search path
1 parent d4f7a48 commit 75a258e

6 files changed

Lines changed: 35 additions & 16 deletions

File tree

core/context.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func GetSqlTableFromContext(ctx *sql.Context, databaseName string, tableName dol
199199
var searchPath []string
200200
if len(tableName.Schema) == 0 {
201201
// If a schema was not provided, then we'll use the search path
202-
searchPath, err = resolve.SearchPath(ctx)
202+
searchPath, err = SearchPath(ctx)
203203
if err != nil {
204204
return nil, err
205205
}
@@ -228,6 +228,30 @@ func GetSqlTableFromContext(ctx *sql.Context, databaseName string, tableName dol
228228
return nil, nil
229229
}
230230

231+
// SearchPath returns the effective schema search path for the current session
232+
func SearchPath(ctx *sql.Context) ([]string, error) {
233+
path, err := resolve.SearchPath(ctx)
234+
if err != nil {
235+
return nil, err
236+
}
237+
238+
// pg_catalog is *always* implicitly part of the search path as the first element, unless it's specifically
239+
// included later. This allows users to override built-in names with user-defined names, but they have to
240+
// opt in to that behavior.
241+
hasPgCatalog := false
242+
for _, schema := range path {
243+
if schema == "pg_catalog" {
244+
hasPgCatalog = true
245+
break
246+
}
247+
}
248+
249+
if !hasPgCatalog {
250+
path = append([]string{"pg_catalog"}, path...)
251+
}
252+
return path, nil
253+
}
254+
231255
// GetExtensionsCollectionFromContext returns the extensions collection from the given context. Will always return a
232256
// collection if no error is returned.
233257
func GetExtensionsCollectionFromContext(ctx *sql.Context, database string) (*extensions.Collection, error) {

server/functions/pg_table_is_visible.go

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

1717
import (
18-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
18+
"github.com/dolthub/doltgresql/core"
1919
"github.com/dolthub/go-mysql-server/sql"
2020

2121
"github.com/dolthub/doltgresql/core/id"
@@ -38,7 +38,7 @@ var pg_table_is_visible_oid = framework.Function1{
3838
Strict: true,
3939
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
4040
oidVal := val.(id.Id)
41-
paths, err := resolve.SearchPath(ctx)
41+
paths, err := core.SearchPath(ctx)
4242
if err != nil {
4343
return false, err
4444
}

server/functions/pg_type_is_visible.go

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

1717
import (
18-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
18+
"github.com/dolthub/doltgresql/core"
1919
"github.com/dolthub/go-mysql-server/sql"
2020

2121
"github.com/dolthub/doltgresql/core/id"
@@ -48,7 +48,7 @@ var pg_type_is_visible = framework.Function1{
4848
schemaName := oidVal.Segment(0)
4949

5050
// Get the current search path
51-
searchPath, err := resolve.SearchPath(ctx)
51+
searchPath, err := core.SearchPath(ctx)
5252
if err != nil {
5353
return false, err
5454
}

server/functions/regclass.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"strconv"
2020

2121
"github.com/cockroachdb/errors"
22-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
22+
"github.com/dolthub/doltgresql/core"
2323
"github.com/dolthub/go-mysql-server/sql"
2424

2525
"github.com/dolthub/doltgresql/core/id"
@@ -65,7 +65,7 @@ var regclassin = framework.Function1{
6565
switch len(sections) {
6666
case 1:
6767
database = ctx.GetCurrentDatabase()
68-
searchSchemas, err = resolve.SearchPath(ctx)
68+
searchSchemas, err = core.SearchPath(ctx)
6969
if err != nil {
7070
return id.Null, err
7171
}
@@ -79,7 +79,7 @@ var regclassin = framework.Function1{
7979
searchSchemas = []string{sections[2]}
8080
relationName = sections[4]
8181
default:
82-
return id.Null, errors.Errorf("regclass failed validation")
82+
return id.Null, errors.Errorf("unexpected input for regclass: %s", input)
8383
}
8484

8585
// Iterate over all of the items to find which relation matches.

server/node/create_trigger.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package node
1616

1717
import (
1818
"github.com/cockroachdb/errors"
19-
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
2019
"github.com/dolthub/go-mysql-server/sql"
2120
"github.com/dolthub/go-mysql-server/sql/plan"
2221
vitess "github.com/dolthub/vitess/go/vt/sqlparser"
@@ -180,11 +179,10 @@ func loadFunction(ctx *sql.Context, funcCollection *functions.Collection, funcID
180179
return functions.Function{}, err
181180
}
182181
} else {
183-
searchPaths, err := resolve.SearchPath(ctx)
182+
searchPaths, err := core.SearchPath(ctx)
184183
if err != nil {
185184
return functions.Function{}, err
186185
}
187-
searchPaths = append(searchPaths, "pg_catalog") // This isn't included in the search path but functions use it
188186
for _, searchPath := range searchPaths {
189187
function, err = funcCollection.GetFunction(ctx, id.NewFunction(searchPath, funcID.FunctionName()))
190188
if err != nil {

testing/go/prepared_statement_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,15 +1334,14 @@ var pgCatalogTests = []ScriptTest{
13341334
},
13351335
},
13361336
{
1337-
Name: "pg_class",
1337+
Name: "pg_class",
1338+
Focus: true,
13381339
SetUpScript: []string{
13391340
`CREATE SCHEMA testschema;`,
13401341
`CREATE TABLE testschema.testtable (id int primary key, v1 text)`,
13411342
},
13421343
Assertions: []ScriptTestAssertion{
13431344
{
1344-
// https://github.com/dolthub/doltgresql/issues/2217
1345-
Skip: true,
13461345
Query: `SELECT c.oid,d.description,pg_catalog.pg_get_expr(c.relpartbound, c.oid) as partition_expr, pg_catalog.pg_get_partkeydef(c.oid) as partition_key
13471346
FROM pg_catalog.pg_class c
13481347
LEFT OUTER JOIN pg_catalog.pg_description d ON d.objoid=c.oid AND d.objsubid=0 AND d.classoid='pg_class'::regclass
@@ -1351,8 +1350,6 @@ WHERE c.relnamespace=$1 AND c.relkind not in ('i','I','c') and c.oid not in (sel
13511350
Expected: []sql.Row{{1712283605, nil, nil, ""}},
13521351
},
13531352
{
1354-
// https://github.com/dolthub/doltgresql/issues/2217
1355-
Skip: true,
13561353
Query: `SELECT d.description from pg_catalog.pg_description d WHERE d.classoid='pg_class'::regclass`,
13571354
// TODO: add expected values
13581355
},

0 commit comments

Comments
 (0)