Skip to content

Commit c8a6678

Browse files
committed
Fix some JSON function rendering issues on TiDB
1 parent bcd70a8 commit c8a6678

File tree

7 files changed

+56
-15
lines changed

7 files changed

+56
-15
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MariaDBLegacyDialect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
127127
@Override
128128
public AggregateSupport getAggregateSupport() {
129129
return getVersion().isSameOrAfter( 10, 2 )
130-
? MySQLAggregateSupport.LONGTEXT_INSTANCE
130+
? MySQLAggregateSupport.forMariaDB( this )
131131
: AggregateSupportImpl.INSTANCE;
132132
}
133133

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MySQLLegacyDialect.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,7 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
392392

393393
@Override
394394
public AggregateSupport getAggregateSupport() {
395-
return getMySQLVersion().isSameOrAfter( 5, 7 )
396-
? MySQLAggregateSupport.JSON_INSTANCE
397-
: super.getAggregateSupport();
395+
return MySQLAggregateSupport.forMySQL( this );
398396
}
399397

400398
@Deprecated

hibernate-core/src/main/java/org/hibernate/dialect/MariaDBDialect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
125125

126126
@Override
127127
public AggregateSupport getAggregateSupport() {
128-
return MySQLAggregateSupport.LONGTEXT_INSTANCE;
128+
return MySQLAggregateSupport.forMariaDB( this );
129129
}
130130

131131
@Override

hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
440440

441441
@Override
442442
public AggregateSupport getAggregateSupport() {
443-
return MySQLAggregateSupport.JSON_INSTANCE;
443+
return MySQLAggregateSupport.forMySQL( this );
444444
}
445445

446446
@Deprecated(since="6.4")

hibernate-core/src/main/java/org/hibernate/dialect/TiDBDialect.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package org.hibernate.dialect;
66

77
import org.hibernate.LockOptions;
8+
import org.hibernate.dialect.aggregate.AggregateSupport;
9+
import org.hibernate.dialect.aggregate.MySQLAggregateSupport;
810
import org.hibernate.dialect.sequence.SequenceSupport;
911
import org.hibernate.dialect.sequence.TiDBSequenceSupport;
1012
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
@@ -90,6 +92,11 @@ public SequenceSupport getSequenceSupport() {
9092
return TiDBSequenceSupport.INSTANCE;
9193
}
9294

