From 5e83dc6417f7bb6e74f78da3a41d3cf1626424d5 Mon Sep 17 00:00:00 2001 From: baiyangtx Date: Mon, 9 Sep 2024 20:15:11 +0800 Subject: [PATCH] Make TableFormat extensible| mapper convert --- .../converter/TableFormatConverter.java | 72 +++++++++++++++++++ .../persistence/mapper/TableMetaMapper.java | 31 +++++--- 2 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 amoro-ams/src/main/java/org/apache/amoro/server/persistence/converter/TableFormatConverter.java diff --git a/amoro-ams/src/main/java/org/apache/amoro/server/persistence/converter/TableFormatConverter.java b/amoro-ams/src/main/java/org/apache/amoro/server/persistence/converter/TableFormatConverter.java new file mode 100644 index 0000000000..ada463cd47 --- /dev/null +++ b/amoro-ams/src/main/java/org/apache/amoro/server/persistence/converter/TableFormatConverter.java @@ -0,0 +1,72 @@ +/* + * 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.amoro.server.persistence.converter; + +import org.apache.amoro.TableFormat; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; +import org.apache.ibatis.type.TypeHandler; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@MappedTypes(TableFormat.class) +@MappedJdbcTypes(JdbcType.VARCHAR) +public class TableFormatConverter implements TypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, TableFormat parameter, JdbcType jdbcType) + throws SQLException { + if (parameter == null) { + ps.setString(i, ""); + } else { + ps.setString(i, parameter.name()); + } + } + + @Override + public TableFormat getResult(ResultSet rs, String columnName) throws SQLException { + String res = rs.getString(columnName); + if (res == null) { + return null; + } + return TableFormat.valueOf(res); + } + + @Override + public TableFormat getResult(ResultSet rs, int columnIndex) throws SQLException { + String res = rs.getString(columnIndex); + if (res == null) { + return null; + } + return TableFormat.valueOf(res); + } + + @Override + public TableFormat getResult(CallableStatement cs, int columnIndex) throws SQLException { + String res = cs.getString(columnIndex); + if (res == null) { + return null; + } + return TableFormat.valueOf(res); + } +} diff --git a/amoro-ams/src/main/java/org/apache/amoro/server/persistence/mapper/TableMetaMapper.java b/amoro-ams/src/main/java/org/apache/amoro/server/persistence/mapper/TableMetaMapper.java index f056dcb5e8..a9c605e8ec 100644 --- a/amoro-ams/src/main/java/org/apache/amoro/server/persistence/mapper/TableMetaMapper.java +++ b/amoro-ams/src/main/java/org/apache/amoro/server/persistence/mapper/TableMetaMapper.java @@ -23,6 +23,7 @@ import org.apache.amoro.server.persistence.converter.Long2TsConverter; import org.apache.amoro.server.persistence.converter.Map2StringConverter; import org.apache.amoro.server.persistence.converter.MapLong2StringConverter; +import org.apache.amoro.server.persistence.converter.TableFormatConverter; import org.apache.amoro.server.table.TableMetadata; import org.apache.amoro.server.table.TableRuntime; import org.apache.amoro.server.table.TableRuntimeMeta; @@ -77,7 +78,10 @@ Integer decTableCount( @Result(property = "tableIdentifier.tableName", column = "table_name"), @Result(property = "tableIdentifier.database", column = "db_name"), @Result(property = "tableIdentifier.catalog", column = "catalog_name"), - @Result(property = "tableIdentifier.format", column = "format"), + @Result( + property = "tableIdentifier.format", + column = "format", + typeHandler = TableFormatConverter.class), @Result(property = "primaryKey", column = "primary_key"), @Result(property = "tableLocation", column = "table_location"), @Result(property = "baseLocation", column = "base_location"), @@ -112,7 +116,10 @@ Integer decTableCount( @Result(property = "tableIdentifier.catalog", column = "catalog_name"), @Result(property = "tableIdentifier.database", column = "db_name"), @Result(property = "tableIdentifier.tableName", column = "table_name"), - @Result(property = "tableIdentifier.format", column = "format"), + @Result( + property = "tableIdentifier.format", + column = "format", + typeHandler = TableFormatConverter.class), @Result(property = "primaryKey", column = "primary_key"), @Result(property = "tableLocation", column = "table_location"), @Result(property = "baseLocation", column = "base_location"), @@ -182,7 +189,10 @@ int commitTableChange( @Result(property = "tableIdentifier.catalog", column = "catalog_name"), @Result(property = "tableIdentifier.database", column = "db_name"), @Result(property = "tableIdentifier.tableName", column = "table_name"), - @Result(property = "tableIdentifier.format", column = "format"), + @Result( + property = "tableIdentifier.format", + column = "format", + typeHandler = TableFormatConverter.class), @Result(property = "primaryKey", column = "primary_key"), @Result(property = "tableLocation", column = "table_location"), @Result(property = "baseLocation", column = "base_location"), @@ -217,7 +227,10 @@ int commitTableChange( @Result(property = "tableIdentifier.catalog", column = "catalog_name"), @Result(property = "tableIdentifier.database", column = "db_name"), @Result(property = "tableIdentifier.tableName", column = "table_name"), - @Result(property = "tableIdentifier.format", column = "format"), + @Result( + property = "tableIdentifier.format", + column = "format", + typeHandler = TableFormatConverter.class), @Result(property = "primaryKey", column = "primary_key"), @Result(property = "tableLocation", column = "table_location"), @Result(property = "baseLocation", column = "base_location"), @@ -267,7 +280,7 @@ Integer deleteTableIdByName( @Result(property = "tableName", column = "table_name"), @Result(property = "database", column = "db_name"), @Result(property = "catalog", column = "catalog_name"), - @Result(property = "format", column = "format"), + @Result(property = "format", column = "format", typeHandler = TableFormatConverter.class) }) ServerTableIdentifier selectTableIdentifier( @Param("catalogName") String catalogName, @@ -282,7 +295,7 @@ ServerTableIdentifier selectTableIdentifier( @Result(property = "catalog", column = "catalog_name"), @Result(property = "database", column = "db_name"), @Result(property = "tableName", column = "table_name"), - @Result(property = "format", column = "format") + @Result(property = "format", column = "format", typeHandler = TableFormatConverter.class) }) List selectTableIdentifiersByDb( @Param("catalogName") String catalogName, @Param("databaseName") String databaseName); @@ -295,7 +308,7 @@ List selectTableIdentifiersByDb( @Result(property = "catalog", column = "catalog_name"), @Result(property = "database", column = "db_name"), @Result(property = "tableName", column = "table_name"), - @Result(property = "format", column = "format") + @Result(property = "format", column = "format", typeHandler = TableFormatConverter.class) }) List selectTableIdentifiersByCatalog( @Param("catalogName") String catalogName); @@ -306,7 +319,7 @@ List selectTableIdentifiersByCatalog( @Result(property = "catalog", column = "catalog_name"), @Result(property = "database", column = "db_name"), @Result(property = "tableName", column = "table_name"), - @Result(property = "format", column = "format") + @Result(property = "format", column = "format", typeHandler = TableFormatConverter.class) }) List selectAllTableIdentifiers(); @@ -380,7 +393,7 @@ List selectTableIdentifiersByCatalog( @Result(property = "catalogName", column = "catalog_name"), @Result(property = "dbName", column = "db_name"), @Result(property = "tableName", column = "table_name"), - @Result(property = "format", column = "format"), + @Result(property = "format", column = "format", typeHandler = TableFormatConverter.class), @Result(property = "currentSnapshotId", column = "current_snapshot_id"), @Result(property = "currentChangeSnapshotId", column = "current_change_snapshotId"), @Result(property = "lastOptimizedSnapshotId", column = "last_optimized_snapshotId"),