use of org.apache.calcite.schema.Table in project calcite by apache.
the class CalciteSchema method getTablesBasedOnNullaryFunctions.
/**
* Returns tables derived from explicit and implicit functions
* that take zero parameters.
*/
public final NavigableMap<String, Table> getTablesBasedOnNullaryFunctions() {
ImmutableSortedMap.Builder<String, Table> builder = new ImmutableSortedMap.Builder<>(NameSet.COMPARATOR);
for (Map.Entry<String, FunctionEntry> entry : nullaryFunctionMap.map().entrySet()) {
final Function function = entry.getValue().getFunction();
if (function instanceof TableMacro) {
assert function.getParameters().isEmpty();
final Table table = ((TableMacro) function).apply(ImmutableList.of());
builder.put(entry.getKey(), table);
}
}
// add tables derived from implicit functions
addImplicitTablesBasedOnNullaryFunctionsToBuilder(builder);
return Compatible.INSTANCE.navigableMap(builder.build());
}
use of org.apache.calcite.schema.Table in project calcite by apache.
the class CachingCalciteSchema method getImplicitTable.
protected TableEntry getImplicitTable(String tableName, boolean caseSensitive) {
final long now = System.currentTimeMillis();
final NameSet implicitTableNames = implicitTableCache.get(now);
for (String tableName2 : implicitTableNames.range(tableName, caseSensitive)) {
final Table table = schema.getTable(tableName2);
if (table != null) {
return tableEntry(tableName2, table);
}
}
return null;
}
use of org.apache.calcite.schema.Table in project calcite by apache.
the class CachingCalciteSchema method addImplicitTablesBasedOnNullaryFunctionsToBuilder.
protected void addImplicitTablesBasedOnNullaryFunctionsToBuilder(ImmutableSortedMap.Builder<String, Table> builder) {
ImmutableSortedMap<String, Table> explicitTables = builder.build();
final long now = System.currentTimeMillis();
final NameSet set = implicitFunctionCache.get(now);
for (String s : set.iterable()) {
// explicit table wins.
if (explicitTables.containsKey(s)) {
continue;
}
for (Function function : schema.getFunctions(s)) {
if (function instanceof TableMacro && function.getParameters().isEmpty()) {
final Table table = ((TableMacro) function).apply(ImmutableList.of());
builder.put(s, table);
}
}
}
}
use of org.apache.calcite.schema.Table in project calcite by apache.
the class CachingCalciteSchema method getImplicitTableBasedOnNullaryFunction.
protected TableEntry getImplicitTableBasedOnNullaryFunction(String tableName, boolean caseSensitive) {
final long now = System.currentTimeMillis();
final NameSet set = implicitFunctionCache.get(now);
for (String s : set.range(tableName, caseSensitive)) {
for (Function function : schema.getFunctions(s)) {
if (function instanceof TableMacro && function.getParameters().isEmpty()) {
final Table table = ((TableMacro) function).apply(ImmutableList.of());
return tableEntry(tableName, table);
}
}
}
return null;
}
use of org.apache.calcite.schema.Table in project calcite by apache.
the class CalcitePrepareImpl method analyze_.
private AnalyzeViewResult analyze_(SqlValidator validator, String sql, SqlNode sqlNode, RelRoot root, boolean fail) {
final RexBuilder rexBuilder = root.rel.getCluster().getRexBuilder();
RelNode rel = root.rel;
final RelNode viewRel = rel;
Project project;
if (rel instanceof Project) {
project = (Project) rel;
rel = project.getInput();
} else {
project = null;
}
Filter filter;
if (rel instanceof Filter) {
filter = (Filter) rel;
rel = filter.getInput();
} else {
filter = null;
}
TableScan scan;
if (rel instanceof TableScan) {
scan = (TableScan) rel;
} else {
scan = null;
}
if (scan == null) {
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.modifiableViewMustBeBasedOnSingleTable());
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
final RelOptTable targetRelTable = scan.getTable();
final RelDataType targetRowType = targetRelTable.getRowType();
final Table table = targetRelTable.unwrap(Table.class);
final List<String> tablePath = targetRelTable.getQualifiedName();
assert table != null;
List<Integer> columnMapping;
final Map<Integer, RexNode> projectMap = new HashMap<>();
if (project == null) {
columnMapping = ImmutableIntList.range(0, targetRowType.getFieldCount());
} else {
columnMapping = new ArrayList<>();
for (Ord<RexNode> node : Ord.zip(project.getProjects())) {
if (node.e instanceof RexInputRef) {
RexInputRef rexInputRef = (RexInputRef) node.e;
int index = rexInputRef.getIndex();
if (projectMap.get(index) != null) {
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.moreThanOneMappedColumn(targetRowType.getFieldList().get(index).getName(), Util.last(tablePath)));
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
projectMap.put(index, rexBuilder.makeInputRef(viewRel, node.i));
columnMapping.add(index);
} else {
columnMapping.add(-1);
}
}
}
final RexNode constraint;
if (filter != null) {
constraint = filter.getCondition();
} else {
constraint = rexBuilder.makeLiteral(true);
}
final List<RexNode> filters = new ArrayList<>();
// If we put a constraint in projectMap above, then filters will not be empty despite
// being a modifiable view.
final List<RexNode> filters2 = new ArrayList<>();
boolean retry = false;
RelOptUtil.inferViewPredicates(projectMap, filters, constraint);
if (fail && !filters.isEmpty()) {
final Map<Integer, RexNode> projectMap2 = new HashMap<>();
RelOptUtil.inferViewPredicates(projectMap2, filters2, constraint);
if (!filters2.isEmpty()) {
throw validator.newValidationError(sqlNode, RESOURCE.modifiableViewMustHaveOnlyEqualityPredicates());
}
retry = true;
}
// Check that all columns that are not projected have a constant value
for (RelDataTypeField field : targetRowType.getFieldList()) {
final int x = columnMapping.indexOf(field.getIndex());
if (x >= 0) {
assert Util.skip(columnMapping, x + 1).indexOf(field.getIndex()) < 0 : "column projected more than once; should have checked above";
// target column is projected
continue;
}
if (projectMap.get(field.getIndex()) != null) {
// constant expression
continue;
}
if (field.getType().isNullable()) {
// don't need expression for nullable columns; NULL suffices
continue;
}
if (fail) {
throw validator.newValidationError(sqlNode, RESOURCE.noValueSuppliedForViewColumn(field.getName(), Util.last(tablePath)));
}
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, null, null, null, null, false);
}
final boolean modifiable = filters.isEmpty() || retry && filters2.isEmpty();
return new AnalyzeViewResult(this, validator, sql, sqlNode, validator.getValidatedNodeType(sqlNode), root, modifiable ? table : null, ImmutableList.copyOf(tablePath), constraint, ImmutableIntList.copyOf(columnMapping), modifiable);
}
Aggregations