-
Notifications
You must be signed in to change notification settings - Fork 3.6k
HHH-18837 Oracle epoch extraction doesn't work with dates #10296
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
base: main
Are you sure you want to change the base?
Conversation
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 @peter1123581321, left a few comments.
hibernate-core/src/test/java/org/hibernate/orm/test/query/OracleDateTypeTest.java
Outdated
Show resolved
Hide resolved
hibernate-core/src/main/java/org/hibernate/dialect/function/ExtractFunction.java
Outdated
Show resolved
Hide resolved
3fabc9e
to
01e548f
Compare
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.
Much better @peter1123581321, left another round of minor comments.
final ExtractUnit field = (ExtractUnit) sqlAstArguments.get( 0 ); | ||
final TemporalUnit unit = field.getUnit(); | ||
final Expression expression = (Expression) sqlAstArguments.get( 1 ); | ||
final TemporalType temporalType; | ||
if ( unit.equals( TemporalUnit.EPOCH ) && (temporalType = getSqlTemporalType( | ||
expression.getExpressionType() )) != null ) { | ||
return temporalType == TemporalType.DATE | ||
? "trunc((cast(from_tz(cast(?2 as timestamp),'UTC') as date) - date '1970-1-1')*86400)" | ||
: "trunc((cast(?2 at time zone 'UTC' as date) - date '1970-1-1')*86400)"; | ||
} | ||
return dialect.extractPattern( unit ); |
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.
final ExtractUnit field = (ExtractUnit) sqlAstArguments.get( 0 ); | |
final TemporalUnit unit = field.getUnit(); | |
final Expression expression = (Expression) sqlAstArguments.get( 1 ); | |
final TemporalType temporalType; | |
if ( unit.equals( TemporalUnit.EPOCH ) && (temporalType = getSqlTemporalType( | |
expression.getExpressionType() )) != null ) { | |
return temporalType == TemporalType.DATE | |
? "trunc((cast(from_tz(cast(?2 as timestamp),'UTC') as date) - date '1970-1-1')*86400)" | |
: "trunc((cast(?2 at time zone 'UTC' as date) - date '1970-1-1')*86400)"; | |
} | |
return dialect.extractPattern( unit ); | |
final ExtractUnit field = (ExtractUnit) sqlAstArguments.get( 0 ); | |
final TemporalUnit unit = field.getUnit(); | |
if ( unit == EPOCH ) { | |
final Expression expression = (Expression) sqlAstArguments.get( 1 ); | |
final JdbcMappingContainer type = expression.getExpressionType(); | |
final TemporalType temporalType = type != null ? getSqlTemporalType( type ) : null; | |
if ( temporalType == TemporalType.DATE ) { | |
return "trunc((cast(from_tz(cast(?2 as timestamp),'UTC') as date) - date '1970-1-1')*86400)"; | |
} | |
} | |
return dialect.extractPattern( unit ); |
* Oracle extract() function | ||
*/ | ||
public void extract_oracle(Dialect dialect) { | ||
functionRegistry.register( "extract", new OracleExtractFunction( dialect, typeConfiguration ) ); |
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.
Let's just instantiate the custom function directly in OracleDialect
, no need to add a function here.
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.
I was about to say the same thing :)
@@ -624,7 +626,6 @@ public String extractPattern(TemporalUnit unit) { | |||
case HOUR -> "to_number(to_char(?2,'HH24'))"; | |||
case MINUTE -> "to_number(to_char(?2,'MI'))"; | |||
case SECOND -> "to_number(to_char(?2,'SS'))"; | |||
case EPOCH -> "trunc((cast(?2 at time zone 'UTC' as date) - date '1970-1-1')*86400)"; |
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.
I think we still need to keep this line, the extractPattern
method is also used in duration arithmetic IIRC.
01e548f
to
170063c
Compare
This PR fixes an error when using
epoch
with dates on Oracle.I added an overload of
extractPattern
(which is overridden inOracleDialect
) to handle differentTemporalType
.This new method is only used in
ExtractFunction
in context ofOracleDialect
.https://hibernate.atlassian.net/browse/HHH-18837
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.