From 484929d1f8e5fbcded26cc5466757e2805e3ac76 Mon Sep 17 00:00:00 2001 From: Ulrich Kramer Date: Tue, 17 Oct 2023 17:44:15 +0200 Subject: [PATCH] Add Schemas.getExistingSubSchema to avoid NullPointerException --- .../org/apache/calcite/schema/Schemas.java | 22 +++++++++++-------- .../apache/calcite/util/BuiltInMethod.java | 1 + 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/schema/Schemas.java b/core/src/main/java/org/apache/calcite/schema/Schemas.java index c007701bd255..4e3343511fc3 100644 --- a/core/src/main/java/org/apache/calcite/schema/Schemas.java +++ b/core/src/main/java/org/apache/calcite/schema/Schemas.java @@ -51,13 +51,7 @@ import java.lang.reflect.Type; import java.sql.Connection; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.apache.calcite.jdbc.CalciteSchema.LatticeEntry; @@ -125,9 +119,8 @@ public static Expression subSchemaExpression(SchemaPlus schema, String name, // (Type) schemaExpression.getSubSchema("name") final Expression schemaExpression = expression(schema); Expression call = - Expressions.call( + Expressions.call(BuiltInMethod.SCHEMAS_GET_EXISTING_SUB_SCHEMA.method, schemaExpression, - BuiltInMethod.SCHEMA_GET_SUB_SCHEMA.method, Expressions.constant(name)); //CHECKSTYLE: IGNORE 2 //noinspection unchecked @@ -137,6 +130,15 @@ public static Expression subSchemaExpression(SchemaPlus schema, String name, return call; } + public static SchemaPlus getExistingSubSchema(SchemaPlus schema, String name){ + SchemaPlus subSchema = schema.getSubSchema(name); + if ( subSchema == null ) { + throw new NoSuchElementException("Sub schema " + name + " not found in " + schema.toString()); + } + return subSchema; + } + + /** Converts a schema expression to a given type by calling the * {@link SchemaPlus#unwrap(Class)} method. */ public static Expression unwrap(Expression call, Class type) { @@ -648,4 +650,6 @@ private static class PathImpl return Pair.right(pairs); } } + + } diff --git a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java index 8865e1024964..b4d9edbf9bc1 100644 --- a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java +++ b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java @@ -161,6 +161,7 @@ public enum BuiltInMethod { SCHEMA_PLUS_ADD_TABLE(SchemaPlus.class, "add", String.class, Table.class), SCHEMA_PLUS_REMOVE_TABLE(SchemaPlus.class, "removeTable", String.class), SCHEMA_PLUS_UNWRAP(SchemaPlus.class, "unwrap", Class.class), + SCHEMAS_GET_EXISTING_SUB_SCHEMA(Schemas.class, "getExistingSubSchema", SchemaPlus.class, String.class), SCHEMAS_ENUMERABLE_SCANNABLE(Schemas.class, "enumerable", ScannableTable.class, DataContext.class), SCHEMAS_ENUMERABLE_FILTERABLE(Schemas.class, "enumerable",