Search in sources :

Example 91 with Table

use of herddb.model.Table in project herddb by diennea.

the class LocalTableSnapshotRecoveryTest method test_1.

@Test
public void test_1() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace(TableSpace.DEFAULT, 10000);
        Table table = Table.builder().tablespace(TableSpace.DEFAULT).name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TestUtils.execute(manager, "INSERT INTO t1(id,name) values(?,?)", Arrays.asList("a", "b"));
        manager.getTableSpaceManager(TableSpace.DEFAULT).checkpoint(false, false, false);
        TestUtils.execute(manager, "INSERT INTO t1(id,name) values(?,?)", Arrays.asList("c", "d"));
        manager.getTableSpaceManager(TableSpace.DEFAULT).getTableManager("t1").flush();
        TestUtils.execute(manager, "INSERT INTO t1(id,name) values(?,?)", Arrays.asList("e", "f"));
        try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM t1", Collections.emptyList())) {
            assertEquals(3, scan.consume().size());
        }
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        manager.waitForTablespace(TableSpace.DEFAULT, 10000);
        try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM t1", Collections.emptyList())) {
            assertEquals(3, scan.consume().size());
        }
    }
}
Also used : Path(java.nio.file.Path) FileCommitLogManager(herddb.file.FileCommitLogManager) Table(herddb.model.Table) DataScanner(herddb.model.DataScanner) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) FileDataStorageManager(herddb.file.FileDataStorageManager) Test(org.junit.Test)

Example 92 with Table

use of herddb.model.Table in project herddb by diennea.

the class PrimaryIndexScanRangeTest method primaryIndexPrefixScanTest.

@Test
public void primaryIndexPrefixScanTest() 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);
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("n1", ColumnTypes.INTEGER).column("n2", ColumnTypes.INTEGER).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("n1").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('a',1,5,'n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('b',2,5,'n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('c',3,6,'n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('d',4,7,'n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,n1,n2,name) values('e',5,5,'n2')", Collections.emptyList());
        performBasicPlannerTests(manager);
        assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("t1").isKeyToPageSortedAscending());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 93 with Table

use of herddb.model.Table in project herddb by diennea.

the class CalcitePlanner method planBindableTableScan.

private PlannerOp planBindableTableScan(BindableTableScan scan, RelDataType rowType) {
    if (rowType == null) {
        rowType = scan.getRowType();
    }
    final String tableSpace = scan.getTable().getQualifiedName().get(0);
    final TableImpl tableImpl = (TableImpl) scan.getTable().unwrap(org.apache.calcite.schema.Table.class);
    Table table = tableImpl.tableManager.getTable();
    SQLRecordPredicate predicate = null;
    if (!scan.filters.isEmpty()) {
        CompiledSQLExpression where = null;
        if (scan.filters.size() == 1) {
            RexNode expr = scan.filters.get(0);
            where = SQLExpressionCompiler.compileExpression(expr);
        } else {
            CompiledSQLExpression[] operands = new CompiledSQLExpression[scan.filters.size()];
            int i = 0;
            for (RexNode expr : scan.filters) {
                CompiledSQLExpression condition = SQLExpressionCompiler.compileExpression(expr);
                operands[i++] = condition;
            }
            where = new CompiledMultiAndExpression(operands);
        }
        predicate = new SQLRecordPredicate(table, null, where);
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        IndexUtils.discoverIndexOperations(tableSpace, where, table, predicate, scan, tableSpaceManager);
    }
    List<RexNode> projections = new ArrayList<>(scan.projects.size());
    int i = 0;
    for (int fieldpos : scan.projects) {
        projections.add(new RexInputRef(fieldpos, rowType.getFieldList().get(i++).getType()));
    }
    Projection projection = buildProjection(projections, rowType, true, table.columns);
    ScanStatement scanStatement = new ScanStatement(tableSpace, table.name, projection, predicate, null, null);
    scanStatement.setTableDef(table);
    return new BindableTableScanOp(scanStatement);
}
Also used : CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) ArrayList(java.util.ArrayList) Projection(herddb.model.Projection) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TableSpaceManager(herddb.core.TableSpaceManager) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode) ScanStatement(herddb.model.commands.ScanStatement) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Example 94 with Table

use of herddb.model.Table in project herddb by diennea.

the class CalcitePlanner method planDelete.

private PlannerOp planDelete(EnumerableTableModify dml) {
    PlannerOp input = convertRelNode(dml.getInput(), null, false, false);
    final String tableSpace = dml.getTable().getQualifiedName().get(0);
    final String tableName = dml.getTable().getQualifiedName().get(1);
    final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
    Table table = tableImpl.tableManager.getTable();
    DeleteStatement delete = null;
    if (input instanceof TableScanOp) {
        delete = new DeleteStatement(tableSpace, tableName, null, null);
    } else if (input instanceof FilterOp) {
        FilterOp filter = (FilterOp) input;
        if (filter.getInput() instanceof TableScanOp) {
            SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
            delete = new DeleteStatement(tableSpace, tableName, null, pred);
        }
    } else if (input instanceof BindableTableScanOp) {
        BindableTableScanOp filter = (BindableTableScanOp) input;
        Predicate pred = filter.getStatement().getPredicate();
        delete = new DeleteStatement(tableSpace, tableName, null, pred);
    }
    if (delete != null) {
        return new SimpleDeleteOp(delete);
    } else {
        return new DeleteOp(tableSpace, tableName, input);
    }
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) FilteredTableScanOp(herddb.model.planner.FilteredTableScanOp) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) TableScanOp(herddb.model.planner.TableScanOp) FilterOp(herddb.model.planner.FilterOp) DeleteStatement(herddb.model.commands.DeleteStatement) Predicate(herddb.model.Predicate) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) DeleteOp(herddb.model.planner.DeleteOp) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Example 95 with Table

