1
|
|
/* |
2
|
|
* Copyright OpenSearch Contributors |
3
|
|
* SPDX-License-Identifier: Apache-2.0 |
4
|
|
*/ |
5
|
|
|
6
|
|
|
7
|
|
package org.opensearch.sql.expression.datetime; |
8
|
|
|
9
|
|
import com.google.common.collect.ImmutableList; |
10
|
|
import java.time.LocalDate; |
11
|
|
import java.util.Arrays; |
12
|
|
import java.util.Calendar; |
13
|
|
import lombok.AllArgsConstructor; |
14
|
|
import org.opensearch.sql.exception.SemanticCheckException; |
15
|
|
|
16
|
|
@AllArgsConstructor |
17
|
|
class CalendarLookup { |
18
|
|
|
19
|
|
/** |
20
|
|
* Get a calendar for the specific mode. |
21
|
|
* @param mode Mode to get calendar for. |
22
|
|
* @param date Date to get calendar for. |
23
|
|
*/ |
24
|
|
private static Calendar getCalendar(int mode, LocalDate date) { |
25
|
4
1. getCalendar : changed conditional boundary → KILLED
2. getCalendar : changed conditional boundary → KILLED
3. getCalendar : negated conditional → KILLED
4. getCalendar : negated conditional → KILLED
|
if ((mode < 0) || (mode > 7)) { |
26
|
|
throw new SemanticCheckException( |
27
|
|
String.format("mode:%s is invalid, please use mode value between 0-7", mode)); |
28
|
|
} |
29
|
2
1. getCalendar : Replaced integer modulus with multiplication → KILLED
2. getCalendar : negated conditional → KILLED
|
int day = (mode % 2 == 0) ? Calendar.SUNDAY : Calendar.MONDAY; |
30
|
1
1. getCalendar : negated conditional → KILLED
|
if (ImmutableList.of(1, 3).contains(mode)) { |
31
|
1
1. getCalendar : replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
return getCalendar(day, 5, date); |
32
|
1
1. getCalendar : negated conditional → KILLED
|
} else if (ImmutableList.of(4, 6).contains(mode)) { |
33
|
1
1. getCalendar : replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
return getCalendar(day, 4, date); |
34
|
|
} else { |
35
|
1
1. getCalendar : replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
return getCalendar(day, 7, date); |
36
|
|
} |
37
|
|
} |
38
|
|
|
39
|
|
/** |
40
|
|
* Set first day of week, minimal days in first week and date in calendar. |
41
|
|
* @param firstDayOfWeek the given first day of the week. |
42
|
|
* @param minimalDaysInWeek the given minimal days required in the first week of the year. |
43
|
|
* @param date the given date. |
44
|
|
*/ |
45
|
|
private static Calendar getCalendar(int firstDayOfWeek, int minimalDaysInWeek, LocalDate date) { |
46
|
|
Calendar calendar = Calendar.getInstance(); |
47
|
1
1. getCalendar : removed call to java/util/Calendar::setFirstDayOfWeek → KILLED
|
calendar.setFirstDayOfWeek(firstDayOfWeek); |
48
|
1
1. getCalendar : removed call to java/util/Calendar::setMinimalDaysInFirstWeek → KILLED
|
calendar.setMinimalDaysInFirstWeek(minimalDaysInWeek); |
49
|
2
1. getCalendar : Replaced integer subtraction with addition → KILLED
2. getCalendar : removed call to java/util/Calendar::set → KILLED
|
calendar.set(date.getYear(), date.getMonthValue() - 1, date.getDayOfMonth()); |
50
|
1
1. getCalendar : replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
return calendar; |
51
|
|
} |
52
|
|
|
53
|
|
/** |
54
|
|
* Returns week number for date according to mode. |
55
|
|
* @param mode Integer for mode. Valid mode values are 0 to 7. |
56
|
|
* @param date LocalDate for date. |
57
|
|
*/ |
58
|
|
static int getWeekNumber(int mode, LocalDate date) { |
59
|
|
Calendar calendar = getCalendar(mode, date); |
60
|
|
int weekNumber = calendar.get(Calendar.WEEK_OF_YEAR); |
61
|
2
1. getWeekNumber : changed conditional boundary → SURVIVED
2. getWeekNumber : negated conditional → KILLED
|
if ((weekNumber > 51) |
62
|
2
1. getWeekNumber : changed conditional boundary → SURVIVED
2. getWeekNumber : negated conditional → KILLED
|
&& (calendar.get(Calendar.DAY_OF_MONTH) < 7) |
63
|
1
1. getWeekNumber : negated conditional → KILLED
|
&& Arrays.asList(0, 1, 4, 5).contains(mode)) { |
64
|
|
weekNumber = 0; |
65
|
|
} |
66
|
1
1. getWeekNumber : replaced int return with 0 for org/opensearch/sql/expression/datetime/CalendarLookup::getWeekNumber → KILLED
|
return weekNumber; |
67
|
|
} |
68
|
|
|
69
|
|
/** |
70
|
|
* Returns year for date according to mode. |
71
|
|
* @param mode Integer for mode. Valid mode values are 0 to 7. |
72
|
|
* @param date LocalDate for date. |
73
|
|
*/ |
74
|
|
static int getYearNumber(int mode, LocalDate date) { |
75
|
|
Calendar calendar = getCalendar(mode, date); |
76
|
|
int weekNumber = getWeekNumber(mode, date); |
77
|
|
int yearNumber = calendar.get(Calendar.YEAR); |
78
|
4
1. getYearNumber : changed conditional boundary → SURVIVED
2. getYearNumber : changed conditional boundary → SURVIVED
3. getYearNumber : negated conditional → KILLED
4. getYearNumber : negated conditional → KILLED
|
if ((weekNumber > 51) && (calendar.get(Calendar.DAY_OF_MONTH) < 7)) { |
79
|
1
1. getYearNumber : Changed increment from -1 to 1 → KILLED
|
yearNumber--; |
80
|
|
} |
81
|
1
1. getYearNumber : replaced int return with 0 for org/opensearch/sql/expression/datetime/CalendarLookup::getYearNumber → KILLED
|
return yearNumber; |
82
|
|
} |
83
|
|
} |
| | Mutations |
25 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] changed conditional boundary → KILLED 2.2 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] changed conditional boundary → KILLED 3.3 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:modeInUnsupportedFormat()] negated conditional → KILLED 4.4 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:modeInUnsupportedFormat()] negated conditional → KILLED
|
29 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] Replaced integer modulus with multiplication → KILLED 2.2 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
30 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
31 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
32 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
33 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
35 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
47 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] removed call to java/util/Calendar::setFirstDayOfWeek → KILLED
|
48 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] removed call to java/util/Calendar::setMinimalDaysInFirstWeek → KILLED
|
49 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] Replaced integer subtraction with addition → KILLED 2.2 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] removed call to java/util/Calendar::set → KILLED
|
50 |
|
1.1 Location : getCalendar Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] replaced return value with null for org/opensearch/sql/expression/datetime/CalendarLookup::getCalendar → KILLED
|
61 |
|
1.1 Location : getWeekNumber Killed by : none changed conditional boundary → SURVIVED 2.2 Location : getWeekNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
62 |
|
1.1 Location : getWeekNumber Killed by : none changed conditional boundary → SURVIVED 2.2 Location : getWeekNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
63 |
|
1.1 Location : getWeekNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] negated conditional → KILLED
|
66 |
|
1.1 Location : getWeekNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:week()] replaced int return with 0 for org/opensearch/sql/expression/datetime/CalendarLookup::getWeekNumber → KILLED
|
78 |
|
1.1 Location : getYearNumber Killed by : none changed conditional boundary → SURVIVED 2.2 Location : getYearNumber Killed by : none changed conditional boundary → SURVIVED 3.3 Location : getYearNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:date_format()] negated conditional → KILLED 4.4 Location : getYearNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:date_format()] negated conditional → KILLED
|
79 |
|
1.1 Location : getYearNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:date_format()] Changed increment from -1 to 1 → KILLED
|
81 |
|
1.1 Location : getYearNumber Killed by : org.opensearch.sql.expression.datetime.DateTimeFunctionTest.[engine:junit-jupiter]/[class:org.opensearch.sql.expression.datetime.DateTimeFunctionTest]/[method:date_format()] replaced int return with 0 for org/opensearch/sql/expression/datetime/CalendarLookup::getYearNumber → KILLED
|