Skip to content

Commit

Permalink
parser: add global variable to control ttl syntax (#39483)
Browse files Browse the repository at this point in the history
close #39482
  • Loading branch information
YangKeao authored Dec 1, 2022
1 parent c3565a1 commit 6a4b909
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 0 deletions.
5 changes: 5 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
Expand Down Expand Up @@ -1524,6 +1525,8 @@ func TestRenameMultiTables(t *testing.T) {
}

func TestCreateTableWithTTL(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand All @@ -1543,6 +1546,8 @@ func TestCreateTableWithTTL(t *testing.T) {
}

func TestAlterTTLInfo(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
Expand Down
1 change: 1 addition & 0 deletions executor/showtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_test(
"//executor",
"//infoschema",
"//meta/autoid",
"//parser",
"//parser/auth",
"//parser/model",
"//parser/mysql",
Expand Down
3 changes: 3 additions & 0 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
Expand Down Expand Up @@ -1991,6 +1992,8 @@ func TestShowLimitReturnRow(t *testing.T) {
}

func TestShowTTLOption(t *testing.T) {
parser.TTLFeatureGate = true

store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
Expand Down
1 change: 1 addition & 0 deletions parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"lexer.go",
"misc.go",
"parser.go",
"ttlfeaturegate.go",
"yy_parser.go",
],
importpath = "github.com/pingcap/tidb/parser",
Expand Down
3 changes: 3 additions & 0 deletions parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ast_test
import (
"testing"

"github.com/pingcap/tidb/parser"
. "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -842,6 +843,8 @@ func TestFlashBackDatabaseRestore(t *testing.T) {
}

func TestTableOptionTTLRestore(t *testing.T) {
parser.TTLFeatureGate = true

sourceSQL1 := "create table t (created_at datetime) ttl = created_at + INTERVAL 1 YEAR"
sourceSQL2 := "alter table t ttl_enable = 'OFF'"
sourceSQL3 := "alter table t remove ttl"
Expand Down
12 changes: 12 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11924,6 +11924,10 @@ yynewstate:
}
case 38:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
parser.yyVAL.item = &ast.AlterTableSpec{
Tp: ast.AlterTableRemoveTTL,
}
Expand Down Expand Up @@ -20038,6 +20042,10 @@ yynewstate:
}
case 2153:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
parser.yyVAL.item = &ast.TableOption{
Tp: ast.TableOptionTTL,
ColumnName: &ast.ColumnName{Name: model.NewCIStr(yyS[yypt-4].ident)},
Expand All @@ -20047,6 +20055,10 @@ yynewstate:
}
case 2154:
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
onOrOff := strings.ToLower(yyS[yypt-0].ident)
if onOrOff == "on" {
parser.yyVAL.item = &ast.TableOption{Tp: ast.TableOptionTTLEnable, BoolValue: true}
Expand Down
12 changes: 12 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,10 @@ AlterTableSpecSingleOpt:
}
| "REMOVE" "TTL"
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
$$ = &ast.AlterTableSpec{
Tp: ast.AlterTableRemoveTTL,
}
Expand Down Expand Up @@ -11779,6 +11783,10 @@ TableOption:
}
| "TTL" EqOpt Identifier '+' "INTERVAL" Literal TimeUnit
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
$$ = &ast.TableOption{
Tp: ast.TableOptionTTL,
ColumnName: &ast.ColumnName{Name: model.NewCIStr($3)},
Expand All @@ -11788,6 +11796,10 @@ TableOption:
}
| "TTL_ENABLE" EqOpt stringLit
{
if !TTLFeatureGate {
yylex.AppendError(ErrSyntax)
return 1
}
onOrOff := strings.ToLower($3)
if onOrOff == "on" {
$$ = &ast.TableOption{Tp: ast.TableOptionTTLEnable, BoolValue: true}
Expand Down
20 changes: 20 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7014,6 +7014,8 @@ func TestIntervalPartition(t *testing.T) {
}

func TestTTLTableOption(t *testing.T) {
parser.TTLFeatureGate = true

table := []testCase{
// create table with various temporal interval
{"create table t (created_at datetime) TTL = created_at + INTERVAL 3.1415 YEAR", true, "CREATE TABLE `t` (`created_at` DATETIME) TTL = `created_at` + INTERVAL 3.1415 YEAR"},
Expand Down Expand Up @@ -7041,3 +7043,21 @@ func TestTTLTableOption(t *testing.T) {

RunTest(t, table, false)
}

func TestTTLFeatureGate(t *testing.T) {
parser.TTLFeatureGate = false

table := []testCase{
{"create table t (created_at datetime) TTL = created_at + INTERVAL 3.1415 YEAR", false, ""},
{"create table t (created_at datetime) TTL_ENABLE = 'OFF'", false, ""},
{"create table t (created_at datetime) TTL created_at + INTERVAL 1 YEAR TTL_ENABLE 'OFF'", false, ""},
{"create table t (created_at datetime) /*T![ttl] ttl=created_at + INTERVAL 1 YEAR ttl_enable='ON'*/", false, ""},
{"alter table t TTL = created_at + INTERVAL 1 MONTH", false, ""},
{"alter table t TTL_ENABLE = 'ON'", false, ""},
{"alter table t TTL = created_at + INTERVAL 1 MONTH TTL_ENABLE 'OFF'", false, ""},
{"alter table t /*T![ttl] ttl=created_at + INTERVAL 1 YEAR ttl_enable='ON'*/", false, ""},
{"alter table t remove ttl", false, ""},
}

RunTest(t, table, false)
}
17 changes: 17 additions & 0 deletions parser/ttlfeaturegate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

// TTLFeatureGate determines whether to enable the ttl related syntax in parser
var TTLFeatureGate = false
1 change: 1 addition & 0 deletions ttl/cache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_test(
flaky = True,
deps = [
":cache",
"//parser",
"//parser/model",
"//testkit",
"//testkit/testsetup",
Expand Down
5 changes: 5 additions & 0 deletions ttl/cache/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/ttl/cache"
Expand All @@ -28,6 +29,8 @@ import (
)

func TestNewTTLTable(t *testing.T) {
parser.TTLFeatureGate = true

cases := []struct {
db string
tbl string
Expand Down Expand Up @@ -161,6 +164,8 @@ func TestNewTTLTable(t *testing.T) {
}

func TestEvalTTLExpireTime(t *testing.T) {
parser.TTLFeatureGate = true

store, do := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("create table test.t(a int, t datetime) ttl = `t` + interval 1 day")
Expand Down

0 comments on commit 6a4b909

Please sign in to comment.