Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-933] Add null check in RelBuilder.scan() and throw a nice ex… #159

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -591,6 +591,9 @@ ExInst<CalciteException> illegalArgumentForTableFunctionCall(String a0,

@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
13 changes: 7 additions & 6 deletions core/src/main/java/org/apache/calcite/tools/RelBuilder.java
Expand Up @@ -45,19 +45,17 @@
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexUtil;
import org.apache.calcite.runtime.CalciteException;
import org.apache.calcite.runtime.Resources;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.server.CalciteServerStatement;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.util.ImmutableBitSet;
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.Util;
import org.apache.calcite.sql.validate.SqlValidatorException;
import org.apache.calcite.util.*;
import org.apache.calcite.util.mapping.Mapping;
import org.apache.calcite.util.mapping.Mappings;

Expand Down Expand Up @@ -650,6 +648,9 @@ public AggCall max(String alias, RexNode operand) {
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;
Expand Down
Expand Up @@ -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
30 changes: 30 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
Expand Up @@ -116,6 +116,36 @@ private String str(RelNode r) {
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 because ZZZ does not exist");
} 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("Table names are case-sensitive and scan of 'emp' should have failed");
} catch (Exception e) {
assertThat(e.getMessage(), is("Table 'emp' not found"));
}
}

@Test public void testScanFilterTrue() {
// Equivalent SQL:
// SELECT *
Expand Down