Skip to content

Commit

Permalink
rename mode->weekMode
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Jan 2, 2025
1 parent 087af5f commit eef1157
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
36 changes: 18 additions & 18 deletions go/vt/schemadiff/partitioning_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

const (
ModeUndefined = math.MinInt
WeekModeUndefined = math.MinInt
)

// TemporalRangePartitioningAnalysis is the result of analyzing a table for temporal range partitioning.
Expand All @@ -41,7 +41,7 @@ type TemporalRangePartitioningAnalysis struct {
MinimalInterval datetime.IntervalType // The minimal interval that the table is partitioned by (e.g. if partitioned by TO_DAYS, the minimal interval is 1 day)
Col *ColumnDefinitionEntity // The column used in the RANGE expression
FuncExpr *sqlparser.FuncExpr // The function used in the RANGE expression, if any
Mode int // The mode used in the WEEK function, if that's what's used
WeekMode int // The mode used in the WEEK function, if that's what's used
MaxvaluePartition *sqlparser.PartitionDefinition // The partition that has MAXVALUE, if any
HighestValueDateTime datetime.DateTime // The datetime value of the highest partition (excluding MAXVALUE)
HighestValueIntVal int64 // The integer value of the highest partition (excluding MAXVALUE)
Expand Down Expand Up @@ -145,13 +145,13 @@ func supportedPartitioningScheme(expr sqlparser.Expr, createTableEntity *CreateT
return false, false, nil
}

// extractFuncMode extracts the mode argument from a WEEK/YEARWEEK function, if applicable.
// extractFuncWeekMode extracts the mode argument from a WEEK/YEARWEEK function, if applicable.
// It returns a ModeUndefined when not applicable.
func extractFuncMode(funcExpr *sqlparser.FuncExpr) (int, error) {
func extractFuncWeekMode(funcExpr *sqlparser.FuncExpr) (int, error) {
switch funcExpr.Name.Lowered() {
case "week", "yearweek":
default:
return ModeUndefined, nil
return WeekModeUndefined, nil
}
if len(funcExpr.Exprs) <= 1 {
return 0, nil
Expand Down Expand Up @@ -200,7 +200,7 @@ func AnalyzeTemporalRangePartitioning(createTableEntity *CreateTableEntity) (*Te
}

analysis.IsRangeColumns = len(partitionOption.ColList) > 0
analysis.Mode = ModeUndefined
analysis.WeekMode = WeekModeUndefined
switch len(partitionOption.ColList) {
case 0:
// This is a PARTITION BY RANGE(expr), where "expr" can be just column name, or a complex expression.
Expand All @@ -222,11 +222,11 @@ func AnalyzeTemporalRangePartitioning(createTableEntity *CreateTableEntity) (*Te
case *sqlparser.FuncExpr:
analysis.FuncExpr = sqlparser.CloneRefOfFuncExpr(node)
node.Name = sqlparser.NewIdentifierCI(node.Name.Lowered())
mode, err := extractFuncMode(node)
mode, err := extractFuncWeekMode(node)
if err != nil {
return false, err
}
analysis.Mode = mode
analysis.WeekMode = mode
}
return true, nil
}, expr)
Expand Down Expand Up @@ -312,7 +312,7 @@ func AnalyzeTemporalRangePartitioning(createTableEntity *CreateTableEntity) (*Te
if err != nil {
return analysis, err
}
highestValueDateTime, err = truncateDateTime(highestValueDateTime, analysis.MinimalInterval, analysis.Mode)
highestValueDateTime, err = truncateDateTime(highestValueDateTime, analysis.MinimalInterval, analysis.WeekMode)
if err != nil {
return analysis, err
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func applyFuncExprToDateTime(dt datetime.DateTime, funcExpr *sqlparser.FuncExpr)
case "to_days":
intval = int64(datetime.MysqlDayNumber(dt.Date.Year(), dt.Date.Month(), dt.Date.Day()))
case "yearweek":
mode, err := extractFuncMode(funcExpr)
mode, err := extractFuncWeekMode(funcExpr)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -461,8 +461,8 @@ func temporalPartitionName(dt datetime.DateTime, resolution datetime.IntervalTyp
// e.g. if resolution is IntervalDay, the time part is removed.
// If resolution is IntervalMonth, the day part is set to 1.
// etc.
// `mode` is used for WEEK calculations, see https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_week
func truncateDateTime(dt datetime.DateTime, interval datetime.IntervalType, mode int) (datetime.DateTime, error) {
// `weekMode` is used for WEEK calculations, see https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_week
func truncateDateTime(dt datetime.DateTime, interval datetime.IntervalType, weekMode int) (datetime.DateTime, error) {
if interval >= datetime.IntervalHour {
// Remove the minutes, seconds, subseconds parts
hourInterval := datetime.ParseIntervalInt64(int64(dt.Time.Hour()), datetime.IntervalHour, false)
Expand All @@ -481,13 +481,13 @@ func truncateDateTime(dt datetime.DateTime, interval datetime.IntervalType, mode
// IntervalWeek = IntervalDay | intervalMulti, which is larger than IntervalYear, so we interject here
// Get back to the first day of the week:
var startOfWeekInterval *datetime.Interval
switch mode {
switch weekMode {
case 0, 2, 4, 6:
startOfWeekInterval = datetime.ParseIntervalInt64(-int64(dt.Date.Weekday()), datetime.IntervalDay, false)
case 1, 3, 5, 7:
startOfWeekInterval = datetime.ParseIntervalInt64(-int64(dt.Date.Weekday()-1), datetime.IntervalDay, false)
default:
return dt, fmt.Errorf("invalid mode value %d for WEEK/YEARWEEK function", mode)
return dt, fmt.Errorf("invalid mode value %d for WEEK/YEARWEEK function", weekMode)
}
var ok bool
dt, _, ok = dt.AddInterval(startOfWeekInterval, 0, false)
Expand Down Expand Up @@ -524,7 +524,7 @@ func truncateDateTime(dt datetime.DateTime, interval datetime.IntervalType, mode
// e.g. "prepare 7 days ahead, starting from today".
// The function computes values of existing partitions to determine how many new partitions are actually
// required to satisfy the terms.
func TemporalRangePartitioningNextRotation(createTableEntity *CreateTableEntity, interval datetime.IntervalType, mode int, prepareAheadCount int, reference time.Time) (diffs []*AlterTableEntityDiff, err error) {
func TemporalRangePartitioningNextRotation(createTableEntity *CreateTableEntity, interval datetime.IntervalType, weekMode int, prepareAheadCount int, reference time.Time) (diffs []*AlterTableEntityDiff, err error) {
analysis, err := AnalyzeTemporalRangePartitioning(createTableEntity)
if err != nil {
return nil, err
Expand All @@ -546,10 +546,10 @@ func TemporalRangePartitioningNextRotation(createTableEntity *CreateTableEntity,
if intervalIsTooSmall {
return nil, fmt.Errorf("interval %s is less than the minimal interval %s for table %s", interval.ToString(), analysis.MinimalInterval.ToString(), createTableEntity.Name())
}
if analysis.Mode != ModeUndefined && mode != analysis.Mode {
return nil, fmt.Errorf("mode %d is different from the mode %d used in table %s", mode, analysis.Mode, createTableEntity.Name())
if analysis.WeekMode != WeekModeUndefined && weekMode != analysis.WeekMode {
return nil, fmt.Errorf("mode %d is different from the mode %d used in table %s", weekMode, analysis.WeekMode, createTableEntity.Name())
}
referenceDatetime, err := truncateDateTime(datetime.NewDateTimeFromStd(reference), interval, mode)
referenceDatetime, err := truncateDateTime(datetime.NewDateTimeFromStd(reference), interval, weekMode)
if err != nil {
return nil, err
}
Expand Down
36 changes: 18 additions & 18 deletions go/vt/schemadiff/partitioning_analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestTruncateDateTime(t *testing.T) {

tcases := []struct {
interval datetime.IntervalType
mode int
weekMode int
expect string
expectErr error
}{
Expand All @@ -206,41 +206,41 @@ func TestTruncateDateTime(t *testing.T) {
},
{
interval: datetime.IntervalWeek,
mode: 1,
weekMode: 1,
expect: "2024-12-16 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 2,
weekMode: 2,
expect: "2024-12-15 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 3,
weekMode: 3,
expect: "2024-12-16 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 4,
weekMode: 4,
expect: "2024-12-15 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 5,
weekMode: 5,
expect: "2024-12-16 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 6,
weekMode: 6,
expect: "2024-12-15 00:00:00",
},
{
interval: datetime.IntervalWeek,
mode: 7,
weekMode: 7,
expect: "2024-12-16 00:00:00",
}, {
interval: datetime.IntervalWeek,
mode: 8,
weekMode: 8,
expectErr: fmt.Errorf("invalid mode value 8 for WEEK/YEARWEEK function"),
},
{
Expand All @@ -264,7 +264,7 @@ func TestTruncateDateTime(t *testing.T) {
}
for _, tcase := range tcases {
t.Run(tcase.interval.ToString(), func(t *testing.T) {
truncated, err := truncateDateTime(dt, tcase.interval, tcase.mode)
truncated, err := truncateDateTime(dt, tcase.interval, tcase.weekMode)
if tcase.expectErr != nil {
require.Error(t, err)
assert.EqualError(t, err, tcase.expectErr.Error())
Expand Down Expand Up @@ -746,7 +746,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name string
create string
interval datetime.IntervalType
mode int
weekMode int
prepareAheadCount int
expactMaxValue bool
expectStatements []string
Expand Down Expand Up @@ -920,7 +920,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name: "week(0) interval with 4 weeks",
create: "CREATE TABLE t (id int, created_at DATETIME, PRIMARY KEY(id, created_at)) PARTITION BY RANGE COLUMNS (created_at) (PARTITION p0 VALUES LESS THAN ('2024-12-19 09:00:00'))",
interval: datetime.IntervalWeek,
mode: 0,
weekMode: 0,
prepareAheadCount: 4,
expectStatements: []string{
"ALTER TABLE `t` ADD PARTITION (PARTITION `p20241215` VALUES LESS THAN ('2024-12-22 00:00:00'))",
Expand All @@ -933,7 +933,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name: "week(1) interval with 4 weeks",
create: "CREATE TABLE t (id int, created_at DATETIME, PRIMARY KEY(id, created_at)) PARTITION BY RANGE COLUMNS (created_at) (PARTITION p0 VALUES LESS THAN ('2024-12-19 09:00:00'))",
interval: datetime.IntervalWeek,
mode: 1,
weekMode: 1,
prepareAheadCount: 4,
expectStatements: []string{
"ALTER TABLE `t` ADD PARTITION (PARTITION `p20241216` VALUES LESS THAN ('2024-12-23 00:00:00'))",
Expand All @@ -951,7 +951,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
PARTITION p_somename VALUES LESS THAN ('2024-12-30 00:00:00')
)`,
interval: datetime.IntervalWeek,
mode: 1,
weekMode: 1,
prepareAheadCount: 4,
expectStatements: []string{
"ALTER TABLE `t` ADD PARTITION (PARTITION `p20241230` VALUES LESS THAN ('2025-01-06 00:00:00'))",
Expand All @@ -962,7 +962,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name: "yearweek(0) function with 4 weeks",
create: "CREATE TABLE t (id int, created_at DATETIME, PRIMARY KEY(id, created_at)) PARTITION BY RANGE (YEARWEEK(created_at, 0)) (PARTITION p0 VALUES LESS THAN (YEARWEEK('2024-12-19 09:00:00', 1)))",
interval: datetime.IntervalWeek,
mode: 0,
weekMode: 0,
prepareAheadCount: 4,
expectStatements: []string{
"ALTER TABLE `t` ADD PARTITION (PARTITION `p20241215` VALUES LESS THAN (202451))",
Expand All @@ -975,7 +975,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name: "yearweek(1) function with 4 weeks",
create: "CREATE TABLE t (id int, created_at DATETIME, PRIMARY KEY(id, created_at)) PARTITION BY RANGE (YEARWEEK(created_at, 1)) (PARTITION p0 VALUES LESS THAN (YEARWEEK('2024-12-19 09:00:00', 1)))",
interval: datetime.IntervalWeek,
mode: 1,
weekMode: 1,
prepareAheadCount: 4,
expectStatements: []string{
"ALTER TABLE `t` ADD PARTITION (PARTITION `p20241216` VALUES LESS THAN (202452))",
Expand All @@ -988,7 +988,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
name: "incompatible week mode",
create: "CREATE TABLE t (id int, created_at DATETIME, PRIMARY KEY(id, created_at)) PARTITION BY RANGE (YEARWEEK(created_at, 0)) (PARTITION p0 VALUES LESS THAN (YEARWEEK('2024-12-19 09:00:00', 1)))",
interval: datetime.IntervalWeek,
mode: 1,
weekMode: 1,
prepareAheadCount: 4,
expectErr: fmt.Errorf("mode 1 is different from the mode 0 used in table t"),
},
Expand Down Expand Up @@ -1053,7 +1053,7 @@ func TestTemporalRangePartitioningNextRotation(t *testing.T) {
entity, err := NewCreateTableEntityFromSQL(env, tcase.create)
require.NoError(t, err)

diffs, err := TemporalRangePartitioningNextRotation(entity, tcase.interval, tcase.mode, tcase.prepareAheadCount, reference)
diffs, err := TemporalRangePartitioningNextRotation(entity, tcase.interval, tcase.weekMode, tcase.prepareAheadCount, reference)
if tcase.expectErr != nil {
require.Error(t, err)
assert.EqualError(t, err, tcase.expectErr.Error())
Expand Down

0 comments on commit eef1157

Please sign in to comment.