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]." + } +}