Search in sources :

Example 16 with OrderBy

use of org.teiid.query.sql.lang.OrderBy in project teiid by teiid.

the class TempTable method createTupleSource.

public TupleSource createTupleSource(final List<? extends Expression> projectedCols, final Criteria condition, OrderBy orderBy) throws TeiidComponentException, TeiidProcessingException {
    // special handling for count(*)
    boolean agg = false;
    for (Expression singleElementSymbol : projectedCols) {
        if (singleElementSymbol instanceof ExpressionSymbol && ((ExpressionSymbol) singleElementSymbol).getExpression() instanceof AggregateSymbol) {
            agg = true;
            break;
        }
    }
    if (agg) {
        if (condition == null) {
            long count = this.getRowCount();
            return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), (int) Math.min(Integer.MAX_VALUE, count))).iterator());
        }
        orderBy = null;
    }
    IndexInfo primary = new IndexInfo(this, projectedCols, condition, orderBy, true);
    IndexInfo ii = primary;
    if ((indexTables != null || (!this.updatable && allowImplicitIndexing && condition != null && this.getRowCount() > 2 * this.getTree().getPageSize(true))) && (condition != null || orderBy != null) && ii.valueSet.size() != 1) {
        // $NON-NLS-1$ //$NON-NLS-2$
        LogManager.logDetail(LogConstants.CTX_DQP, "Considering indexes on table", this, "for query", projectedCols, condition, orderBy);
        long rowCost = this.tree.getRowCount();
        long bestCost = estimateCost(orderBy, ii, rowCost);
        if (this.indexTables != null) {
            for (TempTable table : this.indexTables.values()) {
                IndexInfo secondary = new IndexInfo(table, projectedCols, condition, orderBy, false);
                long cost = estimateCost(orderBy, secondary, rowCost);
                if (cost < bestCost) {
                    ii = secondary;
                    bestCost = cost;
                }
            }
        }
        if (ii == primary && allowImplicitIndexing) {
            // TODO: detect if it should be covering
            if (createImplicitIndexIfNeeded(condition)) {
                IndexInfo secondary = new IndexInfo(this.indexTables.values().iterator().next(), projectedCols, condition, orderBy, false);
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Created an implicit index ", secondary.table);
                long cost = estimateCost(orderBy, secondary, rowCost);
                if (cost < bestCost) {
                    ii = secondary;
                    bestCost = cost;
                } else {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    LogManager.logDetail(LogConstants.CTX_DQP, "Did not utilize the implicit index");
                }
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        LogManager.logDetail(LogConstants.CTX_DQP, "Choose index", ii.table, "covering:", ii.coveredCriteria, "ordering:", ii.ordering);
        if (ii.covering) {
            return ii.table.createTupleSource(projectedCols, condition, orderBy, ii, agg);
        }
        List<ElementSymbol> pkColumns = this.columns.subList(0, this.tree.getKeyLength());
        if (ii.ordering != null) {
            // use order and join
            primary.valueTs = ii.table.createTupleSource(pkColumns, ii.coveredCriteria, orderBy, ii, agg);
            primary.ordering = null;
            return createTupleSource(projectedCols, ii.nonCoveredCriteria, null, primary, agg);
        }
        // order by pk to localize lookup costs, then join
        OrderBy pkOrderBy = new OrderBy();
        for (ElementSymbol elementSymbol : pkColumns) {
            pkOrderBy.addVariable(elementSymbol);
        }
        primary.valueTs = ii.table.createTupleSource(pkColumns, ii.coveredCriteria, pkOrderBy, ii, agg);
        return createTupleSource(projectedCols, ii.nonCoveredCriteria, orderBy, primary, agg);
    }
    return createTupleSource(projectedCols, condition, orderBy, ii, agg);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Expression(org.teiid.query.sql.symbol.Expression) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol)

Example 17 with OrderBy

use of org.teiid.query.sql.lang.OrderBy in project teiid by teiid.

the class TestGroupingNode method helpTestLookupFunctionInAggregate.

private void helpTestLookupFunctionInAggregate(int batchSize) throws Exception {
    BufferManagerImpl mgr = BufferManagerFactory.createBufferManager();
    mgr.setProcessorBatchSize(batchSize);
    // Set up
    GroupingNode node = new GroupingNode(1);
    List outputElements = new ArrayList();
    // $NON-NLS-1$
    ElementSymbol col1 = new ElementSymbol("col1");
    col1.setType(Integer.class);
    // $NON-NLS-1$
    ElementSymbol col2 = new ElementSymbol("col2");
    col2.setType(Integer.class);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    Function func = new Function("lookup", new Expression[] { new Constant("pm1.g1"), new Constant("e2"), new Constant("e1"), col2 });
    // $NON-NLS-1$
    FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, Integer.class });
    func.setFunctionDescriptor(desc);
    func.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    outputElements.add(col1);
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("COUNT", false, func));
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("SUM", false, func));
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("SUM", true, func));
    node.setElements(outputElements);
    List groupingElements = new ArrayList();
    groupingElements.add(col1);
    node.setOrderBy(new OrderBy(groupingElements).getOrderByItems());
    // $NON-NLS-1$ //$NON-NLS-2$
    CommandContext context = new CommandContext("pid", "test", null, null, 1);
    FakeDataManager dataMgr = new FakeDataManager();
    dataMgr.setThrowBlocked(true);
    Map valueMap = new HashMap();
    valueMap.put(new Integer(0), new Integer(1));
    valueMap.put(new Integer(1), new Integer(2));
    valueMap.put(new Integer(2), new Integer(3));
    valueMap.put(new Integer(3), new Integer(4));
    valueMap.put(new Integer(4), new Integer(5));
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    dataMgr.defineCodeTable("pm1.g1", "e1", "e2", valueMap);
    List[] expected = new List[] { Arrays.asList(new Object[] { null, new Integer(1), new Long(4), new Long(4) }), Arrays.asList(new Object[] { new Integer(0), new Integer(1), new Long(5), new Long(5) }), Arrays.asList(new Object[] { new Integer(1), new Integer(1), new Long(3), new Long(3) }), Arrays.asList(new Object[] { new Integer(2), new Integer(4), new Long(9), new Long(5) }), Arrays.asList(new Object[] { new Integer(3), new Integer(1), new Long(1), new Long(1) }), Arrays.asList(new Object[] { new Integer(4), new Integer(2), new Long(7), new Long(7) }), Arrays.asList(new Object[] { new Integer(5), new Integer(1), new Long(4), new Long(4) }), Arrays.asList(new Object[] { new Integer(6), new Integer(2), new Long(9), new Long(9) }) };
    helpProcess(mgr, node, context, expected, dataMgr);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) BufferManagerImpl(org.teiid.common.buffer.impl.BufferManagerImpl) CommandContext(org.teiid.query.util.CommandContext) FakeDataManager(org.teiid.query.processor.FakeDataManager) HashMap(java.util.HashMap) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) Function(org.teiid.query.sql.symbol.Function) AggregateFunction(org.teiid.query.function.aggregate.AggregateFunction) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 18 with OrderBy

