Skip to content

Commit 5e98537

Browse files
craig[bot]mgartnerKeithChkev-cao
committed
149480: opt/optgen: refactor NormalizeLikeAny and compile string and integer literals as Go values r=mgartner a=mgartner #### opt: refactor NormalizeLikeAny The NormalizeLikeAny rule no longer allocates, memoizes, and interns a constant string expression for the literal "%" value. Release note: None #### opt/optgen: compile string and integer literals as Go values Previously, optgen would sometimes compile string and integer literals, e.g., `"foo"` and `1`, as Go strings and integers, and other times as `*tree.DString`s and `*tree.DInt`s, depending on the context. Now, these literals are always compiled as Go values. In addition to helping prevent confusion, this can help avoid unnecessary allocations for datums when they are not needed, e.g., see `NormalizeLikeAny`. Release note: None 149485: changefeedccl: add CHANGEFEED privilege to DB and Schema privileges r=rafiss a=KeithCh add CHANGEFEED privilege to DB and Schema privileges to support database/schema-level changefeeds. Resolves: #149470 Release note: None 149606: roachtest: fail on bad table lookup when fingerprinting fixtures r=msbutler a=kev-cao Some fixtures are missing table fingerprints, which results in failing restore roachtests. However, the failure is actually caused by bad fixture fingerprints, which were silently ignored due to the fact that we simply log an error and continue if we are unable to fetch all tables of a database. This commit updates the roachtest to properly fail if we fail to fetch a table. Informs: #149555, #149556, #149557, #149558, #149559, #149560, #149561 Co-authored-by: Marcus Gartner <[email protected]> Co-authored-by: Keith Chow <[email protected]> Co-authored-by: Kevin Cao <[email protected]>
4 parents cb093a6 + addc3c6 + 234b6fd + ff83aa1 commit 5e98537

File tree

17 files changed

+71
-47
lines changed

17 files changed

+71
-47
lines changed

