Search in sources :

Example 56 with Column

use of herddb.model.Column in project herddb by diennea.

the class JSQLParserPlanner method buildSelectBody.

private PlannerOp buildSelectBody(String defaultTableSpace, int maxRows, SelectBody selectBody, boolean forceScan) throws StatementExecutionException {
    if (selectBody instanceof SetOperationList) {
        SetOperationList list = (SetOperationList) selectBody;
        return buildSetOperationList(defaultTableSpace, maxRows, list, forceScan);
    }
    checkSupported(selectBody instanceof PlainSelect, selectBody.getClass().getName());
    PlainSelect plainSelect = (PlainSelect) selectBody;
    checkSupported(!plainSelect.getMySqlHintStraightJoin());
    checkSupported(!plainSelect.getMySqlSqlCalcFoundRows());
    checkSupported(!plainSelect.getMySqlSqlNoCache());
    checkSupported(plainSelect.getDistinct() == null);
    checkSupported(plainSelect.getFetch() == null);
    checkSupported(plainSelect.getFirst() == null);
    checkSupported(plainSelect.getForUpdateTable() == null);
    checkSupported(plainSelect.getForXmlPath() == null);
    checkSupported(plainSelect.getHaving() == null);
    checkSupported(plainSelect.getIntoTables() == null);
    checkSupported(plainSelect.getOffset() == null);
    checkSupported(plainSelect.getOptimizeFor() == null);
    checkSupported(plainSelect.getOracleHierarchical() == null);
    checkSupported(plainSelect.getOracleHint() == null);
    checkSupported(plainSelect.getSkip() == null);
    checkSupported(plainSelect.getWait() == null);
    checkSupported(plainSelect.getKsqlWindow() == null);
    FromItem fromItem = plainSelect.getFromItem();
    checkSupported(fromItem instanceof net.sf.jsqlparser.schema.Table);
    OpSchema primaryTableSchema = getTableSchema(defaultTableSpace, (net.sf.jsqlparser.schema.Table) fromItem);
    OpSchema[] joinedTables = new OpSchema[0];
    int totalJoinOutputFieldsCount = primaryTableSchema.columns.length;
    if (plainSelect.getJoins() != null) {
        joinedTables = new OpSchema[plainSelect.getJoins().size()];
        int i = 0;
        for (Join join : plainSelect.getJoins()) {
            checkSupported(!join.isApply());
            checkSupported(!join.isCross());
            checkSupported(!join.isFull() || (join.isFull() && join.isOuter()));
            checkSupported(!join.isSemi());
            checkSupported(!join.isStraight());
            checkSupported(!join.isWindowJoin());
            checkSupported(join.getJoinWindow() == null);
            checkSupported(join.getUsingColumns() == null);
            FromItem rightItem = join.getRightItem();
            checkSupported(rightItem instanceof net.sf.jsqlparser.schema.Table);
            OpSchema joinedTable = getTableSchema(defaultTableSpace, (net.sf.jsqlparser.schema.Table) rightItem);
            joinedTables[i++] = joinedTable;
            totalJoinOutputFieldsCount += joinedTable.columns.length;
        }
    }
    int pos = 0;
    String[] joinOutputFieldnames = new String[totalJoinOutputFieldsCount];
    ColumnRef[] joinOutputColumns = new ColumnRef[totalJoinOutputFieldsCount];
    System.arraycopy(primaryTableSchema.columnNames, 0, joinOutputFieldnames, 0, primaryTableSchema.columnNames.length);
    System.arraycopy(primaryTableSchema.columns, 0, joinOutputColumns, 0, primaryTableSchema.columns.length);
    pos += primaryTableSchema.columnNames.length;
    for (OpSchema joinedTable : joinedTables) {
        System.arraycopy(joinedTable.columnNames, 0, joinOutputFieldnames, pos, joinedTable.columnNames.length);
        System.arraycopy(joinedTable.columns, 0, joinOutputColumns, pos, joinedTable.columns.length);
        pos += joinedTable.columnNames.length;
    }
    OpSchema currentSchema = primaryTableSchema;
    // single JOIN only at the moment
    checkSupported(joinedTables.length <= 1);
    if (joinedTables.length > 0) {
        currentSchema = new OpSchema(primaryTableSchema.tableSpace, null, null, joinOutputFieldnames, joinOutputColumns);
    }
    List<SelectItem> selectItems = plainSelect.getSelectItems();
    checkSupported(!selectItems.isEmpty());
    Predicate predicate = null;
    TupleComparator comparator = null;
    ScanLimits limits = null;
    boolean identityProjection = false;
    Projection projection;
    List<SelectExpressionItem> selectedFields = new ArrayList<>(selectItems.size());
    boolean containsAggregatedFunctions = false;
    if (selectItems.size() == 1 && selectItems.get(0) instanceof AllColumns) {
        projection = Projection.IDENTITY(currentSchema.columnNames, ColumnRef.toColumnsArray(currentSchema.columns));
        identityProjection = true;
    } else {
        checkSupported(!selectItems.isEmpty());
        for (SelectItem item : selectItems) {
            if (item instanceof SelectExpressionItem) {
                SelectExpressionItem selectExpressionItem = (SelectExpressionItem) item;
                selectedFields.add(selectExpressionItem);
                if (SQLParserExpressionCompiler.detectAggregatedFunction(selectExpressionItem.getExpression()) != null) {
                    containsAggregatedFunctions = true;
                }
            } else if (item instanceof AllTableColumns) {
                // expand  alias.* to the full list of columns (according to pyhsical schema!)
                AllTableColumns allTablesColumn = (AllTableColumns) item;
                net.sf.jsqlparser.schema.Table table = allTablesColumn.getTable();
                String tableName = fixMySqlBackTicks(table.getName());
                boolean found = false;
                if (primaryTableSchema.isTableOrAlias(tableName)) {
                    for (ColumnRef col : primaryTableSchema.columns) {
                        net.sf.jsqlparser.schema.Column c = new net.sf.jsqlparser.schema.Column(table, col.name);
                        SelectExpressionItem selectExpressionItem = new SelectExpressionItem(c);
                        selectedFields.add(selectExpressionItem);
                        found = true;
                    }
                } else {
                    for (OpSchema joinedTableSchema : joinedTables) {
                        if (joinedTableSchema.isTableOrAlias(tableName)) {
                            for (ColumnRef col : joinedTableSchema.columns) {
                                net.sf.jsqlparser.schema.Column c = new net.sf.jsqlparser.schema.Column(table, col.name);
                                SelectExpressionItem selectExpressionItem = new SelectExpressionItem(c);
                                selectedFields.add(selectExpressionItem);
                            }
                            found = true;
                            break;
                        }
                    }
                }
                if (!found) {
                    checkSupported(false, "Bad table ref " + tableName + ".*");
                }
            } else {
                checkSupported(false);
            }
        }
        if (!containsAggregatedFunctions) {
            // building the projection
            // we have the current physical schema, we can create references by position (as Calcite does)
            // in order to not need accessing columns by name and also making it easier to support
            // ZeroCopyProjections
            projection = buildProjection(selectedFields, true, currentSchema);
        } else {
            // start by full table scan, the AggregateOp operator will create the final projection
            projection = Projection.IDENTITY(currentSchema.columnNames, ColumnRef.toColumnsArray(currentSchema.columns));
            identityProjection = true;
        }
    }
    TableSpaceManager tableSpaceManager = this.manager.getTableSpaceManager(primaryTableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(primaryTableSchema.name);
    Table tableImpl = tableManager.getTable();
    CompiledSQLExpression whereExpression = null;
    if (plainSelect.getWhere() != null) {
        whereExpression = SQLParserExpressionCompiler.compileExpression(plainSelect.getWhere(), currentSchema);
        if (joinedTables.length == 0 && whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(primaryTableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, selectBody, tableSpaceManager);
            predicate = sqlWhere;
        }
    }
    // start with a TableScan + filters
    ScanStatement scan = new ScanStatement(primaryTableSchema.tableSpace, primaryTableSchema.name, Projection.IDENTITY(primaryTableSchema.columnNames, ColumnRef.toColumnsArray(primaryTableSchema.columns)), predicate, comparator, limits);
    scan.setTableDef(tableImpl);
    PlannerOp op = new BindableTableScanOp(scan);
    PlannerOp[] scanJoinedTables = new PlannerOp[joinedTables.length];
    int ji = 0;
    for (OpSchema joinedTable : joinedTables) {
        ScanStatement scanSecondaryTable = new ScanStatement(joinedTable.tableSpace, joinedTable.name, Projection.IDENTITY(joinedTable.columnNames, ColumnRef.toColumnsArray(joinedTable.columns)), null, null, null);
        scan.setTableDef(tableImpl);
        checkSupported(joinedTable.tableSpace.equalsIgnoreCase(primaryTableSchema.tableSpace));
        PlannerOp opSecondaryTable = new BindableTableScanOp(scanSecondaryTable);
        scanJoinedTables[ji++] = opSecondaryTable;
    }
    if (scanJoinedTables.length > 0) {
        // assuming only one JOIN clause
        Join joinClause = plainSelect.getJoins().get(0);
        List<CompiledSQLExpression> joinConditions = new ArrayList<>();
        if (joinClause.isNatural()) {
            // NATURAL join adds a constraint on every column with the same name
            List<CompiledSQLExpression> naturalJoinConstraints = new ArrayList<>();
            int posInRowLeft = 0;
            for (ColumnRef ref : primaryTableSchema.columns) {
                int posInRowRight = primaryTableSchema.columns.length;
                for (ColumnRef ref2 : joinedTables[0].columns) {
                    // assuming only one join
                    if (ref2.name.equalsIgnoreCase(ref.name)) {
                        CompiledSQLExpression equals = new CompiledEqualsExpression(new AccessCurrentRowExpression(posInRowLeft, ref.type), new AccessCurrentRowExpression(posInRowRight, ref2.type));
                        naturalJoinConstraints.add(equals);
                    }
                    posInRowRight++;
                }
                posInRowLeft++;
            }
            CompiledSQLExpression naturalJoin = new CompiledMultiAndExpression(naturalJoinConstraints.toArray(new CompiledSQLExpression[0]));
            joinConditions.add(naturalJoin);
        }
        // handle "ON" clause
        Expression onExpression = joinClause.getOnExpression();
        if (onExpression != null) {
            // TODO: this works for INNER join, but not for LEFT/RIGHT joins
            CompiledSQLExpression onCondition = SQLParserExpressionCompiler.compileExpression(onExpression, currentSchema);
            joinConditions.add(onCondition);
        }
        op = new JoinOp(joinOutputFieldnames, ColumnRef.toColumnsArray(joinOutputColumns), new int[0], op, new int[0], scanJoinedTables[0], // generateNullsOnLeft
        joinClause.isRight() || (joinClause.isFull() && joinClause.isOuter()), // generateNullsOnRight
        joinClause.isLeft() || (joinClause.isFull() && joinClause.isOuter()), // mergeJoin
        false, // "ON" conditions
        joinConditions);
        // handle "WHERE" in case of JOIN
        if (whereExpression != null) {
            op = new FilterOp(op, whereExpression);
        }
    }
    // add aggregations
    if (containsAggregatedFunctions) {
        checkSupported(scanJoinedTables.length == 0);
        op = planAggregate(selectedFields, currentSchema, op, currentSchema, plainSelect.getGroupBy());
        currentSchema = new OpSchema(currentSchema.tableSpace, currentSchema.name, currentSchema.alias, ColumnRef.toColumnsRefsArray(currentSchema.name, op.getOutputSchema()));
    }
    // add order by
    if (plainSelect.getOrderByElements() != null) {
        op = planSort(op, currentSchema, plainSelect.getOrderByElements());
    }
    // add limit
    if (plainSelect.getLimit() != null) {
        checkSupported(scanJoinedTables.length == 0);
        // cannot mix LIMIT and TOP
        checkSupported(plainSelect.getTop() == null);
        Limit limit = plainSelect.getLimit();
        CompiledSQLExpression offset;
        if (limit.getOffset() != null) {
            offset = SQLParserExpressionCompiler.compileExpression(limit.getOffset(), currentSchema);
        } else {
            offset = new ConstantExpression(0, ColumnTypes.NOTNULL_LONG);
        }
        CompiledSQLExpression rowCount = null;
        if (limit.getRowCount() != null) {
            rowCount = SQLParserExpressionCompiler.compileExpression(limit.getRowCount(), currentSchema);
        }
        op = new LimitOp(op, rowCount, offset);
    }
    if (plainSelect.getTop() != null) {
        checkSupported(scanJoinedTables.length == 0);
        // cannot mix LIMIT and TOP
        checkSupported(plainSelect.getLimit() == null);
        Top limit = plainSelect.getTop();
        CompiledSQLExpression rowCount = null;
        if (limit.getExpression() != null) {
            rowCount = SQLParserExpressionCompiler.compileExpression(limit.getExpression(), currentSchema);
        }
        op = new LimitOp(op, rowCount, new ConstantExpression(0, ColumnTypes.NOTNULL_LONG));
    }
    if (!containsAggregatedFunctions && !identityProjection) {
        // add projection
        op = new ProjectOp(projection, op);
    }
    // additional maxrows from JDBC PreparedStatement
    if (maxRows > 0) {
        op = new LimitOp(op, new ConstantExpression(maxRows, ColumnTypes.NOTNULL_LONG), new ConstantExpression(0, ColumnTypes.NOTNULL_LONG)).optimize();
    }
    return op;
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) Projection(herddb.model.Projection) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) TableSpaceManager(herddb.core.TableSpaceManager) PlannerOp(herddb.model.planner.PlannerOp) ScanLimits(herddb.model.ScanLimits) ProjectOp(herddb.model.planner.ProjectOp) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Top(net.sf.jsqlparser.statement.select.Top) AbstractTableManager(herddb.core.AbstractTableManager) ColumnRef(herddb.sql.expressions.ColumnRef) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) FilterOp(herddb.model.planner.FilterOp) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) TupleComparator(herddb.model.TupleComparator) AllColumns(net.sf.jsqlparser.statement.select.AllColumns) Column(herddb.model.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) FromItem(net.sf.jsqlparser.statement.select.FromItem) ScanStatement(herddb.model.commands.ScanStatement) JoinOp(herddb.model.planner.JoinOp) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) Join(net.sf.jsqlparser.statement.select.Join) LimitOp(herddb.model.planner.LimitOp) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) AllTableColumns(net.sf.jsqlparser.statement.select.AllTableColumns) Limit(net.sf.jsqlparser.statement.select.Limit) OpSchema(herddb.sql.expressions.OpSchema)

