Skip to content

Commit

Permalink
Add Schemas.getExistingSubSchema to avoid NullPointerException
Browse files Browse the repository at this point in the history
  • Loading branch information
kramerul committed Oct 17, 2023
1 parent 8862b2e commit 484929d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/main/java/org/apache/calcite/schema/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -648,4 +650,6 @@ private static class PathImpl
return Pair.right(pairs);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 484929d

Please sign in to comment.