diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/MetaTableName.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/MetaTableName.java new file mode 100644 index 000000000000000..47523f95be829e5 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/MetaTableName.java @@ -0,0 +1,33 @@ +// 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.analysis; + +public class MetaTableName { + + public static final String PARTITIONS = "partitions"; + + private final String tableName; + + public MetaTableName(String tableName) { + this.tableName = tableName; + } + + public String getTableName() { + return tableName; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SysTableName.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SysTableName.java deleted file mode 100644 index a5e82618e64fdeb..000000000000000 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SysTableName.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * // 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.analysis; - -public class SysTableName { -} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java index 1dace1f471490fe..595ad1f1c0377ba 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundRelation.java @@ -17,7 +17,7 @@ package org.apache.doris.nereids.analyzer; -import org.apache.doris.analysis.SysTableName; +import org.apache.doris.analysis.MetaTableName; import org.apache.doris.analysis.TableScanParams; import org.apache.doris.analysis.TableSnapshot; import org.apache.doris.common.Pair; @@ -63,7 +63,7 @@ public class UnboundRelation extends LogicalRelation implements Unbound, BlockFu private final Optional tableSnapshot; // eg: tableName$partitions - private final Optional sysTableName; + private final Optional metaTableName; public UnboundRelation(RelationId id, List nameParts) { this(id, nameParts, Optional.empty(), Optional.empty(), ImmutableList.of(), false, ImmutableList.of(), @@ -109,6 +109,15 @@ public UnboundRelation(RelationId id, List nameParts, List partN tableSnapshot, Optional.empty()); } + public UnboundRelation(RelationId id, List nameParts, List partNames, boolean isTempPart, + List tabletIds, List hints, Optional tableSample, Optional indexName, + TableScanParams scanParams, Optional> indexInSqlString, + Optional tableSnapshot, Optional metaTableName) { + this(id, nameParts, Optional.empty(), Optional.empty(), + partNames, isTempPart, tabletIds, hints, tableSample, indexName, scanParams, indexInSqlString, + tableSnapshot, metaTableName); + } + /** * constructor of UnboundRelation */ @@ -116,7 +125,7 @@ public UnboundRelation(RelationId id, List nameParts, Optional logicalProperties, List partNames, boolean isTempPart, List tabletIds, List hints, Optional tableSample, Optional indexName, TableScanParams scanParams, Optional> indexInSqlString, - Optional tableSnapshot, Optional sysTableName) { + Optional tableSnapshot, Optional metaTableName) { super(id, PlanType.LOGICAL_UNBOUND_RELATION, groupExpression, logicalProperties); this.nameParts = ImmutableList.copyOf(Objects.requireNonNull(nameParts, "nameParts should not null")); this.partNames = ImmutableList.copyOf(Objects.requireNonNull(partNames, "partNames should not null")); @@ -128,7 +137,7 @@ public UnboundRelation(RelationId id, List nameParts, Optional getNameParts() { @@ -150,7 +159,7 @@ public Plan withGroupExpression(Optional groupExpression) { return new UnboundRelation(relationId, nameParts, groupExpression, Optional.of(getLogicalProperties()), partNames, isTempPart, tabletIds, hints, tableSample, indexName, null, indexInSqlString, tableSnapshot, - sysTableName); + metaTableName); } @Override @@ -158,13 +167,13 @@ public Plan withGroupExprLogicalPropChildren(Optional groupExpr Optional logicalProperties, List children) { return new UnboundRelation(relationId, nameParts, groupExpression, logicalProperties, partNames, isTempPart, tabletIds, hints, tableSample, indexName, null, indexInSqlString, tableSnapshot, - sysTableName); + metaTableName); } public UnboundRelation withIndexInSql(Pair index) { return new UnboundRelation(relationId, nameParts, groupExpression, Optional.of(getLogicalProperties()), partNames, isTempPart, tabletIds, hints, tableSample, indexName, null, - Optional.of(index), tableSnapshot, sysTableName); + Optional.of(index), tableSnapshot, metaTableName); } @Override @@ -236,7 +245,7 @@ public Optional getTableSnapshot() { return tableSnapshot; } - public Optional getSysTableName() { - return sysTableName; + public Optional getMetaTableName() { + return metaTableName; } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 36ee590c0e946a0..acc1a9d098b406d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -20,6 +20,7 @@ import org.apache.doris.analysis.ArithmeticExpr.Operator; import org.apache.doris.analysis.BrokerDesc; import org.apache.doris.analysis.ColumnNullableType; +import org.apache.doris.analysis.MetaTableName; import org.apache.doris.analysis.StorageBackend; import org.apache.doris.analysis.TableName; import org.apache.doris.analysis.TableScanParams; @@ -1407,7 +1408,7 @@ protected LogicalPlan withTableAlias(LogicalPlan plan, TableAliasContext ctx) { @Override public LogicalPlan visitTableName(TableNameContext ctx) { - List tableId = visitMultipartIdentifier(ctx.multipartIdentifier()); + List nameParts = visitMultipartIdentifier(ctx.multipartIdentifier()); List partitionNames = new ArrayList<>(); boolean isTempPart = false; if (ctx.specifiedPartition() != null) { @@ -1453,10 +1454,20 @@ public LogicalPlan visitTableName(TableNameContext ctx) { } } + MetaTableName metaTableName = null; + if (ctx.metaTableParams() != null) { + metaTableName = new MetaTableName(ctx.metaTableParams().identifier().getText()); + } + TableSample tableSample = ctx.sample() == null ? null : (TableSample) visit(ctx.sample()); UnboundRelation relation = new UnboundRelation(StatementScopeIdGenerator.newRelationId(), - tableId, partitionNames, isTempPart, tabletIdLists, relationHints, - Optional.ofNullable(tableSample), indexName, scanParams, Optional.ofNullable(tableSnapshot)); + nameParts, Optional.empty(), Optional.empty(), partitionNames, isTempPart, + tabletIdLists, relationHints, + Optional.ofNullable(tableSample), + indexName, scanParams, Optional.empty(), + Optional.ofNullable(tableSnapshot), + Optional.ofNullable(metaTableName)); + LogicalPlan checkedRelation = LogicalPlanBuilderAssistant.withCheckPolicy(relation); LogicalPlan plan = withTableAlias(checkedRelation, ctx.tableAlias()); for (LateralViewContext lateralViewContext : ctx.lateralView()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java index 5b68ba9f4496a1c..a3a8be8ac1f8c4e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindRelation.java @@ -17,6 +17,7 @@ package org.apache.doris.nereids.rules.analysis; +import org.apache.doris.analysis.MetaTableName; import org.apache.doris.catalog.AggStateType; import org.apache.doris.catalog.AggregateType; import org.apache.doris.catalog.Column; @@ -65,6 +66,8 @@ import org.apache.doris.nereids.trees.expressions.functions.agg.Min; import org.apache.doris.nereids.trees.expressions.functions.agg.QuantileUnion; import org.apache.doris.nereids.trees.expressions.functions.agg.Sum; +import org.apache.doris.nereids.trees.expressions.functions.table.PartitionValues; +import org.apache.doris.nereids.trees.expressions.functions.table.TableValuedFunction; import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.PreAggStatus; @@ -377,8 +380,15 @@ private LogicalPlan getLogicalPlan(TableIf table, UnboundRelation unboundRelatio }); List qualifierWithoutTableName = Lists.newArrayList(); qualifierWithoutTableName.addAll(qualifiedTableName.subList(0, qualifiedTableName.size() - 1)); - if (unboundRelation.getSysTableName().isPresent()) { - return new LogicalTVFRelation(); + if (unboundRelation.getMetaTableName().isPresent()) { + String metaTableName = unboundRelation.getMetaTableName().get().getTableName(); + TableValuedFunction tvf; + if (metaTableName.equalsIgnoreCase(MetaTableName.PARTITIONS)) { + tvf = PartitionValues.create(qualifiedTableName); + } else { + throw new AnalysisException("Unsupported meta table: " + metaTableName); + } + return new LogicalTVFRelation(unboundRelation.getRelationId(), tvf); } boolean isView = false; try { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/PartitionValues.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/PartitionValues.java index d206c764c0675ca..0389bb626371aa9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/PartitionValues.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/table/PartitionValues.java @@ -25,6 +25,10 @@ import org.apache.doris.tablefunction.PartitionValuesTableValuedFunction; import org.apache.doris.tablefunction.TableValuedFunctionIf; +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; + +import java.util.List; import java.util.Map; /** @@ -35,6 +39,20 @@ public PartitionValues(Properties properties) { super("partition_values", properties); } + /** + * Create PartitionValues function. + * @param qualifiedTableName ctl.db.tbl + * @return PartitionValues function + */ + public static TableValuedFunction create(List qualifiedTableName) { + Preconditions.checkArgument(qualifiedTableName != null && qualifiedTableName.size() == 3); + Map parameters = Maps.newHashMap(); + parameters.put(PartitionValuesTableValuedFunction.CATALOG, qualifiedTableName.get(0)); + parameters.put(PartitionValuesTableValuedFunction.DB, qualifiedTableName.get(1)); + parameters.put(PartitionValuesTableValuedFunction.TABLE, qualifiedTableName.get(2)); + return new PartitionValues(new Properties(parameters)); + } + @Override public FunctionSignature customSignature() { return FunctionSignature.of(AnyDataType.INSTANCE_WITHOUT_INDEX, getArgumentsTypes()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/PartitionValuesTableValuedFunction.java b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/PartitionValuesTableValuedFunction.java index b8712e668accd0e..ad3c53585d4c559 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/tablefunction/PartitionValuesTableValuedFunction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/tablefunction/PartitionValuesTableValuedFunction.java @@ -56,9 +56,9 @@ public class PartitionValuesTableValuedFunction extends MetadataTableValuedFunct public static final String NAME = "partition_values"; - private static final String CATALOG = "catalog"; - private static final String DB = "database"; - private static final String TABLE = "table"; + public static final String CATALOG = "catalog"; + public static final String DB = "database"; + public static final String TABLE = "table"; private static final ImmutableSet PROPERTIES_SET = ImmutableSet.of(CATALOG, DB, TABLE);