Example 57 with Column

use of herddb.model.Column in project herddb by diennea.

the class JSQLParserPlanner method planerInsertOrUpsert.

private ExecutionPlan planerInsertOrUpsert(String defaultTableSpace, net.sf.jsqlparser.schema.Table table, List<net.sf.jsqlparser.schema.Column> columns, ItemsList itemsList, boolean returnValues, boolean upsert) throws StatementExecutionException, StatementNotSupportedException {
    OpSchema inputSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(inputSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(inputSchema.name);
    Table tableImpl = tableManager.getTable();
    List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
    List<String> keyExpressionToColumn = new ArrayList<>();
    List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
    List<String> valuesColumns = new ArrayList<>();
    boolean invalid = false;
    int index = 0;
    if (columns == null) {
        // INSERT INTO TABLE VALUES (xxxx) (no column list)
        columns = new ArrayList<>();
        for (Column c : tableImpl.getColumns()) {
            columns.add(new net.sf.jsqlparser.schema.Column(c.name));
        }
    }
    if (itemsList instanceof ExpressionList) {
        List<Expression> values = ((ExpressionList) itemsList).getExpressions();
        for (net.sf.jsqlparser.schema.Column column : columns) {
            CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(values.get(index), inputSchema);
            String columnName = fixMySqlBackTicks(column.getColumnName()).toLowerCase();
            if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression || exp instanceof CompiledFunction) {
                boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
                if (!isAlwaysNull) {
                    if (tableImpl.isPrimaryKeyColumn(columnName)) {
                        keyExpressionToColumn.add(columnName);
                        keyValueExpression.add(exp);
                    }
                    Column columnFromSchema = tableImpl.getColumn(columnName);
                    if (columnFromSchema == null) {
                        throw new StatementExecutionException("Column '" + columnName + "' not found in table " + tableImpl.name);
                    }
                    valuesColumns.add(columnFromSchema.name);
                    valuesExpressions.add(exp);
                }
                index++;
            } else {
                checkSupported(false, "Unsupported expression type " + exp.getClass().getName());
                break;
            }
        }
        // handle default values
        for (Column col : tableImpl.getColumns()) {
            if (!valuesColumns.contains(col.name)) {
                if (col.defaultValue != null) {
                    valuesColumns.add(col.name);
                    CompiledSQLExpression defaultValueExpression = makeDefaultValue(col);
                    valuesExpressions.add(defaultValueExpression);
                } else if (ColumnTypes.isNotNullDataType(col.type) && !tableImpl.auto_increment) {
                    throw new StatementExecutionException("Column '" + col.name + "' has no default value and does not allow NULLs");
                }
            }
        }
        DMLStatement statement;
        if (!invalid) {
            RecordFunction keyfunction;
            if (keyValueExpression.isEmpty() && tableImpl.auto_increment) {
                keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
            } else {
                if (keyValueExpression.size() != tableImpl.primaryKey.length) {
                    throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                }
                keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, tableImpl);
            }
            RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, tableImpl, valuesExpressions);
            statement = new InsertStatement(inputSchema.tableSpace, inputSchema.name, keyfunction, valuesfunction, upsert).setReturnValues(returnValues);
        } else {
            throw new StatementNotSupportedException();
        }
        PlannerOp op = new SimpleInsertOp(statement.setReturnValues(returnValues));
        return optimizePlan(op);
    } else if (itemsList instanceof MultiExpressionList) {
        List<ExpressionList> records = ((MultiExpressionList) itemsList).getExprList();
        ValuesOp values = planValuesForInsertOp(columns, tableImpl, inputSchema, records);
        InsertOp op = new InsertOp(tableImpl.tablespace, tableImpl.name, values, returnValues, upsert);
        return optimizePlan(op);
    } else {
        checkSupported(false);
        return null;
    }
}
Also used : ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ValuesOp(herddb.model.planner.ValuesOp) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) Column(herddb.model.Column) TableSpaceManager(herddb.core.TableSpaceManager) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) CompiledFunction(herddb.sql.expressions.CompiledFunction) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) AbstractTableManager(herddb.core.AbstractTableManager) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) DMLStatement(herddb.model.DMLStatement) OpSchema(herddb.sql.expressions.OpSchema) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) InsertOp(herddb.model.planner.InsertOp)

