From 2e9737466040bf454803c0d4a85ab5d3a5690914 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Tue, 9 May 2023 00:01:15 +0200 Subject: [PATCH] HHH-16567 - Remove support for CockroachDB versions older than 22.1 Signed-off-by: Jan Schatteman --- .../dialect/CockroachLegacyDialect.java | 66 ++++++++++++++----- .../hibernate/dialect/CockroachDialect.java | 2 +- nightly.Jenkinsfile | 8 +-- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CockroachLegacyDialect.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CockroachLegacyDialect.java index 4ed4769f10..1b1c9731aa 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CockroachLegacyDialect.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/CockroachLegacyDialect.java @@ -60,6 +60,7 @@ import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor; import org.hibernate.exception.spi.ViolatedConstraintNameExtractor; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.JdbcExceptionHelper; +import org.hibernate.query.SemanticException; import org.hibernate.query.sqm.IntervalType; import org.hibernate.query.sqm.NullOrdering; import org.hibernate.query.sqm.TemporalUnit; @@ -88,6 +89,7 @@ import org.jboss.logging.Logger; import static org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor.extractUsingTemplate; import static org.hibernate.query.sqm.TemporalUnit.DAY; +import static org.hibernate.query.sqm.TemporalUnit.EPOCH; import static org.hibernate.query.sqm.TemporalUnit.NATIVE; import static org.hibernate.type.SqlTypes.*; import static org.hibernate.type.descriptor.DateTimeUtils.appendAsDate; @@ -788,23 +790,53 @@ public class CockroachLegacyDialect extends Dialect { } } else { - switch (unit) { - case YEAR: - return "extract(year from ?3-?2)"; - case QUARTER: - return "(extract(year from ?3-?2)*4+extract(month from ?3-?2)//3)"; - case MONTH: - return "(extract(year from ?3-?2)*12+extract(month from ?3-?2))"; - // Prior to v20, Cockroach didn't support extracting from an interval/duration, - // so we use the extract_duration function - case WEEK: - return "extract_duration(hour from ?3-?2)/168"; - case DAY: - return "extract_duration(hour from ?3-?2)/24"; - case NANOSECOND: - return "extract_duration(microsecond from ?3-?2)*1e3"; - default: - return "extract_duration(?1 from ?3-?2)"; + if (getVersion().isSameOrAfter( 20, 1 )) { + switch (unit) { + case YEAR: + return "extract(year from ?3-?2)"; + case QUARTER: + return "(extract(year from ?3-?2)*4+extract(month from ?3-?2)//3)"; + case MONTH: + return "(extract(year from ?3-?2)*12+extract(month from ?3-?2))"; + case WEEK: //week is not supported by extract() when the argument is a duration + return "(extract(day from ?3-?2)/7)"; + case DAY: + return "extract(day from ?3-?2)"; + //in order to avoid multiple calls to extract(), + //we use extract(epoch from x - y) * factor for + //all the following units: + + // Note that CockroachDB also has an extract_duration function which returns an int, + // but we don't use that here because it is deprecated since v20 + case HOUR: + case MINUTE: + case SECOND: + case NANOSECOND: + case NATIVE: + return "cast(extract(epoch from ?3-?2)" + EPOCH.conversionFactor( unit, this ) + " as int)"; + default: + throw new SemanticException( "unrecognized field: " + unit ); + } + } + else { + switch (unit) { + case YEAR: + return "extract(year from ?3-?2)"; + case QUARTER: + return "(extract(year from ?3-?2)*4+extract(month from ?3-?2)//3)"; + case MONTH: + return "(extract(year from ?3-?2)*12+extract(month from ?3-?2))"; + // Prior to v20, Cockroach didn't support extracting from an interval/duration, + // so we use the extract_duration function + case WEEK: + return "extract_duration(hour from ?3-?2)/168"; + case DAY: + return "extract_duration(hour from ?3-?2)/24"; + case NANOSECOND: + return "extract_duration(microsecond from ?3-?2)*1e3"; + default: + return "extract_duration(?1 from ?3-?2)"; + } } } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java index d7fcbdad9f..955a0c6eca 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java @@ -125,7 +125,7 @@ public class CockroachDialect extends Dialect { // Pre-compile and reuse pattern private static final Pattern CRDB_VERSION_PATTERN = Pattern.compile( "v[\\d]+(\\.[\\d]+)?(\\.[\\d]+)?" ); - protected static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 21, 1 ); + protected static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 22, 1 ); protected final PostgreSQLDriverKind driverKind; diff --git a/nightly.Jenkinsfile b/nightly.Jenkinsfile index 0a86d116a5..9260dc5b63 100644 --- a/nightly.Jenkinsfile +++ b/nightly.Jenkinsfile @@ -40,7 +40,7 @@ stage('Configure') { // new BuildEnvironment( dbName: 'sybase_16' ), // There only is a Sybase ASE 16 image, so no pint in testing that nightly // Long running databases new BuildEnvironment( dbName: 'cockroachdb', node: 'cockroachdb', longRunning: true ), - new BuildEnvironment( dbName: 'cockroachdb_21_2', node: 'cockroachdb', longRunning: true ), + new BuildEnvironment( dbName: 'cockroachdb_22_1', node: 'cockroachdb', longRunning: true ), new BuildEnvironment( dbName: 'hana_cloud', dbLockableResource: 'hana-cloud', dbLockResourceAsHost: true ) ]; @@ -226,11 +226,11 @@ stage('Build') { sh "./docker_db.sh cockroachdb" state[buildEnv.tag]['containerName'] = "cockroach" break; - case "cockroachdb_21_2": + case "cockroachdb_22_1": docker.withRegistry('https://index.docker.io/v1/', 'hibernateci.hub.docker.com') { - docker.image('cockroachdb/cockroach:v21.2.16').pull() + docker.image('cockroachdb/cockroach:v22.1.13').pull() } - sh "./docker_db.sh cockroachdb_21_2" + sh "./docker_db.sh cockroachdb_22_1" state[buildEnv.tag]['containerName'] = "cockroach" break; }