Search in sources :

Example 26 with Column

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

the class CalcitePlanner method buildProjection.

private Projection buildProjection(final List<RexNode> projects, final RelDataType rowType, final boolean allowIdentity, final Column[] tableSchema) {
    boolean allowZeroCopyProjection = true;
    List<CompiledSQLExpression> fields = new ArrayList<>(projects.size());
    Column[] columns = new Column[projects.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    int[] zeroCopyProjections = new int[fieldNames.length];
    boolean identity = allowIdentity && tableSchema != null && tableSchema.length == fieldNames.length;
    for (RexNode node : projects) {
        CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
        if (exp instanceof AccessCurrentRowExpression) {
            AccessCurrentRowExpression accessCurrentRowExpression = (AccessCurrentRowExpression) exp;
            int mappedIndex = accessCurrentRowExpression.getIndex();
            zeroCopyProjections[i] = mappedIndex;
            if (i != mappedIndex) {
                identity = false;
            }
        } else {
            allowZeroCopyProjection = false;
        }
        fields.add(exp);
        Column col = Column.column(rowType.getFieldNames().get(i).toLowerCase(), convertToHerdType(node.getType()));
        identity = identity && col.name.equals(tableSchema[i].name);
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    if (allowZeroCopyProjection) {
        if (identity) {
            return Projection.IDENTITY(fieldNames, columns);
        }
        return new ProjectOp.ZeroCopyProjection(fieldNames, columns, zeroCopyProjections);
    } else {
        return new ProjectOp.BasicProjection(fieldNames, columns, fields);
    }
}
Also used : ArrayList(java.util.ArrayList) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Column(herddb.model.Column) RexNode(org.apache.calcite.rex.RexNode)

Example 27 with Column

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

the class JSQLParserPlanner method buildAlterStatement.

private Statement buildAlterStatement(String defaultTableSpace, Alter alter) throws StatementExecutionException {
    if (alter.getTable() == null) {
        throw new StatementExecutionException("missing table name");
    }
    String tableSpace = alter.getTable().getSchemaName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    tableSpace = fixMySqlBackTicks(tableSpace);
    List<Column> addColumns = new ArrayList<>();
    List<Column> modifyColumns = new ArrayList<>();
    List<String> dropColumns = new ArrayList<>();
    List<String> dropForeignKeys = new ArrayList<>();
    List<ForeignKeyDef> addForeignKeys = new ArrayList<>();
    String tableName = fixMySqlBackTicks(alter.getTable().getName().toLowerCase());
    if (alter.getAlterExpressions() == null || alter.getAlterExpressions().size() != 1) {
        throw new StatementExecutionException("supported multi-alter operation '" + alter + "'");
    }
    AlterExpression alterExpression = alter.getAlterExpressions().get(0);
    AlterOperation operation = alterExpression.getOperation();
    Boolean changeAutoIncrement = null;
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
    if (tableSpaceManager == null) {
        throw new StatementExecutionException("bad tablespace '" + tableSpace + "'");
    }
    Table table = getTable(defaultTableSpace, alter.getTable());
    switch(operation) {
        case ADD:
            {
                if (alterExpression.getColDataTypeList() != null) {
                    List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
                    for (AlterExpression.ColumnDataType cl : cols) {
                        List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
                        int type = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
                        Column newColumn = Column.column(fixMySqlBackTicks(cl.getColumnName()), type, decodeDefaultValue(cl, type));
                        addColumns.add(newColumn);
                    }
                } else if (alterExpression.getIndex() != null && alterExpression.getIndex() instanceof ForeignKeyIndex) {
                    ForeignKeyDef fkIndex = parseForeignKeyIndex((ForeignKeyIndex) alterExpression.getIndex(), table, tableName, tableSpace);
                    addForeignKeys.add(fkIndex);
                } else {
                    throw new StatementExecutionException("Unrecognized ALTER TABLE ADD ... statement");
                }
            }
            break;
        case DROP:
            if (alterExpression.getColumnName() != null) {
                dropColumns.add(fixMySqlBackTicks(alterExpression.getColumnName()));
            } else if (alterExpression.getConstraintName() != null) {
                dropForeignKeys.add(fixMySqlBackTicks(alterExpression.getConstraintName()));
            } else {
                throw new StatementExecutionException("Unrecognized ALTER TABLE DROP ... statement");
            }
            break;
        case MODIFY:
            {
                List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
                for (AlterExpression.ColumnDataType cl : cols) {
                    String columnName = fixMySqlBackTicks(cl.getColumnName().toLowerCase());
                    Column oldColumn = table.getColumn(columnName);
                    if (oldColumn == null) {
                        throw new StatementExecutionException("bad column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
                    }
                    Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(tableName);
                    if (indexes != null) {
                        for (AbstractIndexManager am : indexes.values()) {
                            for (String indexedColumn : am.getColumnNames()) {
                                indexedColumn = fixMySqlBackTicks(indexedColumn);
                                if (indexedColumn.equalsIgnoreCase(oldColumn.name)) {
                                    throw new StatementExecutionException("cannot alter indexed " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'," + "index name is " + am.getIndexName());
                                }
                            }
                        }
                    }
                    List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
                    int newType = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
                    if (oldColumn.type != newType) {
                        if (ColumnTypes.isNotNullToNullConversion(oldColumn.type, newType)) {
                        // allow change from "STRING NOT NULL" to "STRING NULL"
                        } else if (ColumnTypes.isNullToNotNullConversion(oldColumn.type, newType)) {
                        // allow change from "STRING NULL" to "STRING NOT NULL"
                        // it will require a check on table at execution time
                        } else {
                            throw new StatementExecutionException("cannot change datatype to " + ColumnTypes.typeToString(newType) + " for column " + columnName + " (" + ColumnTypes.typeToString(oldColumn.type) + ") in table " + tableName + " in tablespace '" + tableSpace + "'");
                        }
                    }
                    if (table.isPrimaryKeyColumn(columnName)) {
                        boolean new_auto_increment = decodeAutoIncrement(columnSpecs);
                        if (new_auto_increment && table.primaryKey.length > 1) {
                            throw new StatementExecutionException("cannot add auto_increment flag to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
                        }
                        if (table.auto_increment != new_auto_increment) {
                            changeAutoIncrement = new_auto_increment;
                        }
                    }
                    Bytes newDefault = oldColumn.defaultValue;
                    if (containsDefaultClause(cl)) {
                        newDefault = decodeDefaultValue(cl, newType);
                    }
                    Column newColumnDef = Column.column(columnName, newType, oldColumn.serialPosition, newDefault);
                    modifyColumns.add(newColumnDef);
                }
            }
            break;
        case CHANGE:
            {
                String columnName = alterExpression.getColOldName();
                List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
                if (cols.size() != 1) {
                    throw new StatementExecutionException("bad CHANGE column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
                }
                AlterExpression.ColumnDataType cl = cols.get(0);
                Column oldColumn = table.getColumn(columnName);
                if (oldColumn == null) {
                    throw new StatementExecutionException("bad column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
                }
                Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(tableName);
                if (indexes != null) {
                    for (AbstractIndexManager am : indexes.values()) {
                        for (String indexedColumn : am.getColumnNames()) {
                            indexedColumn = fixMySqlBackTicks(indexedColumn);
                            if (indexedColumn.equalsIgnoreCase(oldColumn.name)) {
                                throw new StatementExecutionException("cannot alter indexed " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'," + "index name is " + am.getIndexName());
                            }
                        }
                    }
                }
                List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
                int newType = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
                if (oldColumn.type != newType) {
                    throw new StatementExecutionException("cannot change datatype to " + ColumnTypes.typeToString(newType) + " for column " + columnName + " (" + ColumnTypes.typeToString(oldColumn.type) + ") in table " + tableName + " in tablespace '" + tableSpace + "'");
                }
                if (table.isPrimaryKeyColumn(columnName)) {
                    boolean new_auto_increment = decodeAutoIncrement(columnSpecs);
                    if (new_auto_increment && table.primaryKey.length > 1) {
                        throw new StatementExecutionException("cannot add auto_increment flag to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
                    }
                    if (table.auto_increment != new_auto_increment) {
                        changeAutoIncrement = new_auto_increment;
                    }
                }
                String renameTo = fixMySqlBackTicks(cl.getColumnName().toLowerCase());
                if (renameTo != null) {
                    columnName = renameTo;
                }
                Column newColumnDef = Column.column(columnName, newType, oldColumn.serialPosition, oldColumn.defaultValue);
                modifyColumns.add(newColumnDef);
            }
            break;
        default:
            throw new StatementExecutionException("supported alter operation '" + alter + "'");
    }
    return new AlterTableStatement(addColumns, modifyColumns, dropColumns, changeAutoIncrement, tableName.toLowerCase(), tableSpace, null, dropForeignKeys, addForeignKeys);
}
Also used : AlterOperation(net.sf.jsqlparser.statement.alter.AlterOperation) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) AbstractIndexManager(herddb.core.AbstractIndexManager) AlterTableStatement(herddb.model.commands.AlterTableStatement) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) Bytes(herddb.utils.Bytes) Column(herddb.model.Column) TableSpaceManager(herddb.core.TableSpaceManager) 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) ForeignKeyDef(herddb.model.ForeignKeyDef) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) Map(java.util.Map)

Example 28 with Column

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

the class CalcitePlanner method planValues.

private PlannerOp planValues(EnumerableValues op) {
    List<List<CompiledSQLExpression>> tuples = new ArrayList<>(op.getTuples().size());
    RelDataType rowType = op.getRowType();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    for (ImmutableList<RexLiteral> tuple : op.getTuples()) {
        List<CompiledSQLExpression> row = new ArrayList<>(tuple.size());
        for (RexLiteral node : tuple) {
            CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
            row.add(exp);
        }
        tuples.add(row);
    }
    int i = 0;
    String[] fieldNames = new String[fieldList.size()];
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName(), convertToHerdType(field.getType()));
        fieldNames[i] = field.getName();
        columns[i++] = col;
    }
    return new ValuesOp(manager.getNodeId(), fieldNames, columns, tuples);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) ValuesOp(herddb.model.planner.ValuesOp) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Column(herddb.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 29 with Column

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

the class CalcitePlanner method planAggregate.

private PlannerOp planAggregate(EnumerableAggregate op, RelDataType rowType, boolean returnValues) {
    List<RelDataTypeField> fieldList = op.getRowType().getFieldList();
    List<AggregateCall> calls = op.getAggCallList();
    String[] fieldnames = new String[fieldList.size()];
    String[] aggtypes = new String[calls.size()];
    Column[] columns = new Column[fieldList.size()];
    List<Integer> groupedFiledsIndexes = op.getGroupSet().toList();
    List<List<Integer>> argLists = new ArrayList<>(calls.size());
    int i = 0;
    int idaggcall = 0;
    for (RelDataTypeField c : fieldList) {
        int type = convertToHerdType(c.getType());
        Column co = Column.column(c.getName(), type);
        columns[i] = co;
        fieldnames[i] = c.getName().toLowerCase();
        i++;
    }
    for (AggregateCall call : calls) {
        aggtypes[idaggcall++] = call.getAggregation().getName();
        argLists.add(call.getArgList());
    }
    PlannerOp input = convertRelNode(op.getInput(), null, returnValues, false);
    return new AggregateOp(input, fieldnames, columns, aggtypes, argLists, groupedFiledsIndexes);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) AggregateOp(herddb.model.planner.AggregateOp) Column(herddb.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 30 with Column

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

the class CalcitePlanner method planEnumerableNestedLoopJoin.

private PlannerOp planEnumerableNestedLoopJoin(EnumerableNestedLoopJoin op, RelDataType rowType) {
    PlannerOp left = convertRelNode(op.getLeft(), null, false, false);
    PlannerOp right = convertRelNode(op.getRight(), null, false, false);
    CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(op.getCondition());
    final RelDataType _rowType = rowType == null ? op.getRowType() : rowType;
    List<RelDataTypeField> fieldList = _rowType.getFieldList();
    Column[] columns = new Column[fieldList.size()];
    String[] fieldNames = new String[columns.length];
    int i = 0;
    for (RelDataTypeField field : fieldList) {
        Column col = Column.column(field.getName().toLowerCase(), convertToHerdType(field.getType()));
        fieldNames[i] = col.name;
        columns[i++] = col;
    }
    return new NestedLoopJoinOp(fieldNames, columns, left, right, condition, op.getJoinType(), false);
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) PlannerOp(herddb.model.planner.PlannerOp) Column(herddb.model.Column) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) NestedLoopJoinOp(herddb.model.planner.NestedLoopJoinOp)

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