mchades commented on code in PR #3961:
URL: https://github.com/apache/gravitino/pull/3961#discussion_r1678919003


##########
catalogs/catalog-jdbc-doris/src/main/java/com/datastrato/gravitino/catalog/doris/operation/DorisTablePartitionOperations.java:
##########
@@ -0,0 +1,341 @@
+/*
+ * 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 com.datastrato.gravitino.catalog.doris.operation;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.datastrato.gravitino.catalog.doris.utils.DorisUtils;
+import com.datastrato.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
+import com.datastrato.gravitino.catalog.jdbc.converter.JdbcTypeConverter;
+import 
com.datastrato.gravitino.catalog.jdbc.operation.JdbcTablePartitionOperations;
+import com.datastrato.gravitino.exceptions.GravitinoRuntimeException;
+import com.datastrato.gravitino.exceptions.NoSuchPartitionException;
+import com.datastrato.gravitino.exceptions.PartitionAlreadyExistsException;
+import com.datastrato.gravitino.rel.expressions.literals.Literal;
+import com.datastrato.gravitino.rel.expressions.literals.Literals;
+import com.datastrato.gravitino.rel.expressions.transforms.Transform;
+import com.datastrato.gravitino.rel.expressions.transforms.Transforms;
+import com.datastrato.gravitino.rel.partitions.ListPartition;
+import com.datastrato.gravitino.rel.partitions.Partition;
+import com.datastrato.gravitino.rel.partitions.Partitions;
+import com.datastrato.gravitino.rel.partitions.RangePartition;
+import com.datastrato.gravitino.rel.types.Type;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import javax.sql.DataSource;
+
+public final class DorisTablePartitionOperations extends 
JdbcTablePartitionOperations {
+  private static final String PARTITION_TYPE_VALUE_PATTERN_STRING =
+      "types: \\[([^\\]]+)\\]; keys: \\[([^\\]]+)\\];";
+  private static final Pattern PARTITION_TYPE_VALUE_PATTERN =
+      Pattern.compile(PARTITION_TYPE_VALUE_PATTERN_STRING);
+
+  private final JdbcExceptionConverter exceptionConverter;
+  private final JdbcTypeConverter typeConverter;
+
+  public DorisTablePartitionOperations(
+      DataSource dataSource,
+      String databaseName,
+      String tableName,
+      JdbcExceptionConverter exceptionConverter,
+      JdbcTypeConverter typeConverter) {
+    super(dataSource, databaseName, tableName);
+    checkArgument(exceptionConverter != null, "exceptionConverter is null");
+    checkArgument(typeConverter != null, "typeConverter is null");
+    this.exceptionConverter = exceptionConverter;
+    this.typeConverter = typeConverter;
+  }
+
+  @Override
+  public String[] listPartitionNames() {
+    try (Connection connection = getConnection(databaseName)) {
+      String showPartitionsSql = String.format("SHOW PARTITIONS FROM `%s`", 
tableName);
+      try (Statement statement = connection.createStatement();
+          ResultSet result = statement.executeQuery(showPartitionsSql)) {
+        ImmutableList.Builder<String> partitionNames = ImmutableList.builder();
+        while (result.next()) {
+          partitionNames.add(result.getString("PartitionName"));
+        }
+        return partitionNames.build().stream().toArray(String[]::new);
+      }
+    } catch (SQLException e) {
+      throw exceptionConverter.toGravitinoException(e);
+    }
+  }
+
+  @Override
+  public Partition[] listPartitions() {
+    try (Connection connection = getConnection(databaseName)) {
+      Transform partitionInfo = getPartitionInfo(connection);
+      Map<String, Type> columnTypes = getColumnType(connection);
+      String showPartitionsSql = String.format("SHOW PARTITIONS FROM `%s`", 
tableName);
+      try (Statement statement = connection.createStatement();
+          ResultSet result = statement.executeQuery(showPartitionsSql)) {
+        ImmutableList.Builder<Partition> partitions = ImmutableList.builder();
+        while (result.next()) {
+          partitions.add(fromDorisPartition(result, partitionInfo, 
columnTypes));
+        }
+        return partitions.build().stream().toArray(Partition[]::new);
+      }
+    } catch (SQLException e) {
+      throw exceptionConverter.toGravitinoException(e);
+    }
+  }
+
+  @Override
+  public Partition getPartition(String partitionName) throws 
NoSuchPartitionException {
+    try (Connection connection = getConnection(databaseName)) {
+      Transform partitionInfo = getPartitionInfo(connection);
+      Map<String, Type> columnTypes = getColumnType(connection);
+      String showPartitionsSql =
+          String.format(
+              "SHOW PARTITIONS FROM `%s` WHERE PartitionName = \"%s\"", 
tableName, partitionName);
+      try (Statement statement = connection.createStatement();
+          ResultSet result = statement.executeQuery(showPartitionsSql)) {
+        while (result.next()) {
+          return fromDorisPartition(result, partitionInfo, columnTypes);
+        }
+      }
+    } catch (SQLException e) {
+      throw exceptionConverter.toGravitinoException(e);
+    }
+    throw new NoSuchPartitionException("Partition %s does not exist", 
partitionName);
+  }
+
+  @Override
+  public Partition addPartition(Partition partition) throws 
PartitionAlreadyExistsException {
+    try (Connection connection = getConnection(databaseName)) {
+      Transform partitionInfo = getPartitionInfo(connection);
+
+      String addPartitionSqlFormat = "ALTER TABLE `%s` ADD PARTITION `%s` 
VALUES %s";
+      String partitionValues;
+      Partition added;
+
+      if (partition instanceof RangePartition) {
+        Preconditions.checkArgument(
+            partitionInfo instanceof Transforms.RangeTransform,
+            "Table %s is partitioned by list, but trying to add a range 
partition",
+            tableName);
+
+        RangePartition rangePartition = (RangePartition) partition;
+        partitionValues = buildRangePartitionValues(rangePartition);
+
+        // The partition properties actually cannot be passed into Doris, we 
just return an empty
+        // map instead.
+        added =
+            Partitions.range(
+                rangePartition.name(),
+                rangePartition.upper(),
+                rangePartition.lower(),
+                Collections.emptyMap());
+      } else if (partition instanceof ListPartition) {
+        Preconditions.checkArgument(
+            partitionInfo instanceof Transforms.ListTransform,
+            "Table %s is partitioned by range, but trying to add a list 
partition",
+            tableName);
+
+        ListPartition listPartition = (ListPartition) partition;
+        partitionValues =
+            buildListPartitionValues(
+                listPartition, ((Transforms.ListTransform) 
partitionInfo).fieldNames().length);
+
+        added =
+            Partitions.list(listPartition.name(), listPartition.lists(), 
Collections.emptyMap());
+      } else {
+        throw new IllegalArgumentException("Unsupported partition type of 
Doris");
+      }
+
+      try (Statement statement = connection.createStatement()) {
+        statement.executeUpdate(
+            String.format(addPartitionSqlFormat, tableName, partition.name(), 
partitionValues));
+        return added;
+      }
+    } catch (SQLException e) {
+      throw exceptionConverter.toGravitinoException(e);
+    }
+  }
+
+  @Override
+  public boolean dropPartition(String partitionName) {
+    try (Connection connection = getConnection(databaseName)) {
+      String dropPartitionsSql =
+          String.format("ALTER TABLE `%s` DROP PARTITION `%s`", tableName, 
partitionName);
+      try (Statement statement = connection.createStatement()) {
+        statement.executeUpdate(dropPartitionsSql);
+        return true;
+      }
+    } catch (SQLException e) {
+      GravitinoRuntimeException exception = 
exceptionConverter.toGravitinoException(e);
+      if (exception instanceof NoSuchPartitionException) {
+        return false;
+      }
+      throw exception;
+    }
+  }
+
+  private Transform getPartitionInfo(Connection connection) throws 
SQLException {
+    String showCreateTableSql = String.format("SHOW CREATE TABLE `%s`", 
tableName);
+    try (Statement statement = connection.createStatement();
+        ResultSet result = statement.executeQuery(showCreateTableSql)) {
+      StringBuilder createTableSql = new StringBuilder();
+      while (result.next()) {
+        createTableSql.append(result.getString("Create Table"));
+      }
+      Optional<Transform> transform =
+          DorisUtils.extractPartitionInfoFromSql(createTableSql.toString());
+      return transform.orElseThrow(
+          () ->
+              new UnsupportedOperationException(
+                  String.format("%s is not a partitioned table", tableName)));
+    }
+  }
+
+  private Partition fromDorisPartition(
+      ResultSet resultSet, Transform partitionInfo, Map<String, Type> 
columnTypes)
+      throws SQLException {
+    String partitionName = resultSet.getString("PartitionName");
+    String partitionKey = resultSet.getString("PartitionKey");
+    String partitionValues = resultSet.getString("Range");
+    ImmutableMap.Builder<String, String> propertiesBuilder = 
ImmutableMap.builder();
+    propertiesBuilder.put("PartitionId", resultSet.getString("PartitionId"));
+    propertiesBuilder.put("VisibleVersion", 
resultSet.getString("VisibleVersion"));
+    propertiesBuilder.put("VisibleVersionTime", 
resultSet.getString("VisibleVersionTime"));
+    propertiesBuilder.put("State", resultSet.getString("State"));
+    propertiesBuilder.put("PartitionKey", partitionKey);
+    propertiesBuilder.put("DataSize", resultSet.getString("DataSize"));
+    propertiesBuilder.put("IsInMemory", resultSet.getString("IsInMemory"));
+    ImmutableMap<String, String> properties = propertiesBuilder.build();

Review Comment:
   we should use a class such as `DorisPartitionPropertiesMetadata` to manage 
those reserved properties



-- 
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: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to