Repository: calcite Updated Branches: refs/heads/master a2880dd59 -> 3d88b254a
[CALCITE-1919] NullPointerException when target in ReflectiveSchema belongs to root package (Lim Chee Hau) Add null checking before get name from package. Add test case (Julian Hyde). Close apache/calcite#505 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/3d88b254 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/3d88b254 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/3d88b254 Branch: refs/heads/master Commit: 3d88b254a703fa2c4b20e7952802c5cd3f38ffc2 Parents: a2880dd Author: Lim Chee Hau <[email protected]> Authored: Wed Aug 2 13:16:05 2017 +0200 Committer: Julian Hyde <[email protected]> Committed: Wed Aug 2 11:56:02 2017 -0700 ---------------------------------------------------------------------- core/src/test/java/RootEmployee.java | 32 ++++++++++++++++++++ core/src/test/java/RootHr.java | 29 ++++++++++++++++++ .../calcite/test/ReflectiveSchemaTest.java | 25 +++++++++++++++ .../org/apache/calcite/linq4j/tree/Types.java | 1 + 4 files changed, 87 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/3d88b254/core/src/test/java/RootEmployee.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/RootEmployee.java b/core/src/test/java/RootEmployee.java new file mode 100644 index 0000000..288785c --- /dev/null +++ b/core/src/test/java/RootEmployee.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +/** Equivalent to + * {@link org.apache.calcite.examples.foodmart.java.JdbcExample.Employee}, but + * belongs to the unnamed (root) package. */ +public class RootEmployee { + public final int empid; + public final String name; + + /** Creates a RootEmployee. */ + public RootEmployee(int empid, String name) { + this.empid = empid; + this.name = name; + } +} + +// End RootEmployee.java http://git-wip-us.apache.org/repos/asf/calcite/blob/3d88b254/core/src/test/java/RootHr.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/RootHr.java b/core/src/test/java/RootHr.java new file mode 100644 index 0000000..e537656 --- /dev/null +++ b/core/src/test/java/RootHr.java @@ -0,0 +1,29 @@ +/* + * 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. + */ + +/** Equivalent to + * {@link org.apache.calcite.examples.foodmart.java.JdbcExample.Hr}, but + * belongs to the unnamed (root) package. */ +public class RootHr { + public final RootEmployee[] emps = { + new RootEmployee(100, "Bill"), + new RootEmployee(200, "Eric"), + new RootEmployee(150, "Sebastian"), + }; +} + +// End RootHr.java http://git-wip-us.apache.org/repos/asf/calcite/blob/3d88b254/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java b/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java index ab6b0b8..801e401 100644 --- a/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java +++ b/core/src/test/java/org/apache/calcite/test/ReflectiveSchemaTest.java @@ -20,6 +20,7 @@ import org.apache.calcite.adapter.java.ReflectiveSchema; import org.apache.calcite.avatica.util.DateTimeUtils; import org.apache.calcite.config.Lex; import org.apache.calcite.jdbc.CalciteConnection; +import org.apache.calcite.jdbc.Driver; import org.apache.calcite.linq4j.Enumerable; import org.apache.calcite.linq4j.Linq4j; import org.apache.calcite.linq4j.QueryProvider; @@ -57,10 +58,12 @@ import java.util.Arrays; import java.util.BitSet; import java.util.Date; import java.util.List; +import java.util.Properties; import static org.apache.calcite.test.JdbcTest.Employee; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -724,6 +727,28 @@ public class ReflectiveSchemaTest { .returnsUnordered("V=1970-01-01"); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919] + * NPE when target in ReflectiveSchema belongs to the unnamed package</a>. */ + @Test public void testReflectiveSchemaInUnnamedPackage() throws Exception { + final Driver driver = new Driver(); + try (CalciteConnection connection = (CalciteConnection) + driver.connect("jdbc:calcite:", new Properties())) { + SchemaPlus rootSchema = connection.getRootSchema(); + final Class<?> c = Class.forName("RootHr"); + final Object o = c.getDeclaredConstructor().newInstance(); + rootSchema.add("hr", new ReflectiveSchema(o)); + connection.setSchema("hr"); + final Statement statement = connection.createStatement(); + final String sql = "select * from \"emps\""; + final ResultSet resultSet = statement.executeQuery(sql); + final String expected = "empid=100; name=Bill\n" + + "empid=200; name=Eric\n" + + "empid=150; name=Sebastian\n"; + assertThat(CalciteAssert.toString(resultSet), is(expected)); + } + } + /** Extension to {@link Employee} with a {@code hireDate} column. */ public static class EmployeeWithHireDate extends Employee { public final java.sql.Date hireDate; http://git-wip-us.apache.org/repos/asf/calcite/blob/3d88b254/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Types.java ---------------------------------------------------------------------- diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Types.java b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Types.java index d178a44..b4f233a 100644 --- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Types.java +++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Types.java @@ -252,6 +252,7 @@ public abstract class Types { } String className = clazz.getName(); if (!clazz.isPrimitive() + && clazz.getPackage() != null && clazz.getPackage().getName().equals("java.lang")) { return className.substring("java.lang.".length()); }
