You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/11/18 13:58:35 UTC

[shardingsphere] branch master updated: Fix no resource, select contains system schema NPE. (#22264)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new ce86cd68f10 Fix no resource, select contains system schema NPE. (#22264)
ce86cd68f10 is described below

commit ce86cd68f10c1cd75edd5ba770efa69db1f08d5c
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Fri Nov 18 21:58:19 2022 +0800

    Fix no resource, select contains system schema NPE. (#22264)
---
 .../executor/FilterableTableScanExecutor.java      |  6 +-
 .../executor/FilterableTableScanExecutorTest.java  | 67 ++++++++++++++++++++++
 2 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutor.java b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutor.java
index 4b64293888e..41a674beccf 100644
--- a/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutor.java
+++ b/kernel/sql-federation/executor/core/src/main/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutor.java
@@ -122,6 +122,9 @@ public final class FilterableTableScanExecutor implements TableScanExecutor {
         String databaseName = executorContext.getDatabaseName();
         String schemaName = executorContext.getSchemaName();
         DatabaseType databaseType = DatabaseTypeEngine.getTrunkDatabaseType(optimizerContext.getParserContext(databaseName).getDatabaseType().getType());
+        if (databaseType.getSystemSchemas().contains(schemaName)) {
+            return executeByShardingSphereData(databaseName, schemaName, table);
+        }
         SqlString sqlString = createSQLString(table, (FilterableScanNodeExecutorContext) scanContext, SQLDialectFactory.getSQLDialect(databaseType));
         SQLFederationExecutorContext federationContext = executorContext.getFederationContext();
         QueryContext queryContext = createQueryContext(federationContext.getMetaData(), sqlString, databaseType);
@@ -132,9 +135,6 @@ public final class FilterableTableScanExecutor implements TableScanExecutor {
             federationContext.getExecutionUnits().addAll(context.getExecutionUnits());
             return createEmptyEnumerable();
         }
-        if (databaseType.getSystemSchemas().contains(schemaName)) {
-            return executeByShardingSphereData(databaseName, schemaName, table);
-        }
         return execute(databaseType, queryContext, database, context);
     }
     
diff --git a/kernel/sql-federation/executor/core/src/test/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutorTest.java b/kernel/sql-federation/executor/core/src/test/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutorTest.java
new file mode 100644
index 00000000000..79c1ef6b69d
--- /dev/null
+++ b/kernel/sql-federation/executor/core/src/test/java/org/apache/shardingsphere/sqlfederation/executor/FilterableTableScanExecutorTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.shardingsphere.sqlfederation.executor;
+
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Enumerator;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereDatabaseData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereRowData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereSchemaData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereTableData;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
+import org.apache.shardingsphere.sqlfederation.optimizer.context.OptimizerContext;
+import org.apache.shardingsphere.sqlfederation.optimizer.executor.ScanNodeExecutorContext;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class FilterableTableScanExecutorTest {
+    
+    @Test
+    public void assertExecuteWithShardingSphereData() {
+        OptimizerContext optimizerContext = mock(OptimizerContext.class, RETURNS_DEEP_STUBS);
+        when(optimizerContext.getParserContext(any()).getDatabaseType().getType()).thenReturn("PostgreSQL");
+        TableScanExecutorContext executorContext = mock(TableScanExecutorContext.class);
+        when(executorContext.getDatabaseName()).thenReturn("db");
+        when(executorContext.getSchemaName()).thenReturn("pg_catalog");
+        ShardingSphereData shardingSphereData = mock(ShardingSphereData.class, RETURNS_DEEP_STUBS);
+        ShardingSphereDatabaseData databaseData = mock(ShardingSphereDatabaseData.class, RETURNS_DEEP_STUBS);
+        when(shardingSphereData.getDatabaseData().get("db")).thenReturn(databaseData);
+        ShardingSphereSchemaData schemaData = mock(ShardingSphereSchemaData.class, RETURNS_DEEP_STUBS);
+        when(databaseData.getSchemaData().get("pg_catalog")).thenReturn(schemaData);
+        ShardingSphereTableData tableData = mock(ShardingSphereTableData.class);
+        when(tableData.getRows()).thenReturn(Collections.singletonList(new ShardingSphereRowData(Collections.singletonList(1))));
+        when(schemaData.getTableData().get("test")).thenReturn(tableData);
+        ShardingSphereTable shardingSphereTable = mock(ShardingSphereTable.class);
+        when(shardingSphereTable.getName()).thenReturn("test");
+        Enumerable<Object[]> enumerable = new FilterableTableScanExecutor(null, null, null, optimizerContext, null, executorContext, shardingSphereData, null)
+                .execute(shardingSphereTable, mock(ScanNodeExecutorContext.class));
+        Enumerator<Object[]> actual = enumerable.enumerator();
+        actual.moveNext();
+        Object[] row = actual.current();
+        assertThat(row[0], is(1));
+    }
+}