-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-27987: Implement YESTERDAY UDF in Hive #4985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
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 hidden or 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
92 changes: 92 additions & 0 deletions
92
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFYesterday.java
This file contains hidden or 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,92 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 org.apache.hadoop.hive.ql.udf.generic; | ||
MRKKmrkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.apache.hadoop.hive.common.type.Date; | ||
| import org.apache.hadoop.hive.ql.exec.Description; | ||
| import org.apache.hadoop.hive.ql.exec.UDFArgumentException; | ||
| import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException; | ||
| import org.apache.hadoop.hive.ql.metadata.HiveException; | ||
| import org.apache.hadoop.hive.ql.session.SessionState; | ||
| import org.apache.hadoop.hive.ql.udf.UDFType; | ||
| import org.apache.hadoop.hive.serde2.io.DateWritableV2; | ||
| import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; | ||
| import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; | ||
|
|
||
| import java.time.ZonedDateTime; | ||
|
|
||
| // This function is not a deterministic function, but a runtime constant. | ||
| // The return value is constant within a query but can be different between queries. | ||
| @UDFType(deterministic = false, runtimeConstant = true) | ||
| @Description(name = "yesterday", | ||
| value = "_FUNC_() - Returns yesterday's date at the start of query evaluation. The same as 'date_sub(current_date, 1)'" | ||
| + " All calls of yesterday within the same query return the same value.") | ||
| @NDV(maxNdv = 1) | ||
| public class GenericUDFYesterday extends GenericUDF { | ||
|
|
||
| protected DateWritableV2 yesterday; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be private. |
||
|
|
||
|
|
||
| public DateWritableV2 getYesterday() { | ||
| return yesterday; | ||
| } | ||
|
|
||
| public void setYesterday(DateWritableV2 yesterday) { | ||
| this.yesterday = yesterday; | ||
| } | ||
|
Comment on lines
+45
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove |
||
|
|
||
| @Override | ||
| public ObjectInspector initialize(ObjectInspector[] arguments) | ||
| throws UDFArgumentException { | ||
| if (arguments.length != 0) { | ||
| throw new UDFArgumentLengthException( | ||
| "The function YESTERDAY does not take any arguments, but found " | ||
| + arguments.length); | ||
| } | ||
|
|
||
| if (yesterday == null) { | ||
| SessionState ss = SessionState.get(); | ||
| ZonedDateTime dateTime = ss.getQueryCurrentTimestamp().atZone( | ||
| ss.getConf().getLocalTimeZone()).minusDays(1L); | ||
| Date dateVal = Date.of(dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth()); | ||
| yesterday = new DateWritableV2(dateVal); | ||
| } | ||
|
|
||
| return PrimitiveObjectInspectorFactory.writableDateObjectInspector; | ||
| } | ||
|
|
||
| @Override | ||
| public Object evaluate(DeferredObject[] arguments) throws HiveException { | ||
| return this.yesterday; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayString(String[] children) { | ||
| return "YESTERDAY()"; | ||
| } | ||
|
|
||
| @Override | ||
| public void copyToNewInstance(Object newInstance) throws UDFArgumentException { | ||
| super.copyToNewInstance(newInstance); | ||
|
|
||
| GenericUDFYesterday other = (GenericUDFYesterday) newInstance; | ||
| if (this.yesterday != null) { | ||
| other.yesterday = new DateWritableV2(this.yesterday); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,50 @@ | ||
| --! qt:dataset:alltypesorc | ||
|
|
||
| set hive.test.currenttimestamp =2024-01-18 01:02:03; | ||
|
|
||
| -- desc | ||
| desc function yesterday; | ||
| desc function extended yesterday; | ||
|
|
||
| -- query | ||
| select | ||
| yesterday() - yesterday(), | ||
| yesterday() | ||
| from alltypesorc limit 5; | ||
|
|
||
| select yesterday(); | ||
|
|
||
| -- as argument for various date udfs | ||
| select | ||
| to_date(yesterday()), | ||
| year(yesterday()), | ||
| month(yesterday()), | ||
| day(yesterday()), | ||
| weekofyear(yesterday()), | ||
| datediff(yesterday(), yesterday()), | ||
| to_date(date_add(yesterday(), 31)), | ||
| to_date(date_sub(yesterday(), 31)), | ||
| last_day(yesterday()), | ||
| next_day(yesterday(),'FRIDAY') | ||
| from alltypesorc limit 5; | ||
|
|
||
| -- insert | ||
| drop table if exists tmp_runtimeconstant; | ||
| create temporary table tmp_runtimeconstant(d date); | ||
| insert into table tmp_runtimeconstant | ||
| select yesterday() from alltypesorc limit 5; | ||
| select | ||
| d = date_sub(current_date, 1), | ||
| d = date_add(current_date, -1), | ||
| d = date_sub(from_unixtime(unix_timestamp()), 1), | ||
| d = date_add(from_unixtime(unix_timestamp()), -1), | ||
| d = from_unixtime(unix_timestamp() - 1 * 24 * 60 * 60, 'yyyy-MM-dd'), | ||
| d = date_sub(current_timestamp, 1), | ||
| d = date_add(current_timestamp, -1) | ||
| from tmp_runtimeconstant; | ||
|
|
||
| -- where | ||
| select | ||
| count(*) as cnt | ||
| from tmp_runtimeconstant | ||
| where yesterday() != d; |
This file contains hidden or 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 hidden or 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,150 @@ | ||
| PREHOOK: query: desc function yesterday | ||
| PREHOOK: type: DESCFUNCTION | ||
| POSTHOOK: query: desc function yesterday | ||
| POSTHOOK: type: DESCFUNCTION | ||
| yesterday() - Returns yesterday's date at the start of query evaluation. The same as 'date_sub(current_date, 1)' All calls of yesterday within the same query return the same value. | ||
| PREHOOK: query: desc function extended yesterday | ||
| PREHOOK: type: DESCFUNCTION | ||
| POSTHOOK: query: desc function extended yesterday | ||
| POSTHOOK: type: DESCFUNCTION | ||
| yesterday() - Returns yesterday's date at the start of query evaluation. The same as 'date_sub(current_date, 1)' All calls of yesterday within the same query return the same value. | ||
| Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDFYesterday | ||
| Function type:BUILTIN | ||
| PREHOOK: query: select | ||
| yesterday() - yesterday(), | ||
| yesterday() | ||
| from alltypesorc limit 5 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@alltypesorc | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select | ||
| yesterday() - yesterday(), | ||
| yesterday() | ||
| from alltypesorc limit 5 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@alltypesorc | ||
| #### A masked pattern was here #### | ||
| 0 00:00:00.000000000 2024-01-17 | ||
| 0 00:00:00.000000000 2024-01-17 | ||
| 0 00:00:00.000000000 2024-01-17 | ||
| 0 00:00:00.000000000 2024-01-17 | ||
| 0 00:00:00.000000000 2024-01-17 | ||
| PREHOOK: query: select yesterday() | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: _dummy_database@_dummy_table | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select yesterday() | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: _dummy_database@_dummy_table | ||
| #### A masked pattern was here #### | ||
| 2024-01-17 | ||
| PREHOOK: query: select | ||
| to_date(yesterday()), | ||
| year(yesterday()), | ||
| month(yesterday()), | ||
| day(yesterday()), | ||
| weekofyear(yesterday()), | ||
| datediff(yesterday(), yesterday()), | ||
| to_date(date_add(yesterday(), 31)), | ||
| to_date(date_sub(yesterday(), 31)), | ||
| last_day(yesterday()), | ||
| next_day(yesterday(),'FRIDAY') | ||
| from alltypesorc limit 5 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@alltypesorc | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select | ||
| to_date(yesterday()), | ||
| year(yesterday()), | ||
| month(yesterday()), | ||
| day(yesterday()), | ||
| weekofyear(yesterday()), | ||
| datediff(yesterday(), yesterday()), | ||
| to_date(date_add(yesterday(), 31)), | ||
| to_date(date_sub(yesterday(), 31)), | ||
| last_day(yesterday()), | ||
| next_day(yesterday(),'FRIDAY') | ||
| from alltypesorc limit 5 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@alltypesorc | ||
| #### A masked pattern was here #### | ||
| 2024-01-17 2024 1 17 3 0 2024-02-17 2023-12-17 2024-01-31 2024-01-19 | ||
| 2024-01-17 2024 1 17 3 0 2024-02-17 2023-12-17 2024-01-31 2024-01-19 | ||
| 2024-01-17 2024 1 17 3 0 2024-02-17 2023-12-17 2024-01-31 2024-01-19 | ||
| 2024-01-17 2024 1 17 3 0 2024-02-17 2023-12-17 2024-01-31 2024-01-19 | ||
| 2024-01-17 2024 1 17 3 0 2024-02-17 2023-12-17 2024-01-31 2024-01-19 | ||
| PREHOOK: query: drop table if exists tmp_runtimeconstant | ||
| PREHOOK: type: DROPTABLE | ||
| PREHOOK: Output: database:default | ||
| POSTHOOK: query: drop table if exists tmp_runtimeconstant | ||
| POSTHOOK: type: DROPTABLE | ||
| POSTHOOK: Output: database:default | ||
| PREHOOK: query: create temporary table tmp_runtimeconstant(d date) | ||
| PREHOOK: type: CREATETABLE | ||
| PREHOOK: Output: database:default | ||
| PREHOOK: Output: default@tmp_runtimeconstant | ||
| POSTHOOK: query: create temporary table tmp_runtimeconstant(d date) | ||
| POSTHOOK: type: CREATETABLE | ||
| POSTHOOK: Output: database:default | ||
| POSTHOOK: Output: default@tmp_runtimeconstant | ||
| PREHOOK: query: insert into table tmp_runtimeconstant | ||
| select yesterday() from alltypesorc limit 5 | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@alltypesorc | ||
| PREHOOK: Output: default@tmp_runtimeconstant | ||
| POSTHOOK: query: insert into table tmp_runtimeconstant | ||
| select yesterday() from alltypesorc limit 5 | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@alltypesorc | ||
| POSTHOOK: Output: default@tmp_runtimeconstant | ||
| POSTHOOK: Lineage: tmp_runtimeconstant.d SIMPLE [] | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| unix_timestamp(void) is deprecated. Use current_timestamp instead. | ||
| PREHOOK: query: select | ||
| d = date_sub(current_date, 1), | ||
| d = date_add(current_date, -1), | ||
| d = date_sub(from_unixtime(unix_timestamp()), 1), | ||
| d = date_add(from_unixtime(unix_timestamp()), -1), | ||
| d = from_unixtime(unix_timestamp() - 1 * 24 * 60 * 60, 'yyyy-MM-dd'), | ||
| d = date_sub(current_timestamp, 1), | ||
| d = date_add(current_timestamp, -1) | ||
| from tmp_runtimeconstant | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@tmp_runtimeconstant | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select | ||
| d = date_sub(current_date, 1), | ||
| d = date_add(current_date, -1), | ||
| d = date_sub(from_unixtime(unix_timestamp()), 1), | ||
| d = date_add(from_unixtime(unix_timestamp()), -1), | ||
| d = from_unixtime(unix_timestamp() - 1 * 24 * 60 * 60, 'yyyy-MM-dd'), | ||
| d = date_sub(current_timestamp, 1), | ||
| d = date_add(current_timestamp, -1) | ||
| from tmp_runtimeconstant | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@tmp_runtimeconstant | ||
| #### A masked pattern was here #### | ||
| true true true true true true true | ||
| true true true true true true true | ||
| true true true true true true true | ||
| true true true true true true true | ||
| true true true true true true true | ||
| PREHOOK: query: select | ||
| count(*) as cnt | ||
| from tmp_runtimeconstant | ||
| where yesterday() != d | ||
| PREHOOK: type: QUERY | ||
| PREHOOK: Input: default@tmp_runtimeconstant | ||
| #### A masked pattern was here #### | ||
| POSTHOOK: query: select | ||
| count(*) as cnt | ||
| from tmp_runtimeconstant | ||
| where yesterday() != d | ||
| POSTHOOK: type: QUERY | ||
| POSTHOOK: Input: default@tmp_runtimeconstant | ||
| #### A masked pattern was here #### | ||
| 0 |
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.
Uh oh!
There was an error while loading. Please reload this page.