AshinGau commented on code in PR #19909: URL: https://github.com/apache/doris/pull/19909#discussion_r1200253792
########## fe/fe-core/src/main/java/org/apache/doris/catalog/external/HudiExternalTable.java: ########## @@ -0,0 +1,125 @@ +// 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.doris.catalog.external; + +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.Type; +import org.apache.doris.datasource.hudi.HudiHMSExternalCatalog; +import org.apache.doris.thrift.THudiTable; +import org.apache.doris.thrift.TTableDescriptor; +import org.apache.doris.thrift.TTableType; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.avro.Schema; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hudi.common.table.HoodieTableConfig; +import org.apache.hudi.common.util.Option; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class HudiExternalTable extends ExternalTable { + + public HudiExternalTable(long id, String name, String dbName, HudiHMSExternalCatalog catalog) { + super(id, name, catalog, dbName, TableType.HUDI_EXTERNAL_TABLE); + } + + public String getHudiCatalogType() { + return ((HudiHMSExternalCatalog) catalog).getHudiCatalogType(); + } + + protected synchronized void makeSureInitialized() { + super.makeSureInitialized(); + if (!objectCreated) { + objectCreated = true; + } + } + + @Override + public TTableDescriptor toThrift() { + List<Column> schema = getFullSchema(); + String hudiCatalogType = getHudiCatalogType(); + if (hudiCatalogType.equals("hms")) { + THudiTable hudiTable = new THudiTable(); + hudiTable.setDbName(dbName); + hudiTable.setTableName(dbName); + + TTableDescriptor tTableDescriptor = new TTableDescriptor(getId(), TTableType.HIVE_TABLE, schema.size(), 0, + getName(), dbName); + tTableDescriptor.setHudiTable(hudiTable); + return tTableDescriptor; + } + throw new UnsupportedOperationException("hudi not supported the catalog type :" + hudiCatalogType); + } + + public static String fromAvroHudiTypeToHiveTypeString(Schema avroSchema) { + Schema.Type columnType = avroSchema.getType(); + switch (columnType) { Review Comment: How about `decimal` and `timestamp` ... types? ########## fe/fe-core/src/main/java/org/apache/doris/catalog/external/HudiExternalDatabase.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.doris.catalog.external; + +import org.apache.doris.catalog.TableIf; +import org.apache.doris.datasource.ExternalCatalog; +import org.apache.doris.datasource.InitDatabaseLog; +import org.apache.doris.datasource.hudi.HudiHMSExternalCatalog; +import org.apache.doris.persist.gson.GsonPostProcessable; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class HudiExternalDatabase extends ExternalDatabase<HudiExternalTable> implements GsonPostProcessable { Review Comment: We should register the subtype of `DatabaseIf` in `org.apache.doris.persist.gson.GsonUtils` ########## fe/fe-core/src/main/java/org/apache/doris/planner/external/hudi/HudiScanNode.java: ########## @@ -0,0 +1,231 @@ +// 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.doris.planner.external.hudi; + +import org.apache.doris.analysis.TupleDescriptor; +import org.apache.doris.catalog.HiveMetaStoreClientHelper; +import org.apache.doris.catalog.external.ExternalTable; +import org.apache.doris.catalog.external.HMSExternalTable; +import org.apache.doris.catalog.external.HudiExternalTable; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.UserException; +import org.apache.doris.planner.PlanNodeId; +import org.apache.doris.planner.external.HiveScanNode; +import org.apache.doris.planner.external.TableFormatType; +import org.apache.doris.spi.Split; +import org.apache.doris.statistics.StatisticalType; +import org.apache.doris.thrift.TFileFormatType; +import org.apache.doris.thrift.TFileRangeDesc; +import org.apache.doris.thrift.THudiFileDesc; +import org.apache.doris.thrift.TTableFormatFileDesc; + +import avro.shaded.com.google.common.base.Preconditions; +import org.apache.avro.Schema; +import org.apache.commons.collections.CollectionUtils; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hudi.avro.HoodieAvroUtils; +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.BaseFile; +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieBaseFile; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.TableSchemaResolver; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.view.HoodieTableFileSystemView; +import org.apache.hudi.common.util.Option; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.stream.Collectors; + +public class HudiScanNode extends HiveScanNode { + + private static final Logger LOG = LogManager.getLogger(HudiScanNode.class); + + private HudiSource source; + + // table location -> HoodieTableMetaClient + + /** + * External file scan node for Query Hudi table + * needCheckColumnPriv: Some of ExternalFileScanNode do not need to check column priv + * eg: s3 tvf + * These scan nodes do not have corresponding catalog/database/table info, so no need to do priv check + */ + public HudiScanNode(PlanNodeId id, TupleDescriptor desc, boolean needCheckColumnPriv) { + super(id, desc, "HUDI_SCAN_NODE", StatisticalType.HUDI_SCAN_NODE, needCheckColumnPriv); + } + + @Override + public TFileFormatType getFileFormatType() { + return TFileFormatType.FORMAT_PARQUET; + } + + @Override + public List<String> getPathPartitionKeys() { + return Collections.emptyList(); + } + + + @Override + protected void doInitialize() throws UserException { + ExternalTable table = (ExternalTable) desc.getTable(); + if (table.isView()) { + throw new AnalysisException( + String.format("Querying external view '%s.%s' is not supported", table.getDbName(), + table.getName())); + } + computeColumnFilter(); + initBackendPolicy(); + if (table instanceof HMSExternalTable) { + source = new HudiHMSSource((HMSExternalTable) table, desc, columnNameToRange); + } + Preconditions.checkNotNull(source); + initSchemaParams(); + } + + + public static void setHudiParams(TFileRangeDesc rangeDesc, HudiSplit hudiSplit) { + TTableFormatFileDesc tableFormatFileDesc = new TTableFormatFileDesc(); + tableFormatFileDesc.setTableFormatType(hudiSplit.getTableFormatType().value()); + THudiFileDesc fileDesc = new THudiFileDesc(); + fileDesc.setBasePath(hudiSplit.getBasePath()); + fileDesc.setDataFilePath(hudiSplit.getDataFilePath()); + fileDesc.setDeltaFilePaths(hudiSplit.getHudiDeltaLogs()); + tableFormatFileDesc.setHudiParams(fileDesc); + rangeDesc.setTableFormatParams(tableFormatFileDesc); + } + + @Override + public List<Split> getSplits() throws UserException { + // TODO expressions + // List<Expression> expressions = new ArrayList<>(); + + // for (Expr conjunct : conjuncts) { + // Expression expression = null; + // if (expression != null) { + // expressions.add(expression); + // } + // } + + HMSExternalTable targetTable = (HMSExternalTable) source.getTargetTable(); + HoodieTableMetaClient hudiClient = HiveMetaStoreClientHelper.getHudiClient(targetTable); + + hudiClient.reloadActiveTimeline(); + + String partitionName = getHudiPartitionName(targetTable, hudiClient); + + List<Split> splits = new ArrayList<>(); + + String basePath = targetTable.getRemoteTable().getSd().getLocation(); + String inputFormat = targetTable.getRemoteTable().getSd().getInputFormat(); + String serdeLib = targetTable.getRemoteTable().getSd().getSerdeInfo().getSerializationLib(); + + TableSchemaResolver schemaUtil = new TableSchemaResolver(hudiClient); + Schema hudiSchema; + try { + hudiSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaUtil.getTableAvroSchema()); + } catch (Exception e) { + throw new RuntimeException("Cannot get hudi table schema."); + } + + List<String> columnNames = new ArrayList<>(); + List<String> columnTypes = new ArrayList<>(); + List<FieldSchema> allFields = targetTable.getRemoteTable().getSd().getCols(); + allFields.addAll(targetTable.getRemoteTable().getPartitionKeys()); + + for (Schema.Field hudiField : hudiSchema.getFields()) { + + String columnName = hudiField.name().toLowerCase(Locale.ROOT); + Optional<FieldSchema> field = allFields.stream() + .filter(f -> f.getName().equals(columnName)).findFirst(); + if (!field.isPresent()) { + String errorMsg = String.format("Hudi column %s not exists in hive metastore.", hudiField.name()); Review Comment: How is this situation caused? Can the [synchronization tools](https://hudi.apache.org/docs/syncing_metastore#hive-sync-tool) maintain strong consistency in the table schema of the hive schema? ########## fe/fe-core/src/main/java/org/apache/doris/datasource/InternalCatalog.java: ########## @@ -2431,14 +2432,17 @@ private void createHudiTable(Database db, CreateTableStmt stmt) throws DdlExcept HiveConf hiveConf = new HiveConf(); hiveConf.set(HMSProperties.HIVE_METASTORE_URIS, hudiTable.getTableProperties().get(HudiProperty.HUDI_HIVE_METASTORE_URIS)); + Review Comment: Many unexpected changes. ########## fe/fe-core/src/main/java/org/apache/doris/planner/external/hudi/HudiScanNode.java: ########## @@ -0,0 +1,231 @@ +// 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.doris.planner.external.hudi; + +import org.apache.doris.analysis.TupleDescriptor; +import org.apache.doris.catalog.HiveMetaStoreClientHelper; +import org.apache.doris.catalog.external.ExternalTable; +import org.apache.doris.catalog.external.HMSExternalTable; +import org.apache.doris.catalog.external.HudiExternalTable; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.UserException; +import org.apache.doris.planner.PlanNodeId; +import org.apache.doris.planner.external.HiveScanNode; +import org.apache.doris.planner.external.TableFormatType; +import org.apache.doris.spi.Split; +import org.apache.doris.statistics.StatisticalType; +import org.apache.doris.thrift.TFileFormatType; +import org.apache.doris.thrift.TFileRangeDesc; +import org.apache.doris.thrift.THudiFileDesc; +import org.apache.doris.thrift.TTableFormatFileDesc; + +import avro.shaded.com.google.common.base.Preconditions; +import org.apache.avro.Schema; +import org.apache.commons.collections.CollectionUtils; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hudi.avro.HoodieAvroUtils; +import org.apache.hudi.common.fs.FSUtils; +import org.apache.hudi.common.model.BaseFile; +import org.apache.hudi.common.model.FileSlice; +import org.apache.hudi.common.model.HoodieBaseFile; +import org.apache.hudi.common.model.HoodieLogFile; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.TableSchemaResolver; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.view.HoodieTableFileSystemView; +import org.apache.hudi.common.util.Option; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Optional; +import java.util.stream.Collectors; + +public class HudiScanNode extends HiveScanNode { + + private static final Logger LOG = LogManager.getLogger(HudiScanNode.class); + + private HudiSource source; + + // table location -> HoodieTableMetaClient + + /** + * External file scan node for Query Hudi table + * needCheckColumnPriv: Some of ExternalFileScanNode do not need to check column priv + * eg: s3 tvf + * These scan nodes do not have corresponding catalog/database/table info, so no need to do priv check + */ + public HudiScanNode(PlanNodeId id, TupleDescriptor desc, boolean needCheckColumnPriv) { + super(id, desc, "HUDI_SCAN_NODE", StatisticalType.HUDI_SCAN_NODE, needCheckColumnPriv); + } + + @Override + public TFileFormatType getFileFormatType() { + return TFileFormatType.FORMAT_PARQUET; + } + + @Override + public List<String> getPathPartitionKeys() { + return Collections.emptyList(); + } + + + @Override + protected void doInitialize() throws UserException { + ExternalTable table = (ExternalTable) desc.getTable(); + if (table.isView()) { + throw new AnalysisException( + String.format("Querying external view '%s.%s' is not supported", table.getDbName(), + table.getName())); + } + computeColumnFilter(); + initBackendPolicy(); + if (table instanceof HMSExternalTable) { + source = new HudiHMSSource((HMSExternalTable) table, desc, columnNameToRange); + } + Preconditions.checkNotNull(source); + initSchemaParams(); + } + + + public static void setHudiParams(TFileRangeDesc rangeDesc, HudiSplit hudiSplit) { + TTableFormatFileDesc tableFormatFileDesc = new TTableFormatFileDesc(); + tableFormatFileDesc.setTableFormatType(hudiSplit.getTableFormatType().value()); + THudiFileDesc fileDesc = new THudiFileDesc(); + fileDesc.setBasePath(hudiSplit.getBasePath()); + fileDesc.setDataFilePath(hudiSplit.getDataFilePath()); + fileDesc.setDeltaFilePaths(hudiSplit.getHudiDeltaLogs()); + tableFormatFileDesc.setHudiParams(fileDesc); + rangeDesc.setTableFormatParams(tableFormatFileDesc); + } + + @Override + public List<Split> getSplits() throws UserException { + // TODO expressions + // List<Expression> expressions = new ArrayList<>(); + + // for (Expr conjunct : conjuncts) { + // Expression expression = null; + // if (expression != null) { + // expressions.add(expression); + // } + // } + + HMSExternalTable targetTable = (HMSExternalTable) source.getTargetTable(); + HoodieTableMetaClient hudiClient = HiveMetaStoreClientHelper.getHudiClient(targetTable); + + hudiClient.reloadActiveTimeline(); + + String partitionName = getHudiPartitionName(targetTable, hudiClient); + + List<Split> splits = new ArrayList<>(); + + String basePath = targetTable.getRemoteTable().getSd().getLocation(); + String inputFormat = targetTable.getRemoteTable().getSd().getInputFormat(); + String serdeLib = targetTable.getRemoteTable().getSd().getSerdeInfo().getSerializationLib(); + + TableSchemaResolver schemaUtil = new TableSchemaResolver(hudiClient); + Schema hudiSchema; + try { + hudiSchema = HoodieAvroUtils.createHoodieWriteSchema(schemaUtil.getTableAvroSchema()); + } catch (Exception e) { + throw new RuntimeException("Cannot get hudi table schema."); + } + + List<String> columnNames = new ArrayList<>(); + List<String> columnTypes = new ArrayList<>(); + List<FieldSchema> allFields = targetTable.getRemoteTable().getSd().getCols(); + allFields.addAll(targetTable.getRemoteTable().getPartitionKeys()); + + for (Schema.Field hudiField : hudiSchema.getFields()) { + + String columnName = hudiField.name().toLowerCase(Locale.ROOT); + Optional<FieldSchema> field = allFields.stream() + .filter(f -> f.getName().equals(columnName)).findFirst(); + if (!field.isPresent()) { + String errorMsg = String.format("Hudi column %s not exists in hive metastore.", hudiField.name()); + throw new IllegalArgumentException(errorMsg); + } + columnNames.add(columnName); + String columnType = HudiExternalTable.fromAvroHudiTypeToHiveTypeString(hudiField.schema()); + columnTypes.add(columnType); + } + + try { + String globPath = String.format("%s/%s/*", hudiClient.getBasePath(), partitionName); + List<FileStatus> statuses = FSUtils.getGlobStatusExcludingMetaFolder(hudiClient.getRawFs(), + new Path(globPath)); + + HoodieTimeline timeline = hudiClient.getCommitsAndCompactionTimeline().filterCompletedInstants(); + Option<HoodieInstant> latestInstant = timeline.lastInstant(); Review Comment: So currently only support MOR from the last snapshots. Can we reserve interfaces to support `Time Travel Query` and `Incremental query`. -- 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...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org