Skip to content

Commit

Permalink
Merge pull request #83216 from ecwall/backport22.1-83122
Browse files Browse the repository at this point in the history
release-22.1: sql: disable setting ttl = 'off'
  • Loading branch information
ecwall authored Jun 22, 2022
2 parents 7fb1cf5 + 4aadac4 commit 77e17ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 77 deletions.
37 changes: 6 additions & 31 deletions pkg/sql/logictest/testdata/logic_test/row_level_ttl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ CREATE INDEX tbl_idx ON tbl (text)
statement ok
ROLLBACK

# Cannot reset TTL with SET (ttl = off)
statement error setting "ttl = 'off'" is not permitted
ALTER TABLE tbl SET (ttl = 'off')

# Test when we drop the TTL, ensure column is dropped and the scheduled job is removed.
statement ok
ALTER TABLE tbl RESET (ttl)
Expand All @@ -138,36 +142,6 @@ WHERE label LIKE 'row-level-ttl-%'
----
0

# Check the same thing with SET (ttl = off)
statement ok
DROP TABLE tbl

statement ok
CREATE TABLE tbl (
id INT PRIMARY KEY,
text TEXT,
FAMILY (id, text)
) WITH (ttl_expire_after = '10 minutes')

statement ok
ALTER TABLE tbl SET (ttl = 'off')

query T
SELECT create_statement FROM [SHOW CREATE TABLE tbl]
----
CREATE TABLE public.tbl (
id INT8 NOT NULL,
text STRING NULL,
CONSTRAINT tbl_pkey PRIMARY KEY (id ASC),
FAMILY fam_0_id_text_crdb_internal_expiration (id, text)
)

query I
SELECT count(1) FROM [SHOW SCHEDULES]
WHERE label LIKE 'row-level-ttl-%'
----
0

# Ensure schedules are removed on DROP TABLE.
statement ok
DROP TABLE tbl;
Expand Down Expand Up @@ -380,7 +354,8 @@ ACTIVE @hourly root

statement ok
CREATE TABLE no_ttl_table ();
ALTER TABLE no_ttl_table SET (ttl = 'off');

statement error unsetting TTL automatic column not yet implemented
ALTER TABLE no_ttl_table SET (ttl_automatic_column = 'off')

statement error "ttl_expire_after" must be set
Expand Down
92 changes: 46 additions & 46 deletions pkg/sql/storageparam/tablestorageparam/table_storage_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func floatFromDatum(evalCtx *tree.EvalContext, key string, datum tree.Datum) (fl
return s, nil
}

func (po *Setter) getOrCreateRowLevelTTL() *catpb.RowLevelTTL {
rowLevelTTL := &po.tableDesc.RowLevelTTL
if *rowLevelTTL == nil {
*rowLevelTTL = &catpb.RowLevelTTL{}
}
return *rowLevelTTL
}

type tableParam struct {
onSet func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error
onReset func(po *Setter, evalCtx *tree.EvalContext, key string) error
Expand Down Expand Up @@ -146,13 +154,18 @@ var tableParams = map[string]tableParam{
if err != nil {
return err
}
if setTrue && po.tableDesc.RowLevelTTL == nil {
if setTrue {
// Set the base struct, but do not populate it.
// An error from runPostChecks will appear if the requisite fields are not set.
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
if !setTrue && po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL = nil
po.getOrCreateRowLevelTTL()
} else {
return errors.WithHintf(
pgerror.Newf(
pgcode.InvalidParameterValue,
`setting "ttl = 'off'" is not permitted`,
),
"use `RESET (ttl)` to remove TTL from the table",
)
}
return nil
},
Expand All @@ -169,8 +182,7 @@ var tableParams = map[string]tableParam{
}
if setTrue {
po.setAutomaticColumn = true
}
if !setTrue && po.tableDesc.RowLevelTTL != nil {
} else {
return unimplemented.NewWithIssue(76916, "unsetting TTL automatic column not yet implemented")
}
return nil
Expand Down Expand Up @@ -209,10 +221,8 @@ var tableParams = map[string]tableParam{
key,
)
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
po.tableDesc.RowLevelTTL.DurationExpr = catpb.Expression(tree.Serialize(d))
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.DurationExpr = catpb.Expression(tree.Serialize(d))
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -227,17 +237,15 @@ var tableParams = map[string]tableParam{
},
`ttl_select_batch_size`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := paramparse.DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLBatchSize(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.SelectBatchSize = val
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.SelectBatchSize = val
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -249,17 +257,15 @@ var tableParams = map[string]tableParam{
},
`ttl_delete_batch_size`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := paramparse.DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLBatchSize(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeleteBatchSize = val
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.DeleteBatchSize = val
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -271,17 +277,15 @@ var tableParams = map[string]tableParam{
},
`ttl_range_concurrency`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := paramparse.DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLRangeConcurrency(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.RangeConcurrency = val
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.RangeConcurrency = val
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -293,17 +297,15 @@ var tableParams = map[string]tableParam{
},
`ttl_delete_rate_limit`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := paramparse.DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLRateLimit(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeleteRateLimit = val
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.DeleteRateLimit = val
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -315,34 +317,32 @@ var tableParams = map[string]tableParam{
},
`ttl_label_metrics`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
po.tableDesc.RowLevelTTL.LabelMetrics = val
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.LabelMetrics = val
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL.LabelMetrics = false
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.LabelMetrics = false
}
return nil
},
},
`ttl_job_cron`: {
onSet: func(ctx context.Context, po *Setter, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
str, err := paramparse.DatumAsString(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLCronExpr(key, str); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeletionCron = str
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.DeletionCron = str
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
Expand All @@ -358,14 +358,14 @@ var tableParams = map[string]tableParam{
if err != nil {
return err
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
po.tableDesc.RowLevelTTL.Pause = b
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.Pause = b
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL.Pause = false
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.Pause = false
}
return nil
},
},
Expand All @@ -375,17 +375,17 @@ var tableParams = map[string]tableParam{
if err != nil {
return err
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
if err := tabledesc.ValidateTTLRowStatsPollInterval(key, d); err != nil {
return err
}
po.tableDesc.RowLevelTTL.RowStatsPollInterval = d
rowLevelTTL := po.getOrCreateRowLevelTTL()
rowLevelTTL.RowStatsPollInterval = d
return nil
},
onReset: func(po *Setter, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL.RowStatsPollInterval = 0
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.RowStatsPollInterval = 0
}
return nil
},
},
Expand Down

0 comments on commit 77e17ee

Please sign in to comment.