Skip to content

Commit

Permalink
TEIID-2731 adding a stable sort option
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Nov 8, 2013
1 parent a4ebf9b commit de9eccd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -39,6 +39,7 @@
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.core.util.Assertion;
import org.teiid.core.util.PropertiesUtils;
import org.teiid.language.SortSpecification.NullOrdering;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
Expand Down Expand Up @@ -114,6 +115,10 @@ public String toString() {
private TupleBuffer workingBuffer;
private long[] attempts = new long[2];
private boolean nonBlocking;

private static boolean STABLE_SORT = PropertiesUtils.getBooleanProperty(System.getProperties(), "org.teiid.requireStableSort", false); //$NON-NLS-1$

private boolean stableSort = STABLE_SORT;

public SortUtility(TupleSource sourceID, List<OrderByItem> items, Mode mode, BufferManager bufferMgr,
String groupName, List<? extends Expression> schema) {
Expand Down Expand Up @@ -325,7 +330,7 @@ protected void initialSort(boolean onePass) throws TeiidComponentException, Teii
}
}
TupleBufferTupleSource ts = workingBuffer.createIndexedTupleSource(source != null);
ts.setReverse(workingBuffer.getRowCount() > this.batchSize);
ts.setReverse((!stableSort || mode == Mode.DUP_REMOVE) && workingBuffer.getRowCount() > this.batchSize);
processed+=this.workingBuffer.getRowCount();
maxRows = Math.max(1, (totalReservedBuffers/schemaSize))*batchSize;
if (mode == Mode.SORT) {
Expand Down Expand Up @@ -560,5 +565,13 @@ public void remove() {
public void setNonBlocking(boolean b) {
this.nonBlocking = b;
}

public void setStableSort(boolean stableSort) {
this.stableSort = stableSort;
}

void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}

}
Expand Up @@ -384,5 +384,25 @@ private void helpTestAllSorts(int batches) throws Exception {
FakeDataStore.sampleData1(dataMgr, RealMetadataFactory.example1Cached());
TestProcessor.helpProcess(plan, dataMgr, new List[]{Collections.singletonList(null)});
}

@Test public void testStableSort() throws Exception {
ElementSymbol es1 = new ElementSymbol("e1"); //$NON-NLS-1$
es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
TupleBuffer tsid = bm.createTupleBuffer(Arrays.asList(es1, es1), "test", TupleSourceType.PROCESSOR); //$NON-NLS-1$
tsid.addTuple(Arrays.asList(1, 1));
tsid.addTuple(Arrays.asList(1, 2));
tsid.addTuple(Arrays.asList(1, 3));
tsid.close();
SortUtility su = new SortUtility(tsid.createIndexedTupleSource(), Arrays.asList(es1), Arrays.asList(Boolean.TRUE), Mode.SORT, bm, "test", tsid.getSchema()); //$NON-NLS-1$
su.setBatchSize(1);
su.setStableSort(true);
TupleBuffer out = su.sort();
TupleSource ts = out.createIndexedTupleSource();
assertEquals(Arrays.asList(1,1), ts.nextTuple());
assertEquals(Arrays.asList(1,2), ts.nextTuple());
assertEquals(Arrays.asList(1,3), ts.nextTuple());
assertNull(ts.nextTuple());
}

}

0 comments on commit de9eccd

Please sign in to comment.