pkg/backup/show_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ GRANT UPDATE ON top_secret TO agent_bond;
371371
`GRANT CREATE, USAGE ON SCHEMA public TO public; ` +
372372
`GRANT ALL ON SCHEMA public TO root WITH GRANT OPTION; `, `root`},
373373
{`locator`, `schema`, `GRANT ALL ON SCHEMA locator TO admin WITH GRANT OPTION; ` +
374-
`GRANT CREATE ON SCHEMA locator TO agent_bond; ` +
374+
`GRANT CHANGEFEED, CREATE ON SCHEMA locator TO agent_bond; ` +
375375
`GRANT ALL ON SCHEMA locator TO m; ` +
376376
`GRANT ALL ON SCHEMA locator TO root WITH GRANT OPTION; `, `root`},
377377
{`continent`, `type`, `GRANT ALL ON TYPE continent TO admin WITH GRANT OPTION; ` +

pkg/ccl/changefeedccl/changefeed_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,9 +3707,6 @@ func TestChangefeedGrant(t *testing.T) {
37073707
rootDB := sqlutils.MakeSQLRunner(s.DB)
37083708
rootDB.Exec(t, `create user guest`)
37093709

3710-
// GRANT CHANGEFEED ON DATABASE is an error.
3711-
rootDB.ExpectErr(t, `invalid privilege type CHANGEFEED for database`, `GRANT CHANGEFEED ON DATABASE d TO guest`)
3712-
37133710
// CHANGEFEED can be granted as a default privilege on all new tables in a schema
37143711
rootDB.ExecMultiple(t,
37153712
`ALTER DEFAULT PRIVILEGES IN SCHEMA d.public GRANT CHANGEFEED ON TABLES TO guest`,

pkg/cmd/roachtest/tests/backup_fixtures.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,7 @@ func (bd *backupDriver) fingerprintFixture(ctx context.Context) map[string]strin
415415
conn := bd.c.Conn(ctx, bd.t.L(), 1)
416416
defer conn.Close()
417417
return fingerprintDatabase(
418-
ctx, bd.t, conn,
419-
bd.sp.fixture.DatabaseName(), bd.getLatestAOST(sqlutils.MakeSQLRunner(conn)),
418+
bd.t, conn, bd.sp.fixture.DatabaseName(), bd.getLatestAOST(sqlutils.MakeSQLRunner(conn)),
420419
)
421420
}
422421

@@ -440,10 +439,10 @@ func (bd *backupDriver) getLatestAOST(sql *sqlutils.SQLRunner) string {
440439
// and returns a map of fully qualified table names to their fingerprints.
441440
// If AOST is not provided, the current time is used as the AOST.
442441
func fingerprintDatabase(
443-
ctx context.Context, t test.Test, conn *gosql.DB, dbName string, aost string,
442+
t test.Test, conn *gosql.DB, dbName string, aost string,
444443
) map[string]string {
445444
sql := sqlutils.MakeSQLRunner(conn)
446-
tables := getDatabaseTables(ctx, t, sql, dbName)
445+
tables := getDatabaseTables(t, sql, dbName)
447446
if len(tables) == 0 {
448447
t.L().Printf("no tables found in database %s", dbName)
449448
return nil
@@ -480,9 +479,7 @@ func fingerprintDatabase(
480479
// fixture.
481480
// Note: This assumes there aren't any funky characters in the identifiers, so
482481
// nothing is SQL-escaped.
483-
func getDatabaseTables(
484-
ctx context.Context, t test.Test, sql *sqlutils.SQLRunner, db string,
485-
) []string {
482+
func getDatabaseTables(t test.Test, sql *sqlutils.SQLRunner, db string) []string {
486483
tablesQuery := fmt.Sprintf(`SELECT schema_name, table_name FROM [SHOW TABLES FROM %s]`, db)
487484
rows := sql.Query(t, tablesQuery)
488485
defer rows.Close()
@@ -491,8 +488,7 @@ func getDatabaseTables(
491488
for rows.Next() {
492489
var schemaName, tableName string
493490
if err := rows.Scan(&schemaName, &tableName); err != nil {
494-
t.L().Printf("error scanning table name: %v", err)
495-
continue
491+
require.NoError(t, err, "error scanning table name")
496492
}
497493
tables = append(tables, fmt.Sprintf(`%s.%s.%s`, db, schemaName, tableName))
498494
}

pkg/cmd/roachtest/tests/restore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ func (rd *restoreDriver) maybeValidateFingerprint(ctx context.Context) {
931931
require.NoError(rd.t, err)
932932
defer conn.Close()
933933
fingerprint := fingerprintDatabase(
934-
ctx, rd.t, conn, rd.sp.backup.fixture.DatabaseName(), "", /* aost */
934+
rd.t, conn, rd.sp.backup.fixture.DatabaseName(), "", /* aost */
935935
)
936936
require.Equal(rd.t, rd.fixtureMetadata.Fingerprint, fingerprint, "fingerprint mismatch after restore")
937937
}

pkg/sql/catalog/catpb/privilege_test.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ func TestPrivilege(t *testing.T) {
138138
},
139139
privilege.Table,
140140
},
141-
// Ensure revoking BACKUP, CONNECT, CREATE, DROP, SELECT, INSERT, DELETE, UPDATE, ZONECONFIG, RESTORE
141+
// Ensure revoking BACKUP, CONNECT, CREATE, DROP, SELECT, INSERT, DELETE, UPDATE, ZONECONFIG, RESTORE, CHANGEFEED
142142
// from a user with ALL privilege on a database leaves the user with no privileges.
143143
{testUser,
144144
privilege.List{privilege.ALL},
145145
privilege.List{privilege.BACKUP, privilege.CONNECT, privilege.CREATE, privilege.DROP, privilege.SELECT,
146-
privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG, privilege.RESTORE},
146+
privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG, privilege.RESTORE, privilege.CHANGEFEED},
147147
[]catpb.UserPrivilege{
148148
{User: username.AdminRoleName(), Privileges: []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}},
149149
},
@@ -555,6 +555,16 @@ func TestGrantWithGrantOption(t *testing.T) {
555555
privilege.List{privilege.ALL, privilege.CREATE},
556556
privilege.List{privilege.ALL},
557557
privilege.List{privilege.ALL}},
558+
{catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, username.AdminRoleName()),
559+
testUser, privilege.Schema,
560+
privilege.List{privilege.CHANGEFEED},
561+
privilege.List{privilege.CHANGEFEED},
562+
privilege.List{privilege.CHANGEFEED}},
563+
{catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, username.AdminRoleName()),
564+
testUser, privilege.Database,
565+
privilege.List{privilege.CHANGEFEED},
566+
privilege.List{privilege.CHANGEFEED},
567+
privilege.List{privilege.CHANGEFEED}},
558568
}
559569

560570
for tcNum, tc := range testCases {
@@ -651,6 +661,20 @@ func TestRevokeWithGrantOption(t *testing.T) {
651661
privilege.List{},
652662
privilege.List{},
653663
true},
664+
{catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CHANGEFEED}, privilege.List{privilege.CHANGEFEED}, username.AdminRoleName()),
665+
testUser, privilege.Database,
666+
false,
667+
privilege.List{privilege.CHANGEFEED},
668+
privilege.List{},
669+
privilege.List{},
670+
true},
671+
{catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CHANGEFEED}, privilege.List{privilege.CHANGEFEED}, username.AdminRoleName()),
672+
testUser, privilege.Schema,
673+
false,
674+
privilege.List{privilege.CHANGEFEED},
675+
privilege.List{},
676+
privilege.List{},
677+
true},
654678
}
655679

