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);
}
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()]));
}
}
Aggregations