use of herddb.model.Table in project herddb by diennea.

the class CalcitePlanner method planUpdate.

private PlannerOp planUpdate(EnumerableTableModify dml, boolean returnValues) {
    PlannerOp input = convertRelNode(dml.getInput(), null, false, false);
    List<String> updateColumnList = dml.getUpdateColumnList();
    List<RexNode> sourceExpressionList = dml.getSourceExpressionList();
    final String tableSpace = dml.getTable().getQualifiedName().get(0);
    final String tableName = dml.getTable().getQualifiedName().get(1);
    final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
    Table table = tableImpl.tableManager.getTable();
    List<CompiledSQLExpression> expressionsForValue = new ArrayList<>(sourceExpressionList.size());
    List<CompiledSQLExpression> expressionsForKey = new ArrayList<>(sourceExpressionList.size());
    List<String> updateColumnListInValue = new ArrayList<>(updateColumnList.size());
    List<String> updateColumnListInPk = new ArrayList<>();
    for (int i = 0; i < updateColumnList.size(); i++) {
        String columnName = updateColumnList.get(i);
        boolean isPk = table.isPrimaryKeyColumn(columnName);
        RexNode node = sourceExpressionList.get(i);
        CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
        if (isPk) {
            updateColumnListInPk.add(columnName);
            expressionsForKey.add(exp);
        } else {
            updateColumnListInValue.add(columnName);
            expressionsForValue.add(exp);
        }
    }
    if (expressionsForKey.isEmpty()) {
        // standard UPDATE, we are not updating any column in the PK
        RecordFunction function = new SQLRecordFunction(updateColumnListInValue, table, expressionsForValue);
        UpdateStatement update = null;
        if (input instanceof TableScanOp) {
            update = new UpdateStatement(tableSpace, tableName, null, function, null);
        } else if (input instanceof FilterOp) {
            FilterOp filter = (FilterOp) input;
            if (filter.getInput() instanceof TableScanOp) {
                SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
                update = new UpdateStatement(tableSpace, tableName, null, function, pred);
            }
        } else if (input instanceof ProjectOp) {
            ProjectOp proj = (ProjectOp) input;
            if (proj.getInput() instanceof TableScanOp) {
                update = new UpdateStatement(tableSpace, tableName, null, function, null);
            } else if (proj.getInput() instanceof FilterOp) {
                FilterOp filter = (FilterOp) proj.getInput();
                if (filter.getInput() instanceof TableScanOp) {
                    SQLRecordPredicate pred = new SQLRecordPredicate(table, null, filter.getCondition());
                    update = new UpdateStatement(tableSpace, tableName, null, function, pred);
                }
            } else if (proj.getInput() instanceof FilteredTableScanOp) {
                FilteredTableScanOp filter = (FilteredTableScanOp) proj.getInput();
                Predicate pred = filter.getPredicate();
                update = new UpdateStatement(tableSpace, tableName, null, function, pred);
            } else if (proj.getInput() instanceof BindableTableScanOp) {
                BindableTableScanOp filter = (BindableTableScanOp) proj.getInput();
                ScanStatement scan = filter.getStatement();
                if (scan.getComparator() == null && scan.getLimits() == null && scan.getTableDef() != null) {
                    Predicate pred = scan.getPredicate();
                    update = new UpdateStatement(tableSpace, tableName, null, function, pred);
                }
            }
        }
        if (update != null) {
            return new SimpleUpdateOp(update.setReturnValues(returnValues));
        } else {
            return new UpdateOp(tableSpace, tableName, input, returnValues, function);
        }
    } else {
        // bad stuff ! we are updating the PK, we need to transform this to a sequence of delete and inserts
        // ReplaceOp won't execute the two statements atomically
        RecordFunction functionForValue = new SQLRecordFunction(updateColumnListInValue, table, expressionsForValue);
        SQLRecordKeyFunction functionForKey = new SQLRecordKeyFunction(updateColumnListInPk, expressionsForKey, table);
        return new ReplaceOp(tableSpace, tableName, input, returnValues, functionForKey, functionForValue);
    }
}
Also used : SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp) UpdateOp(herddb.model.planner.UpdateOp) FilterOp(herddb.model.planner.FilterOp) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) ReplaceOp(herddb.model.planner.ReplaceOp) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ScanStatement(herddb.model.commands.ScanStatement) UpdateStatement(herddb.model.commands.UpdateStatement) PlannerOp(herddb.model.planner.PlannerOp) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) FilteredTableScanOp(herddb.model.planner.FilteredTableScanOp) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) TableScanOp(herddb.model.planner.TableScanOp) ProjectOp(herddb.model.planner.ProjectOp) FilteredTableScanOp(herddb.model.planner.FilteredTableScanOp) SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp) RexNode(org.apache.calcite.rex.RexNode) BindableTableScanOp(herddb.model.planner.BindableTableScanOp)

Aggregations

Table (herddb.model.Table)288 Test (org.junit.Test)191 CreateTableStatement (herddb.model.commands.CreateTableStatement)181 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)132 InsertStatement (herddb.model.commands.InsertStatement)118 DataScanner (herddb.model.DataScanner)110 Index (herddb.model.Index)98 ScanStatement (herddb.model.commands.ScanStatement)98 GetStatement (herddb.model.commands.GetStatement)85 TransactionContext (herddb.model.TransactionContext)84 GetResult (herddb.model.GetResult)81 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)80 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)80 TranslatedQuery (herddb.sql.TranslatedQuery)78 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)77 Bytes (herddb.utils.Bytes)76 StatementExecutionException (herddb.model.StatementExecutionException)73 Record (herddb.model.Record)72 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)72 Path (java.nio.file.Path)64