656680
for tcNum, tc := range testCases {

pkg/sql/logictest/testdata/logic_test/alter_default_privileges_for_schema

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ database_name schema_name grantee privilege_type is_grantable
8484
test s2 admin ALL true
8585
test s2 root ALL true
8686
test s2 testuser CREATE false
87+
test s2 testuser CHANGEFEED false
8788
test s2 testuser2 CREATE false
89+
test s2 testuser2 CHANGEFEED false
8890

8991
statement ok
9092
ALTER DEFAULT PRIVILEGES REVOKE ALL ON SCHEMAS FROM testuser, testuser2

pkg/sql/logictest/testdata/logic_test/grant_database

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ SHOW GRANTS ON DATABASE a
5858
a admin ALL true
5959
a public CONNECT false
6060
a readwrite BACKUP true
61+
a readwrite CHANGEFEED true
6162
a readwrite CREATE true
6263
a readwrite DROP true
6364
a readwrite RESTORE true
6465
a readwrite ZONECONFIG true
6566
a root ALL true
6667
a test-user BACKUP true
68+
a test-user CHANGEFEED true
6769
a test-user CREATE true
6870
a test-user DROP true
6971
a test-user RESTORE true
@@ -74,11 +76,13 @@ SHOW GRANTS ON DATABASE a FOR readwrite, "test-user"
7476
----
7577
a public CONNECT false
7678
a readwrite BACKUP true
79+
a readwrite CHANGEFEED true
7780
a readwrite CREATE true
7881
a readwrite DROP true
7982
a readwrite RESTORE true
8083
a readwrite ZONECONFIG true
8184
a test-user BACKUP true
85+
a test-user CHANGEFEED true
8286
a test-user CREATE true
8387
a test-user DROP true
8488
a test-user RESTORE true
@@ -93,12 +97,14 @@ SHOW GRANTS ON DATABASE a
9397
a admin ALL true
9498
a public CONNECT false
9599
a readwrite BACKUP true
100+
a readwrite CHANGEFEED true
96101
a readwrite CREATE true
97102
a readwrite DROP true
98103
a readwrite RESTORE true
99104
a readwrite ZONECONFIG true
100105
a root ALL true
101106
a test-user BACKUP true
107+
a test-user CHANGEFEED true
102108
a test-user DROP true
103109
a test-user RESTORE true
104110
a test-user ZONECONFIG true
@@ -111,6 +117,7 @@ SHOW GRANTS ON DATABASE a FOR readwrite, "test-user"
111117
----
112118
a public CONNECT false
113119
a readwrite BACKUP true
120+
a readwrite CHANGEFEED true
114121
a readwrite CREATE true
115122
a readwrite DROP true
116123
a readwrite RESTORE true

pkg/sql/opt/norm/general_funcs.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,16 +1540,16 @@ func (c *CustomFuncs) CanAddConstInts(first tree.Datum, second tree.Datum) bool
15401540
return ok
15411541
}
15421542

