Skip to content

Commit c9d7737

Browse files
committed
sql: small fixes to nameconcatoid
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
1 parent 969b575 commit c9d7737

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pkg/sql/information_schema.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,7 @@ var informationSchemaRoleRoutineGrantsTable = virtualSchemaTable{
17741774

17751775
_, overloads := builtinsregistry.GetBuiltinProperties(name)
17761776
for _, o := range overloads {
1777-
fnSpecificName := tree.NewDString(fmt.Sprintf("%s_%d", fnNameStr, o.Oid))
1777+
fnSpecificName := tree.NewDString(nameConcatOid(fnNameStr, o.Oid))
17781778
for _, grantee := range roleNameForBuiltins {
17791779
if err := addRow(
17801780
tree.DNull, // grantor
@@ -1816,7 +1816,8 @@ var informationSchemaRoleRoutineGrantsTable = virtualSchemaTable{
18161816
return err
18171817
}
18181818
scNameStr := tree.NewDString(sc.GetName())
1819-
fnSpecificName := tree.NewDString(fmt.Sprintf("%s_%d", fn.GetName(), catid.FuncIDToOID(fn.GetID())))
1819+
1820+
fnSpecificName := tree.NewDString(nameConcatOid(fn.GetName(), catid.FuncIDToOID(fn.GetID())))
18201821
fnName := tree.NewDString(fn.GetName())
18211822
for _, u := range privs {
18221823
userNameStr := tree.NewDString(u.User.Normalized())
@@ -2955,3 +2956,16 @@ func userCanSeeDescriptor(
29552956
func descriptorIsVisible(desc catalog.Descriptor, allowAdding bool) bool {
29562957
return desc.Public() || (allowAdding && desc.Adding())
29572958
}
2959+
2960+
// nameConcatOid is a Go version of the nameconcatoid builtin function. The
2961+
// result is the same as fmt.Sprintf("%s_%d", s, o) except that, if it would not
2962+
// fit in 63 characters, we make it do so by truncating the name input (not the
2963+
// oid).
2964+
func nameConcatOid(s string, o oid.Oid) string {
2965+
const maxLen = 63
2966+
oidStr := strconv.Itoa(int(o))
2967+
if len(s)+1+len(oidStr) <= maxLen {
2968+
return s + "_" + oidStr
2969+
}
2970+
return s[:maxLen-1-len(oidStr)] + "_" + oidStr
2971+
}

pkg/sql/logictest/testdata/logic_test/pg_builtins

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,9 @@ select nameconcatoid(repeat('a', 58) || 'bbbbbbbbbb', 2);
882882
----
883883
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb_2
884884

885+
query TI
886+
select nameconcatoid(repeat('a', 62), 2), length(nameconcatoid(repeat('a', 62), 2))
887+
----
888+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2 63
889+
885890
subtest end

pkg/sql/sem/builtins/pg_builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ var pgBuiltins = map[string]builtinDefinition{
21912191
ReturnType: tree.FixedReturnType(types.Name),
21922192
Body: `
21932193
SELECT
2194-
CASE WHEN length($1::text || '_' || $2::text) > 64
2194+
CASE WHEN length($1::text || '_' || $2::text) > 63
21952195
THEN (substring($1 from 1 for 63 - length($2::text) - 1) || '_' || $2::text)::name
21962196
ELSE ($1::text || '_' || $2::text)::name
21972197
END

0 commit comments

Comments
 (0)