Skip to content

Commit

Permalink
Merge #39785
Browse files Browse the repository at this point in the history
39785: sql: fix has_column_privilege builtin r=yuzefovich a=yuzefovich

Previously, has_column_privilege would treat its second argument
(when it is an integer) that specifies the column to check the
privileges on as an ordinal index of the column, but when a column
is dropped from the table, ordinal indices are adjusted, so
ordinal_position of information_schema.columns would contain a
different from attnum of pg_catalog.pg_attribute number. Now
this is fixed.

Fixes: #39703.

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Aug 21, 2019
2 parents 93860e6 + 1e2b25b commit 3f1fcb3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
33 changes: 33 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/privilege_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,36 @@ query B
SELECT has_type_privilege('bar', 'text'::REGTYPE::OID, 'USAGE')
----
true

# Regression test for #39703.

statement ok
DROP TABLE IF EXISTS hcp_test; CREATE TABLE hcp_test (a INT8, b INT8, c INT8)

statement ok
ALTER TABLE hcp_test DROP COLUMN b

query TI
SELECT attname, attnum FROM pg_attribute WHERE attrelid = 'hcp_test'::REGCLASS
----
a 1
c 3
rowid 4

query B
SELECT has_column_privilege('hcp_test'::REGCLASS, 1, 'SELECT')
----
true

statement error column 2 of relation hcp_test does not exist
SELECT has_column_privilege('hcp_test'::REGCLASS, 2, 'SELECT')

query B
SELECT has_column_privilege('hcp_test'::REGCLASS, 3, 'SELECT')
----
true

query B
SELECT has_column_privilege('hcp_test'::REGCLASS, 4, 'SELECT')
----
true
14 changes: 9 additions & 5 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,19 +1031,23 @@ SELECT description
colArg := tree.UnwrapDatum(ctx, args[1])
switch t := colArg.(type) {
case *tree.DString:
colPred = "column_name = $1"
// When colArg is a string, it specifies the attribute name.
colPred = "attname = $1"
case *tree.DInt:
colPred = "ordinal_position = $1"
// When colArg is an integer, it specifies the attribute number.
colPred = "attnum = $1"
default:
log.Fatalf(ctx.Ctx(), "expected arg type %T", t)
log.Fatalf(ctx.Ctx(), "unexpected arg type %T", t)
}

if r, err := ctx.InternalExecutor.QueryRow(
ctx.Ctx(), "has-column-privilege",
ctx.Txn,
fmt.Sprintf(`
SELECT column_name FROM information_schema.columns
WHERE %s AND %s`, pred, colPred), colArg); err != nil {
SELECT attname FROM pg_attribute
WHERE attrelid = '%s.%s.%s'::REGCLASS AND %s`,
tn.CatalogName, tn.SchemaName, tn.TableName, colPred), colArg,
); err != nil {
return nil, err
} else if r == nil {
return nil, pgerror.Newf(pgcode.UndefinedColumn,
Expand Down

0 comments on commit 3f1fcb3

Please sign in to comment.