yuanoOo commented on code in PR #5228:
URL: https://github.com/apache/gravitino/pull/5228#discussion_r1815924753
##########
catalogs/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.java:
##########
@@ -58,10 +94,134 @@ protected String generateCreateTableSql(
Distribution distribution,
Index[] indexes) {
if (ArrayUtils.isNotEmpty(partitioning)) {
- throw new UnsupportedOperationException("Currently not support Partition
tables.");
+ throw new UnsupportedOperationException(
+ "Currently we do not support Partitioning in oceanbase");
+ }
+
+ if (!Distributions.NONE.equals(distribution)) {
+ throw new UnsupportedOperationException("OceanBase does not support
distribution");
+ }
+
+ validateIncrementCol(columns, indexes);
Review Comment:
Refactored.
##########
catalogs/catalog-jdbc-oceanbase/src/main/java/org/apache/gravitino/catalog/oceanbase/operation/OceanBaseTableOperations.java:
##########
@@ -58,10 +94,134 @@ protected String generateCreateTableSql(
Distribution distribution,
Index[] indexes) {
if (ArrayUtils.isNotEmpty(partitioning)) {
- throw new UnsupportedOperationException("Currently not support Partition
tables.");
+ throw new UnsupportedOperationException(
+ "Currently we do not support Partitioning in oceanbase");
+ }
+
+ if (!Distributions.NONE.equals(distribution)) {
+ throw new UnsupportedOperationException("OceanBase does not support
distribution");
+ }
+
+ validateIncrementCol(columns, indexes);
+ StringBuilder sqlBuilder = new StringBuilder();
+ sqlBuilder.append(String.format("CREATE TABLE `%s` (\n", tableName));
+
+ // Add columns
+ for (int i = 0; i < columns.length; i++) {
+ JdbcColumn column = columns[i];
+ sqlBuilder
+ .append(SPACE)
+ .append(SPACE)
+ .append(BACK_QUOTE)
+ .append(column.name())
+ .append(BACK_QUOTE);
+
+ appendColumnDefinition(column, sqlBuilder);
+ // Add a comma for the next column, unless it's the last one
+ if (i < columns.length - 1) {
+ sqlBuilder.append(",\n");
+ }
+ }
+
+ appendIndexesSql(indexes, sqlBuilder);
+
+ sqlBuilder.append("\n)");
+
+ // Add table comment if specified
+ if (StringUtils.isNotEmpty(comment)) {
+ sqlBuilder.append(" COMMENT='").append(comment).append("'");
+ }
+
+ // Add table properties
+ if (MapUtils.isNotEmpty(properties)) {
+ sqlBuilder.append(
+ properties.entrySet().stream()
+ .map(entry -> String.format("%s = %s", entry.getKey(),
entry.getValue()))
+ .collect(Collectors.joining(",\n", "\n", "")));
+ }
+
+ // Return the generated SQL statement
+ String result = sqlBuilder.append(";").toString();
+
+ LOG.info("Generated create table:{} sql: {}", tableName, result);
+ return result;
+ }
+
+ /**
+ * The auto-increment column will be verified. There can only be one
auto-increment column and it
+ * must be the primary key or unique index.
+ *
+ * @param columns jdbc column
+ * @param indexes table indexes
+ */
+ private static void validateIncrementCol(JdbcColumn[] columns, Index[]
indexes) {
+ // Check auto increment column
+ List<JdbcColumn> autoIncrementCols =
+
Arrays.stream(columns).filter(Column::autoIncrement).collect(Collectors.toList());
+ String autoIncrementColsStr =
+
autoIncrementCols.stream().map(JdbcColumn::name).collect(Collectors.joining(",",
"[", "]"));
+ Preconditions.checkArgument(
+ autoIncrementCols.size() <= 1,
+ "Only one column can be auto-incremented. There are multiple
auto-increment columns in your table: "
+ + autoIncrementColsStr);
+ if (!autoIncrementCols.isEmpty()) {
+ Optional<Index> existAutoIncrementColIndexOptional =
+ Arrays.stream(indexes)
+ .filter(
+ index ->
+ Arrays.stream(index.fieldNames())
+ .flatMap(Arrays::stream)
+ .anyMatch(
+ s ->
+
StringUtils.equalsIgnoreCase(autoIncrementCols.get(0).name(), s)))
+ .filter(
+ index ->
+ index.type() == Index.IndexType.PRIMARY_KEY
+ || index.type() == Index.IndexType.UNIQUE_KEY)
+ .findAny();
+ Preconditions.checkArgument(
+ existAutoIncrementColIndexOptional.isPresent(),
+ "Incorrect table definition; there can be only one auto column and
it must be defined as a key");
+ }
+ }
+
+ public static void appendIndexesSql(Index[] indexes, StringBuilder
sqlBuilder) {
+ for (Index index : indexes) {
+ String fieldStr = getIndexFieldStr(index.fieldNames());
+ sqlBuilder.append(",\n");
+ switch (index.type()) {
+ case PRIMARY_KEY:
+ if (null != index.name()
+ && !StringUtils.equalsIgnoreCase(
+ index.name(), Indexes.DEFAULT_MYSQL_PRIMARY_KEY_NAME)) {
+ throw new IllegalArgumentException("Primary key name must be
PRIMARY in OceanBase");
+ }
+ sqlBuilder.append("CONSTRAINT ").append("PRIMARY KEY
(").append(fieldStr).append(")");
+ break;
+ case UNIQUE_KEY:
+ sqlBuilder.append("CONSTRAINT ");
+ if (null != index.name()) {
+
sqlBuilder.append(BACK_QUOTE).append(index.name()).append(BACK_QUOTE);
+ }
+ sqlBuilder.append(" UNIQUE (").append(fieldStr).append(")");
+ break;
+ default:
+ throw new IllegalArgumentException("OceanBase doesn't support index
: " + index.type());
+ }
}
+ }
- throw new UnsupportedOperationException("Not implemented yet.");
+ private static String getIndexFieldStr(String[][] fieldNames) {
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]