Example 58 with Column

use of herddb.model.Column in project herddb by diennea.

the class SQLRecordKeyFunction method computeNewValue.

@Override
@SuppressFBWarnings("BC_UNCONFIRMED_CAST")
public byte[] computeNewValue(Record previous, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
    SQLStatementEvaluationContext statementEvaluationContext = (SQLStatementEvaluationContext) context;
    if (isConstant) {
        byte[] cachedResult = statementEvaluationContext.getConstant(this);
        if (cachedResult != null) {
            return cachedResult;
        }
    }
    final DataAccessor prevRecord = previous == null ? DataAccessor.NULL : previous.getDataAccessor(tableContext.getTable());
    Map<String, Object> pk = new HashMap<>();
    for (int i = 0; i < columns.length; i++) {
        herddb.model.Column c = columns[i];
        CompiledSQLExpression expression = expressions.get(i);
        Object value = expression.evaluate(prevRecord, context);
        if (value == null) {
            throw new InvalidNullValueForKeyException("error while converting primary key " + pk + ", keys cannot be null");
        }
        try {
            value = RecordSerializer.convert(c.type, value);
        } catch (StatementExecutionException err) {
            throw new StatementExecutionException("error on column " + c.name + " (" + ColumnTypes.typeToString(c.type) + "):" + err.getMessage(), err);
        }
        pk.put(c.name, value);
    }
    try {
        // maybe this is only a partial primary key
        byte[] result = RecordSerializer.serializePrimaryKeyRaw(pk, table, pkColumnNames);
        if (isConstant) {
            statementEvaluationContext.cacheConstant(this, result);
        }
        return result;
    } catch (Exception err) {
        throw new StatementExecutionException("error while converting primary key " + pk + ": " + err, err);
    }
}
Also used : HashMap(java.util.HashMap) DataAccessor(herddb.utils.DataAccessor) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) InvalidNullValueForKeyException(herddb.model.InvalidNullValueForKeyException) StatementExecutionException(herddb.model.StatementExecutionException) StatementExecutionException(herddb.model.StatementExecutionException) InvalidNullValueForKeyException(herddb.model.InvalidNullValueForKeyException) Column(herddb.model.Column) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 59 with Column

