Skip to content

Commit

Permalink
Merge pull request #640 from dolthub/jennifer/current-schema
Browse files Browse the repository at this point in the history
current_schema function return non `"$user"` schema
  • Loading branch information
jennifersp authored Aug 21, 2024
2 parents a64aeba + 6335429 commit 9b6c177
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
3 changes: 1 addition & 2 deletions server/functions/current_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package functions

import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/doltgresql/server/functions/framework"
Expand All @@ -34,7 +33,7 @@ var current_schema = framework.Function0{
IsNonDeterministic: true,
Strict: true,
Callable: func(ctx *sql.Context) (any, error) {
schemas, err := resolve.SearchPath(ctx)
schemas, err := GetCurrentSchemas(ctx)
if err != nil {
return nil, err
}
Expand Down
31 changes: 26 additions & 5 deletions server/functions/current_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package functions

import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"strings"

"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/doltgresql/postgres/parser/sessiondata"

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
Expand All @@ -36,12 +36,12 @@ var current_schemas = framework.Function1{
Parameters: [1]pgtypes.DoltgresType{pgtypes.Bool},
IsNonDeterministic: true,
Strict: true,
Callable: func(ctx *sql.Context, _ [2]pgtypes.DoltgresType, val1 any) (any, error) {
Callable: func(ctx *sql.Context, _ [2]pgtypes.DoltgresType, val any) (any, error) {
schemas := make([]any, 0)
if val1.(bool) {
if val.(bool) {
schemas = append(schemas, sessiondata.PgCatalogName)
}
searchPaths, err := resolve.SearchPath(ctx)
searchPaths, err := GetCurrentSchemas(ctx)
if err != nil {
return nil, err
}
Expand All @@ -51,3 +51,24 @@ var current_schemas = framework.Function1{
return schemas, nil
},
}

// GetCurrentSchemas returns all the schemas in the search_path setting, with elements like "$user" excluded
func GetCurrentSchemas(ctx *sql.Context) ([]string, error) {
searchPathVar, err := ctx.GetSessionVariable(ctx, "search_path")
if err != nil {
return nil, err
}

pathElems := strings.Split(searchPathVar.(string), ",")
var path []string

for _, schemaName := range pathElems {
schemaName = strings.Trim(schemaName, " ")
if schemaName == "\"$user\"" {
continue
}
path = append(path, schemaName)
}

return path, nil
}
20 changes: 15 additions & 5 deletions testing/go/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ func TestSystemInformationFunctions(t *testing.T) {
Query: `SELECT current_schema();`,
Cols: []string{"\"current_schema\""},
Expected: []sql.Row{
{"postgres"},
{"public"},
},
},
{
Query: `SELECT current_schema();`,
Expected: []sql.Row{
{"postgres"},
{"public"},
},
},
{
Expand All @@ -444,7 +444,7 @@ func TestSystemInformationFunctions(t *testing.T) {
},
},
{
Query: `SET SEARCH_PATH TO public;`,
Query: `SET SEARCH_PATH TO public, test_schema;`,
Expected: []sql.Row{},
},
{
Expand All @@ -459,6 +459,16 @@ func TestSystemInformationFunctions(t *testing.T) {
{"public"},
},
},
{
Query: `SET SEARCH_PATH TO test_schema, public;`,
Expected: []sql.Row{},
},
{
Query: `SELECT current_schema();`,
Expected: []sql.Row{
{"test_schema"},
},
},
// TODO: Implement table function for current_schema
{
Query: `SELECT * FROM current_schema();`,
Expand All @@ -483,13 +493,13 @@ func TestSystemInformationFunctions(t *testing.T) {
Query: `SELECT current_schemas(true);`,
Cols: []string{"current_schemas"},
Expected: []sql.Row{
{"{pg_catalog,postgres,public}"},
{"{pg_catalog,public}"},
},
},
{ // TODO: Not sure why Postgres does not display "$user" here
Query: `SELECT current_schemas(false);`,
Expected: []sql.Row{
{"{postgres,public}"},
{"{public}"},
},
},
{
Expand Down

0 comments on commit 9b6c177

Please sign in to comment.