[Fix](Nereids) Fix duplicated name in view does not throw exception (#20374)

When using nereids, if we have duplicated name in output of view, we need to throw an exception. A check rule was added in bindExpression rule set
This commit is contained in:
LiBinfeng
2023-06-05 16:10:54 +08:00
committed by GitHub
parent 20bf309ffb
commit f0b0bda04a
3 changed files with 89 additions and 6 deletions

View File

@ -67,6 +67,7 @@ import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalRepeat;
import org.apache.doris.nereids.trees.plans.logical.LogicalSetOperation;
import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
import org.apache.doris.nereids.trees.plans.logical.LogicalTVFRelation;
import org.apache.doris.nereids.trees.plans.logical.UsingJoin;
import org.apache.doris.nereids.util.TypeCoercionUtils;
@ -538,6 +539,13 @@ public class BindExpression implements AnalysisRuleFactory {
UnboundTVFRelation relation = ctx.root;
return bindTableValuedFunction(relation, ctx.statementContext);
})
),
RuleType.BINDING_SUBQUERY_ALIAS_SLOT.build(
logicalSubQueryAlias().thenApply(ctx -> {
LogicalSubQueryAlias<Plan> subQueryAlias = ctx.root;
checkSameNameSlot(subQueryAlias.child(0).getOutput(), subQueryAlias.getAlias());
return subQueryAlias;
})
)
).stream().map(ruleCondition).collect(ImmutableList.toImmutableList());
}
@ -677,6 +685,18 @@ public class BindExpression implements AnalysisRuleFactory {
return new LogicalTVFRelation(unboundTVFRelation.getId(), (TableValuedFunction) function);
}
private void checkSameNameSlot(List<Slot> childOutputs, String subQueryAlias) {
Set<String> nameSlots = new HashSet<>();
for (Slot s : childOutputs) {
if (nameSlots.contains(s.getName())) {
throw new AnalysisException("Duplicated inline view column alias: '" + s.getName()
+ "'" + " in inline view: '" + subQueryAlias + "'");
} else {
nameSlots.add(s.getName());
}
}
}
private BoundFunction bindTableGeneratingFunction(UnboundFunction unboundFunction,
CascadesContext cascadesContext) {
List<Expression> boundArguments = unboundFunction.getArguments().stream()

View File

@ -104,12 +104,6 @@ public class ExplainInsertCommandTest extends TestWithFeService {
}
@Test
public void testInsertIntoDuplicateKeyTableWithCast() throws Exception {
String sql = "explain insert into t1 select * from (select cast(k1 as varchar), 1, 1, 1 from src) t";
Assertions.assertEquals(4, getOutputFragment(sql).getOutputExprs().size());
}
@Test
public void testInsertIntoSomeColumns() throws Exception {
String sql = "explain insert into t1 (v1, v2) select v1 + 1, v2 + 4 from src";

View File

@ -0,0 +1,69 @@
// 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.
suite("inlineview_with_project") {
sql "SET enable_nereids_planner=true"
sql "SET enable_fallback_to_original_planner=false"
sql """
drop table if exists issue_19611_t0;
"""
sql """
drop table if exists issue_19611_t1;
"""
sql """
create table issue_19611_t0 (c0 int)
ENGINE=OLAP
DISTRIBUTED BY HASH(c0) BUCKETS 5
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
);
"""
sql """
create table issue_19611_t1 (c0 int)
ENGINE=OLAP
DISTRIBUTED BY HASH(c0) BUCKETS 5
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2"
);
"""
test {
sql """
select * from (
select * from issue_19611_t0, issue_19611_t1 where issue_19611_t1.c0 != 0
union select * from issue_19611_t0, issue_19611_t1 where issue_19611_t1.c0 = 0) tmp;
"""
exception "errCode = 2, detailMessage = Unexpected exception: Duplicated inline view column alias: 'c0' in inline view: 'tmp'"
}
sql """
drop table if exists issue_19611_t0;
"""
sql """
drop table if exists issue_19611_t1;
"""
}