95+
@Override
96+
public AggregateSupport getAggregateSupport() {
97+
return MySQLAggregateSupport.forTiDB( this );
98+
}
99+
93100
@Override
94101
public SequenceInformationExtractor getSequenceInformationExtractor() {
95102
return SequenceInformationExtractorTiDBDatabaseImpl.INSTANCE;

hibernate-core/src/main/java/org/hibernate/dialect/aggregate/MySQLAggregateSupport.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.dialect.aggregate;
66

7+
import org.hibernate.dialect.Dialect;
78
import org.hibernate.internal.util.StringHelper;
89
import org.hibernate.mapping.Column;
910
import org.hibernate.metamodel.mapping.JdbcMapping;
@@ -48,13 +49,32 @@
4849

4950
public class MySQLAggregateSupport extends AggregateSupportImpl {
5051

51-
public static final AggregateSupport JSON_INSTANCE = new MySQLAggregateSupport( true );
52-
public static final AggregateSupport LONGTEXT_INSTANCE = new MySQLAggregateSupport( false );
52+
private static final AggregateSupport JSON_INSTANCE = new MySQLAggregateSupport( true, false );
53+
private static final AggregateSupport JSON_WITH_UUID_INSTANCE = new MySQLAggregateSupport( true, true );
54+
private static final AggregateSupport LONGTEXT_INSTANCE = new MySQLAggregateSupport( false, false );
5355

5456
private final boolean jsonType;
57+
private final boolean uuidFunctions;
5558

56-
public MySQLAggregateSupport(boolean jsonType) {
59+
private MySQLAggregateSupport(boolean jsonType, boolean uuidFunctions) {
5760
this.jsonType = jsonType;
61+
this.uuidFunctions = uuidFunctions;
62+
}
63+
64+
public static AggregateSupport forMySQL(Dialect dialect) {
65+
return dialect.getVersion().isSameOrAfter( 8 )
66+
? JSON_WITH_UUID_INSTANCE
67+
: dialect.getVersion().isSameOrAfter( 5, 7 )
68+
? JSON_INSTANCE
69+
: AggregateSupportImpl.INSTANCE;
70+
}
71+
72+
public static AggregateSupport forTiDB(Dialect dialect) {
73+
return JSON_WITH_UUID_INSTANCE;
74+
}
75+
76+
public static AggregateSupport forMariaDB(Dialect dialect) {
77+
return LONGTEXT_INSTANCE;
5878
}
5979

6080
@Override
@@ -92,10 +112,19 @@ public String aggregateComponentCustomReadExpression(
92112
);
93113
case UUID:
94114
if ( column.getJdbcMapping().getJdbcType().isBinary() ) {
95-
return template.replace(
96-
placeholder,
97-
"unhex(replace(json_unquote(" + queryExpression( aggregateParentReadExpression, columnExpression ) + "),'-',''))"
98-
);
115+
if ( uuidFunctions ) {
116+
return template.replace(
117+
placeholder,
118+
"uuid_to_bin(json_unquote(" + queryExpression( aggregateParentReadExpression, columnExpression ) + "))"
119+
);
120+
}
121+
else {
122+
return template.replace(
123+
placeholder,
124+
"unhex(replace(json_unquote(" + queryExpression( aggregateParentReadExpression,
125+
columnExpression ) + "),'-',''))"
126+
);
127+
}
99128
}
100129
// Fall-through intended
101130
default:
@@ -142,7 +171,7 @@ private String queryExpression(String aggregateParentReadExpression, String colu
142171
}
143172
}
144173

145-
private static String jsonCustomWriteExpression(String customWriteExpression, JdbcMapping jdbcMapping) {
174+
private String jsonCustomWriteExpression(String customWriteExpression, JdbcMapping jdbcMapping) {
146175
final int sqlTypeCode = jdbcMapping.getJdbcType().getDefaultSqlTypeCode();
147176
switch ( sqlTypeCode ) {
148177
case BINARY:
@@ -159,7 +188,12 @@ private static String jsonCustomWriteExpression(String customWriteExpression, Jd
159188
return "date_format(" + customWriteExpression + ",'%Y-%m-%dT%T.%fZ')";
160189
case UUID:
161190
if ( jdbcMapping.getJdbcType().isBinary() ) {
162-
return "regexp_replace(lower(hex(" + customWriteExpression + ")),'^(.{8})(.{4})(.{4})(.{4})(.{12})$','$1-$2-$3-$4-$5')";
191+
if ( uuidFunctions ) {
192+
return "bin_to_uuid(" + customWriteExpression + ")";
193+
}
194+
else {
195+
return "regexp_replace(lower(hex(" + customWriteExpression + ")),'^(.{8})(.{4})(.{4})(.{4})(.{12})$','$1-$2-$3-$4-$5')";
196+
}
163197
}
164198
// Fall-through intended
165199
default:

hibernate-core/src/test/java/org/hibernate/orm/test/instant/InstantWithNormalizedTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hibernate.cfg.AvailableSettings;
1111
import org.hibernate.cfg.JdbcSettings;
1212
import org.hibernate.dialect.MySQLDialect;
13+
import org.hibernate.dialect.TiDBDialect;
1314
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
1415
import org.hibernate.testing.orm.junit.Jpa;
1516
import org.hibernate.testing.orm.junit.Setting;
@@ -31,6 +32,7 @@
3132
{@Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE"),
3233
@Setting(name = JdbcSettings.JDBC_TIME_ZONE, value = "+01:00")})
3334
@SkipForDialect(dialectClass = MySQLDialect.class, reason = "MySQL hangs dropping the table")
35+
@SkipForDialect(dialectClass = TiDBDialect.class, reason = "Values stored in timestamp DDL type columns get the JDBC time zone offset subtracted")
3436
public class InstantWithNormalizedTest {
3537
@Test
3638
public void test(EntityManagerFactoryScope scope) {

0 commit comments

Comments
 (0)