use of herddb.sql.TranslatedQuery in project herddb by diennea.
the class RunHerdDB070Test method test.
@Test
public void test() throws Exception {
String file = "herddb.070.joinerror.zip";
File dbdatadir = folder.newFolder("dbdata070_" + file);
try (InputStream in = RunHerdDB070Test.class.getResourceAsStream(file)) {
ZIPUtils.unZip(in, dbdatadir);
}
System.out.println("UNZIPPED TO " + dbdatadir);
final Path dbdata = dbdatadir.toPath().resolve("herddb.070.joinerror").resolve("dbdata");
Path metadataPath = dbdata.resolve("metadata");
Path dataPath = dbdata.resolve("data");
Path logsPath = dbdata.resolve("txlog");
Path tmoDir = dbdata.resolve("tmp");
assertTrue(Files.isDirectory(metadataPath));
assertTrue(Files.isDirectory(dataPath));
assertTrue(Files.isDirectory(logsPath));
Path nodeid = dataPath.resolve("nodeid");
assertTrue(Files.isRegularFile(nodeid));
String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
System.out.println("id:" + id);
String expectedNodeId = "asino";
assertTrue(id.endsWith("\n" + expectedNodeId));
try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
final String tableSpace = "herd";
final String tableName = "testtable";
assertEquals(expectedNodeId, manager.getNodeId());
assertTrue(manager.waitForTablespace(tableSpace, 10000));
AbstractTableManager tableManagerLicense = manager.getTableSpaceManager("herd").getTableManager("license");
Table tableLicense = tableManagerLicense.getTable();
System.out.println("TABLE PK: " + Arrays.toString(tableLicense.primaryKey));
for (Column c : tableLicense.columns) {
System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
}
AbstractTableManager tableManagerCustomer = manager.getTableSpaceManager("herd").getTableManager("customer");
Table tableCustomer = tableManagerCustomer.getTable();
System.out.println("TABLE PK: " + Arrays.toString(tableCustomer.primaryKey));
for (Column c : tableCustomer.columns) {
System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(15, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM customer", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(7, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT t0.license_id,c.customer_id FROM license t0, customer c WHERE c.customer_id = 3 AND t0.customer_id = 3 AND c.customer_id = t0.customer_id\n" + " ", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(9, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license t0, customer c WHERE c.customer_id = t0.customer_id", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(15, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
}
}
use of herddb.sql.TranslatedQuery in project herddb by diennea.
the class UpgradeFrom050WithBrinIndexesTest method test.
private void test(TestCase tcase, final DBManager manager, final String tableSpace) throws StatementExecutionException, DataScannerException {
System.out.println("QUERY: " + tcase.query);
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, tcase.query, Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
int size = 0;
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
for (DataAccessor r : scan1.consume()) {
System.out.println("FOUND " + r.toMap());
size++;
}
}
System.out.println("SIZE: " + size);
IndexOperation indexOperation = scan.getPredicate().getIndexOperation();
System.out.println("OPERATION: " + indexOperation);
assertTrue(tcase.expectedIndexAccessType.isAssignableFrom(scan.getPredicate().getIndexOperation().getClass()));
assertEquals(tcase.expectedRecordCount, size);
}
use of herddb.sql.TranslatedQuery in project herddb by diennea.
the class UpgradeFrom050WithBrinIndexesTest method test.
private void test(String file) throws Exception {
File dbdatadir = folder.newFolder("dbdata050_" + file);
try (InputStream in = UpgradeFrom050WithBrinIndexesTest.class.getResourceAsStream(file)) {
ZIPUtils.unZip(in, dbdatadir);
}
final Path dbdata = dbdatadir.toPath().resolve("dbdata");
Path metadataPath = dbdata.resolve("metadata");
Path dataPath = dbdata.resolve("data");
Path logsPath = dbdata.resolve("txlog");
Path tmoDir = dbdata.resolve("tmp");
assertTrue(Files.isDirectory(metadataPath));
assertTrue(Files.isDirectory(dataPath));
assertTrue(Files.isDirectory(logsPath));
Path nodeid = dataPath.resolve("nodeid");
assertTrue(Files.isRegularFile(nodeid));
String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
System.out.println("id:" + id);
String expectedNodeId = "capra";
assertTrue(id.endsWith("\n" + expectedNodeId));
try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
final String tableSpace = "herd";
final String tableName = "testtable";
assertEquals(expectedNodeId, manager.getNodeId());
assertTrue(manager.waitForTablespace(tableSpace, 10000));
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
List<Index> indexes = tableManager.getAvailableIndexes();
for (Index e : indexes) {
System.out.println("INDEX: " + e);
assertEquals(e.type, Index.TYPE_BRIN);
}
assertEquals(4, indexes.size());
for (Column c : tableManager.getTable().getColumns()) {
System.out.println("COLUMN :" + c);
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM " + tableName + " ORDER BY pk,n1,n2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
for (DataAccessor r : scan1.consume()) {
System.out.println("RECORD " + r.toMap());
}
}
}
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1", SecondaryIndexSeek.class, 4), manager, tableSpace);
// this could be SecondaryIndexSeek but we have more indexes and the planner is not so smart
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2=3", SecondaryIndexPrefixScan.class, 2), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2>=3", SecondaryIndexRangeScan.class, 3), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1 and n2=3", SecondaryIndexPrefixScan.class, 1), manager, tableSpace);
}
}
use of herddb.sql.TranslatedQuery in project herddb by diennea.
the class ServerSideConnectionPeer method executeUpdate.
/**
* This method is like {@link #handleExecuteStatement(herddb.proto.Pdu, herddb.network.Channel) } but in "local" mode,
* we do not want here to marshal/unmarshal values, in order to save resources
*/
public DMLResult executeUpdate(String tablespace, String query, long txId, boolean returnValues, List<Object> parameters) throws HDBException {
// ensure we are dealing with the same data types that we see when the request id coming from the wire
parameters = PduCodec.normalizeParametersList(parameters);
TransactionContext transactionContext = new TransactionContext(txId);
TranslatedQuery translatedQuery;
try {
translatedQuery = server.getManager().getPlanner().translate(tablespace, query, parameters, false, true, returnValues, -1);
Statement statement = translatedQuery.plan.mainStatement;
CompletableFuture<StatementExecutionResult> res = server.getManager().executePlanAsync(translatedQuery.plan, translatedQuery.context, transactionContext);
CompletableFuture<DMLResult> finalResult = res.handle((result, err) -> {
if (err != null) {
while (err instanceof CompletionException) {
err = err.getCause();
}
if (err instanceof DuplicatePrimaryKeyException) {
throw new CompletionException(new SQLIntegrityConstraintViolationException(err));
} else {
throw new CompletionException(new SQLException(err));
}
}
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
if (returnValues && dml.getKey() != null) {
TableAwareStatement tableStatement = statement.unwrap(TableAwareStatement.class);
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
final Map<RawString, Object> newRecord = new HashMap<>();
Object newKey = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
newRecord.put(RAWSTRING_KEY, newKey);
if (dml.getNewvalue() != null) {
Map<String, Object> toBean = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
toBean.forEach((k, v) -> {
newRecord.put(RawString.of(k), v);
});
}
return new DMLResult(dml.getUpdateCount(), newKey, newRecord, dml.transactionId);
} else {
return new DMLResult(dml.getUpdateCount(), null, null, dml.transactionId);
}
} else if (result instanceof DDLStatementExecutionResult) {
return new DMLResult(1, null, null, result.transactionId);
} else {
throw new CompletionException(new SQLException("Unknown result type " + result.getClass() + ": " + result));
}
});
return finalResult.get();
} catch (Throwable err) {
while (err instanceof CompletionException) {
err = err.getCause();
}
throw new HDBException(err);
}
}
use of herddb.sql.TranslatedQuery in project herddb by diennea.
the class ServerSideConnectionPeer method handleExecuteStatements.
private void handleExecuteStatements(Pdu message, Channel channel) {
long transactionId = PduCodec.ExecuteStatements.readTx(message);
String tableSpace = PduCodec.ExecuteStatements.readTablespace(message);
long statementId = PduCodec.ExecuteStatements.readStatementId(message);
String query = statementId > 0 ? preparedStatements.resolveQuery(tableSpace, statementId) : PduCodec.ExecuteStatements.readQuery(message);
if (query == null) {
ByteBuf error = PduCodec.ErrorResponse.writeMissingPreparedStatementError(message.messageId, "bad statement id: " + statementId);
channel.sendReplyMessage(message.messageId, error);
message.close();
return;
}
boolean returnValues = PduCodec.ExecuteStatements.readReturnValues(message);
PduCodec.ListOfListsReader statementParameters = PduCodec.ExecuteStatements.startReadStatementsParameters(message);
int numStatements = statementParameters.getNumLists();
List<List<Object>> batch = new ArrayList<>(numStatements);
for (int i = 0; i < numStatements; i++) {
PduCodec.ObjectListReader parametersReader = statementParameters.nextList();
List<Object> batchParams = new ArrayList<>(parametersReader.getNumParams());
for (int j = 0; j < parametersReader.getNumParams(); j++) {
batchParams.add(parametersReader.nextObject());
}
batch.add(batchParams);
}
RunningStatementsStats runningStatements = server.getManager().getRunningStatements();
RunningStatementInfo statementInfo = new RunningStatementInfo(query, System.currentTimeMillis(), tableSpace, "", numStatements);
try {
List<TranslatedQuery> queries = new ArrayList<>();
for (int i = 0; i < numStatements; i++) {
List<Object> parameters = batch.get(i);
TranslatedQuery translatedQuery = server.getManager().getPlanner().translate(tableSpace, query, parameters, false, true, returnValues, -1);
queries.add(translatedQuery);
}
List<Long> updateCounts = new CopyOnWriteArrayList<>();
List<Map<String, Object>> otherDatas = new CopyOnWriteArrayList<>();
class ComputeNext implements BiConsumer<StatementExecutionResult, Throwable> {
int current;
public ComputeNext(int current) {
this.current = current;
}
@Override
public void accept(StatementExecutionResult result, Throwable error) {
if (error != null) {
ByteBuf errorMsg = composeErrorResponse(message.messageId, error);
channel.sendReplyMessage(message.messageId, errorMsg);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
return;
}
if (result instanceof DMLStatementExecutionResult) {
DMLStatementExecutionResult dml = (DMLStatementExecutionResult) result;
Map<String, Object> otherData = Collections.emptyMap();
if (returnValues && dml.getKey() != null) {
TranslatedQuery translatedQuery = queries.get(current - 1);
Statement statement = translatedQuery.plan.mainStatement;
TableAwareStatement tableStatement = (TableAwareStatement) statement;
Table table = server.getManager().getTableSpaceManager(statement.getTableSpace()).getTableManager(tableStatement.getTable()).getTable();
Object key = RecordSerializer.deserializePrimaryKey(dml.getKey(), table);
otherData = new HashMap<>();
otherData.put("_key", key);
if (dml.getNewvalue() != null) {
Map<String, Object> newvalue = RecordSerializer.toBean(new Record(dml.getKey(), dml.getNewvalue()), table);
otherData.putAll(newvalue);
}
}
updateCounts.add((long) dml.getUpdateCount());
otherDatas.add(otherData);
} else if (result instanceof DDLStatementExecutionResult) {
Map<String, Object> otherData = Collections.emptyMap();
updateCounts.add(1L);
otherDatas.add(otherData);
} else {
ByteBuf response = PduCodec.ErrorResponse.write(message.messageId, "bad result type " + result.getClass() + " (" + result + ")");
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
return;
}
long newTransactionId = result.transactionId;
if (current == queries.size()) {
try {
ByteBuf response = PduCodec.ExecuteStatementsResult.write(message.messageId, updateCounts, otherDatas, newTransactionId);
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Internal error", t);
}
return;
}
TranslatedQuery nextPlannedQuery = queries.get(current);
TransactionContext transactionContext = new TransactionContext(newTransactionId);
CompletableFuture<StatementExecutionResult> nextPromise = server.getManager().executePlanAsync(nextPlannedQuery.plan, nextPlannedQuery.context, transactionContext);
nextPromise.whenComplete(new ComputeNext(current + 1));
}
}
TransactionContext transactionContext = new TransactionContext(transactionId);
TranslatedQuery firstTranslatedQuery = queries.get(0);
server.getManager().executePlanAsync(firstTranslatedQuery.plan, firstTranslatedQuery.context, transactionContext).whenComplete(new ComputeNext(1));
} catch (HerdDBInternalException err) {
ByteBuf response = composeErrorResponse(message.messageId, err);
channel.sendReplyMessage(message.messageId, response);
message.close();
runningStatements.unregisterRunningStatement(statementInfo);
}
}
Aggregations