Skip to content
Open
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
5 changes: 5 additions & 0 deletions server/functions/framework/compiled_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ func (c *CompiledFunction) GetQuickFunction(ctx *sql.Context) QuickFunction {
len(c.overload.casts) > 0 {
return nil
}
// QuickFunctions do not implement sql.ExtendedTableFunction, so a function with a multi-column result would lose
// its result columns in a FROM clause, and its rows would not be collapsed into records in a SELECT list.
if len(c.OutParametersSchema()) > 0 {
return nil
}
// QuickFunctions evaluate their argument expressions directly, without the GMS value conversion that
// CompiledFunction.Eval performs, so any GMS-typed arguments (e.g. columns of the dolt_* system tables) must be
// wrapped to convert their values.
Expand Down
2 changes: 2 additions & 0 deletions server/functions/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func Init() {
initPgOpclassIsVisible()
initPgOperatorIsVisible()
initPgOpfamilyIsVisible()
initPgOptionsToTable()
initPgPartitionAncestors()
initPgPostmasterStartTime()
initPgRelationIsPublishable()
Expand All @@ -232,6 +233,7 @@ func Init() {
initPi()
initPower()
initQuoteIdent()
initQuoteLiteral()
initRadians()
initRandom()
initRegexpReplace()
Expand Down
67 changes: 67 additions & 0 deletions server/functions/pg_options_to_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2026 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 (
"io"
"strings"

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

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

// initPgOptionsToTable registers the functions to the catalog.
func initPgOptionsToTable() {
framework.RegisterFunction(pg_options_to_table_text_array)
}

// pgOptionsToTableName is the name of the pg_options_to_table function.
const pgOptionsToTableName = "pg_options_to_table"

// pg_options_to_table_text_array represents the PostgreSQL function of the same name. It splits each "name=value"
// entry of an options array, such as pg_class.reloptions, into a row. As in PostgreSQL, an entry is split at its first
// "=", and an entry without one has a NULL value.
var pg_options_to_table_text_array = framework.Function1{
Name: pgOptionsToTableName,
Return: pgtypes.Record, // SETOF record
Parameters: [1]*pgtypes.DoltgresType{pgtypes.TextArray},
Strict: true,
SRF: true,
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
options := val.([]any)
i := 0
return pgtypes.NewSetReturningFunctionRowIter(func(ctx *sql.Context) (sql.Row, error) {
for i < len(options) {
option := options[i]
i++
if option == nil {
continue
}
name, value, found := strings.Cut(option.(string), "=")
if !found {
return sql.Row{name, nil}, nil
}
return sql.Row{name, value}, nil
}
return nil, io.EOF
}), nil
},
OutParams: sql.Schema{
{Name: "option_name", Type: pgtypes.Text, Default: nil, Nullable: false, Source: pgOptionsToTableName},
{Name: "option_value", Type: pgtypes.Text, Default: nil, Nullable: true, Source: pgOptionsToTableName},
},
}
99 changes: 99 additions & 0 deletions server/functions/quote_literal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2026 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"
)

// initQuoteLiteral registers the functions to the catalog.
func initQuoteLiteral() {
framework.RegisterFunction(quote_literal_text)
framework.RegisterFunction(quote_literal_anyelement)
framework.RegisterFunction(quote_nullable_text)
framework.RegisterFunction(quote_nullable_anyelement)
}

// quote_literal_text represents the PostgreSQL function of the same name, taking the same parameters. The quoting is
// done by quoteLiteral, which is shared with format's %L.
var quote_literal_text = framework.Function1{
Name: "quote_literal",
Return: pgtypes.Text,
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Text},
Strict: true,
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
str, err := framework.UnwrapString(ctx, val)
if err != nil {
return nil, err
}
return quoteLiteral(str), nil
},
}

// quote_literal_anyelement represents the PostgreSQL function of the same name, taking the same parameters. The value
// is converted to its text form before being quoted.
var quote_literal_anyelement = framework.Function1{
Name: "quote_literal",
Return: pgtypes.Text,
Parameters: [1]*pgtypes.DoltgresType{pgtypes.AnyElement},
Strict: true,
Callable: func(ctx *sql.Context, paramsAndReturn [2]*pgtypes.DoltgresType, val any) (any, error) {
str, err := paramsAndReturn[0].IoOutput(ctx, val)
if err != nil {
return nil, err
}
return quoteLiteral(str), nil
},
}

