Search in sources :

Example 16 with CompiledSQLExpression

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);
}
Also used : ScanResult(herddb.model.ScanResult) DataScanner(herddb.model.DataScanner) Transaction(herddb.model.Transaction) List(java.util.List) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Tuple(herddb.model.Tuple)

Example 17 with CompiledSQLExpression

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()]));
    }
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression)

Example 18 with CompiledSQLExpression

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);
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) Record(herddb.model.Record) RecordFunction(herddb.model.RecordFunction) StatementExecutionException(herddb.model.StatementExecutionException) TableContext(herddb.model.TableContext) Table(herddb.model.Table) HashMap(java.util.HashMap) Function(java.util.function.Function) RecordSerializer(herddb.codec.RecordSerializer) ColumnTypes(herddb.model.ColumnTypes) List(java.util.List) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Map(java.util.Map) StatementEvaluationContext(herddb.model.StatementEvaluationContext) DataAccessor(herddb.utils.DataAccessor) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException)

Example 19 with CompiledSQLExpression

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()]));
    }
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression)

Example 20 with CompiledSQLExpression

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);
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) Expression(net.sf.jsqlparser.expression.Expression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TableSpaceManager(herddb.core.TableSpaceManager) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) DMLStatement(herddb.model.DMLStatement)

Aggregations

CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)34 Column (herddb.model.Column)17 ArrayList (java.util.ArrayList)16 Table (herddb.model.Table)15 PlannerOp (herddb.model.planner.PlannerOp)15 StatementExecutionException (herddb.model.StatementExecutionException)13 RecordFunction (herddb.model.RecordFunction)9 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)9 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)8 ConstantExpression (herddb.sql.expressions.ConstantExpression)8 TableSpaceManager (herddb.core.TableSpaceManager)7 AccessCurrentRowExpression (herddb.sql.expressions.AccessCurrentRowExpression)7 CompiledMultiAndExpression (herddb.sql.expressions.CompiledMultiAndExpression)7 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)7 AbstractTableManager (herddb.core.AbstractTableManager)6 ScanStatement (herddb.model.commands.ScanStatement)6 Expression (net.sf.jsqlparser.expression.Expression)6 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)6 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)6 RelDataType (org.apache.calcite.rel.type.RelDataType)6