TimothyDing commented on code in PR #9963: URL: https://github.com/apache/gravitino/pull/9963#discussion_r2844260063
########## catalogs-contrib/catalog-jdbc-hologres/src/main/java/org/apache/gravitino/catalog/hologres/operation/HologresTableOperations.java: ########## @@ -0,0 +1,1089 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.catalog.hologres.operation; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import javax.sql.DataSource; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.StringIdentifier; +import org.apache.gravitino.catalog.jdbc.JdbcColumn; +import org.apache.gravitino.catalog.jdbc.JdbcTable; +import org.apache.gravitino.catalog.jdbc.config.JdbcConfig; +import org.apache.gravitino.catalog.jdbc.converter.JdbcColumnDefaultValueConverter; +import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter; +import org.apache.gravitino.catalog.jdbc.converter.JdbcTypeConverter; +import org.apache.gravitino.catalog.jdbc.operation.DatabaseOperation; +import org.apache.gravitino.catalog.jdbc.operation.JdbcTableOperations; +import org.apache.gravitino.catalog.jdbc.operation.RequireDatabaseOperation; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NoSuchTableException; +import org.apache.gravitino.rel.Column; +import org.apache.gravitino.rel.TableChange; +import org.apache.gravitino.rel.expressions.NamedReference; +import org.apache.gravitino.rel.expressions.UnparsedExpression; +import org.apache.gravitino.rel.expressions.distributions.Distribution; +import org.apache.gravitino.rel.expressions.distributions.Distributions; +import org.apache.gravitino.rel.expressions.distributions.Strategy; +import org.apache.gravitino.rel.expressions.transforms.Transform; +import org.apache.gravitino.rel.expressions.transforms.Transforms; +import org.apache.gravitino.rel.indexes.Index; + +/** + * Table operations for Hologres. + * + * <p>Hologres is PostgreSQL-compatible, so most table operations follow PostgreSQL conventions. + * However, Hologres has specific features like table properties (orientation, distribution_key, + * etc.) that are handled through the WITH clause in CREATE TABLE statements. + */ +public class HologresTableOperations extends JdbcTableOperations + implements RequireDatabaseOperation { + + public static final String HOLO_QUOTE = "\""; + public static final String NEW_LINE = "\n"; + public static final String ALTER_TABLE = "ALTER TABLE "; + public static final String ALTER_COLUMN = "ALTER COLUMN "; + public static final String IS = " IS '"; + public static final String COLUMN_COMMENT = "COMMENT ON COLUMN "; + public static final String TABLE_COMMENT = "COMMENT ON TABLE "; + + private static final String HOLOGRES_NOT_SUPPORT_NESTED_COLUMN_MSG = + "Hologres does not support nested column names."; + + private String database; + private HologresSchemaOperations schemaOperations; + + @Override + public void initialize( + DataSource dataSource, + JdbcExceptionConverter exceptionMapper, + JdbcTypeConverter jdbcTypeConverter, + JdbcColumnDefaultValueConverter jdbcColumnDefaultValueConverter, + Map<String, String> conf) { + super.initialize( + dataSource, exceptionMapper, jdbcTypeConverter, jdbcColumnDefaultValueConverter, conf); + database = new JdbcConfig(conf).getJdbcDatabase(); + Preconditions.checkArgument( + StringUtils.isNotBlank(database), + "The `jdbc-database` configuration item is mandatory in Hologres."); + } + + @Override + public void setDatabaseOperation(DatabaseOperation databaseOperation) { + this.schemaOperations = (HologresSchemaOperations) databaseOperation; + } + + @Override + public List<String> listTables(String schemaName) throws NoSuchSchemaException { + try (Connection connection = getConnection(schemaName)) { + if (!schemaOperations.schemaExists(connection, schemaName)) { + throw new NoSuchSchemaException("No such schema: %s", schemaName); + } + final List<String> names = Lists.newArrayList(); + try (ResultSet tables = getTables(connection)) { + while (tables.next()) { + if (Objects.equals(tables.getString("TABLE_SCHEM"), schemaName)) { + names.add(tables.getString("TABLE_NAME")); + } + } + } + LOG.info("Finished listing tables size {} for schema name {} ", names.size(), schemaName); + return names; + } catch (final SQLException se) { + throw this.exceptionMapper.toGravitinoException(se); + } + } + + @Override + protected JdbcTable.Builder getTableBuilder( + ResultSet tablesResult, String databaseName, String tableName) throws SQLException { + boolean found = false; + JdbcTable.Builder builder = null; + while (tablesResult.next() && !found) { + String tableNameInResult = tablesResult.getString("TABLE_NAME"); + String tableSchemaInResultLowerCase = tablesResult.getString("TABLE_SCHEM"); + if (Objects.equals(tableNameInResult, tableName) + && Objects.equals(tableSchemaInResultLowerCase, databaseName)) { + builder = getBasicJdbcTableInfo(tablesResult); + found = true; + } + } + + if (!found) { + throw new NoSuchTableException("Table %s does not exist in %s.", tableName, databaseName); + } + + return builder; + } + + @Override + protected JdbcColumn.Builder getColumnBuilder( + ResultSet columnsResult, String databaseName, String tableName) throws SQLException { + JdbcColumn.Builder builder = null; + if (Objects.equals(columnsResult.getString("TABLE_NAME"), tableName) + && Objects.equals(columnsResult.getString("TABLE_SCHEM"), databaseName)) { + builder = getBasicJdbcColumnInfo(columnsResult); + } + return builder; + } + + @Override + protected String generateCreateTableSql( + String tableName, + JdbcColumn[] columns, + String comment, + Map<String, String> properties, + Transform[] partitioning, + Distribution distribution, + Index[] indexes) { + boolean isLogicalPartition = + MapUtils.isNotEmpty(properties) + && "true".equalsIgnoreCase(properties.get("is_logical_partitioned_table")); + StringBuilder sqlBuilder = new StringBuilder(); + sqlBuilder + .append("CREATE TABLE ") + .append(HOLO_QUOTE) + .append(tableName) + .append(HOLO_QUOTE) + .append(" (") + .append(NEW_LINE); + + // Add columns + for (int i = 0; i < columns.length; i++) { + JdbcColumn column = columns[i]; + sqlBuilder.append(" ").append(HOLO_QUOTE).append(column.name()).append(HOLO_QUOTE); + + appendColumnDefinition(column, sqlBuilder); + // Add a comma for the next column, unless it's the last one + if (i < columns.length - 1) { + sqlBuilder.append(",").append(NEW_LINE); + } + } + appendIndexesSql(indexes, sqlBuilder); + sqlBuilder.append(NEW_LINE).append(")"); + + // Append partitioning clause if specified + if (ArrayUtils.isNotEmpty(partitioning)) { + appendPartitioningSql(partitioning, isLogicalPartition, sqlBuilder); + } + + // Build WITH clause combining distribution and Hologres-specific table properties + // Supported properties: orientation, distribution_key, clustering_key, event_time_column, + // bitmap_columns, dictionary_encoding_columns, time_to_live_in_seconds, table_group, etc. + List<String> withEntries = new ArrayList<>(); + + // Add distribution_key from Distribution parameter + if (!Distributions.NONE.equals(distribution)) { + validateDistribution(distribution); + String distributionColumns = + Arrays.stream(distribution.expressions()) + .map(Object::toString) + .collect(Collectors.joining(",")); + withEntries.add("distribution_key = '" + distributionColumns + "'"); + } + + // Add user-specified properties (filter out read-only / internally-handled properties) + if (MapUtils.isNotEmpty(properties)) { + properties.forEach( + (key, value) -> { + if (!"distribution_key".equals(key) + && !"is_logical_partitioned_table".equals(key) + && !"primary_key".equals(key)) { + withEntries.add(key + " = '" + value + "'"); Review Comment: @yuqi1129 Thank you for your suggestion. I have fixed this issue in -- 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]
