use of com.questdb.store.factory.configuration.ColumnMetadata in project questdb by bluestreak01.
the class QueryCompiler method createWriter.
public JournalWriter createWriter(Factory factory, ParsedModel model) throws ParserException, JournalException {
if (model.getModelType() != ParsedModel.CREATE_JOURNAL) {
throw new IllegalArgumentException("create table statement expected");
}
clearState();
CreateJournalModel cm = (CreateJournalModel) model;
final String name = cm.getName().token;
switch(factory.getConfiguration().exists(name)) {
case JournalConfiguration.EXISTS:
throw QueryError.$(cm.getName().position, "Journal already exists");
case JournalConfiguration.EXISTS_FOREIGN:
throw QueryError.$(cm.getName().position, "Name is reserved");
default:
break;
}
JournalStructure struct = cm.getStruct();
RecordSource rs;
QueryModel queryModel = cm.getQueryModel();
if (queryModel != null) {
rs = compile(queryModel, factory);
} else {
rs = null;
}
if (struct == null) {
assert rs != null;
RecordMetadata metadata = rs.getMetadata();
CharSequenceObjHashMap<ColumnCastModel> castModels = cm.getColumnCastModels();
// validate cast models
for (int i = 0, n = castModels.size(); i < n; i++) {
ColumnCastModel castModel = castModels.valueQuick(i);
if (metadata.getColumnIndexQuiet(castModel.getName().token) == -1) {
throw QueryError.invalidColumn(castModel.getName().position, castModel.getName().token);
}
}
struct = createStructure(name, metadata, castModels);
}
try {
validateAndSetTimestamp(struct, cm.getTimestamp());
validateAndSetPartitionBy(struct, cm.getPartitionBy());
ExprNode recordHint = cm.getRecordHint();
if (recordHint != null) {
try {
struct.recordCountHint(Numbers.parseInt(recordHint.token));
} catch (NumericException e) {
throw QueryError.$(recordHint.position, "Bad int");
}
}
ObjList<ColumnIndexModel> columnIndexModels = cm.getColumnIndexModels();
for (int i = 0, n = columnIndexModels.size(); i < n; i++) {
ColumnIndexModel cim = columnIndexModels.getQuick(i);
ExprNode nn = cim.getName();
ColumnMetadata m = struct.getColumnMetadata(nn.token);
if (m == null) {
throw QueryError.invalidColumn(nn.position, nn.token);
}
switch(m.getType()) {
case ColumnType.INT:
case ColumnType.LONG:
case ColumnType.SYMBOL:
case ColumnType.STRING:
m.indexed = true;
m.distinctCountHint = cim.getBuckets();
break;
default:
throw QueryError.$(nn.position, "Type index not supported");
}
}
JournalWriter w = factory.writer(struct);
w.setSequentialAccess(true);
if (rs != null) {
try {
copy(factory, rs, w);
} catch (Throwable e) {
w.close();
throw e;
}
}
return w;
} catch (Throwable e) {
Misc.free(rs);
throw e;
}
}
use of com.questdb.store.factory.configuration.ColumnMetadata in project questdb by bluestreak01.
the class SymbolIndexProxy method openIndex.
private KVIndex openIndex() throws JournalException {
JournalMetadata<T> meta = partition.getJournal().getMetadata();
ColumnMetadata columnMetadata = meta.getColumnQuick(columnIndex);
if (!columnMetadata.indexed) {
throw new JournalException("There is no index for column: %s", columnMetadata.name);
}
return new KVIndex(new File(partition.getPartitionDir(), columnMetadata.name), columnMetadata.distinctCountHint, meta.getRecordHint(), meta.getTxCountHint(), partition.getJournal().getMode(), txAddress, sequentialAccess);
}
use of com.questdb.store.factory.configuration.ColumnMetadata in project questdb by bluestreak01.
the class VwapAggregator method prepare.
@Override
public void prepare(ObjList<RecordColumnMetadata> columns, int offset) {
columns.add(INTERNAL_COL_AMOUNT);
columns.add(INTERNAL_COL_QUANTITY);
columns.add(new ColumnMetadata().setName(getName()).setType(ColumnType.DOUBLE));
sumAmtIdx = offset;
sumQtyIdx = offset + 1;
vwap = offset + 2;
}
use of com.questdb.store.factory.configuration.ColumnMetadata in project questdb by bluestreak01.
the class ResultSet method compare.
private static <T> int compare(Journal<T> journal, int[] columns, long rightRowID, long leftRowID) throws JournalException {
int result = 0;
long leftLocalRowID = Rows.toLocalRowID(leftRowID);
long rightLocalRowID = Rows.toLocalRowID(rightRowID);
Partition<T> leftPart = journal.getPartition(Rows.toPartitionIndex(leftRowID), true);
Partition<T> rightPart = journal.getPartition(Rows.toPartitionIndex(rightRowID), true);
for (int column : columns) {
ColumnMetadata meta = journal.getMetadata().getColumnQuick(column);
String leftStr;
String rightStr;
switch(meta.type) {
case ColumnType.STRING:
leftStr = leftPart.getStr(leftLocalRowID, column);
rightStr = rightPart.getStr(rightLocalRowID, column);
if (leftStr == null && rightStr == null) {
result = 0;
} else if (leftStr == null) {
result = 1;
} else if (rightStr == null) {
result = -1;
} else {
result = rightStr.compareTo(leftStr);
}
break;
default:
switch(meta.type) {
case ColumnType.INT:
result = compare(rightPart.getInt(rightLocalRowID, column), leftPart.getInt(leftLocalRowID, column));
break;
case ColumnType.LONG:
case ColumnType.DATE:
result = compare(rightPart.getLong(rightLocalRowID, column), leftPart.getLong(leftLocalRowID, column));
break;
case ColumnType.DOUBLE:
result = compare(rightPart.getDouble(rightLocalRowID, column), leftPart.getDouble(leftLocalRowID, column));
break;
case ColumnType.FLOAT:
result = compare(rightPart.getFloat(rightLocalRowID, column), leftPart.getFloat(leftLocalRowID, column));
break;
case ColumnType.SYMBOL:
int leftSymIndex = leftPart.getInt(leftLocalRowID, column);
int rightSymIndex = rightPart.getInt(rightLocalRowID, column);
if (leftSymIndex == SymbolTable.VALUE_IS_NULL && rightSymIndex == SymbolTable.VALUE_IS_NULL) {
result = 0;
} else if (leftSymIndex == SymbolTable.VALUE_IS_NULL) {
result = 1;
} else if (rightSymIndex == SymbolTable.VALUE_IS_NULL) {
result = -1;
} else {
leftStr = meta.symbolTable.value(leftSymIndex);
rightStr = meta.symbolTable.value(rightSymIndex);
if (leftStr == null || rightStr == null) {
throw new JournalException("Corrupt column [%s] !", meta);
}
result = rightStr.compareTo(leftStr);
}
break;
default:
throw new JournalException("Unsupported type: " + meta.type);
}
}
if (result != 0) {
break;
}
}
return result;
}
use of com.questdb.store.factory.configuration.ColumnMetadata in project questdb by bluestreak01.
the class TestUtils method assertOrder.
public static <T> void assertOrder(JournalIterator<T> rs) {
ColumnMetadata meta = rs.getJournal().getMetadata().getTimestampMetadata();
long max = 0;
for (T obj : rs) {
long timestamp = Unsafe.getUnsafe().getLong(obj, meta.offset);
if (timestamp < max) {
throw new AssertionError("Timestamp out of order. [ " + Dates.toString(timestamp) + " < " + Dates.toString(max) + "]");
}
max = timestamp;
}
}
Aggregations