Search in sources :

Example 1 with CompiledMultiAndExpression

use of herddb.sql.expressions.CompiledMultiAndExpression in project herddb by diennea.

the class CalcitePlanner method planBindableTableScan.

private PlannerOp planBindableTableScan(BindableTableScan scan, RelDataType rowType) {
    if (rowType == null) {
        rowType = scan.getRowType();
    }
    final String tableSpace = scan.getTable().getQualifiedName().get(0);
    final TableImpl tableImpl = (TableImpl) scan.getTable().unwrap(org.apache.calcite.schema.Table.class);
    Table table = tableImpl.tableManager.getTable();
    SQLRecordPredicate predicate = null;
    if (!scan.filters.isEmpty()) {
        CompiledSQLExpression where = null;
        if (scan.filters.size() == 1) {
            RexNode expr = scan.filters.get(0);
            where = SQLExpressionCompiler.compileExpression(expr);
        } else {
            CompiledSQLExpression[] operands = new CompiledSQLExpression[scan.filters.size()];
            int i = 0;
            for (RexNode expr : scan.filters) {
                CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(expr);
                operands[i++] = condition;
            }
            where = new CompiledMultiAndExpression(operands);
        }
        predicate = new SQLRecordPredicate(table, null, where);
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        IndexOperation op = scanForIndexAccess(where, table, tableSpaceManager);
        predicate.setIndexOperation(op);
        CompiledSQLExpression filterPk = findFiltersOnPrimaryKey(table, where);
        if (filterPk != null) {
            filterPk = remapPositionalAccessToToPrimaryKeyAccessor(filterPk, table, scan);
        }
        predicate.setPrimaryKeyFilter(filterPk);
    }
    List<RexNode> projections = new ArrayList<>(scan.projects.size());
    int i = 0;
    for (int fieldpos : scan.projects) {
        projections.add(new RexInputRef(fieldpos, rowType.getFieldList().get(i++).getType()));
    }
    Projection projection = buildProjection(projections, rowType, true, table.columns);
    ScanStatement scanStatement = new ScanStatement(tableSpace, table.name, projection, predicate, null, null);
    scanStatement.setTableDef(table);
    return new BindableTableScanOp(scanStatement);
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) Table(herddb.model.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) ArrayList(java.util.ArrayList) Projection(herddb.model.Projection) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) IndexOperation(herddb.index.IndexOperation) TableSpaceManager(herddb.core.TableSpaceManager) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode) ScanStatement(herddb.model.commands.ScanStatement) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Example 2 with CompiledMultiAndExpression

use of herddb.sql.expressions.CompiledMultiAndExpression in project herddb by diennea.

the class CalcitePlanner method findFiltersOnPrimaryKey.

private CompiledSQLExpression findFiltersOnPrimaryKey(Table table, CompiledSQLExpression where) throws StatementExecutionException {
    List<CompiledSQLExpression> expressions = new ArrayList<>();
    for (String pk : table.primaryKey) {
        List<CompiledSQLExpression> conditions = where.scanForConstraintsOnColumn(pk, table);
        if (conditions.isEmpty()) {
            break;
        }
        expressions.addAll(conditions);
    }
    if (expressions.isEmpty()) {
        // no match at all, there is no direct constraint on PK
        return null;
    } else if (expressions.size() == 1) {
        return expressions.get(0);
    } else {
        return new CompiledMultiAndExpression(expressions.toArray(new CompiledSQLExpression[expressions.size()]));
    }
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression)

Aggregations

CompiledMultiAndExpression (herddb.sql.expressions.CompiledMultiAndExpression)2 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)2 ArrayList (java.util.ArrayList)2 TableSpaceManager (herddb.core.TableSpaceManager)1 IndexOperation (herddb.index.IndexOperation)1 Projection (herddb.model.Projection)1 Table (herddb.model.Table)1 ScanStatement (herddb.model.commands.ScanStatement)1 BindableTableScanOp (herddb.model.planner.BindableTableScanOp)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1 RexInputRef (org.apache.calcite.rex.RexInputRef)1 RexNode (org.apache.calcite.rex.RexNode)1 ModifiableTable (org.apache.calcite.schema.ModifiableTable)1 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)1 ScannableTable (org.apache.calcite.schema.ScannableTable)1 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)1