Skip to content

Commit c493269

Browse files
committed
fix: update
Signed-off-by: zacsun <[email protected]>
1 parent b1ec1cd commit c493269

File tree

4 files changed

+43
-55
lines changed

4 files changed

+43
-55
lines changed

api/src/main/java/org/apache/gravitino/rel/types/Types.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public static TimeType get() {
343343
* @return A {@link TimeType} with the given precision.
344344
*/
345345
public static TimeType of(Integer precision) {
346-
return new TimeType(Optional.of(precision));
346+
return new TimeType(Optional.ofNullable(precision));
347347
}
348348

349349
private final Optional<Integer> precision;
@@ -352,14 +352,9 @@ private TimeType(Optional<Integer> precision) {
352352
this.precision = precision;
353353
}
354354

355-
/** @return True if the time type has precision specified, false otherwise. */
356-
public boolean hasPrecision() {
357-
return precision.isPresent();
358-
}
359-
360355
/** @return The precision of the time type. */
361-
public Integer precision() {
362-
return precision.orElse(null);
356+
public Optional<Integer> precision() {
357+
return precision;
363358
}
364359

365360
@Override
@@ -369,10 +364,7 @@ public Name name() {
369364

370365
@Override
371366
public String simpleString() {
372-
if (!precision.isPresent()) {
373-
return "time";
374-
}
375-
return String.format("time(%d)", precision.get());
367+
return precision.map(integer -> String.format("time(%d)", integer)).orElse("time");
376368
}
377369

378370
@Override
@@ -435,14 +427,9 @@ public boolean hasTimeZone() {
435427
return withTimeZone;
436428
}
437429

438-
/** @return True if the timestamp type has precision specified, false otherwise. */
439-
public boolean hasPrecision() {
440-
return precision.isPresent();
441-
}
442-
443430
/** @return The precision of the timestamp type. */
444-
public Integer precision() {
445-
return precision.orElse(null);
431+
public Optional<Integer> precision() {
432+
return precision;
446433
}
447434

448435
@Override
@@ -453,12 +440,13 @@ public Name name() {
453440
/** @return The simple string representation of the timestamp type. */
454441
@Override
455442
public String simpleString() {
456-
if (!hasPrecision()) {
457-
return withTimeZone ? "timestamp_tz" : "timestamp";
458-
}
459-
return withTimeZone
460-
? String.format("timestamp_tz(%d)", precision())
461-
: String.format("timestamp(%d)", precision());
443+
return precision
444+
.map(
445+
integer ->
446+
withTimeZone
447+
? String.format("timestamp_tz(%d)", integer)
448+
: String.format("timestamp(%d)", integer))
449+
.orElse(withTimeZone ? "timestamp_tz" : "timestamp");
462450
}
463451

464452
@Override

catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/converter/MysqlTypeConverter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,15 @@ public Type toGravitino(JdbcTypeBean typeBean) {
7272
case DATE:
7373
return Types.DateType.get();
7474
case TIME:
75-
return typeBean.getDatetimePrecision() == null
76-
? Types.TimeType.get()
77-
: Types.TimeType.of(typeBean.getDatetimePrecision());
75+
return Types.TimeType.of(typeBean.getDatetimePrecision());
7876
// MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back
7977
// from UTC to the current time zone for retrieval. (This does not occur for other types
8078
// such as DATETIME.) see more details:
8179
// https://dev.mysql.com/doc/refman/8.0/en/datetime.html
8280
case TIMESTAMP:
83-
return typeBean.getDatetimePrecision() == null
84-
? Types.TimestampType.withTimeZone()
85-
: Types.TimestampType.withTimeZone(typeBean.getDatetimePrecision());
81+
return Types.TimestampType.withTimeZone(typeBean.getDatetimePrecision());
8682
case DATETIME:
87-
return typeBean.getDatetimePrecision() == null
88-
? Types.TimestampType.withoutTimeZone()
89-
: Types.TimestampType.withoutTimeZone(typeBean.getDatetimePrecision());
83+
return Types.TimestampType.withoutTimeZone(typeBean.getDatetimePrecision());
9084
case DECIMAL:
9185
return Types.DecimalType.of(typeBean.getColumnSize(), typeBean.getScale());
9286
case VARCHAR:
@@ -144,8 +138,8 @@ public String fromGravitino(Type type) {
144138
// such as DATETIME.) see more details: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
145139
Types.TimestampType timestampType = (Types.TimestampType) type;
146140
String baseType = timestampType.hasTimeZone() ? TIMESTAMP : DATETIME;
147-
return timestampType.hasPrecision()
148-
? String.format("%s(%d)", baseType, timestampType.precision())
141+
return timestampType.precision().isPresent()
142+
? String.format("%s(%d)", baseType, timestampType.precision().get())
149143
: baseType;
150144
} else if (type instanceof Types.DecimalType) {
151145
return type.simpleString();

catalogs/catalog-jdbc-mysql/src/main/java/org/apache/gravitino/catalog/mysql/operation/MysqlTableOperations.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,14 +673,30 @@ private static void validateIndexes(Index[] indexes, JdbcColumn[] columns) {
673673
}
674674
}
675675

676+
/**
677+
* Calculates the precision for datetime-related types based on the column size from MySQL JDBC
678+
* driver. This calculation is derived from MySQL's internal representation of datetime types: -
679+
* TIME: Format is 'HH:MM:SS' (8 chars) + precision - TIMESTAMP/DATETIME: Format is 'YYYY-MM-DD
680+
* HH:MM:SS' (19 chars) + precision
681+
*
682+
* <p>The precision is calculated by subtracting the base format length from the total column
683+
* size. For example: - TIME(6) has columnSize = 14 (8 + 6) - TIMESTAMP(6) has columnSize = 25 (19
684+
* + 6)
685+
*
686+
* @param typeName The data type name (TIME, TIMESTAMP, or DATETIME)
687+
* @param columnSize The total column size from MySQL JDBC driver
688+
* @return The precision value (number of fractional seconds digits)
689+
*/
676690
@Override
677691
public int calculateDatetimePrecision(String typeName, int columnSize) {
678692
String upperTypeName = typeName.toUpperCase();
679693
switch (upperTypeName) {
680694
case "TIME":
695+
// TIME format: 'HH:MM:SS' (8 chars) + precision
681696
return columnSize > 8 ? columnSize - 9 : 0;
682697
case "TIMESTAMP":
683698
case "DATETIME":
699+
// TIMESTAMP/DATETIME format: 'YYYY-MM-DD HH:MM:SS' (19 chars) + precision
684700
return columnSize > 19 ? columnSize - 20 : 0;
685701
}
686702
return 0;

trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/catalog/jdbc/mysql/MySQLDataTypeTransformer.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public io.trino.spi.type.Type getTrinoType(Type type) {
5959

6060
private static TimestampWithTimeZoneType getTimestampWithTimeZoneType(
6161
Types.TimestampType timestampType) {
62-
if (!timestampType.hasPrecision()) {
63-
return TimestampWithTimeZoneType.TIMESTAMP_TZ_SECONDS;
64-
}
65-
switch (timestampType.precision()) {
62+
int precision = timestampType.precision().orElse(0);
63+
switch (precision) {
6664
case TIMESTAMP_PRECISION_SECONDS:
6765
return TimestampWithTimeZoneType.TIMESTAMP_TZ_SECONDS;
6866
case TIMESTAMP_PRECISION_MILLIS:
@@ -72,17 +70,13 @@ private static TimestampWithTimeZoneType getTimestampWithTimeZoneType(
7270
default:
7371
throw new TrinoException(
7472
GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
75-
"Invalid MySQL timestamp precision: "
76-
+ timestampType.precision()
77-
+ ". Valid values are 0, 3, 6");
73+
"Invalid MySQL timestamp precision: " + precision + ". Valid values are 0, 3, 6");
7874
}
7975
}
8076

8177
private static TimestampType getTimestampType(Types.TimestampType timestampType) {
82-
if (!timestampType.hasPrecision()) {
83-
return TimestampType.TIMESTAMP_SECONDS;
84-
}
85-
switch (timestampType.precision()) {
78+
int precision = timestampType.precision().orElse(0);
79+
switch (precision) {
8680
case TIMESTAMP_PRECISION_SECONDS:
8781
return TimestampType.TIMESTAMP_SECONDS;
8882
case TIMESTAMP_PRECISION_MILLIS:
@@ -92,17 +86,13 @@ private static TimestampType getTimestampType(Types.TimestampType timestampType)
9286
default:
9387
throw new TrinoException(
9488
GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
95-
"Invalid MySQL datetime precision: "
96-
+ timestampType.precision()
97-
+ ". Valid values are 0, 3, 6");
89+
"Invalid MySQL datetime precision: " + precision + ". Valid values are 0, 3, 6");
9890
}
9991
}
10092

10193
private static TimeType getTimeType(Types.TimeType timeType) {
102-
if (!timeType.hasPrecision()) {
103-
return TimeType.TIME_SECONDS;
104-
}
105-
switch (timeType.precision()) {
94+
int precision = timeType.precision().orElse(0);
95+
switch (precision) {
10696
case TIMESTAMP_PRECISION_SECONDS:
10797
return TimeType.TIME_SECONDS;
10898
case TIMESTAMP_PRECISION_MILLIS:
@@ -112,7 +102,7 @@ private static TimeType getTimeType(Types.TimeType timeType) {
112102
default:
113103
throw new TrinoException(
114104
GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT,
115-
"Invalid MySQL time precision: " + timeType.precision() + ". Valid values are 0, 3, 6");
105+
"Invalid MySQL time precision: " + precision + ". Valid values are 0, 3, 6");
116106
}
117107
}
118108

0 commit comments

Comments
 (0)