use of com.questdb.ql.select.SelectedColumnsRecordSource in project questdb by bluestreak01.
the class HashJoinRecordSourceTest method testHashJoinRecordSource.
@Test
public void testHashJoinRecordSource() throws Exception {
bw.append(new Band().setName("band1").setType("rock").setUrl("http://band1.com"));
bw.append(new Band().setName("band2").setType("blues").setUrl("http://band2.com"));
bw.append(new Band().setName("band3").setType("jazz").setUrl("http://band3.com"));
bw.append(new Band().setName("band1").setType("jazz").setUrl("http://new.band1.com"));
bw.commit();
aw.append(new Album().setName("album X").setBand("band1").setGenre("pop"));
aw.append(new Album().setName("album Y").setBand("band3").setGenre("metal"));
aw.append(new Album().setName("album BZ").setBand("band1").setGenre("rock"));
aw.commit();
StringSink sink = new StringSink();
RecordSourcePrinter p = new RecordSourcePrinter(sink);
RecordSource joinResult = new SelectedColumnsRecordSource(new HashJoinRecordSource(new JournalRecordSource(new JournalPartitionSource(bw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(bw.getMetadata().getColumnIndex("name"));
}
}, new JournalRecordSource(new JournalPartitionSource(aw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(aw.getMetadata().getColumnIndex("band"));
}
}, false, 4 * 1024 * 1024, 4 * 1024 * 1024, 1024 * 1024, new RecordKeyCopierCompiler(new BytecodeAssembler())), new ObjList<CharSequence>() {
{
add("genre");
}
});
p.print(joinResult, getFactory());
Assert.assertEquals("pop\n" + "rock\n" + "metal\n" + "pop\n" + "rock\n", sink.toString());
}
use of com.questdb.ql.select.SelectedColumnsRecordSource in project questdb by bluestreak01.
the class HashJoinRecordSourceTest method testHashJoinJournalRecordSource.
@Test
public void testHashJoinJournalRecordSource() throws Exception {
bw.append(new Band().setName("band1").setType("rock").setUrl("http://band1.com"));
bw.append(new Band().setName("band2").setType("blues").setUrl("http://band2.com"));
bw.append(new Band().setName("band3").setType("jazz").setUrl("http://band3.com"));
bw.append(new Band().setName("band1").setType("jazz").setUrl("http://new.band1.com"));
bw.commit();
aw.append(new Album().setName("album X").setBand("band1").setGenre("pop"));
aw.append(new Album().setName("album Y").setBand("band3").setGenre("metal"));
aw.append(new Album().setName("album BZ").setBand("band1").setGenre("rock"));
aw.commit();
StringSink sink = new StringSink();
RecordSourcePrinter p = new RecordSourcePrinter(sink);
RecordSource joinResult = new SelectedColumnsRecordSource(new HashJoinRecordSource(new JournalRecordSource(new JournalPartitionSource(bw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(bw.getMetadata().getColumnIndex("name"));
}
}, new JournalRecordSource(new JournalPartitionSource(aw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(aw.getMetadata().getColumnIndex("band"));
}
}, false, 4 * 1024 * 1024, 4 * 1024 * 1024, 1024 * 1024, new RecordKeyCopierCompiler(new BytecodeAssembler())), new ObjList<CharSequence>() {
{
add("genre");
}
});
p.print(joinResult, getFactory());
Assert.assertEquals("pop\n" + "rock\n" + "metal\n" + "pop\n" + "rock\n", sink.toString());
}
use of com.questdb.ql.select.SelectedColumnsRecordSource in project questdb by bluestreak01.
the class VirtualColumnTest method testSelectedColumns.
@Test
public void testSelectedColumns() throws Exception {
try (final JournalWriter w = getFactory().writer(new JournalStructure("xyz") {
{
$str("ccy");
$double("bid");
}
})) {
Rnd rnd = new Rnd();
for (int i = 0; i < 100; i++) {
JournalEntryWriter ew = w.entryWriter();
ew.putStr(0, rnd.nextString(10));
ew.putDouble(1, rnd.nextDouble());
ew.append();
}
w.commit();
StringSink sink = new StringSink();
RecordSourcePrinter p = new RecordSourcePrinter(sink);
BootstrapEnv env = new BootstrapEnv();
env.configuration = new ServerConfiguration();
final AddDoubleOperator plus = (AddDoubleOperator) AddDoubleOperator.FACTORY.newInstance(0, env);
plus.setName("plus");
plus.setLhs(new DoubleRecordSourceColumn(w.getMetadata().getColumnIndex("bid"), 0));
plus.setRhs(new DoubleConstant(12.5, 0));
// select ccy, bid+12.5 plus from xyz
try (RecordSource src = new SelectedColumnsRecordSource(new VirtualColumnRecordSource(compile("xyz"), new ObjList<VirtualColumn>() {
{
add(plus);
}
}), new ObjList<CharSequence>() {
{
add("ccy");
add("plus");
}
})) {
p.print(src, getFactory());
final String expected = "VTJWCPSWHY\t-91.521850585938\n" + "PEHNRXGZSX\t12.500020634160\n" + "IBBTGPGWFF\t12.500000567185\n" + "DEYYQEHBHF\t12.500000401164\n" + "LPDXYSBEOU\t396.572387695313\n" + "SHRUEDRQQU\t121.855468750000\n" + "FJGETJRSZS\t211.250000000000\n" + "RFBVTMHGOO\t185.296875000000\n" + "VDZJMYICCX\t-1011.500000000000\n" + "UICWEKGHVU\t12.500425009843\n" + "DOTSEDYYCT\t569.500000000000\n" + "OLYXWCKYLS\t12.500553289865\n" + "DSWUGSHOLN\t-1000.967773437500\n" + "IQBZXIOVIK\t524.500000000000\n" + "MSSUQSRLTK\t12.500000776007\n" + "SJOJIPHZEP\t187.274871826172\n" + "VLTOVLJUML\t-64.928833007813\n" + "HMLLEOYPHR\t12.500327562877\n" + "ZIMNZZRMFM\t251.132812500000\n" + "ZGHWVDKFLO\t12.500102697388\n" + "OXPKRGIIHY\t140.500000000000\n" + "OQMYSSMPGL\t-131.921875000000\n" + "HNZHZSQLDG\t844.500000000000\n" + "GIFOUSZMZV\t-187.500000000000\n" + "BNDCQCEHNO\t15.102588653564\n" + "ELLKKHTWNW\t12.500969694171\n" + "FLRBROMNXK\t12.500000548919\n" + "ZULIGYVFZF\t-314.750000000000\n" + "ZLUOGXHFVW\t12.500002116648\n" + "SRGOONFCLT\t322.601058959961\n" + "KFMQNTOGMX\t12.500012478828\n" + "LGMXSLUQDY\t12.500013214448\n" + "HNIMYFFDTN\t12.500000001910\n" + "FLPBNHGZWW\t707.673828125000\n" + "NGTNLEGPUH\t12.705350898206\n" + "UGGLNYRZLC\t650.500000000000\n" + "MIGQZVKHTL\t12.500000040298\n" + "SLQVFGPPRG\t-1011.500000000000\n" + "BHYSBQYMIZ\t17.836447119713\n" + "VTNPIWZNFK\t12.500000012570\n" + "VMCGFNWGRM\t-115.500000000000\n" + "GIJYDVRVNG\t12.500000052204\n" + "EQODRZEIWF\t-18.500000000000\n" + "KYHQQUWQOE\t892.500000000000\n" + "NEBQQEMXDK\t38.339271545410\n" + "JCTIZKYFLU\t780.500000000000\n" + "QSNPXMKJSM\t12.504184104619\n" + "XEYVTUPDHH\t858.323730468750\n" + "IWHPZRHHMG\t49.850353240967\n" + "YYFLSVIHDW\t-387.500000000000\n" + "EVMLKCJBEV\t12.500000914462\n" + "HLIHYBTVZN\t12.500001439041\n" + "NXFSUWPNXH\t-187.148437500000\n" + "TZODWKOCPF\t12.502563251997\n" + "PVKNCBWLNL\t12.897523656487\n" + "WQXYPOVFDB\t21.450848102570\n" + "NIJEEHRUGP\t460.500000000000\n" + "BTKVSBEGMI\t403.500000000000\n" + "NLKFNUHNRJ\t12.500000048669\n" + "BWVLOMPBET\t12.500038398017\n" + "KRIVOCUGPU\t15.429819107056\n" + "FIVQFNIZOS\t827.500000000000\n" + "SEPGIUQZHE\t12.517051883508\n" + "QHNOJIGFIN\t613.587127685547\n" + "QVZWEVQTQO\t-479.500000000000\n" + "XTPNHTDCEB\t-953.500000000000\n" + "XBBZVRLPTY\t83.310325622559\n" + "GYFUXCDKDW\t12.500837845349\n" + "DXCBJFRPXZ\t12.500008696692\n" + "XUNYQXTGNJ\t18.859375000000\n" + "LLEYMIWTCW\t804.742187500000\n" + "ORGFIEVMKP\t-713.500000000000\n" + "GPYKKBMQMU\t12.500000000000\n" + "CIHCNPUGJO\t-749.740234375000\n" + "EUKWMDNZZB\t12.500000031117\n" + "KOJSOLDYRO\t564.500000000000\n" + "PUNRPSMIFD\t86.447616577148\n" + "DKOEZBRQSQ\t-723.500000000000\n" + "DIHHNSSTCR\t12.500002963134\n" + "PVQFULMERT\t12.500005817310\n" + "QBUYZVQQHS\t12.500000011688\n" + "PZPBHLNEJR\t-991.125000000000\n" + "IKDISGQFYQ\t364.009208679199\n" + "GPZNYVLTPK\t12.500702012621\n" + "QFNPOYNNCT\t12.500445737198\n" + "NSXHHDILEL\t12.501584486396\n" + "MMZSCJOUOU\t655.365234375000\n" + "ENFELWWRSL\t-921.768554687500\n" + "QHGJBFQBBK\t116.408081054688\n" + "JZZYNPPBXB\t-931.500000000000\n" + "RIIYMHOWKC\t12.500000542615\n" + "ZNLCNGZTOY\t12.500080315222\n" + "XRSFPVRQLG\t12.500000093226\n" + "ONNLITWGLF\t12.500000000000\n" + "QWPKLHTIIG\t-771.250000000000\n" + "YYPDVRGRQG\t12.500376001219\n" + "PHKOWBVDEG\t12.500002666791\n" + "XGZMDJTHMH\t-658.000000000000\n" + "VZHCNXZEQG\t-512.007812500000\n" + "LUCFTLNKYT\t12.500000001835\n";
Assert.assertEquals(expected, sink.toString());
}
}
}
use of com.questdb.ql.select.SelectedColumnsRecordSource in project questdb by bluestreak01.
the class HashJoinRecordSourceTest method testOuterHashJoin.
@Test
public void testOuterHashJoin() throws Exception {
bw.append(new Band().setName("band1").setType("rock").setUrl("http://band1.com"));
bw.append(new Band().setName("band2").setType("blues").setUrl("http://band2.com"));
bw.append(new Band().setName("band3").setType("jazz").setUrl("http://band3.com"));
bw.append(new Band().setName("band1").setType("jazz").setUrl("http://new.band1.com"));
bw.append(new Band().setName("band5").setType("jazz").setUrl("http://new.band5.com"));
bw.commit();
aw.append(new Album().setName("album X").setBand("band1").setGenre("pop"));
aw.append(new Album().setName("album Y").setBand("band3").setGenre("metal"));
aw.append(new Album().setName("album BZ").setBand("band1").setGenre("rock"));
aw.commit();
StringSink sink = new StringSink();
RecordSourcePrinter p = new RecordSourcePrinter(sink);
RecordSource joinResult = new SelectedColumnsRecordSource(new HashJoinRecordSource(new JournalRecordSource(new JournalPartitionSource(bw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(bw.getMetadata().getColumnIndex("name"));
}
}, new JournalRecordSource(new JournalPartitionSource(aw.getMetadata(), false), new AllRowSource()), new IntList() {
{
add(aw.getMetadata().getColumnIndex("band"));
}
}, true, 4 * 1024 * 1024, 4 * 1024 * 1024, 1024 * 1024, new RecordKeyCopierCompiler(new BytecodeAssembler())), new ObjList<CharSequence>() {
{
add("genre");
add("url");
}
});
p.print(joinResult, getFactory());
Assert.assertEquals("pop\thttp://band1.com\n" + "rock\thttp://band1.com\n" + "\thttp://band2.com\n" + "metal\thttp://band3.com\n" + "pop\thttp://new.band1.com\n" + "rock\thttp://new.band1.com\n" + "\thttp://new.band5.com\n", sink.toString());
}
use of com.questdb.ql.select.SelectedColumnsRecordSource in project questdb by bluestreak01.
the class QueryCompiler method selectColumns0.
private RecordSource selectColumns0(final RecordSource recordSource, QueryModel model) throws ParserException {
final ObjList<QueryColumn> columns = model.getColumns();
final CharSequenceIntHashMap columnNameHistogram = model.getColumnNameHistogram();
final RecordMetadata meta = recordSource.getMetadata();
this.outerVirtualColumns.clear();
this.innerVirtualColumn.clear();
this.aggregators.clear();
this.selectedColumns.clear();
this.selectedColumnAliases.clear();
this.groupKeyColumns.clear();
this.aggregateColumnSequence = 0;
this.analyticColumns.clear();
RecordSource rs = recordSource;
try {
// create virtual columns from select list
for (int i = 0, k = columns.size(); i < k; i++) {
final QueryColumn qc = columns.getQuick(i);
final ExprNode node = qc.getAst();
final boolean analytic = qc instanceof AnalyticColumn;
if (!analytic && node.type == ExprNode.LITERAL) {
// check literal column validity
if (meta.getColumnIndexQuiet(node.token) == -1) {
throw QueryError.invalidColumn(node.position, node.token);
}
// check if this column is exists in more than one journal/result set
if (columnNameHistogram.get(node.token) > 0) {
throw QueryError.ambiguousColumn(node.position);
}
// add to selected columns
selectedColumns.add(node.token);
addAlias(node.position, qc.getName());
groupKeyColumns.add(node.token);
continue;
}
// generate missing alias for everything else
if (qc.getAlias() == null) {
qc.of(createAlias(aggregateColumnSequence++), node.position, node);
}
selectedColumns.add(qc.getAlias());
addAlias(node.position, qc.getAlias());
// outright aggregate
if (!analytic && node.type == ExprNode.FUNCTION && FunctionFactories.isAggregate(node.token)) {
aggregators.add(qc);
continue;
}
// check if this expression references aggregate function
if (node.type == ExprNode.OPERATION || node.type == ExprNode.FUNCTION) {
int beforeSplit = aggregators.size();
splitAggregates(node, aggregators);
if (beforeSplit < aggregators.size()) {
outerVirtualColumns.add(qc);
continue;
}
}
if (analytic) {
if (qc.getAst().type != ExprNode.FUNCTION) {
// todo: not hit by test
throw QueryError.$(qc.getAst().position, "Analytic function expected");
}
if (aggregators.size() > 0) {
throw QueryError.$(qc.getAst().position, "Analytic function is not allowed in context of aggregation. Use sub-query.");
}
AnalyticColumn ac = (AnalyticColumn) qc;
analyticColumns.add(ac);
} else {
// this is either a constant or non-aggregate expression
// either case is a virtual column
innerVirtualColumn.add(qc);
}
}
// if virtual columns are present, create record source to calculate them
if (innerVirtualColumn.size() > 0) {
// this is either a constant or non-aggregate expression
// either case is a virtual column
ObjList<VirtualColumn> virtualColumns = new ObjList<>();
final String timestampName;
if (model.getSampleBy() != null) {
timestampName = meta.getColumnName(getTimestampIndex(model, model.getTimestamp(), meta));
} else {
timestampName = null;
}
for (int i = 0, n = innerVirtualColumn.size(); i < n; i++) {
QueryColumn qc = innerVirtualColumn.getQuick(i);
if (Chars.equalsNc(qc.getAlias(), timestampName)) {
throw QueryError.$(qc.getAliasPosition() == -1 ? qc.getAst().position : qc.getAliasPosition(), "Alias clashes with implicit sample column name");
}
VirtualColumn vc = virtualColumnBuilder.createVirtualColumn(model, qc.getAst(), meta);
vc.setName(qc.getAlias());
virtualColumns.add(vc);
groupKeyColumns.add(qc.getAlias());
}
rs = new VirtualColumnRecordSource(rs, virtualColumns);
}
// if aggregators present, wrap record source into group-by source
if (aggregators.size() > 0) {
rs = compileAggregates(rs, model);
} else {
ExprNode sampleBy = model.getSampleBy();
if (sampleBy != null) {
throw QueryError.$(sampleBy.position, "There are no aggregation columns");
}
}
if (outerVirtualColumns.size() > 0) {
rs = compileOuterVirtualColumns(rs, model);
}
if (analyticColumns.size() > 0) {
rs = compileAnalytic(rs, model);
}
if (selectedColumns.size() > 0) {
// wrap underlying record source into selected columns source.
rs = new SelectedColumnsRecordSource(rs, selectedColumns, selectedColumnAliases);
}
return rs;
} catch (ParserException e) {
Misc.free(rs);
throw e;
}
}
Aggregations