Skip to content

Commit

Permalink
Adaptive external table method
Browse files Browse the repository at this point in the history
  • Loading branch information
seawinde committed Nov 25, 2024
1 parent 0ed4d6f commit 34836de
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,18 +461,14 @@ protected Pair<Map<BaseTableInfo, Set<String>>, Map<BaseTableInfo, Set<String>>>
return Pair.of(ImmutableMap.of(), ImmutableMap.of());
}
// Collect the mv related base table partitions which query used
Map<BaseTableInfo, Set<Partition>> queryUsedBaseTablePartitions = new LinkedHashMap<>();
Map<BaseTableInfo, Set<String>> queryUsedBaseTablePartitions = new LinkedHashMap<>();
queryUsedBaseTablePartitions.put(relatedPartitionTable, new HashSet<>());
queryPlan.accept(new StructInfo.QueryScanPartitionsCollector(), queryUsedBaseTablePartitions);
// Bail out, not check invalid partition if not olap scan, support later
if (queryUsedBaseTablePartitions.isEmpty()) {
return Pair.of(ImmutableMap.of(), ImmutableMap.of());
}
Set<String> queryUsedBaseTablePartitionNameSet = queryUsedBaseTablePartitions.get(relatedPartitionTable)
.stream()
.map(Partition::getName)
.collect(Collectors.toSet());

Set<String> queryUsedBaseTablePartitionNameSet = queryUsedBaseTablePartitions.get(relatedPartitionTable);
Collection<Partition> mvValidPartitions = MTMVRewriteUtil.getMTMVCanRewritePartitions(mtmv,
cascadesContext.getConnectContext(), System.currentTimeMillis(), false);
Set<String> mvValidPartitionNameSet = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
package org.apache.doris.nereids.rules.exploration.mv;

import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.Partition;
import org.apache.doris.catalog.PartitionItem;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.common.Pair;
import org.apache.doris.datasource.ExternalTable;
import org.apache.doris.datasource.hive.HMSExternalCatalog;
import org.apache.doris.datasource.hive.HMSExternalTable;
import org.apache.doris.datasource.hive.HiveMetaStoreCache;
Expand Down Expand Up @@ -76,7 +74,6 @@

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -735,21 +732,21 @@ public Plan visitLogicalOlapScan(LogicalOlapScan olapScan,
* Collect partitions on base table
*/
public static class QueryScanPartitionsCollector extends DefaultPlanVisitor<Plan,
Map<BaseTableInfo, Set<Partition>>> {
Map<BaseTableInfo, Set<String>>> {
@Override
public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation,
Map<BaseTableInfo, Set<Partition>> targetTablePartitionMap) {
Map<BaseTableInfo, Set<String>> targetTablePartitionMap) {
TableIf table = catalogRelation.getTable();
BaseTableInfo relatedPartitionTable = new BaseTableInfo(table);
if (!targetTablePartitionMap.containsKey(relatedPartitionTable)) {
return catalogRelation;
}
Set<String> tablePartitions = targetTablePartitionMap.get(relatedPartitionTable);
if (catalogRelation instanceof LogicalOlapScan) {
// Handle olap table
LogicalOlapScan logicalOlapScan = (LogicalOlapScan) catalogRelation;
Set<Partition> tablePartitions = targetTablePartitionMap.get(relatedPartitionTable);
for (Long partitionId : logicalOlapScan.getSelectedPartitionIds()) {
tablePartitions.add(logicalOlapScan.getTable().getPartition(partitionId));
tablePartitions.add(logicalOlapScan.getTable().getPartition(partitionId).getName());
}
} else if (catalogRelation instanceof LogicalFileScan
&& catalogRelation.getTable() instanceof HMSExternalTable) {
Expand All @@ -762,9 +759,8 @@ public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation,
HiveMetaStoreCache.HivePartitionValues hivePartitionValues = cache.getPartitionValues(
externalTable.getDbName(), externalTable.getName(), externalTable.getPartitionColumnTypes());
BiMap<Long, String> idToName = hivePartitionValues.getPartitionNameToIdMap().inverse();
Set<String> hiveTablePartitions = new HashSet<>();
for (Entry<Long, PartitionItem> partitionEntry : selectedPartitions.selectedPartitions.entrySet()) {
hiveTablePartitions.add(idToName.get(partitionEntry.getKey()));
tablePartitions.add(idToName.get(partitionEntry.getKey()));
}
} else {
// Not support to partition check now, doesn't try to compensate when part partition become invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.UserException;
import org.apache.doris.datasource.hive.HMSExternalTable;
import org.apache.doris.mtmv.BaseTableInfo;
import org.apache.doris.mtmv.MTMVRelatedTableIf;
import org.apache.doris.nereids.analyzer.UnboundRelation;
Expand Down Expand Up @@ -301,16 +302,17 @@ public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation,
MTMVRelatedTableIf targetTable = (MTMVRelatedTableIf) table;
for (String partitionName : filterTableEntry.getValue()) {
Partition partition = targetTable.getPartition(partitionName);
if (!(targetTable instanceof OlapTable)) {
// check partition is have data or not, only support olap table
break;
}
if (!((OlapTable) targetTable).selectNonEmptyPartitionIds(
if (targetTable instanceof OlapTable && !((OlapTable) targetTable).selectNonEmptyPartitionIds(
Lists.newArrayList(partition.getId())).isEmpty()) {
// Add filter only when partition has data
// Add filter only when partition has data when olap table
partitionHasDataItems.add(
((OlapTable) targetTable).getPartitionInfo().getItem(partition.getId()));
}
if (targetTable instanceof HMSExternalTable) {
// Add filter only when partition has data when hms external table
partitionHasDataItems.add(
((HMSExternalTable) targetTable).getAndCopyPartitionItems().get(partitionName));
}
}
if (partitionHasDataItems.isEmpty()) {
predicates.setNeedAddFilter(false);
Expand Down

0 comments on commit 34836de

Please sign in to comment.