use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.
the class ValuesOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
Iterator<List<CompiledSQLExpression>> it = tuples.iterator();
Transaction transaction = tableSpaceManager.getTransaction(transactionContext.transactionId);
DataScanner res = new DataScanner(transaction, fieldNames, columns) {
@Override
public boolean hasNext() throws DataScannerException {
return it.hasNext();
}
@Override
public DataAccessor next() throws DataScannerException {
Object[] values = new Object[fieldNames.length];
List<CompiledSQLExpression> tuple = it.next();
for (int i = 0; i < values.length; i++) {
Object value = tuple.get(i).evaluate(DataAccessor.NULL, context);
values[i] = value;
}
return new Tuple(fieldNames, values);
}
};
return new ScanResult(transactionContext.transactionId, res);
}
use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.
the class IndexUtils method findFiltersOnPrimaryKey.
private static 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()]));
}
}
use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.
the class SQLRecordFunction method computeNewValue.
@Override
public byte[] computeNewValue(Record previous, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
try {
if (previous != null) {
Map<String, Object> asMap = previous.toBean(table);
DataAccessor bean = previous.getDataAccessor(table);
Function<String, Object> fieldValueComputer = (columnName) -> {
CompiledSQLExpression e = expressionsByColumnName.get(columnName);
if (e == null) {
return asMap.get(columnName);
} else {
herddb.model.Column column = table.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("unknown column " + columnName + " in table " + table.name);
}
try {
return RecordSerializer.convert(column.type, e.evaluate(bean, context));
} catch (StatementExecutionException err) {
throw new StatementExecutionException("error on column " + column.name + " (" + ColumnTypes.typeToString(column.type) + "):" + err.getMessage(), err);
}
}
};
return RecordSerializer.buildRecord(previous.value != null ? previous.value.getLength() : 0, table, fieldValueComputer);
} else {
Function<String, Object> fieldValueComputer = (columnName) -> {
CompiledSQLExpression e = expressionsByColumnName.get(columnName);
if (e == null) {
return null;
} else {
herddb.model.Column column = table.getColumn(columnName);
if (column == null) {
throw new StatementExecutionException("unknown column " + columnName + " in table " + table.name);
}
try {
return RecordSerializer.convert(column.type, e.evaluate(DataAccessor.NULL, context));
} catch (StatementExecutionException err) {
throw new StatementExecutionException("error on column " + column.name + " (" + ColumnTypes.typeToString(column.type) + "):" + err.getMessage(), err);
}
}
};
return RecordSerializer.buildRecord(0, table, fieldValueComputer);
}
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
use of herddb.sql.expressions.CompiledSQLExpression 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()]));
}
}
use of herddb.sql.expressions.CompiledSQLExpression in project herddb by diennea.
the class SQLPlanner method buildUpdateStatement.
private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update s, boolean returnValues) throws StatementExecutionException {
if (s.getTables().size() != 1) {
throw new StatementExecutionException("unsupported multi-table update " + s);
}
net.sf.jsqlparser.schema.Table fromTable = s.getTables().get(0);
String tableSpace = fromTable.getSchemaName();
String tableName = fromTable.getName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
}
Table table = tableManager.getTable();
for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
Column column = table.getColumn(c.getColumnName());
if (column == null) {
throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
}
if (table.isPrimaryKeyColumn(c.getColumnName())) {
throw new StatementExecutionException("updates of fields on the PK (" + Arrays.toString(table.primaryKey) + ") are not supported. Please perform a DELETE and than an INSERT");
}
}
List<CompiledSQLExpression> compiledSQLExpressions = new ArrayList<>();
for (Expression e : s.getExpressions()) {
compiledSQLExpressions.add(SQLExpressionCompiler.compileExpression(null, e));
}
RecordFunction function = new SQLRecordFunction(table, s.getColumns(), compiledSQLExpressions);
// Perform a scan and then update each row
SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
if (where != null) {
Expression expressionWhere = s.getWhere();
discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
}
DMLStatement st = new UpdateStatement(tableSpace, tableName, null, function, where).setReturnValues(returnValues);
return ExecutionPlan.simple(st);
}
Aggregations