use of herddb.model.Column in project herddb by diennea.

the class CompiledBinarySQLExpression method scanForConstraintedValueOnColumnWithOperator.

@Override
public List<CompiledSQLExpression> scanForConstraintedValueOnColumnWithOperator(String column, String operator, BindableTableScanColumnNameResolver columnNameResolver) {
    if (!operator.equals(getOperator())) {
        return Collections.emptyList();
    }
    if ((right instanceof ConstantExpression || right instanceof TypedJdbcParameterExpression || right instanceof JdbcParameterExpression) && (left instanceof AccessCurrentRowExpression)) {
        AccessCurrentRowExpression ex = (AccessCurrentRowExpression) left;
        Column colName = columnNameResolver.resolveColumName(ex.getIndex());
        if (column.equals(colName.name)) {
            return Collections.singletonList(right.cast(colName.type));
        }
    }
    return Collections.emptyList();
}
Also used : Column(herddb.model.Column)

Example 60 with Column

use of herddb.model.Column in project herddb by diennea.

the class CompiledBinarySQLExpression method scanForConstraintsOnColumn.

@Override
public List<CompiledSQLExpression> scanForConstraintsOnColumn(String column, BindableTableScanColumnNameResolver columnNameResolver) {
    if ((right instanceof ConstantExpression || right instanceof TypedJdbcParameterExpression || right instanceof JdbcParameterExpression) && (left instanceof AccessCurrentRowExpression)) {
        AccessCurrentRowExpression ex = (AccessCurrentRowExpression) left;
        Column colName = columnNameResolver.resolveColumName(ex.getIndex());
        if (column.equals(colName.name)) {
            return Collections.singletonList(this);
        }
    }
    return Collections.emptyList();
}
Also used : Column(herddb.model.Column)

Aggregations

Column (herddb.model.Column)68 StatementExecutionException (herddb.model.StatementExecutionException)25 ArrayList (java.util.ArrayList)24 Table (herddb.model.Table)23 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)19 DataAccessor (herddb.utils.DataAccessor)18 HashMap (java.util.HashMap)14 PlannerOp (herddb.model.planner.PlannerOp)12 List (java.util.List)12 Bytes (herddb.utils.Bytes)10 AbstractTableManager (herddb.core.AbstractTableManager)9 RecordFunction (herddb.model.RecordFunction)9 RawString (herddb.utils.RawString)9 IOException (java.io.IOException)9 HashSet (java.util.HashSet)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 Test (org.junit.Test)9 DataScanner (herddb.model.DataScanner)8 Tuple (herddb.model.Tuple)8 InsertStatement (herddb.model.commands.InsertStatement)8