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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations