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 static org.opensearch.sql.data.model.ExprValueUtils.getIntegerValue; | |
10 | import static org.opensearch.sql.data.model.ExprValueUtils.getLongValue; | |
11 | import static org.opensearch.sql.data.model.ExprValueUtils.getStringValue; | |
12 | import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; | |
13 | import static org.opensearch.sql.data.type.ExprCoreType.INTERVAL; | |
14 | import static org.opensearch.sql.data.type.ExprCoreType.LONG; | |
15 | import static org.opensearch.sql.data.type.ExprCoreType.STRING; | |
16 | import static org.opensearch.sql.expression.function.FunctionDSL.define; | |
17 | import static org.opensearch.sql.expression.function.FunctionDSL.impl; | |
18 | import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling; | |
19 | ||
20 | import java.time.Duration; | |
21 | import java.time.Period; | |
22 | import lombok.experimental.UtilityClass; | |
23 | import org.opensearch.sql.data.model.ExprIntervalValue; | |
24 | import org.opensearch.sql.data.model.ExprValue; | |
25 | import org.opensearch.sql.exception.ExpressionEvaluationException; | |
26 | import org.opensearch.sql.expression.function.BuiltinFunctionName; | |
27 | import org.opensearch.sql.expression.function.BuiltinFunctionRepository; | |
28 | import org.opensearch.sql.expression.function.DefaultFunctionResolver; | |
29 | ||
30 | @UtilityClass | |
31 | public class IntervalClause { | |
32 | ||
33 | private static final String MICRO_SECOND = "microsecond"; | |
34 | private static final String SECOND = "second"; | |
35 | private static final String MINUTE = "minute"; | |
36 | private static final String HOUR = "hour"; | |
37 | private static final String DAY = "day"; | |
38 | private static final String WEEK = "week"; | |
39 | private static final String MONTH = "month"; | |
40 | private static final String QUARTER = "quarter"; | |
41 | private static final String YEAR = "year"; | |
42 | ||
43 | public void register(BuiltinFunctionRepository repository) { | |
44 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(interval()); |
45 | } | |
46 | ||
47 | private DefaultFunctionResolver interval() { | |
48 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return define(BuiltinFunctionName.INTERVAL.getName(), |
49 | impl(nullMissingHandling(IntervalClause::interval), INTERVAL, INTEGER, STRING), | |
50 | impl(nullMissingHandling(IntervalClause::interval), INTERVAL, LONG, STRING)); | |
51 | } | |
52 | ||
53 | private ExprValue interval(ExprValue value, ExprValue unit) { | |
54 | switch (getStringValue(unit).toLowerCase()) { | |
55 | case MICRO_SECOND: | |
56 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return microsecond(value); |
57 | case SECOND: | |
58 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return second(value); |
59 | case MINUTE: | |
60 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return minute(value); |
61 | case HOUR: | |
62 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return hour(value); |
63 | case DAY: | |
64 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return day(value); |
65 | case WEEK: | |
66 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return week(value); |
67 | case MONTH: | |
68 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return month(value); |
69 | case QUARTER: | |
70 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return quarter(value); |
71 | case YEAR: | |
72 |
1
1. interval : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::interval → KILLED |
return year(value); |
73 | default: | |
74 | throw new ExpressionEvaluationException( | |
75 | String.format("interval unit %s is not supported", getStringValue(unit))); | |
76 | } | |
77 | } | |
78 | ||
79 | private ExprValue microsecond(ExprValue value) { | |
80 |
2
1. microsecond : Replaced long multiplication with division → KILLED 2. microsecond : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::microsecond → KILLED |
return new ExprIntervalValue(Duration.ofNanos(getLongValue(value) * 1000)); |
81 | } | |
82 | ||
83 | private ExprValue second(ExprValue value) { | |
84 |
1
1. second : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::second → KILLED |
return new ExprIntervalValue(Duration.ofSeconds(getLongValue(value))); |
85 | } | |
86 | ||
87 | private ExprValue minute(ExprValue value) { | |
88 |
1
1. minute : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::minute → KILLED |
return new ExprIntervalValue(Duration.ofMinutes(getLongValue(value))); |
89 | } | |
90 | ||
91 | private ExprValue hour(ExprValue value) { | |
92 |
1
1. hour : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::hour → KILLED |
return new ExprIntervalValue(Duration.ofHours(getLongValue(value))); |
93 | } | |
94 | ||
95 | private ExprValue day(ExprValue value) { | |
96 |
1
1. day : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::day → KILLED |
return new ExprIntervalValue(Duration.ofDays(getIntegerValue(value))); |
97 | } | |
98 | ||
99 | private ExprValue week(ExprValue value) { | |
100 |
1
1. week : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::week → KILLED |
return new ExprIntervalValue(Period.ofWeeks(getIntegerValue(value))); |
101 | } | |
102 | ||
103 | private ExprValue month(ExprValue value) { | |
104 |
1
1. month : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::month → KILLED |
return new ExprIntervalValue(Period.ofMonths(getIntegerValue(value))); |
105 | } | |
106 | ||
107 | private ExprValue quarter(ExprValue value) { | |
108 |
2
1. quarter : Replaced integer multiplication with division → KILLED 2. quarter : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::quarter → KILLED |
return new ExprIntervalValue(Period.ofMonths(getIntegerValue(value) * 3)); |
109 | } | |
110 | ||
111 | private ExprValue year(ExprValue value) { | |
112 |
1
1. year : replaced return value with null for org/opensearch/sql/expression/datetime/IntervalClause::year → KILLED |
return new ExprIntervalValue(Period.ofYears(getIntegerValue(value))); |
113 | } | |
114 | } | |
Mutations | ||
44 |
1.1 |
|
48 |
1.1 |
|
56 |
1.1 |
|
58 |
1.1 |
|
60 |
1.1 |
|
62 |
1.1 |
|
64 |
1.1 |
|
66 |
1.1 |
|
68 |
1.1 |
|
70 |
1.1 |
|
72 |
1.1 |
|
80 |
1.1 2.2 |
|
84 |
1.1 |
|
88 |
1.1 |
|
92 |
1.1 |
|
96 |
1.1 |
|
100 |
1.1 |
|
104 |
1.1 |
|
108 |
1.1 2.2 |
|
112 |
1.1 |