// quote_nullable_text represents the PostgreSQL function of the same name, taking the same parameters. Unlike
// quote_literal, a NULL value returns the unquoted string NULL.
var quote_nullable_text = framework.Function1{
Name: "quote_nullable",
Return: pgtypes.Text,
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Text},
Strict: false,
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
if val == nil {
return "NULL", nil
}
str, err := framework.UnwrapString(ctx, val)
if err != nil {
return nil, err
}
return quoteLiteral(str), nil
},
}

// quote_nullable_anyelement represents the PostgreSQL function of the same name, taking the same parameters.
var quote_nullable_anyelement = framework.Function1{
Name: "quote_nullable",
Return: pgtypes.Text,
Parameters: [1]*pgtypes.DoltgresType{pgtypes.AnyElement},
Strict: false,
Callable: func(ctx *sql.Context, paramsAndReturn [2]*pgtypes.DoltgresType, val any) (any, error) {
if val == nil {
return "NULL", nil
}
str, err := paramsAndReturn[0].IoOutput(ctx, val)
if err != nil {
return nil, err
}
return quoteLiteral(str), nil
},
}
72 changes: 72 additions & 0 deletions testing/go/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,78 @@ func TestSystemInformationFunctions(t *testing.T) {
},
},
},
{
Name: "pg_options_to_table",
SetUpScript: []string{
`CREATE TABLE opts (k INT PRIMARY KEY, o TEXT[]);`,
`INSERT INTO opts VALUES (1, ARRAY['b=1', 'a']), (2, NULL), (3, ARRAY[]::text[]), (4, ARRAY['z=it''s']);`,
},
Assertions: []ScriptTestAssertion{
{
// Entries are split at the first "=", and an entry without one has a NULL value
Query: `SELECT * FROM pg_options_to_table(ARRAY['fillfactor=70', 'a=b=c', 'noval'])`,
ExpectedColNames: []string{"option_name", "option_value"},
Expected: []sql.Row{{"fillfactor", "70"}, {"a", "b=c"}, {"noval", nil}},
},
{
Query: `SELECT option_value FROM pg_options_to_table('{x=1}'::text[])`,
Expected: []sql.Row{{"1"}},
},
{
Query: `SELECT o.option_name FROM pg_options_to_table(ARRAY['x=1', 'y=2']) AS o WHERE o.option_value = '2'`,
Expected: []sql.Row{{"y"}},
},
{
Query: `SELECT * FROM pg_options_to_table(NULL)`,
Expected: []sql.Row{},
},
{
Query: `SELECT * FROM pg_options_to_table(ARRAY[NULL, 'x=1'])`,
Expected: []sql.Row{{"x", "1"}},
},
{
// In a SELECT list, each row is a record
Query: `SELECT pg_options_to_table(ARRAY['x=1', 'y'])`,
Expected: []sql.Row{{[]any{"x", "1"}}, {[]any{"y", nil}}},
},
{
// The form that pg_dump uses, correlated with the outer table
Query: `SELECT k, array_to_string(ARRAY(SELECT quote_ident(option_name) || ' ' || quote_literal(option_value) FROM pg_options_to_table(o) ORDER BY option_name), ', ') FROM opts ORDER BY k`,
Expected: []sql.Row{
{1, `"b" '1'`},
{2, ""},
{3, ""},
{4, `"z" 'it''s'`},
},
},
},
},
{
Name: "quote_literal and quote_nullable",
Assertions: []ScriptTestAssertion{
{
Query: `SELECT quote_literal('abc'), quote_literal('it''s'), quote_literal(E'back\\slash'), quote_literal('')`,
Expected: []sql.Row{{`'abc'`, `'it''s'`, `E'back\\slash'`, `''`}},
},
{
// As in PostgreSQL, the value is converted using its cast to text, so true becomes 'true'
Query: `SELECT quote_literal(42), quote_literal(1.5::numeric), quote_literal(true), quote_literal('2024-01-02'::date)`,
Expected: []sql.Row{{`'42'`, `'1.5'`, `'true'`, `'2024-01-02'`}},
},
{
Query: `SELECT quote_literal(NULL::text) IS NULL, quote_literal(NULL::int) IS NULL`,
Expected: []sql.Row{{"t", "t"}},
},
{
Query: `SELECT quote_nullable('it''s'), quote_nullable(42), quote_nullable(NULL::text), quote_nullable(NULL::int)`,
Expected: []sql.Row{{`'it''s'`, `'42'`, `NULL`, `NULL`}},
},
{
Query: `SELECT pg_typeof(quote_literal('abc'))`,
Expected: []sql.Row{{"text"}},
},
},
},
})
}

Expand Down
Loading