Search in sources :

Example 1 with CrossJoinRecordSource

use of com.questdb.ql.join.CrossJoinRecordSource in project questdb by bluestreak01.

the class QueryCompiler method compileJoins.

private RecordSource compileJoins(QueryModel model, Factory factory) throws ParserException {
    ObjList<QueryModel> joinModels = model.getJoinModels();
    IntList ordered = model.getOrderedJoinModels();
    RecordSource master = null;
    try {
        boolean needColumnNameHistogram = model.getColumns().size() > 0;
        model.getColumnNameHistogram().clear();
        for (int i = 0, n = ordered.size(); i < n; i++) {
            int index = ordered.getQuick(i);
            QueryModel m = joinModels.getQuick(index);
            // compile
            RecordSource slave = m.getRecordSource();
            if (slave == null) {
                slave = compileJournal(m, factory);
            } else {
                slave = filter(m, slave);
            }
            if (m.getAlias() != null) {
                slave.getMetadata().setAlias(m.getAlias().token);
            }
            if (needColumnNameHistogram) {
                model.createColumnNameHistogram(slave);
            }
            // check if this is the root of joins
            if (master == null) {
                // This is an opportunistic check of order by clause
                // to determine if we can get away ordering main record source only
                // Ordering main record source could benefit from rowid access thus
                // making it faster compared to ordering of join record source that
                // doesn't allow rowid access.
                master = analyseAndCompileOrderBy(model, slave);
            } else {
                // not the root, join to "master"
                switch(m.getJoinType()) {
                    case QueryModel.JOIN_CROSS:
                        // there are fields to analyse
                        master = new CrossJoinRecordSource(master, slave);
                        break;
                    case QueryModel.JOIN_ASOF:
                        master = createAsOfJoin(model.getTimestamp(), m, master, slave);
                        break;
                    default:
                        master = createHashJoin(m, master, slave);
                        break;
                }
            }
            // check if there are post-filters
            ExprNode filter = m.getPostJoinWhereClause();
            if (filter != null) {
                master = new FilteredRecordSource(master, virtualColumnBuilder.createVirtualColumn(model, filter, master.getMetadata()), filter);
            }
        }
        if (joinModelIsFalse(model)) {
            return new NoOpJournalRecordSource(master);
        }
        return master;
    } catch (ParserException e) {
        Misc.free(master);
        throw e;
    }
}
Also used : ParserException(com.questdb.ex.ParserException) CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource) RBTreeSortedRecordSource(com.questdb.ql.sort.RBTreeSortedRecordSource) IntervalRecordSource(com.questdb.ql.interval.IntervalRecordSource) AsOfJoinRecordSource(com.questdb.ql.join.AsOfJoinRecordSource) SelectedColumnsRecordSource(com.questdb.ql.select.SelectedColumnsRecordSource) HashJoinRecordSource(com.questdb.ql.join.HashJoinRecordSource) VirtualColumnRecordSource(com.questdb.ql.virtual.VirtualColumnRecordSource) AsOfPartitionedJoinRecordSource(com.questdb.ql.join.AsOfPartitionedJoinRecordSource) CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource)

Example 2 with CrossJoinRecordSource

use of com.questdb.ql.join.CrossJoinRecordSource in project questdb by bluestreak01.

the class JoinStringToSymbolTest method testCrossJoin.

@Test
public void testCrossJoin() throws Exception {
    bw.append(new Band().setName("band1").setType("rock").setUrl("http://band1.com"));
    bw.append(new Band().setName("band2").setType("hiphop").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 BZ").setBand("band1").setGenre("rock"));
    aw.append(new Album().setName("album Y").setBand("band3").setGenre("metal"));
    aw.commit();
    StringSink sink = new StringSink();
    RecordSourcePrinter p = new RecordSourcePrinter(sink);
    try (RecordSource rs = new CrossJoinRecordSource(new JournalRecordSource(new JournalPartitionSource(aw.getMetadata(), false), new AllRowSource()), new JournalRecordSource(new JournalPartitionSource(bw.getMetadata(), false), new AllRowSource()))) {
        p.print(rs, getFactory());
    }
    final String expected = "band1\talbum X\tpop\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://band1.com\trock\t\n" + "band1\talbum X\tpop\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband2\thttp://band2.com\thiphop\t\n" + "band1\talbum X\tpop\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband3\thttp://band3.com\tjazz\t\n" + "band1\talbum X\tpop\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://new.band1.com\tjazz\t\n" + "band1\talbum BZ\trock\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://band1.com\trock\t\n" + "band1\talbum BZ\trock\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband2\thttp://band2.com\thiphop\t\n" + "band1\talbum BZ\trock\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband3\thttp://band3.com\tjazz\t\n" + "band1\talbum BZ\trock\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://new.band1.com\tjazz\t\n" + "band3\talbum Y\tmetal\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://band1.com\trock\t\n" + "band3\talbum Y\tmetal\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband2\thttp://band2.com\thiphop\t\n" + "band3\talbum Y\tmetal\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband3\thttp://band3.com\tjazz\t\n" + "band3\talbum Y\tmetal\t1970-01-01T00:00:00.000Z\t1970-01-01T00:00:00.000Z\tband1\thttp://new.band1.com\tjazz\t\n";
    TestUtils.assertEquals(expected, sink);
}
Also used : CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource) CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource) Album(com.questdb.model.Album) Band(com.questdb.model.Band) StringSink(com.questdb.std.str.StringSink)

Aggregations

CrossJoinRecordSource (com.questdb.ql.join.CrossJoinRecordSource)2 ParserException (com.questdb.ex.ParserException)1 Album (com.questdb.model.Album)1 Band (com.questdb.model.Band)1 IntervalRecordSource (com.questdb.ql.interval.IntervalRecordSource)1 AsOfJoinRecordSource (com.questdb.ql.join.AsOfJoinRecordSource)1 AsOfPartitionedJoinRecordSource (com.questdb.ql.join.AsOfPartitionedJoinRecordSource)1 HashJoinRecordSource (com.questdb.ql.join.HashJoinRecordSource)1 SelectedColumnsRecordSource (com.questdb.ql.select.SelectedColumnsRecordSource)1 RBTreeSortedRecordSource (com.questdb.ql.sort.RBTreeSortedRecordSource)1 VirtualColumnRecordSource (com.questdb.ql.virtual.VirtualColumnRecordSource)1 StringSink (com.questdb.std.str.StringSink)1