-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
### What changes were proposed in this pull request? This PR aims to add storage support for columns in Gravitino. ### Why are the changes needed? With this, we can also support managing columns in Gravitno, like set tags, do column level privileges. Fix: #4867 ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? UTs added.
- Loading branch information
Showing
28 changed files
with
2,449 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
core/src/main/java/org/apache/gravitino/storage/relational/mapper/TableColumnMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.storage.relational.mapper; | ||
|
||
import java.util.List; | ||
import org.apache.gravitino.storage.relational.po.ColumnPO; | ||
import org.apache.ibatis.annotations.DeleteProvider; | ||
import org.apache.ibatis.annotations.InsertProvider; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.SelectProvider; | ||
import org.apache.ibatis.annotations.UpdateProvider; | ||
|
||
public interface TableColumnMapper { | ||
|
||
String COLUMN_TABLE_NAME = "table_column_version_info"; | ||
|
||
@SelectProvider( | ||
type = TableColumnSQLProviderFactory.class, | ||
method = "listColumnPOsByTableIdAndVersion") | ||
List<ColumnPO> listColumnPOsByTableIdAndVersion( | ||
@Param("tableId") Long tableId, @Param("tableVersion") Long tableVersion); | ||
|
||
@InsertProvider(type = TableColumnSQLProviderFactory.class, method = "insertColumnPOs") | ||
void insertColumnPOs(@Param("columnPOs") List<ColumnPO> columnPOs); | ||
|
||
@UpdateProvider(type = TableColumnSQLProviderFactory.class, method = "softDeleteColumnsByTableId") | ||
Integer softDeleteColumnsByTableId(@Param("tableId") Long tableId); | ||
|
||
@UpdateProvider( | ||
type = TableColumnSQLProviderFactory.class, | ||
method = "softDeleteColumnsByMetalakeId") | ||
Integer softDeleteColumnsByMetalakeId(@Param("metalakeId") Long metalakeId); | ||
|
||
@UpdateProvider( | ||
type = TableColumnSQLProviderFactory.class, | ||
method = "softDeleteColumnsByCatalogId") | ||
Integer softDeleteColumnsByCatalogId(@Param("catalogId") Long catalogId); | ||
|
||
@UpdateProvider( | ||
type = TableColumnSQLProviderFactory.class, | ||
method = "softDeleteColumnsBySchemaId") | ||
Integer softDeleteColumnsBySchemaId(@Param("schemaId") Long schemaId); | ||
|
||
@DeleteProvider( | ||
type = TableColumnSQLProviderFactory.class, | ||
method = "deleteColumnPOsByLegacyTimeline") | ||
Integer deleteColumnPOsByLegacyTimeline( | ||
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); | ||
} |
84 changes: 84 additions & 0 deletions
84
...in/java/org/apache/gravitino/storage/relational/mapper/TableColumnSQLProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.storage.relational.mapper; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.gravitino.storage.relational.JDBCBackend; | ||
import org.apache.gravitino.storage.relational.mapper.provider.base.TableColumnBaseSQLProvider; | ||
import org.apache.gravitino.storage.relational.mapper.provider.postgresql.TableColumnPostgreSQLProvider; | ||
import org.apache.gravitino.storage.relational.po.ColumnPO; | ||
import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
public class TableColumnSQLProviderFactory { | ||
|
||
static class TableColumnH2Provider extends TableColumnBaseSQLProvider {} | ||
|
||
static class TableColumnMySQLProvider extends TableColumnBaseSQLProvider {} | ||
|
||
private static final Map<JDBCBackend.JDBCBackendType, TableColumnBaseSQLProvider> | ||
TABLE_COLUMN_SQL_PROVIDERS = | ||
ImmutableMap.of( | ||
JDBCBackend.JDBCBackendType.MYSQL, new TableColumnMySQLProvider(), | ||
JDBCBackend.JDBCBackendType.H2, new TableColumnH2Provider(), | ||
JDBCBackend.JDBCBackendType.POSTGRESQL, new TableColumnPostgreSQLProvider()); | ||
|
||
public static TableColumnBaseSQLProvider getProvider() { | ||
String databaseId = | ||
SqlSessionFactoryHelper.getInstance() | ||
.getSqlSessionFactory() | ||
.getConfiguration() | ||
.getDatabaseId(); | ||
JDBCBackend.JDBCBackendType jdbcBackendType = | ||
JDBCBackend.JDBCBackendType.fromString(databaseId); | ||
return TABLE_COLUMN_SQL_PROVIDERS.get(jdbcBackendType); | ||
} | ||
|
||
public static String listColumnPOsByTableIdAndVersion( | ||
@Param("tableId") Long tableId, @Param("tableVersion") Long tableVersion) { | ||
return getProvider().listColumnPOsByTableIdAndVersion(tableId, tableVersion); | ||
} | ||
|
||
public static String insertColumnPOs(@Param("columnPOs") List<ColumnPO> columnPOs) { | ||
return getProvider().insertColumnPOs(columnPOs); | ||
} | ||
|
||
public static String softDeleteColumnsByTableId(@Param("tableId") Long tableId) { | ||
return getProvider().softDeleteColumnsByTableId(tableId); | ||
} | ||
|
||
public static String deleteColumnPOsByLegacyTimeline( | ||
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { | ||
return getProvider().deleteColumnPOsByLegacyTimeline(legacyTimeline, limit); | ||
} | ||
|
||
public static String softDeleteColumnsByMetalakeId(@Param("metalakeId") Long metalakeId) { | ||
return getProvider().softDeleteColumnsByMetalakeId(metalakeId); | ||
} | ||
|
||
public static String softDeleteColumnsByCatalogId(@Param("catalogId") Long catalogId) { | ||
return getProvider().softDeleteColumnsByCatalogId(catalogId); | ||
} | ||
|
||
public static String softDeleteColumnsBySchemaId(@Param("schemaId") Long schemaId) { | ||
return getProvider().softDeleteColumnsBySchemaId(schemaId); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
.../apache/gravitino/storage/relational/mapper/provider/base/TableColumnBaseSQLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.storage.relational.mapper.provider.base; | ||
|
||
import java.util.List; | ||
import org.apache.gravitino.storage.relational.mapper.TableColumnMapper; | ||
import org.apache.gravitino.storage.relational.po.ColumnPO; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
public class TableColumnBaseSQLProvider { | ||
|
||
public String listColumnPOsByTableIdAndVersion( | ||
@Param("tableId") Long tableId, @Param("tableVersion") Long tableVersion) { | ||
return "SELECT t1.column_id AS columnId, t1.column_name AS columnName," | ||
+ " t1.metalake_id AS metalakeId, t1.catalog_id AS catalogId," | ||
+ " t1.schema_id AS schemaId, t1.table_id AS tableId," | ||
+ " t1.table_version AS tableVersion, t1.column_type AS columnType," | ||
+ " t1.column_comment AS columnComment, t1.column_nullable AS nullable," | ||
+ " t1.column_auto_increment AS autoIncrement," | ||
+ " t1.column_default_value AS defaultValue, t1.column_op_type AS columnOpType," | ||
+ " t1.deleted_at AS deletedAt, t1.audit_info AS auditInfo" | ||
+ " FROM " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " t1 JOIN (" | ||
+ " SELECT column_id, MAX(table_version) AS max_table_version" | ||
+ " FROM " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " WHERE table_id = #{tableId} AND table_version <= #{tableVersion} AND deleted_at = 0" | ||
+ " GROUP BY column_id) t2" | ||
+ " ON t1.column_id = t2.column_id AND t1.table_version = t2.max_table_version"; | ||
} | ||
|
||
public String insertColumnPOs(@Param("columnPOs") List<ColumnPO> columnPOs) { | ||
return "<script>" | ||
+ "INSERT INTO " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ "(column_id, column_name, metalake_id, catalog_id, schema_id, table_id, table_version," | ||
+ " column_type, column_comment, column_nullable, column_auto_increment, " | ||
+ " column_default_value, column_op_type, deleted_at, audit_info)" | ||
+ " VALUES " | ||
+ "<foreach collection='columnPOs' item='item' separator=','>" | ||
+ "(#{item.columnId}, #{item.columnName}, #{item.metalakeId}, #{item.catalogId}," | ||
+ " #{item.schemaId}, #{item.tableId}, #{item.tableVersion}, #{item.columnType}, " | ||
+ " #{item.columnComment}, #{item.nullable}, #{item.autoIncrement}, #{item.defaultValue}," | ||
+ " #{item.columnOpType}, #{item.deletedAt}, #{item.auditInfo})" | ||
+ "</foreach>" | ||
+ "</script>"; | ||
} | ||
|
||
public String softDeleteColumnsByTableId(@Param("tableId") Long tableId) { | ||
return "UPDATE " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" | ||
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" | ||
+ " WHERE table_id = #{tableId} AND deleted_at = 0"; | ||
} | ||
|
||
public String softDeleteColumnsByMetalakeId(@Param("metalakeId") Long metalakeId) { | ||
return "UPDATE " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" | ||
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" | ||
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0"; | ||
} | ||
|
||
public String softDeleteColumnsByCatalogId(@Param("catalogId") Long catalogId) { | ||
return "UPDATE " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" | ||
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" | ||
+ " WHERE catalog_id = #{catalogId} AND deleted_at = 0"; | ||
} | ||
|
||
public String softDeleteColumnsBySchemaId(@Param("schemaId") Long schemaId) { | ||
return "UPDATE " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" | ||
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" | ||
+ " WHERE schema_id = #{schemaId} AND deleted_at = 0"; | ||
} | ||
|
||
public String deleteColumnPOsByLegacyTimeline( | ||
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { | ||
return "DELETE FROM " | ||
+ TableColumnMapper.COLUMN_TABLE_NAME | ||
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; | ||
} | ||
} |
Oops, something went wrong.