From 9c083783c9907b046bc087fbe575480d08f53eb8 Mon Sep 17 00:00:00 2001 From: Jonathan Wei Date: Tue, 9 Mar 2021 16:19:59 -0800 Subject: [PATCH] Don't fail on invalid views in InformationSchema (#10960) * Don't fail on invalid views in InformationSchema * Fix test --- .../sql/calcite/schema/InformationSchema.java | 21 +++++++++++++------ .../sql/calcite/BaseCalciteQueryTest.java | 6 ++++++ .../druid/sql/calcite/CalciteQueryTest.java | 2 ++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/schema/InformationSchema.java b/sql/src/main/java/org/apache/druid/sql/calcite/schema/InformationSchema.java index b35f7a784a7..9aa411bb826 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/schema/InformationSchema.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/schema/InformationSchema.java @@ -46,6 +46,7 @@ import org.apache.calcite.schema.TableMacro; import org.apache.calcite.schema.impl.AbstractSchema; import org.apache.calcite.schema.impl.AbstractTable; import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.druid.java.util.emitter.EmittingLogger; import org.apache.druid.segment.column.RowSignature; import org.apache.druid.segment.column.ValueType; import org.apache.druid.server.security.AuthenticationResult; @@ -64,6 +65,8 @@ import java.util.Set; public class InformationSchema extends AbstractSchema { + private static final EmittingLogger log = new EmittingLogger(InformationSchema.class); + private static final String CATALOG_NAME = "druid"; private static final String SCHEMATA_TABLE = "SCHEMATA"; private static final String TABLES_TABLE = "TABLES"; @@ -357,12 +360,18 @@ public class InformationSchema extends AbstractSchema return null; } - return generateColumnMetadata( - schemaName, - functionName, - viewMacro.apply(ImmutableList.of()), - typeFactory - ); + try { + return generateColumnMetadata( + schemaName, + functionName, + viewMacro.apply(ImmutableList.of()), + typeFactory + ); + } + catch (Exception e) { + log.error(e, "Encountered exception while handling view[%s].", functionName); + return null; + } } } ) diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java index c03c3968aaa..8e039263b32 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/BaseCalciteQueryTest.java @@ -830,6 +830,12 @@ public class BaseCalciteQueryTest extends CalciteTestBase "restrictedView", "SELECT __time, dim1, dim2, m1 FROM druid.forbiddenDatasource WHERE dim2 = 'a'" ); + + viewManager.createView( + plannerFactory, + "invalidView", + "SELECT __time, dim1, dim2, m1 FROM druid.invalidDatasource WHERE dim2 = 'a'" + ); return sqlLifecycleFactory; } diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java index f80ce303cd0..656ae472315 100644 --- a/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java +++ b/sql/src/test/java/org/apache/druid/sql/calcite/CalciteQueryTest.java @@ -889,6 +889,7 @@ public class CalciteQueryTest extends BaseCalciteQueryTest .add(new Object[]{"view", "bview", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "cview", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "dview", "VIEW", "NO", "NO"}) + .add(new Object[]{"view", "invalidView", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "restrictedView", "VIEW", "NO", "NO"}) .build() ); @@ -924,6 +925,7 @@ public class CalciteQueryTest extends BaseCalciteQueryTest .add(new Object[]{"view", "cview", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "dview", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "forbiddenView", "VIEW", "NO", "NO"}) + .add(new Object[]{"view", "invalidView", "VIEW", "NO", "NO"}) .add(new Object[]{"view", "restrictedView", "VIEW", "NO", "NO"}) .build() );