From eef1157440e086af180cb8dbee763ba578c5c877 Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:14:00 +0200 Subject: [PATCH] rename mode->weekMode Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- go/vt/schemadiff/partitioning_analysis.go | 36 +++++++++---------- .../schemadiff/partitioning_analysis_test.go | 36 +++++++++---------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/go/vt/schemadiff/partitioning_analysis.go b/go/vt/schemadiff/partitioning_analysis.go index 8f9c06eb458..a6ef4bddb9a 100644 --- a/go/vt/schemadiff/partitioning_analysis.go +++ b/go/vt/schemadiff/partitioning_analysis.go @@ -30,7 +30,7 @@ import ( ) const ( - ModeUndefined = math.MinInt + WeekModeUndefined = math.MinInt ) // TemporalRangePartitioningAnalysis is the result of analyzing a table for temporal range partitioning. @@ -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) @@ -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 @@ -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. @@ -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) @@ -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 } @@ -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 } @@ -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) @@ -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) @@ -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 @@ -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 } diff --git a/go/vt/schemadiff/partitioning_analysis_test.go b/go/vt/schemadiff/partitioning_analysis_test.go index 0ab1f9a3cde..a9504a45d97 100644 --- a/go/vt/schemadiff/partitioning_analysis_test.go +++ b/go/vt/schemadiff/partitioning_analysis_test.go @@ -188,7 +188,7 @@ func TestTruncateDateTime(t *testing.T) { tcases := []struct { interval datetime.IntervalType - mode int + weekMode int expect string expectErr error }{ @@ -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"), }, { @@ -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()) @@ -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 @@ -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'))", @@ -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'))", @@ -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'))", @@ -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))", @@ -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))", @@ -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"), }, @@ -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())