use of herddb.utils.DataAccessor in project herddb by diennea.
the class ThetaJoinOp method resultProjection.
private Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection(String[] fieldNamesFromLeft, String[] fieldNamesFromRight) {
final DataAccessor nullsOnLeft = DataAccessor.ALL_NULLS(fieldNamesFromLeft);
final DataAccessor nullsOnRight = DataAccessor.ALL_NULLS(fieldNamesFromRight);
return (DataAccessor a, DataAccessor b) -> new ConcatenatedDataAccessor(fieldNames, a != null ? a : nullsOnLeft, b != null ? b : nullsOnRight);
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class ThetaJoinOp 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);
transactionContext = new TransactionContext(resLeft.transactionId);
ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
final long resTransactionId = resRight.transactionId;
final String[] fieldNamesFromLeft = resLeft.dataScanner.getFieldNames();
final String[] fieldNamesFromRight = resRight.dataScanner.getFieldNames();
final Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection = resultProjection(fieldNamesFromLeft, fieldNamesFromRight);
Enumerable<DataAccessor> result = EnumerableDefaults.thetaJoin(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), predicate(resultProjection, context), resultProjection, generateNullsOnLeft, generateNullsOnRight);
EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resTransactionId, fieldNames, columns, result);
return new ScanResult(resTransactionId, joinedScanner);
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class CompiledInExpression method evaluate.
@Override
public Object evaluate(herddb.utils.DataAccessor bean, StatementEvaluationContext context) throws StatementExecutionException {
Object leftValue = left.evaluate(bean, context);
boolean res = false;
if (inExpressions != null) {
for (CompiledSQLExpression exp : inExpressions) {
Object expValue = exp.evaluate(bean, context);
if (objectEquals(leftValue, expValue)) {
res = true;
break;
}
}
} else if (inSubSelectPlain != null) {
List<DataAccessor> subQueryResult = context.executeSubquery(inSubSelectPlain);
for (DataAccessor t : subQueryResult) {
String[] fieldNames = t.getFieldNames();
if (fieldNames.length > 1) {
throw new StatementExecutionException("subquery returned more than one column");
}
Object tuple_value = t.get(fieldNames[0]);
if (objectEquals(leftValue, tuple_value)) {
res = true;
break;
}
}
} else {
throw new StatementExecutionException("Internal error");
}
if (not) {
return !res;
} else {
return res;
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method orderByAliasTest.
@Test
public void orderByAliasTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql as tt ORDER BY tt.N1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(2).get("k1"));
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql as tt ORDER BY tt.N1 desc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
assertEquals(RawString.of("mykey"), result.get(2).get("k1"));
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method sortTest.
@Test
public void sortTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
// }
try (DataScanner scan1 = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "k1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey"), result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT n1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
System.out.println("result:" + result.get(0));
assertArrayEquals(new String[] { "n1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(1, result.get(0).get("n1"));
assertEquals(1, result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT n1 FROM tblspace1.tsql ORDER BY n1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "n1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(1, result.get(0).get("n1"));
assertEquals(1, result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey"), result.get(0).get(1));
assertEquals(1, result.get(0).get("n1"));
assertEquals(1, result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY k1 desc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(0).get(1));
assertEquals(3, result.get(0).get("n1"));
assertEquals(3, result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(0).get(1));
assertEquals(3, result.get(0).get("n1"));
assertEquals(3, result.get(0).get(0));
}
try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertArrayEquals(new String[] { "k1", "n1" }, scan1.getFieldNames());
assertEquals(3, result.size());
assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
assertEquals(RawString.of("mykey3"), result.get(0).get(0));
assertEquals(3, result.get(0).get("n1"));
assertEquals(3, result.get(0).get(1));
}
}
}
Aggregations