use of org.teiid.query.sql.lang.OrderBy in project teiid by teiid.

the class TestGroupingNode method helpTestEmptyGroup.

public void helpTestEmptyGroup(boolean groupBy) throws Exception {
    BufferManager mgr = BufferManagerFactory.getStandaloneBufferManager();
    // $NON-NLS-1$
    ElementSymbol col1 = new ElementSymbol("col1");
    col1.setType(Integer.class);
    // $NON-NLS-1$
    ElementSymbol bigDecimal = new ElementSymbol("value");
    bigDecimal.setType(DataTypeManager.DefaultDataClasses.BIG_DECIMAL);
    // Set up
    GroupingNode node = new GroupingNode(1);
    List outputElements = new ArrayList();
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("SUM", false, bigDecimal));
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("AVG", false, bigDecimal));
    node.setElements(outputElements);
    // Set grouping elements to null
    if (groupBy) {
        List groupingElements = new ArrayList();
        // $NON-NLS-1$
        groupingElements.add(col1.clone());
        node.setOrderBy(new OrderBy(groupingElements).getOrderByItems());
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    CommandContext context = new CommandContext("pid", "test", null, null, 1);
    List[] data = new List[] {};
    List[] expected = new List[] { Arrays.asList(new Object[] { null, null }) };
    if (groupBy) {
        expected = new List[] {};
    }
    List symbols = new ArrayList();
    symbols.add(col1);
    symbols.add(bigDecimal);
    FakeTupleSource dataSource = new FakeTupleSource(symbols, data);
    helpProcess(mgr, node, context, expected, dataSource, null);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) CommandContext(org.teiid.query.util.CommandContext) ArrayList(java.util.ArrayList) FakeTupleSource(org.teiid.query.processor.FakeTupleSource) ArrayList(java.util.ArrayList) List(java.util.List) BufferManager(org.teiid.common.buffer.BufferManager)

Example 19 with OrderBy

use of org.teiid.query.sql.lang.OrderBy in project teiid by teiid.

the class TestGroupingNode method testdefect9842.

