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

Add comment information system functions #497

Merged
merged 5 commits into from
Jul 16, 2024
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
41 changes: 41 additions & 0 deletions server/functions/col_description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package functions

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

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initColDescription registers the functions to the catalog.
func initColDescription() {
framework.RegisterFunction(col_description)
}

// col_description represents the PostgreSQL function of the same name, taking the same parameters.
var col_description = framework.Function2{
Name: "col_description",
Return: pgtypes.Text,
Parameters: [2]pgtypes.DoltgresType{pgtypes.Oid, pgtypes.Int32},
IsNonDeterministic: true,
Strict: true,
Callable: func(ctx *sql.Context, _ [3]pgtypes.DoltgresType, val1 any, val2 any) (any, error) {
// TODO: When we support comments this should return the comment for a table
// column, which is specified by the OID of its table and its column number
return "", nil
},
}
3 changes: 3 additions & 0 deletions server/functions/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Init() {
initCeil()
initCharLength()
initChr()
initColDescription()
initCos()
initCosd()
initCosh()
Expand Down Expand Up @@ -64,6 +65,7 @@ func Init() {
initMinScale()
initMod()
initNextVal()
initObjDescription()
initOctetLength()
initPi()
initPower()
Expand All @@ -78,6 +80,7 @@ func Init() {
initRtrim()
initScale()
initSetVal()
initShobjDescription()
initSign()
initSin()
initSind()
Expand Down
42 changes: 42 additions & 0 deletions server/functions/obj_description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package functions

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

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initObjDescription registers the functions to the catalog.
func initObjDescription() {
framework.RegisterFunction(obj_description)
}

// obj_description represents the PostgreSQL function of the same name, taking the same parameters.
var obj_description = framework.Function2{
Name: "obj_description",
Return: pgtypes.Text,
Parameters: [2]pgtypes.DoltgresType{pgtypes.Oid, pgtypes.Name},
IsNonDeterministic: true,
Strict: true,
Callable: func(ctx *sql.Context, _ [3]pgtypes.DoltgresType, val1 any, val2 any) (any, error) {
// TODO: When we support comments this should return the comment for a
// database object specified by its OID and the name of the containing
// system catalog.
return "", nil
},
}
42 changes: 42 additions & 0 deletions server/functions/shobj_description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package functions

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

"github.com/dolthub/doltgresql/server/functions/framework"
pgtypes "github.com/dolthub/doltgresql/server/types"
)

// initShobjDescription registers the functions to the catalog.
func initShobjDescription() {
framework.RegisterFunction(shobj_description)
}

// shobj_description represents the PostgreSQL function of the same name, taking the same parameters.
var shobj_description = framework.Function2{
Name: "shobj_description",
Return: pgtypes.Text,
Parameters: [2]pgtypes.DoltgresType{pgtypes.Oid, pgtypes.Name},
IsNonDeterministic: true,
Strict: true,
Callable: func(ctx *sql.Context, _ [3]pgtypes.DoltgresType, val1 any, val2 any) (any, error) {
// TODO: When we support comments this should return the comment for a
// shared database object specified by its OID and the name of the
// containing system catalog.
return "", nil
},
}
93 changes: 93 additions & 0 deletions testing/go/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,5 +434,98 @@ func TestSystemInformationFunctions(t *testing.T) {
},
},
},
{
Name: "col_description",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT col_description(100, 1);`,
tbantle22 marked this conversation as resolved.
Show resolved Hide resolved
Cols: []string{"col_description"},
Expected: []sql.Row{
{""},
},
},
{
Query: `SELECT col_description('not_a_table'::regclass, 1);`,
ExpectedErr: `relation "not_a_table" does not exist`,
},
{
Query: `CREATE TABLE test_table (id INT);`,
Expected: []sql.Row{},
},
{
Skip: true, // TODO: Implement column comments
Query: `COMMENT ON COLUMN test_table.id IS 'This is col id';`,
Expected: []sql.Row{},
},
{
Skip: true, // TODO: Implement column object comments
Query: `SELECT col_description('test_table'::regclass, 1);`,
Cols: []string{"col_description"},
Expected: []sql.Row{
{"This is col id"},
},
},
},
},
{
Name: "obj_description",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT obj_description(100, 'pg_class');`,
Cols: []string{"obj_description"},
Expected: []sql.Row{
{""},
},
},
{
Query: `SELECT obj_description('does-not-exist'::regproc, 'pg_class');`,
ExpectedErr: `function "does-not-exist" does not exist`,
},
{
Skip: true, // TODO: Implement database object comments
Query: `SELECT obj_description('sinh'::regproc, 'pg_proc');`,
Cols: []string{"col_description"},
Expected: []sql.Row{
{"hyperbolic sine"},
},
},
},
},
{
Name: "shobj_description",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT shobj_description(100, 'pg_class');`,
Cols: []string{"shobj_description"},
Expected: []sql.Row{
{""},
},
},
{
Query: `SELECT shobj_description('does-not-exist'::regproc, 'pg_class');`,
ExpectedErr: `function "does-not-exist" does not exist`,
},
{
Skip: true, // TODO: Implement tablespaces
Query: `CREATE TABLESPACE tblspc_2 LOCATION '/';`,
Expected: []sql.Row{},
},
{
Skip: true, // TODO: Implement shared database object comments
Query: `COMMENT ON TABLESPACE tblspc_2 IS 'Store a few of the things';`,
Expected: []sql.Row{},
},
{
Skip: true, // TODO: Implement shared database object comments
Query: `SELECT shobj_description(
(SELECT oid FROM pg_tablespace WHERE spcname = 'tblspc_2'),
'pg_tablespace');`,
Cols: []string{"shobj_description"},
Expected: []sql.Row{
{"Store a few of the things"},
},
},
},
},
})
}
41 changes: 40 additions & 1 deletion testing/go/information_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,48 @@ func TestInfoSchemaTables(t *testing.T) {
{"postgres", "public"},
},
},
{
Query: `SELECT table_catalog, table_schema, table_name FROM information_schema.tables WHERE table_schema='public';`,
Expected: []sql.Row{
{"postgres", "public", "test_table"},
},
},
{
Query: `CREATE SCHEMA test_schema;`,
Expected: []sql.Row{},
},
{
Query: `SET SEARCH_PATH TO test_schema;`,
Expected: []sql.Row{},
},
{
Query: `CREATE TABLE test_table2 (id INT);`,
Expected: []sql.Row{},
},
{
Query: `SELECT DISTINCT table_schema FROM information_schema.tables order by table_schema;`,
Expected: []sql.Row{
{"information_schema"}, {"pg_catalog"}, {"public"}, {"test_schema"},
},
},
{
Query: `SELECT table_catalog, table_schema FROM information_schema.tables group by table_catalog, table_schema order by table_schema;`,
Expected: []sql.Row{
{"postgres", "information_schema"},
{"postgres", "pg_catalog"},
{"postgres", "public"},
{"postgres", "test_schema"},
},
},
{
Query: `SELECT table_catalog, table_schema, table_name FROM information_schema.tables WHERE table_schema='test_schema';`,
Expected: []sql.Row{
{"postgres", "test_schema", "test_table2"},
},
},
{
Skip: true, // TODO: need ENUM type for table_type column
Query: "SELECT * FROM PG_catalog.pg_AGGREGATE ORDER BY aggfnoid;",
Query: "SELECT * FROM information_schema.tables ORDER BY table_name;",
Expected: []sql.Row{},
},
},
Expand Down
Loading