use of herddb.model.StatementExecutionException 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.StatementExecutionException in project herddb by diennea.
the class FilterOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
try {
// TODO merge projection + scan + sort + limit
StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
ScanResult downstreamScanResult = (ScanResult) input;
final DataScanner inputScanner = downstreamScanResult.dataScanner;
FilteredDataScanner filtered = new FilteredDataScanner(inputScanner, condition, context);
return new ScanResult(downstreamScanResult.transactionId, filtered);
} catch (DataScannerException ex) {
throw new StatementExecutionException(ex);
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class NestedLoopJoinOp method execute.
@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
DataScanner leftScanner = resLeft.dataScanner;
transactionContext = new TransactionContext(resLeft.transactionId);
ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
DataScanner rightScanner = resRight.dataScanner;
final JoinType linq4jJoinType = CalciteEnumUtils.toLinq4jJoinType(joinRelType);
if (!linq4jJoinType.generatesNullsOnLeft() && !rightScanner.isRewindSupported()) {
try {
MaterializedRecordSet recordSet = tableSpaceManager.getDbmanager().getRecordSetFactory().createRecordSet(rightScanner.getFieldNames(), rightScanner.getSchema());
rightScanner.forEach(d -> {
recordSet.add(d);
});
recordSet.writeFinished();
SimpleDataScanner materialized = new SimpleDataScanner(rightScanner.getTransaction(), recordSet);
rightScanner.close();
rightScanner = materialized;
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
}
final long resTransactionId = resRight.transactionId;
final String[] fieldNamesFromLeft = leftScanner.getFieldNames();
final String[] fieldNamesFromRight = rightScanner.getFieldNames();
final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
Enumerable<DataAccessor> result = EnumerableDefaults.nestedLoopJoin(leftScanner.createNonRewindableEnumerable(), rightScanner.createRewindOnCloseEnumerable(), predicate(resultProjection, context), resultProjection, linq4jJoinType);
EnumerableDataScanner joinedScanner = new EnumerableDataScanner(rightScanner.getTransaction(), fieldNames, columns, result, leftScanner, rightScanner);
return new ScanResult(resTransactionId, joinedScanner);
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class BackupRestoreTest method test_backup_restore_recover_from_tx_log_with_newtransaction_during_dump.
@Test
public void test_backup_restore_recover_from_tx_log_with_newtransaction_during_dump() throws Exception {
ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
try (Server server_1 = new Server(serverconfig_1)) {
server_1.start();
server_1.waitForStandaloneBoot();
Table table1 = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
Index index = Index.builder().onTable(table1).column("d", ColumnTypes.INTEGER).type(Index.TYPE_BRIN).build();
Table table2 = Table.builder().name("t2").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
server_1.getManager().executeStatement(new CreateTableStatement(table1), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeStatement(new CreateTableStatement(table2), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeStatement(new CreateIndexStatement(index), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 1, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 3, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 4, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
try (Server server_2 = new Server(serverconfig_2)) {
server_2.start();
try (HDBClient client = new HDBClient(client_configuration);
HDBConnection connection = client.openConnection()) {
assertEquals(4, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
ByteArrayOutputStream oo = new ByteArrayOutputStream();
BackupUtils.dumpTableSpace(TableSpace.DEFAULT, 64 * 1024, connection, oo, new ProgressListener() {
@Override
public void log(String type, String message, Map<String, Object> context) {
System.out.println("PROGRESS: " + type + " " + message + " context:" + context);
if (type.equals("beginTable") && "t1".equals(context.get("table"))) {
try {
long tx1 = TestUtils.beginTransaction(server_1.getManager(), TableSpace.DEFAULT);
server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 5, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx1));
TestUtils.commitTransaction(server_1.getManager(), TableSpace.DEFAULT, tx1);
} catch (StatementExecutionException err) {
throw new RuntimeException(err);
}
}
}
});
byte[] backupData = oo.toByteArray();
connection.executeUpdate(TableSpace.DEFAULT, "DELETE FROM t1", 0, false, true, Collections.emptyList());
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
BackupUtils.restoreTableSpace("newts", server_2.getNodeId(), connection, new ByteArrayInputStream(backupData), new ProgressListener() {
});
assertEquals(5, connection.executeScan("newts", "SELECT * FROM newts.t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
assertEquals(1, server_2.getManager().getTableSpaceManager("newts").getIndexesOnTable("t1").size());
}
}
}
}
use of herddb.model.StatementExecutionException in project herddb by diennea.
the class SQLParserExpressionCompiler method compileBinaryExpression.
private static CompiledSQLExpression compileBinaryExpression(BinaryExpression expression, OpSchema tableSchema) {
CompiledSQLExpression left = compileExpression(expression.getLeftExpression(), tableSchema);
CompiledSQLExpression right = compileExpression(expression.getRightExpression(), tableSchema);
if (expression instanceof EqualsTo) {
EqualsTo eq = (EqualsTo) expression;
checkSupported(eq.getOldOracleJoinSyntax() == EqualsTo.NO_ORACLE_JOIN);
checkSupported(eq.getOraclePriorPosition() == EqualsTo.NO_ORACLE_PRIOR);
return new CompiledEqualsExpression(left, right);
} else if (expression instanceof AndExpression) {
return new CompiledAndExpression(left, right);
} else if (expression instanceof OrExpression) {
return new CompiledOrExpression(left, right);
} else if (expression instanceof GreaterThan) {
return new CompiledGreaterThanExpression(left, right);
} else if (expression instanceof GreaterThanEquals) {
return new CompiledGreaterThanEqualsExpression(left, right);
} else if (expression instanceof MinorThan) {
return new CompiledMinorThanExpression(left, right);
} else if (expression instanceof MinorThanEquals) {
return new CompiledMinorThanEqualsExpression(left, right);
} else if (expression instanceof Addition) {
return new CompiledAddExpression(left, right);
} else if (expression instanceof Division) {
return new CompiledDivideExpression(left, right);
} else if (expression instanceof Multiplication) {
return new CompiledMultiplyExpression(left, right);
} else if (expression instanceof LikeExpression) {
LikeExpression eq = (LikeExpression) expression;
if (eq.isCaseInsensitive()) {
// this is not supported by Calcite, do not support it with jSQLParser
throw new StatementExecutionException("ILIKE is not supported");
}
CompiledSQLExpression res;
if (eq.getEscape() != null) {
CompiledSQLExpression escape = new ConstantExpression(eq.getEscape(), ColumnTypes.NOTNULL_STRING);
res = new CompiledLikeExpression(left, right, escape);
} else {
res = new CompiledLikeExpression(left, right);
}
if (eq.isNot()) {
res = new CompiledNotExpression(res);
}
return res;
} else if (expression instanceof Modulo) {
return new CompiledModuloExpression(left, right);
} else if (expression instanceof Subtraction) {
return new CompiledSubtractExpression(left, right);
} else if (expression instanceof NotEqualsTo) {
NotEqualsTo eq = (NotEqualsTo) expression;
checkSupported(eq.getOldOracleJoinSyntax() == EqualsTo.NO_ORACLE_JOIN);
checkSupported(eq.getOraclePriorPosition() == EqualsTo.NO_ORACLE_PRIOR);
return new CompiledNotEqualsExpression(left, right);
} else {
throw new StatementExecutionException("not implemented expression type " + expression.getClass() + ": " + expression);
}
}
Aggregations