This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Support date and time function: week #757
Merged
chloe-zh
merged 17 commits into
opendistro-for-elasticsearch:develop
from
rupal-bq:date-time-function-week
Sep 30, 2020
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a41aaad
add week
7f57158
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
498bdf0
edge case
365b000
fix case 5 & 7
7840b12
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
0814004
add IT
8567662
update doc
9d51b9c
fix table
c869ff0
rename
6d19513
add string type
5ab8631
nit: add newline
ce4473b
fix type in comment
6dc155c
nit
126890b
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
c73e430
add unit tests for null, missing values
4d3eff0
nit
76cd45b
address PR comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...in/java/com/amazon/opendistroforelasticsearch/sql/expression/datetime/CalendarLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.expression.datetime; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue; | ||
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException; | ||
import java.util.Arrays; | ||
import java.util.Calendar; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CalendarLookup { | ||
|
||
private Map<Integer, Calendar> map = new HashMap<>(); | ||
|
||
/** | ||
* Set Calendar in map for all modes. | ||
* @param date ExprValue of Date/Datetime/Timestamp/String type. | ||
*/ | ||
public CalendarLookup(ExprValue date) { | ||
map.put(0, getCalendar(Calendar.SUNDAY, 7, date)); | ||
map.put(1, getCalendar(Calendar.MONDAY, 5, date)); | ||
map.put(2, getCalendar(Calendar.SUNDAY, 7, date)); | ||
map.put(3, getCalendar(Calendar.MONDAY, 5, date)); | ||
map.put(4, getCalendar(Calendar.SUNDAY, 4, date)); | ||
map.put(5, getCalendar(Calendar.MONDAY, 7, date)); | ||
map.put(6, getCalendar(Calendar.SUNDAY, 4, date)); | ||
map.put(7, getCalendar(Calendar.MONDAY, 7, date)); | ||
} | ||
|
||
/** | ||
* Set first day of week, minimal days in first week and date in calendar. | ||
* @param firstDayOfWeek the given first day of the week. | ||
* @param minimalDaysInWeek the given minimal days required in the first week of the year. | ||
* @param date the ExprValue of Date/Datetime/Timestamp/String type. | ||
*/ | ||
private Calendar getCalendar(int firstDayOfWeek, int minimalDaysInWeek, ExprValue date) { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setFirstDayOfWeek(firstDayOfWeek); | ||
calendar.setMinimalDaysInFirstWeek(minimalDaysInWeek); | ||
calendar.set(date.dateValue().getYear(), date.dateValue().getMonthValue() - 1, | ||
date.dateValue().getDayOfMonth()); | ||
return calendar; | ||
} | ||
|
||
/** | ||
* Returns week number for date according to mode. | ||
* @param mode Integer for mode. Valid mode values are 0 to 7. | ||
*/ | ||
public int getWeekNumber(int mode) { | ||
if (map.containsKey(mode)) { | ||
int weekNumber = map.get(mode).get(Calendar.WEEK_OF_YEAR); | ||
if ((weekNumber > 51) | ||
&& (map.get(mode).get(Calendar.DAY_OF_MONTH) < 7) | ||
&& Arrays.asList(0, 1, 4, 5).contains(mode)) { | ||
weekNumber = 0; | ||
} | ||
return weekNumber; | ||
} | ||
throw new SemanticCheckException( | ||
String.format("mode:%s is invalid, please use mode value between 0-7", mode)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this function be static to avoid consturct CalendarLookup every time?
e.g. getWeekNumber(ExprValue date, ExprValue mode)
then define getCalendar(int firstDayOfWeek, int minimalDaysInWeek, Supplier dateProvider)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. added this change.