Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

peter1123581321
Copy link
Contributor

This PR fixes an error when using epoch with dates on Oracle.
I added an overload of extractPattern (which is overridden in OracleDialect) to handle different TemporalType.
This new method is only used in ExtractFunction in context of OracleDialect.

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.


Copy link
Member

@mbellade mbellade left a 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.

@peter1123581321 peter1123581321 marked this pull request as draft June 10, 2025 12:19
@peter1123581321 peter1123581321 requested a review from mbellade June 11, 2025 11:35
@peter1123581321 peter1123581321 marked this pull request as ready for review June 11, 2025 11:35
Copy link
Member

@mbellade mbellade left a 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.

Comment on lines +39 to +49
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 );
Copy link
Member

@mbellade mbellade Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) );
Copy link
Member

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.

Copy link
Member

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)";
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants