[CALCITE-933] RelBuilder.scan() now gives a nice exception if the table does not exist (Andy Grove)
Close apache/incubator-calcite#159 Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/57e1b4c1 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/57e1b4c1 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/57e1b4c1 Branch: refs/heads/branch-release Commit: 57e1b4c14f0369e9b40df99435c6525fea9d1c96 Parents: 690faa5 Author: Andy Grove <[email protected]> Authored: Thu Oct 22 15:44:54 2015 -0600 Committer: Julian Hyde <[email protected]> Committed: Mon Oct 26 11:39:34 2015 -0700 ---------------------------------------------------------------------- .../apache/calcite/runtime/CalciteResource.java | 3 ++ .../org/apache/calcite/tools/RelBuilder.java | 4 +++ .../calcite/runtime/CalciteResource.properties | 1 + .../org/apache/calcite/test/RelBuilderTest.java | 30 ++++++++++++++++++++ 4 files changed, 38 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/57e1b4c1/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java index e74f915..0670948 100644 --- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java +++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java @@ -591,6 +591,9 @@ public interface CalciteResource { @BaseMessage("View is not modifiable. No value is supplied for NOT NULL column ''{0}'' of base table ''{1}''") ExInst<SqlValidatorException> noValueSuppliedForViewColumn(String columnName, String tableName); + + @BaseMessage("Table ''{0}'' not found") + ExInst<CalciteException> tableNotFound(String tableName); } // End CalciteResource.java http://git-wip-us.apache.org/repos/asf/calcite/blob/57e1b4c1/core/src/main/java/org/apache/calcite/tools/RelBuilder.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java index a403c6a..5e6ea45 100644 --- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java +++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java @@ -57,6 +57,7 @@ import org.apache.calcite.util.ImmutableIntList; import org.apache.calcite.util.NlsString; import org.apache.calcite.util.Pair; import org.apache.calcite.util.Stacks; +import org.apache.calcite.util.Static; import org.apache.calcite.util.Util; import org.apache.calcite.util.mapping.Mapping; import org.apache.calcite.util.mapping.Mappings; @@ -650,6 +651,9 @@ public class RelBuilder { public RelBuilder scan(String tableName) { final RelOptTable relOptTable = relOptSchema.getTableForMember(ImmutableList.of(tableName)); + if (relOptTable == null) { + throw Static.RESOURCE.tableNotFound(tableName).ex(); + } final RelNode scan = scanFactory.createScan(cluster, relOptTable); push(scan); return this; http://git-wip-us.apache.org/repos/asf/calcite/blob/57e1b4c1/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties ---------------------------------------------------------------------- diff --git a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties index b3f9cce..9703e1d 100644 --- a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties +++ b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties @@ -193,4 +193,5 @@ CannotStreamValues=Cannot stream VALUES ModifiableViewMustBeBasedOnSingleTable=Modifiable view must be based on a single table MoreThanOneMappedColumn=View is not modifiable. More than one expression maps to column ''{0}'' of base table ''{1}'' NoValueSuppliedForViewColumn=View is not modifiable. No value is supplied for NOT NULL column ''{0}'' of base table ''{1}'' +TableNotFound=Table ''{0}'' not found # End CalciteResource.properties http://git-wip-us.apache.org/repos/asf/calcite/blob/57e1b4c1/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java index 5e11fd1..7ce40c8 100644 --- a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java +++ b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java @@ -116,6 +116,36 @@ public class RelBuilderTest { is("LogicalTableScan(table=[[scott, EMP]])\n")); } + @Test public void testScanInvalidTable() { + // Equivalent SQL: + // SELECT * + // FROM zzz + try { + final RelNode root = + RelBuilder.create(config().build()) + .scan("ZZZ") // this relation does not exist + .build(); + fail("expected error, got " + root); + } catch (Exception e) { + assertThat(e.getMessage(), is("Table 'ZZZ' not found")); + } + } + + @Test public void testScanValidTableWrongCase() { + // Equivalent SQL: + // SELECT * + // FROM "emp" + try { + final RelNode root = + RelBuilder.create(config().build()) + .scan("emp") // the table is named 'EMP', not 'emp' + .build(); + fail("Expected error (table names are case-sensitive), but got " + root); + } catch (Exception e) { + assertThat(e.getMessage(), is("Table 'emp' not found")); + } + } + @Test public void testScanFilterTrue() { // Equivalent SQL: // SELECT *
