Skip to content

sql: implement to_reg* builtins#78652

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
e-mbrown:eb/toreg
Apr 5, 2022
Merged

sql: implement to_reg* builtins#78652
craig[bot] merged 1 commit intocockroachdb:masterfrom
e-mbrown:eb/toreg

Conversation

@e-mbrown
Copy link
Copy Markdown
Contributor

Resolves #77838
This commit implements the to_regclass, to_regnamespace, to_regproc,
to_regprocedure, to_regrole, and to_regtype builtins.

Release note (<category, see below>): The to_regclass, to_regnamespace, to_regproc,
to_regprocedure, to_regrole, and to_regtype builtin functions are now supported,
improving compatibility with PostgreSQL.

@e-mbrown e-mbrown requested a review from a team March 28, 2022 18:49
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@e-mbrown e-mbrown force-pushed the eb/toreg branch 6 times, most recently from 5f8bed7 to d3b71f2 Compare March 29, 2022 17:27
Comment on lines +2135 to +2152
Types: tree.ArgTypes{
{"text", types.String},
},
ReturnType: tree.FixedReturnType(types.RegClass),
Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
typName := tree.MustBeDString(args[0])

int, _ := strconv.Atoi(string(typName))
if int > 0 {
return tree.DNull, nil
}
typOid, err := tree.ParseDOid(ctx, string(typName), types.RegClass)
if err != nil {
//nolint:returnerrcheck
return tree.DNull, nil
}

return typOid, nil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the builtins here are all similar. can we unify this overload to be a function?

e.g.

func makeToRegOverload(typ *types.T, helpText string) builtinDefinition {
   makeBuiltin(tree.FunctionProperties{Category: categorySystemInfo},
		tree.Overload{
			Types: tree.ArgTypes{
				{"text", types.String},
			},
			ReturnType: tree.FixedReturnType(typ),
			Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
				n := tree.MustBeDString(args[0])

				int, _ := strconv.Atoi(string(n))
				if int > 0 {
					return tree.DNull, nil
				}
				typOid, err := tree.ParseDOid(ctx, string(n), typ)
				if err != nil {
					//nolint:returnerrcheck
					return tree.DNull, nil
				}

				return n, nil
			},
			Info:       helpText,
			Volatility: tree.VolatilityStable,
		},
	),
}

Copy link
Copy Markdown
Contributor Author

@e-mbrown e-mbrown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan)


pkg/sql/sem/builtins/pg_builtins.go, line 2152 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…

the builtins here are all similar. can we unify this overload to be a function?

e.g.

func makeToRegOverload(typ *types.T, helpText string) builtinDefinition {
   makeBuiltin(tree.FunctionProperties{Category: categorySystemInfo},
		tree.Overload{
			Types: tree.ArgTypes{
				{"text", types.String},
			},
			ReturnType: tree.FixedReturnType(typ),
			Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
				n := tree.MustBeDString(args[0])

				int, _ := strconv.Atoi(string(n))
				if int > 0 {
					return tree.DNull, nil
				}
				typOid, err := tree.ParseDOid(ctx, string(n), typ)
				if err != nil {
					//nolint:returnerrcheck
					return tree.DNull, nil
				}

				return n, nil
			},
			Info:       helpText,
			Volatility: tree.VolatilityStable,
		},
	),
}

Done.

}

// Make crdb_internal.create_regfoo and to_regfoo builtins.
for i, typ := range []*types.T{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do this:

for _, b := range []struct{
   toRegOverloadHelpText string
   typ *types.T
} {
   {toRegOverloadHgelpText: "Translates a textual relation name to its OID." typ: types.RegClass},
   ...
} {

		typName := b.typ.SQLStandardName()
		builtins["crdb_internal.create_"+typName] = makeCreateRegDef(b.typ)
		builtins["to_"+typName] = makeToRegOverload(b.typ)
}

so we can have everything grouped together

SELECT to_regclass('t1')
----
t1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space

----
NULL


Copy link
Copy Markdown
Contributor

@otan otan Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: delete extra \n

Fn: func(ctx *tree.EvalContext, args tree.Datums) (tree.Datum, error) {
typName := tree.MustBeDString(args[0])

int, _ := strconv.Atoi(string(typName))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we need to strings.TrimSpace here

NULL

# Should return null, but leading space leads to it returning an oid
# Not sure if leading spaces should be removed.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(in pg this returns NULL so we need to remove the space)

Copy link
Copy Markdown
Contributor Author

@e-mbrown e-mbrown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan)


pkg/sql/sem/builtins/pg_builtins.go, line 160 at r2 (raw file):

Previously, otan (Oliver Tan) wrote…

let's do this:

for _, b := range []struct{
   toRegOverloadHelpText string
   typ *types.T
} {
   {toRegOverloadHgelpText: "Translates a textual relation name to its OID." typ: types.RegClass},
   ...
} {

		typName := b.typ.SQLStandardName()
		builtins["crdb_internal.create_"+typName] = makeCreateRegDef(b.typ)
		builtins["to_"+typName] = makeToRegOverload(b.typ)
}

so we can have everything grouped together

Done.


pkg/sql/sem/builtins/pg_builtins.go, line 544 at r2 (raw file):

Previously, otan (Oliver Tan) wrote…

maybe we need to strings.TrimSpace here

Done.


pkg/sql/logictest/testdata/logic_test/pg_builtins, line 661 at r2 (raw file):

Previously, otan (Oliver Tan) wrote…

(in pg this returns NULL so we need to remove the space)

Done.

@e-mbrown e-mbrown force-pushed the eb/toreg branch 2 times, most recently from cb8fbf2 to 6c164bb Compare April 5, 2022 14:29
@rafiss
Copy link
Copy Markdown
Collaborator

rafiss commented Apr 5, 2022

when running dev generate bazel, make sure you don't have any other uncommitted changes that are not related to this commit.

This commit implements the `to_regclass`, `to_regnamespace`, `to_regproc`,
`to_regprocedure`, `to_regrole`, and `to_regtype` builtins.

Release note (sql change): The `to_regclass`, `to_regnamespace`, `to_regproc`,
`to_regprocedure`, `to_regrole`, and `to_regtype` builtin functions are now
supported, improving compatibility with PostgreSQL.
@e-mbrown
Copy link
Copy Markdown
Contributor Author

e-mbrown commented Apr 5, 2022

bors r=otan

@craig craig bot merged commit 985344a into cockroachdb:master Apr 5, 2022
@craig
Copy link
Copy Markdown
Contributor

craig bot commented Apr 5, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

builtins: add to_reg* functions

4 participants