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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,11 @@ public String extractPattern(TemporalUnit unit) {
return "extract(?1 from ?2)";
}

@SuppressWarnings("deprecation")
public String extractPattern(TemporalUnit unit, TemporalType temporalType) {
return extractPattern( unit );
}

/**
* Obtain a pattern for the SQL equivalent to a
* {@code cast()} function call. The resulting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,26 @@ 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)";
default -> super.extractPattern(unit);
};
}

@Override
@SuppressWarnings("deprecation")
public String extractPattern(TemporalUnit unit, TemporalType temporalType) {
return switch ( unit ) {
case EPOCH:
if ( temporalType == TemporalType.DATE ) {
yield "trunc((cast(from_tz(cast(?2 as timestamp),'UTC') as date) - date '1970-1-1')*86400)";
}
else {
yield "trunc((cast(?2 at time zone 'UTC' as date) - date '1970-1-1')*86400)";
}
default:
yield extractPattern( unit );
};
}

@Override @SuppressWarnings("deprecation")
public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) {
final StringBuilder pattern = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/
package org.hibernate.dialect.function;

import jakarta.persistence.TemporalType;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.metamodel.model.domain.ReturnableType;
import org.hibernate.query.SemanticException;
import org.hibernate.query.common.TemporalUnit;
Expand All @@ -24,6 +26,7 @@
import org.hibernate.sql.ast.SqlAstTranslator;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.ExtractUnit;
import org.hibernate.type.BasicType;
import org.hibernate.type.spi.TypeConfiguration;
Expand All @@ -37,6 +40,7 @@
import static org.hibernate.query.common.TemporalUnit.*;
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.TEMPORAL;
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.TEMPORAL_UNIT;
import static org.hibernate.type.spi.TypeConfiguration.getSqlTemporalType;
import static org.hibernate.usertype.internal.AbstractTimeZoneStorageCompositeUserType.ZONE_OFFSET_NAME;

/**
Expand Down Expand Up @@ -69,10 +73,22 @@ public void render(
List<? extends SqlAstNode> sqlAstArguments,
ReturnableType<?> returnType,
SqlAstTranslator<?> walker) {
new PatternRenderer( extractPattern( sqlAstArguments ) ).render( sqlAppender, sqlAstArguments, walker );
}

@SuppressWarnings("deprecation")
private String extractPattern(List<? extends SqlAstNode> sqlAstArguments) {
final ExtractUnit field = (ExtractUnit) sqlAstArguments.get( 0 );
final TemporalUnit unit = field.getUnit();
final String pattern = dialect.extractPattern( unit );
new PatternRenderer( pattern ).render( sqlAppender, sqlAstArguments, walker );
final Expression expression = (Expression) sqlAstArguments.get( 1 );
final TemporalType temporalType;
if ( dialect instanceof OracleDialect && (temporalType = getSqlTemporalType(
expression.getExpressionType() )) != null ) {
return dialect.extractPattern( unit, temporalType );
}
else {
return dialect.extractPattern( unit );
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.query;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;


@SessionFactory
@DomainModel(annotatedClasses = {
OracleDateTypeTest.AuthorDate.class,
OracleDateTypeTest.AuthorTimestamp.class,
OracleDateTypeTest.AuthorTimestampTz.class
})
@RequiresDialect(OracleDialect.class)
@JiraKey("HHH-18837")
public class OracleDateTypeTest {

@Test
void hhh18837test_withDate(SessionFactoryScope scope) {

// given
LocalDate birth = LocalDate.of( 2013, 7, 5 );

// prepare
scope.inTransaction( session -> {

AuthorDate authorDate = new AuthorDate();
authorDate.birth = birth;
session.persist( authorDate );
} );

// assert
scope.inTransaction( session -> {
Long epoch = session.createSelectionQuery( "select epoch(a.birth) from AuthorDate a", Long.class )
.getSingleResult();
assertEquals( birth.atStartOfDay( ZoneOffset.UTC ).toInstant().toEpochMilli() / 1000, epoch );
} );
}

@Test
void hhh18837test_withTimestamp(SessionFactoryScope scope) {

// given
LocalDateTime birth = LocalDate.of( 2013, 7, 5 ).atTime( LocalTime.MIN );

// prepare
scope.inTransaction( session -> {

AuthorTimestamp authorTimestamp = new AuthorTimestamp();
authorTimestamp.birth = birth;
session.persist( authorTimestamp );
} );

// assert
scope.inTransaction( session -> {
Long epoch = session.createSelectionQuery( "select epoch(a.birth) from AuthorTimestamp a", Long.class )
.getSingleResult();
assertEquals( birth.toEpochSecond( ZoneOffset.UTC ), epoch );
} );
}

@Test
void hhh18837test_withTimestampTz(SessionFactoryScope scope) {

// given
ZonedDateTime birth = ZonedDateTime.of( LocalDate.of( 2013, 7, 5 ), LocalTime.MIN,
ZoneId.of( "Europe/Vienna" ) );

// prepare
scope.inTransaction( session -> {

AuthorTimestampTz authorTimestampTz = new AuthorTimestampTz();
authorTimestampTz.birth = birth;
session.persist( authorTimestampTz );
} );

// assert
scope.inTransaction( session -> {
Long epoch = session.createSelectionQuery( "select epoch(a.birth) from AuthorTimestampTz a", Long.class )
.getSingleResult();
assertEquals( birth.toEpochSecond(), epoch );
} );
}

@AfterAll
public void dropTestData(SessionFactoryScope scope) {
scope.dropData();
}

@Entity(name = "AuthorDate")
public static class AuthorDate {

@Id
@GeneratedValue
long id;

LocalDate birth;
}

@Entity(name = "AuthorTimestamp")
public static class AuthorTimestamp {

@Id
@GeneratedValue
long id;

LocalDateTime birth;
}

@Entity(name = "AuthorTimestampTz")
public static class AuthorTimestampTz {

@Id
@GeneratedValue
long id;

ZonedDateTime birth;
}
}