Search in sources :

Example 71 with Table

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());
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) MaterializedViewTable(org.apache.calcite.schema.impl.MaterializedViewTable) Table(org.apache.calcite.schema.Table) StarTable(org.apache.calcite.schema.impl.StarTable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) NameMap(org.apache.calcite.util.NameMap) NavigableMap(java.util.NavigableMap)

Example 72 with Table

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;
}
Also used : Table(org.apache.calcite.schema.Table) NameSet(org.apache.calcite.util.NameSet)

Example 73 with Table

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);
            }
        }
    }
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) Table(org.apache.calcite.schema.Table) NameSet(org.apache.calcite.util.NameSet)

Example 74 with 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;
}
Also used : Function(org.apache.calcite.schema.Function) TableMacro(org.apache.calcite.schema.TableMacro) Table(org.apache.calcite.schema.Table) NameSet(org.apache.calcite.util.NameSet)

Example 75 with Table

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);
}
Also used : TableScan(org.apache.calcite.rel.core.TableScan) Table(org.apache.calcite.schema.Table) StandardConvertletTable(org.apache.calcite.sql2rel.StandardConvertletTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) SqlRexConvertletTable(org.apache.calcite.sql2rel.SqlRexConvertletTable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) Project(org.apache.calcite.rel.core.Project) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) RelOptTable(org.apache.calcite.plan.RelOptTable) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

Table (org.apache.calcite.schema.Table)104 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)38 RelOptTable (org.apache.calcite.plan.RelOptTable)33 Test (org.junit.Test)27 RelDataType (org.apache.calcite.rel.type.RelDataType)22 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)20 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)17 ScannableTable (org.apache.calcite.schema.ScannableTable)17 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)16 FilterableTable (org.apache.calcite.schema.FilterableTable)15 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)15 StreamableTable (org.apache.calcite.schema.StreamableTable)14 ArrayList (java.util.ArrayList)13 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)13 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)12 RelNode (org.apache.calcite.rel.RelNode)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 BitString (org.apache.calcite.util.BitString)10 ImmutableMap (com.google.common.collect.ImmutableMap)9