Skip to content

Commit

Permalink
sql: small fixes to nameconcatoid
Browse files Browse the repository at this point in the history
There was an edge case if the result was exactly 64 characters where the
input would not be truncated correctly. This also makes the
information_schema.role_routine_grants table use the same logic for
computing the specific_name.

Release note: None
  • Loading branch information
rafiss committed Jul 21, 2023
1 parent 969b575 commit c9d7737
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
18 changes: 16 additions & 2 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ var informationSchemaRoleRoutineGrantsTable = virtualSchemaTable{

_, overloads := builtinsregistry.GetBuiltinProperties(name)
for _, o := range overloads {
fnSpecificName := tree.NewDString(fmt.Sprintf("%s_%d", fnNameStr, o.Oid))
fnSpecificName := tree.NewDString(nameConcatOid(fnNameStr, o.Oid))
for _, grantee := range roleNameForBuiltins {
if err := addRow(
tree.DNull, // grantor
Expand Down Expand Up @@ -1816,7 +1816,8 @@ var informationSchemaRoleRoutineGrantsTable = virtualSchemaTable{
return err
}
scNameStr := tree.NewDString(sc.GetName())
fnSpecificName := tree.NewDString(fmt.Sprintf("%s_%d", fn.GetName(), catid.FuncIDToOID(fn.GetID())))

fnSpecificName := tree.NewDString(nameConcatOid(fn.GetName(), catid.FuncIDToOID(fn.GetID())))
fnName := tree.NewDString(fn.GetName())
for _, u := range privs {
userNameStr := tree.NewDString(u.User.Normalized())
Expand Down Expand Up @@ -2955,3 +2956,16 @@ func userCanSeeDescriptor(
func descriptorIsVisible(desc catalog.Descriptor, allowAdding bool) bool {
return desc.Public() || (allowAdding && desc.Adding())
}

// nameConcatOid is a Go version of the nameconcatoid builtin function. The
// result is the same as fmt.Sprintf("%s_%d", s, o) except that, if it would not
// fit in 63 characters, we make it do so by truncating the name input (not the
// oid).
func nameConcatOid(s string, o oid.Oid) string {
const maxLen = 63
oidStr := strconv.Itoa(int(o))
if len(s)+1+len(oidStr) <= maxLen {
return s + "_" + oidStr
}
return s[:maxLen-1-len(oidStr)] + "_" + oidStr
}
5 changes: 5 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -882,4 +882,9 @@ select nameconcatoid(repeat('a', 58) || 'bbbbbbbbbb', 2);
----
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb_2

query TI
select nameconcatoid(repeat('a', 62), 2), length(nameconcatoid(repeat('a', 62), 2))
----
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2 63

subtest end
2 changes: 1 addition & 1 deletion pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ var pgBuiltins = map[string]builtinDefinition{
ReturnType: tree.FixedReturnType(types.Name),
Body: `
SELECT
CASE WHEN length($1::text || '_' || $2::text) > 64
CASE WHEN length($1::text || '_' || $2::text) > 63
THEN (substring($1 from 1 for 63 - length($2::text) - 1) || '_' || $2::text)::name
ELSE ($1::text || '_' || $2::text)::name
END
Expand Down

0 comments on commit c9d7737

Please sign in to comment.