Search in sources :

Example 6 with DataAccessor

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);
}
Also used : DataAccessor(herddb.utils.DataAccessor) AbstractDataAccessor(herddb.utils.AbstractDataAccessor)

Example 7 with DataAccessor

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);
}
Also used : ScanResult(herddb.model.ScanResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor) AbstractDataAccessor(herddb.utils.AbstractDataAccessor)

Example 8 with DataAccessor

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;
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StatementExecutionException(herddb.model.StatementExecutionException)

Example 9 with DataAccessor

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"));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 10 with DataAccessor

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));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

DataAccessor (herddb.utils.DataAccessor)164 DataScanner (herddb.model.DataScanner)118 Test (org.junit.Test)110 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)83 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)82 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)82 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)76 RawString (herddb.utils.RawString)70 DBManager (herddb.core.DBManager)52 TransactionContext (herddb.model.TransactionContext)44 StatementExecutionException (herddb.model.StatementExecutionException)38 Table (herddb.model.Table)34 List (java.util.List)31 DataScannerException (herddb.model.DataScannerException)28 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)27 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)27 Column (herddb.model.Column)22 ScanResult (herddb.model.ScanResult)22 ScanStatement (herddb.model.commands.ScanStatement)22 Bytes (herddb.utils.Bytes)22