Search in sources :

Example 1 with LongTreeChain

use of io.questdb.griffin.engine.orderby.LongTreeChain in project questdb by bluestreak01.

the class CachedAnalyticRecordCursorFactory method getCursor.

@Override
public RecordCursor getCursor(SqlExecutionContext executionContext) throws SqlException {
    recordChain.clear();
    clearTrees();
    resetFunctions();
    final RecordCursor baseCursor = base.getCursor(executionContext);
    // step #1: store source cursor in record list
    // - add record list' row ids to all trees, which will put these row ids in necessary order
    // for this we will be using out comparator, which helps tree compare long values
    // based on record these values are addressing
    long offset = -1;
    final Record record = baseCursor.getRecord();
    final Record chainRightRecord = recordChain.getRecordB();
    if (orderedGroupCount > 0) {
        while (baseCursor.hasNext()) {
            offset = recordChain.put(record, offset);
            recordChain.recordAt(recordChainRecord, offset);
            for (int i = 0; i < orderedGroupCount; i++) {
                orderedSources.getQuick(i).put(recordChainRecord, recordChain, chainRightRecord, comparators.getQuick(i));
            }
        }
    } else {
        while (baseCursor.hasNext()) {
            offset = recordChain.put(record, offset);
        }
    }
    if (orderedGroupCount > 0) {
        for (int i = 0; i < orderedGroupCount; i++) {
            final LongTreeChain tree = orderedSources.getQuick(i);
            final ObjList<AnalyticFunction> functions = orderedFunctions.getQuick(i);
            // step #2: populate all analytic functions with records in order of respective tree
            final LongTreeChain.TreeCursor cursor = tree.getCursor();
            final int functionCount = functions.size();
            while (cursor.hasNext()) {
                offset = cursor.next();
                recordChain.recordAt(recordChainRecord, offset);
                for (int j = 0; j < functionCount; j++) {
                    functions.getQuick(j).pass1(recordChainRecord, offset, recordChain);
                }
            }
        }
    }
    // run pass1 for all unordered functions
    if (unorderedFunctions != null) {
        for (int j = 0, n = unorderedFunctions.size(); j < n; j++) {
            final AnalyticFunction f = unorderedFunctions.getQuick(j);
            recordChain.toTop();
            while (recordChain.hasNext()) {
                f.pass1(recordChainRecord, recordChainRecord.getRowId(), recordChain);
            }
        }
    }
    recordChain.toTop();
    return recordChain;
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) LongTreeChain(io.questdb.griffin.engine.orderby.LongTreeChain)

Aggregations

Record (io.questdb.cairo.sql.Record)1 RecordCursor (io.questdb.cairo.sql.RecordCursor)1 LongTreeChain (io.questdb.griffin.engine.orderby.LongTreeChain)1