From 05d47d43bdc02ba2dc6ae32379d0e95911665f8a Mon Sep 17 00:00:00 2001 From: Chengpeng Yan <41809508+Reminiscent@users.noreply.github.com> Date: Wed, 17 May 2023 19:48:30 +0800 Subject: [PATCH] [Fix](Nereids) check the tableName in catalog (#19695) # Proposed changes In the nereids. Before this PR: when we access some unexists tables. It will report the exception as follows: ``` mysql> select * from tt; ERROR 1105 (HY000): errCode = 2, detailMessage = Unexpected exception: null ``` After this PR, it will get the following results: ``` mysql> select * from tt; ERROR 1105 (HY000): errCode = 2, detailMessage = Unexpected exception: Table [tt] does not exist in database [default_cluster:test]. ``` ## Problem summary It is because in this [function](https://github.com/Reminiscent/doris/blob/f5af07f7b218ad2c1266626970b31dd9a9ee72d4/fe/fe-core/src/main/java/org/apache/doris/nereids/CascadesContext.java#L328), we ignore the exception. So the size of `tables` in `CascadesContext` is zero not null. So we can only get null after `table = cascadesContext.getTableByName(tableName);`. --- .../nereids/rules/analysis/BindRelation.java | 12 ++++++-- .../except/test_analyzer_exception.groovy | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 regression-test/suites/nereids_p0/except/test_analyzer_exception.groovy 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 f03be27f75..def4d383f8 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 @@ -139,9 +139,15 @@ public class BindRelation extends OneAnalysisRuleFactory { } String catalogName = cascadesContext.getConnectContext().getCurrentCatalog().getName(); String dbName = cascadesContext.getConnectContext().getDatabase(); - TableIf table = cascadesContext.getTables() != null - ? cascadesContext.getTableByName(tableName) - : getTable(catalogName, dbName, tableName, cascadesContext.getConnectContext().getEnv()); + TableIf table = null; + if (cascadesContext.getTables() != null) { + table = cascadesContext.getTableByName(tableName); + } + if (table == null) { + // In some cases even if we have already called the "cascadesContext.getTableByName", + // it also gets the null. So, we just check it in the catalog again for safety. + table = getTable(catalogName, dbName, tableName, cascadesContext.getConnectContext().getEnv()); + } // TODO: should generate different Scan sub class according to table's type List tableQualifier = Lists.newArrayList(catalogName, dbName, tableName); diff --git a/regression-test/suites/nereids_p0/except/test_analyzer_exception.groovy b/regression-test/suites/nereids_p0/except/test_analyzer_exception.groovy new file mode 100644 index 0000000000..17a79afcd4 --- /dev/null +++ b/regression-test/suites/nereids_p0/except/test_analyzer_exception.groovy @@ -0,0 +1,30 @@ +// 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("test_analyzer_exception") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + def tbName = "test_analyzer_exception" + def dbName = "test_analyzer_db" + sql "CREATE DATABASE IF NOT EXISTS ${dbName}" + sql "USE ${dbName}" + + test { + sql "SELECT id FROM ${tbName}" + exception "errCode = 2, detailMessage = Unexpected exception: Table [test_analyzer_exception] does not exist in database [default_cluster:test_analyzer_db]." + } +}