Skip to content

Commit

Permalink
[#5359] fix(ob-catalog): fix ob catalog default value parse error (#5393
Browse files Browse the repository at this point in the history
)

### What changes were proposed in this pull request?

fix ob catalog default value parse error

### Why are the changes needed?

For OceanBase catalog, the `IS_GENERATEDCOLUMN` value of the column
ResultSet is set to `NO`, which is causing `isExpression` to be false
and leading to a parse error.

Fix: #5359 

### Does this PR introduce _any_ user-facing change?

no

### How was this patch tested?

tests added

Co-authored-by: mchades <[email protected]>
  • Loading branch information
github-actions[bot] and mchades authored Oct 31, 2024
1 parent 1d7e9d6 commit 5d1b8ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ void testColumnDefaultValueConverter() {
+ " date_col_5 date DEFAULT '2024-04-01',\n"
+ " timestamp_col_1 timestamp default '2012-12-31 11:30:45',\n"
+ " timestamp_col_2 timestamp default 19830905,\n"
+ " timestamp_col_3 timestamp(6) default CURRENT_TIMESTAMP(6),\n"
+ " decimal_6_2_col_1 decimal(6, 2) default 1.2,\n"
+ " bit_col_1 bit default b'1'\n"
+ ");\n";
Expand Down Expand Up @@ -569,6 +570,10 @@ void testColumnDefaultValueConverter() {
Assertions.assertEquals(
Literals.timestampLiteral("1983-09-05T00:00:00"), column.defaultValue());
break;
case "timestamp_col_3":
Assertions.assertEquals(
UnparsedExpression.of("CURRENT_TIMESTAMP(6)"), column.defaultValue());
break;
case "decimal_6_2_col_1":
Assertions.assertEquals(
Literals.decimalLiteral(Decimal.of("1.2", 6, 2)), column.defaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;
import java.util.regex.Pattern;
import org.apache.gravitino.catalog.jdbc.converter.JdbcColumnDefaultValueConverter;
import org.apache.gravitino.catalog.jdbc.converter.JdbcTypeConverter;
import org.apache.gravitino.rel.expressions.Expression;
Expand All @@ -35,6 +36,10 @@

public class OceanBaseColumnDefaultValueConverter extends JdbcColumnDefaultValueConverter {

// match CURRENT_TIMESTAMP or CURRENT_TIMESTAMP(fsp)
private static final Pattern CURRENT_TIMESTAMP =
Pattern.compile("^CURRENT_TIMESTAMP(\\(\\d+\\))?$");

@Override
public Expression toGravitino(
JdbcTypeConverter.JdbcTypeBean type,
Expand All @@ -50,7 +55,7 @@ public Expression toGravitino(
}

if (isExpression) {
if (columnDefaultValue.equals(CURRENT_TIMESTAMP)) {
if (CURRENT_TIMESTAMP.matcher(columnDefaultValue).matches()) {
return DEFAULT_VALUE_OF_CURRENT_TIMESTAMP;
}
// The parsing of OceanBase expressions is complex, so we are not currently undertaking the
Expand Down Expand Up @@ -93,7 +98,7 @@ public Expression toGravitino(
return Literals.timeLiteral(LocalTime.parse(columnDefaultValue, DATE_TIME_FORMATTER));
case JdbcTypeConverter.TIMESTAMP:
case OceanBaseTypeConverter.DATETIME:
return CURRENT_TIMESTAMP.equals(columnDefaultValue)
return CURRENT_TIMESTAMP.matcher(columnDefaultValue).matches()
? DEFAULT_VALUE_OF_CURRENT_TIMESTAMP
: Literals.timestampLiteral(
LocalDateTime.parse(columnDefaultValue, DATE_TIME_FORMATTER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ void testColumnDefaultValueConverter() {
+ " date_col_5 date DEFAULT '2024-04-01',\n"
+ " timestamp_col_1 timestamp default '2012-12-31 11:30:45',\n"
+ " timestamp_col_2 timestamp default 19830905,\n"
+ " timestamp_col_3 timestamp(6) default CURRENT_TIMESTAMP(6),\n"
+ " decimal_6_2_col_1 decimal(6, 2) default 1.2,\n"
+ " bit_col_1 bit default b'1'\n"
+ ");\n";
Expand Down Expand Up @@ -511,6 +512,7 @@ void testColumnDefaultValueConverter() {
break;
case "datetime_col_1":
case "datetime_col_2":
case "timestamp_col_3":
Assertions.assertEquals(DEFAULT_VALUE_OF_CURRENT_TIMESTAMP, column.defaultValue());
break;
case "datetime_col_3":
Expand Down

0 comments on commit 5d1b8ae

Please sign in to comment.