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();
}
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;
}
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();
}
}
}
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());
}
}
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;
}
}
}
Aggregations