morningman commented on code in PR #14160: URL: https://github.com/apache/doris/pull/14160#discussion_r1023669405
########## fe/fe-core/src/main/java/org/apache/doris/statistics/IcebergAnalysisJob.java: ########## @@ -0,0 +1,122 @@ +// 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.statistics; + +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; +import org.apache.doris.statistics.util.StatisticsUtil; + +import org.apache.commons.text.StringSubstitutor; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.HdfsConfiguration; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileScanTask; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableScan; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.types.Types; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class IcebergAnalysisJob extends HMSAnalysisJob { + + private long numRows = 0; + private long dataSize = 0; + private long numNulls = 0; + + public IcebergAnalysisJob(AnalysisJobScheduler analysisJobScheduler, AnalysisJobInfo info) { + super(analysisJobScheduler, info); + } + + private static final String INSERT_TABLE_SQL_TEMPLATE = "INSERT INTO " + + "${internalDB}.${columnStatTbl}" + + " values ('${id}','${catalogId}', '${dbId}', '${tblId}', '${colId}', NULL, " + + "${numRows}, 0, ${nulls}, '0', '0', ${dataSize}, '${update_time}')"; + + + @Override + protected void getColumnStatsByMeta() throws Exception { + Table icebergTable = getIcebergTable(); + TableScan tableScan = icebergTable.newScan().includeColumnStats(); + for (FileScanTask task : tableScan.planFiles()) { + processDataFile(task.file(), task.spec()); + } + updateStats(); + } + + private Table getIcebergTable() { + org.apache.iceberg.hive.HiveCatalog hiveCatalog = new org.apache.iceberg.hive.HiveCatalog(); + Configuration conf = new HdfsConfiguration(); + for (Map.Entry<String, String> entry : table.getCatalog().getCatalogProperty().getProperties().entrySet()) { + conf.set(entry.getKey(), entry.getValue()); + } + Map<String, String> s3Properties = table.getS3Properties(); + for (Map.Entry<String, String> entry : s3Properties.entrySet()) { + conf.set(entry.getKey(), entry.getValue()); + } + hiveCatalog.setConf(conf); + Map<String, String> catalogProperties = new HashMap<>(); + catalogProperties.put("hive.metastore.uris", table.getMetastoreUri()); + catalogProperties.put("uri", table.getMetastoreUri()); + hiveCatalog.initialize("hive", catalogProperties); + return hiveCatalog.loadTable(TableIdentifier.of(table.getDbName(), table.getName())); + } + + private void processDataFile(DataFile dataFile, PartitionSpec partitionSpec) { + int colId = -1; + for (Types.NestedField column : partitionSpec.schema().columns()) { + if (column.name().equals(col.getName())) { + colId = column.fieldId(); + break; + } + } + if (colId == -1) { + throw new RuntimeException(String.format("Column %s not exist.", col.getName())); + } + dataSize += dataFile.columnSizes().get(colId); + numRows += dataFile.recordCount(); + numNulls += dataFile.nullValueCounts().get(colId); + } + + private void updateStats() throws Exception { Review Comment: This should be moved to a unified place. -- 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