Search in sources :

Example 31 with Column

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

the class ShowCreateTableCalculator method calculate.

public static String calculate(boolean showCreateIndex, String tableName, String tableSpace, AbstractTableManager tableManager) {
    Table t = tableManager.getTable();
    if (t == null) {
        throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
    }
    StringBuilder sb = new StringBuilder("CREATE TABLE " + tableSpace + "." + tableName);
    StringJoiner joiner = new StringJoiner(",", "(", ")");
    for (Column c : t.getColumns()) {
        joiner.add(c.name + " " + ColumnTypes.typeToString(c.type) + autoIncrementColumn(t, c) + defaultClause(c));
    }
    if (t.getPrimaryKey().length > 0) {
        joiner.add("PRIMARY KEY(" + String.join(",", t.getPrimaryKey()) + ")");
    }
    if (showCreateIndex) {
        List<Index> indexes = tableManager.getAvailableIndexes();
        if (!indexes.isEmpty()) {
            indexes.forEach(idx -> {
                if (idx.unique) {
                    joiner.add("UNIQUE KEY " + idx.name + " (" + String.join(",", idx.columnNames) + ")");
                } else {
                    joiner.add("INDEX " + idx.name + "(" + String.join(",", idx.columnNames) + ")");
                }
            });
        }
    }
    sb.append(joiner.toString());
    return sb.toString();
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Table(herddb.model.Table) Column(herddb.model.Column) Index(herddb.model.Index) StringJoiner(java.util.StringJoiner)

Example 32 with Column

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

the class SortOp method optimize.

@Override
public PlannerOp optimize() {
    if (input instanceof BindableTableScanOp) {
        BindableTableScanOp op = (BindableTableScanOp) input;
        // we can change the statement, this node will be lost and the tablescan too
        ScanStatement statement = op.getStatement();
        statement.setComparator(this);
        if (fields.length == 1 && directions[0]) {
            Table tableDef = statement.getTableDef();
            if (tableDef.getPrimaryKey().length == 1) {
                if (statement.getProjection() != null && statement.getProjection() instanceof ZeroCopyProjection) {
                    ZeroCopyProjection zeroCopyProjection = (ZeroCopyProjection) statement.getProjection();
                    int index = zeroCopyProjection.mapPosition(fields[0]);
                    Column col = tableDef.resolveColumName(index);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                } else if (statement.getProjection() != null && statement.getProjection() instanceof IdentityProjection) {
                    Column col = tableDef.resolveColumName(fields[0]);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                }
            }
        }
        return new SortedBindableTableScanOp(statement);
    } else if (input instanceof TableScanOp) {
        TableScanOp op = (TableScanOp) input;
        // we can change the statement, this node will be lost and the tablescan too
        ScanStatement statement = op.getStatement();
        statement.setComparator(this);
        if (fields.length == 1 && directions[0]) {
            Table tableDef = statement.getTableDef();
            if (tableDef.getPrimaryKey().length == 1) {
                if (statement.getProjection() != null && statement.getProjection() instanceof ZeroCopyProjection) {
                    ZeroCopyProjection zeroCopyProjection = (ZeroCopyProjection) statement.getProjection();
                    int index = zeroCopyProjection.mapPosition(fields[0]);
                    Column col = tableDef.resolveColumName(index);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                } else if (statement.getProjection() != null && statement.getProjection() instanceof IdentityProjection) {
                    Column col = tableDef.resolveColumName(fields[0]);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                }
            }
        }
        return new SortedTableScanOp(statement);
    }
    return this;
}
Also used : Table(herddb.model.Table) ZeroCopyProjection(herddb.model.planner.ProjectOp.ZeroCopyProjection) Column(herddb.model.Column) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) ScanStatement(herddb.model.commands.ScanStatement)

Example 33 with Column

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

the class RecordSetSuite method testLimitsAfterEndSwap.

@Test
public void testLimitsAfterEndSwap() throws Exception {
    RecordSetFactory factory = buildRecordSetFactory(1);
    Column[] columns = new Column[2];
    columns[0] = Column.column("s1", ColumnTypes.STRING);
    columns[1] = Column.column("n1", ColumnTypes.LONG);
    String[] fieldNames = Column.buildFieldNamesList(columns);
    try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
        for (int i = 0; i < 100; i++) {
            Map<String, Object> record = new HashMap<>();
            String s1 = "test_" + i;
            record.put("s1", s1);
            record.put("n1", i);
            rs.add(new Tuple(record, fieldNames));
        }
        rs.writeFinished();
        rs.applyLimits(new ScanLimitsImpl(20, 100000), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT());
        for (DataAccessor t : rs) {
            fail();
        }
    }
}
Also used : HashMap(java.util.HashMap) DataAccessor(herddb.utils.DataAccessor) ScanLimitsImpl(herddb.model.ScanLimitsImpl) Column(herddb.model.Column) Tuple(herddb.model.Tuple) Test(org.junit.Test)

