use of herddb.core.TableSpaceManager in project herddb by diennea.
the class JSQLParserPlanner method buildUpdateStatement.
private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update update, boolean returnValues) throws StatementExecutionException {
net.sf.jsqlparser.schema.Table table = update.getTable();
// no alias for UPDATE!
checkSupported(table.getAlias() == null);
OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
Table tableImpl = tableManager.getTable();
checkSupported(update.getSelect() == null);
checkSupported(update.getJoins() == null);
checkSupported(update.getOrderByElements() == null);
checkSupported(update.getReturningExpressionList() == null);
checkSupported(update.getStartJoins() == null || update.getStartJoins().isEmpty());
List<Expression> projects = update.getExpressions();
List<CompiledSQLExpression> expressions = new ArrayList<>(projects.size());
int index = 0;
List<String> updateColumnList = new ArrayList<>(projects.size());
for (net.sf.jsqlparser.schema.Column column : update.getColumns()) {
checkSupported(column.getTable() == null);
String columnName = fixMySqlBackTicks(column.getColumnName().toLowerCase());
checkSupported(!tableImpl.isPrimaryKeyColumn(columnName));
updateColumnList.add(columnName);
CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(projects.get(index), tableSchema);
expressions.add(exp);
index++;
}
RecordFunction function = new SQLRecordFunction(updateColumnList, tableImpl, expressions);
Predicate where = null;
if (update.getWhere() != null) {
CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(update.getWhere(), tableSchema);
if (whereExpression != null) {
SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, update, tableSpaceManager);
where = sqlWhere;
}
}
PlannerOp op = new SimpleUpdateOp(new UpdateStatement(tableSchema.tableSpace, tableSchema.name, null, function, where).setReturnValues(returnValues));
return optimizePlan(op);
}
use of herddb.core.TableSpaceManager in project herddb by diennea.
the class JSQLParserPlanner method buildDeleteStatement.
private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete delete) throws StatementExecutionException {
net.sf.jsqlparser.schema.Table table = delete.getTable();
// no alias for DELETE!
checkSupported(table.getAlias() == null);
OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
Table tableImpl = tableManager.getTable();
checkSupported(delete.getLimit() == null);
checkSupported(delete.getJoins() == null);
checkSupported(delete.getOrderByElements() == null);
checkSupported(delete.getTables() == null || delete.getTables().isEmpty());
Predicate where = null;
if (delete.getWhere() != null) {
CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(delete.getWhere(), tableSchema);
if (whereExpression != null) {
SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, delete, tableSpaceManager);
where = sqlWhere;
}
}
PlannerOp op = new SimpleDeleteOp(new DeleteStatement(tableSchema.tableSpace, tableSchema.name, null, where));
return optimizePlan(op);
}
use of herddb.core.TableSpaceManager in project herddb by diennea.
the class ServerSideConnectionPeer method handlePrepareStatement.
private void handlePrepareStatement(Pdu message, Channel channel) {
try {
String query = PduCodec.PrepareStatement.readQuery(message);
String tablespace = PduCodec.PrepareStatement.readTablespace(message);
TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(tablespace);
if (tableSpaceManager == null) {
ByteBuf error = PduCodec.ErrorResponse.writeNotLeaderError(message.messageId, "no such tablespace " + tablespace + " (at " + server.getManager().getNodeId() + ")");
channel.sendReplyMessage(message.messageId, error);
return;
} else if (!tableSpaceManager.isLeader()) {
ByteBuf error = PduCodec.ErrorResponse.writeNotLeaderError(message.messageId, "not leader for " + tablespace);
channel.sendReplyMessage(message.messageId, error);
return;
}
long newId = preparedStatements.prepare(tablespace, query);
channel.sendReplyMessage(message.messageId, PduCodec.PrepareStatementResult.write(message.messageId, newId));
} finally {
message.close();
}
}
use of herddb.core.TableSpaceManager 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);
IndexUtils.discoverIndexOperations(tableSpace, where, table, predicate, scan, tableSpaceManager);
}
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.core.TableSpaceManager in project herddb by diennea.
the class JSQLParserPlanner method getTable.
private Table getTable(String defaultTableSpace, net.sf.jsqlparser.schema.Table table) {
String tableSpace = table.getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
TableSpaceManager tableSpaceManager = getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
clearCache();
throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
}
String tableName = fixMySqlBackTicks(table.getName().toLowerCase());
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + tableName + " here for " + tableSpace);
}
return tableManager.getTable();
}
Aggregations