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);
}
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);
}
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);
}
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);
}
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());
}
Aggregations