@Test
public void testdefect9842() throws Exception {
    BufferManager mgr = BufferManagerFactory.getStandaloneBufferManager();
    // $NON-NLS-1$
    ElementSymbol col1 = new ElementSymbol("col1");
    col1.setType(Integer.class);
    // $NON-NLS-1$
    ElementSymbol bigDecimal = new ElementSymbol("value");
    bigDecimal.setType(DataTypeManager.DefaultDataClasses.BIG_DECIMAL);
    // Set up
    GroupingNode node = new GroupingNode(1);
    List outputElements = new ArrayList();
    outputElements.add(col1);
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("SUM", false, bigDecimal));
    // $NON-NLS-1$ //$NON-NLS-2$
    outputElements.add(new AggregateSymbol("AVG", false, bigDecimal));
    node.setElements(outputElements);
    // Set grouping elements to null
    List groupingElements = new ArrayList();
    groupingElements.add(col1);
    node.setOrderBy(new OrderBy(groupingElements).getOrderByItems());
    // $NON-NLS-1$ //$NON-NLS-2$
    CommandContext context = new CommandContext("pid", "test", null, null, 1);
    List[] data = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(1), new BigDecimal("0.0") }), // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(1), new BigDecimal("1.0") }), // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(2), new BigDecimal("2.0") }), // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(2), new BigDecimal("3.0") }), // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(2), new BigDecimal("4.0") }) };
    List[] expected = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(1), new BigDecimal("1.0"), new BigDecimal(.5) }), // $NON-NLS-1$
    Arrays.asList(new Object[] { new Integer(2), new BigDecimal("9.0"), new BigDecimal("3.0") }) };
    List symbols = new ArrayList();
    symbols.add(col1);
    symbols.add(bigDecimal);
    FakeTupleSource dataSource = new FakeTupleSource(symbols, data);
    helpProcess(mgr, node, context, expected, dataSource, null);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) CommandContext(org.teiid.query.util.CommandContext) ArrayList(java.util.ArrayList) FakeTupleSource(org.teiid.query.processor.FakeTupleSource) ArrayList(java.util.ArrayList) List(java.util.List) BufferManager(org.teiid.common.buffer.BufferManager) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 20 with OrderBy

use of org.teiid.query.sql.lang.OrderBy in project teiid by teiid.

the class TestSortNode method helpTestSort.

private void helpTestSort(List elements, List[] data, List sortElements, List sortTypes, List[] expected, Mode mode) throws TeiidComponentException, TeiidProcessingException {
    BufferManagerImpl mgr = BufferManagerFactory.getTestBufferManager(10000, BATCH_SIZE);
    long reserve = mgr.getReserveBatchBytes();
    // $NON-NLS-1$ //$NON-NLS-2$
    CommandContext context = new CommandContext("pid", "test", null, null, 1);
    BlockingFakeRelationalNode dataNode = new BlockingFakeRelationalNode(2, data);
    dataNode.setReturnPeriod(3);
    dataNode.setElements(elements);
    dataNode.initialize(context, mgr, null);
    SortNode sortNode = new SortNode(1);
    sortNode.setSortElements(new OrderBy(sortElements, sortTypes).getOrderByItems());
    sortNode.setMode(mode);
    sortNode.setElements(elements);
    sortNode.addChild(dataNode);
    sortNode.initialize(context, mgr, null);
    sortNode.open();
    assertTrue(sortNode.hasBuffer());
    int currentRow = 1;
    while (true) {
        try {
            TupleBatch batch = sortNode.nextBatch();
            for (int row = currentRow; row <= batch.getEndRow(); row++) {
                // $NON-NLS-1$
                assertEquals("Rows don't match at " + row, expected[row - 1], batch.getTuple(row));
            }
            currentRow += batch.getRowCount();
            if (batch.getTerminationFlag()) {
                break;
            }
        } catch (BlockedException e) {
        }
    }
    assertEquals(expected.length, currentRow - 1);
    assertEquals(reserve, mgr.getReserveBatchBytes());
}
Also used : OrderBy(org.teiid.query.sql.lang.OrderBy) BufferManagerImpl(org.teiid.common.buffer.impl.BufferManagerImpl) CommandContext(org.teiid.query.util.CommandContext) DupRemoveSortNode(org.teiid.query.optimizer.TestOptimizer.DupRemoveSortNode) BlockedException(org.teiid.common.buffer.BlockedException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Aggregations

OrderBy (org.teiid.query.sql.lang.OrderBy)23 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)15 List (java.util.List)13 ArrayList (java.util.ArrayList)12 Expression (org.teiid.query.sql.symbol.Expression)12 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)10 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)9 OrderByItem (org.teiid.query.sql.lang.OrderByItem)7 CommandContext (org.teiid.query.util.CommandContext)7 SymbolMap (org.teiid.query.sql.util.SymbolMap)6 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)4 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)4 LinkedHashSet (java.util.LinkedHashSet)3 LinkedList (java.util.LinkedList)3 BufferManager (org.teiid.common.buffer.BufferManager)3 Criteria (org.teiid.query.sql.lang.Criteria)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2