Example 34 with Column

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

the class RecordSetSuite method testLimitsNoSwap.

@Test
public void testLimitsNoSwap() throws Exception {
    RecordSetFactory factory = buildRecordSetFactory(Integer.MAX_VALUE);
    Column[] columns = new Column[2];
    columns[0] = Column.column("s1", ColumnTypes.STRING);
    columns[1] = Column.column("n1", ColumnTypes.LONG);
    String[] fieldNames = Column.buildFieldNamesList(columns);
    try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
        Set<String> expected_s1 = new HashSet<>();
        Set<Integer> expected_n1 = new HashSet<>();
        for (int i = 0; i < 100; i++) {
            Map<String, Object> record = new HashMap<>();
            String s1 = "test_" + i;
            record.put("s1", s1);
            record.put("n1", i);
            if (i >= 10 && i < 30) {
                expected_s1.add(s1);
                expected_n1.add(i);
            }
            rs.add(new Tuple(record, fieldNames));
        }
        rs.writeFinished();
        rs.applyLimits(new ScanLimitsImpl(20, 10), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT());
        for (DataAccessor t : rs) {
            expected_s1.remove(t.get("s1").toString());
            expected_n1.remove(t.get("n1"));
        }
        assertTrue(expected_n1.isEmpty());
        assertTrue(expected_s1.isEmpty());
    }
}
Also used : HashMap(java.util.HashMap) DataAccessor(herddb.utils.DataAccessor) ScanLimitsImpl(herddb.model.ScanLimitsImpl) Column(herddb.model.Column) Tuple(herddb.model.Tuple) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Column

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

the class RecordSetSuite method testSortNoSwap.

@Test
public void testSortNoSwap() throws Exception {
    RecordSetFactory factory = buildRecordSetFactory(Integer.MAX_VALUE);
    Column[] columns = new Column[2];
    columns[0] = Column.column("s1", ColumnTypes.STRING);
    columns[1] = Column.column("n1", ColumnTypes.LONG);
    String[] fieldNames = Column.buildFieldNamesList(columns);
    try (MaterializedRecordSet rs = factory.createRecordSet(fieldNames, columns)) {
        for (int i = 0; i < 100; i++) {
            Map<String, Object> record = new HashMap<>();
            String s1 = "test_" + i;
            record.put("s1", s1);
            record.put("n1", i);
            rs.add(new Tuple(record, fieldNames));
        }
        rs.writeFinished();
        // sort descending
        rs.sort(new TupleComparator() {

            @Override
            public int compare(DataAccessor o1, DataAccessor o2) {
                return ((Integer) o2.get(("n1"))).compareTo((Integer) o1.get("n1"));
            }
        });
        int last = Integer.MAX_VALUE;
        for (DataAccessor t : rs) {
            int n1 = (Integer) t.get("n1");
            assertTrue(last > n1);
            last = n1;
        }
    }
}
Also used : HashMap(java.util.HashMap) DataAccessor(herddb.utils.DataAccessor) TupleComparator(herddb.model.TupleComparator) Column(herddb.model.Column) Tuple(herddb.model.Tuple) Test(org.junit.Test)

Aggregations

Column (herddb.model.Column)68 StatementExecutionException (herddb.model.StatementExecutionException)25 ArrayList (java.util.ArrayList)24 Table (herddb.model.Table)23 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)19 DataAccessor (herddb.utils.DataAccessor)18 HashMap (java.util.HashMap)14 PlannerOp (herddb.model.planner.PlannerOp)12 List (java.util.List)12 Bytes (herddb.utils.Bytes)10 AbstractTableManager (herddb.core.AbstractTableManager)9 RecordFunction (herddb.model.RecordFunction)9 RawString (herddb.utils.RawString)9 IOException (java.io.IOException)9 HashSet (java.util.HashSet)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 Test (org.junit.Test)9 DataScanner (herddb.model.DataScanner)8 Tuple (herddb.model.Tuple)8 InsertStatement (herddb.model.commands.InsertStatement)8