1543+
// DInt returns a new *tree.DInt with the given integer value.
1544+
func (c *CustomFuncs) DInt(i tree.DInt) *tree.DInt {
1545+
return tree.NewDInt(i)
1546+
}
1547+
15431548
// IntConst constructs a Const holding a DInt.
15441549
func (c *CustomFuncs) IntConst(d *tree.DInt) opt.ScalarExpr {
15451550
return c.f.ConstructConst(d, types.Int)
15461551
}
15471552

1548-
// StrConst constructs a Const holding a DString.
1549-
func (c *CustomFuncs) StrConst(d *tree.DString) opt.ScalarExpr {
1550-
return c.f.ConstructConst(d, types.String)
1551-
}
1552-
15531553
// StringFromConst extracts a string from a Const expression. It returns the
15541554
// string and a boolean indicating whether the extraction was successful.
15551555
func (c *CustomFuncs) StringFromConst(expr opt.ScalarExpr) (string, bool) {
@@ -1565,14 +1565,13 @@ func (c *CustomFuncs) StringFromConst(expr opt.ScalarExpr) (string, bool) {
15651565
return "", false
15661566
}
15671567

1568-
// EqualConstString compares two Const expressions to see if they hold equal string values.
1569-
func (c *CustomFuncs) EqualConstStrings(left, right opt.ScalarExpr) bool {
1570-
leftStr, okLeft := c.StringFromConst(left)
1571-
rightStr, okRight := c.StringFromConst(right)
1572-
if !okLeft || !okRight {
1573-
return false
1568+
// ConstStringEquals returns true if e is a constant string expression and is
1569+
// equal to other.
1570+
func (c *CustomFuncs) ConstStringEquals(e opt.ScalarExpr, other string) bool {
1571+
if eStr, ok := c.StringFromConst(e); ok {
1572+
return eStr == other
15741573
}
1575-
return leftStr == rightStr
1574+
return false
15761575
}
15771576

15781577
// IsGreaterThan returns true if the first datum compares as greater than the

pkg/sql/opt/norm/rules/decorrelate.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@
707707
$left:*
708708
$right:(Limit $input:* (Const $limit:*) $ordering:*) &
709709
(HasOuterCols $right) &
710-
(IsGreaterThan $limit 1)
710+
(IsGreaterThan $limit (DInt 1))
711711
$on:*
712712
$private:*
713713
)

pkg/sql/opt/norm/rules/groupby.opt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
(ConstructProjectionFromDistinctOn
325325
(Limit
326326
$input
327-
(IntConst 1)
327+
(IntConst (DInt 1))
328328
(GroupingInputOrdering $groupingPrivate)
329329
)
330330
(MakeEmptyColSet)
@@ -610,7 +610,7 @@
610610
# a. The aggregate only references cols from the Window operator's input.
611611
# b. The aggregate is a ConstAgg (or ConstNotNull, AnyNotNull, or FirstAgg)
612612
# that passes through the result of a window function.
613-
#
613+
#
614614
# Assuming all of the above are satisfied, each GroupBy aggregate that only
615615
# references the Window's input can be left alone (5a). Then, each ConstAgg
616616
# referencing a window function can be replaced by that function (5b).

0 commit comments

Comments
 (0)