use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class TestExternalSortExec method testSortSpec.
@Test
public void testSortSpec() {
FieldReference expr = FieldReference.getWithQuotedRef("foo");
Ordering ordering = new Ordering(Ordering.ORDER_ASC, expr, Ordering.NULLS_FIRST);
// Basics
ExternalSort popConfig = new ExternalSort(null, Lists.newArrayList(ordering), false);
assertSame(ordering, popConfig.getOrderings().get(0));
assertFalse(popConfig.getReverse());
assertEquals(SelectionVectorMode.FOUR_BYTE, popConfig.getSVMode());
assertEquals(ExternalSort.OPERATOR_TYPE, popConfig.getOperatorType());
assertEquals(ExternalSort.DEFAULT_SORT_ALLOCATION, popConfig.getInitialAllocation());
assertEquals(AbstractBase.MAX_ALLOCATION, popConfig.getMaxAllocation());
assertTrue(popConfig.isExecutable());
// Non-default settings
popConfig = new ExternalSort(null, Lists.newArrayList(ordering), true);
assertTrue(popConfig.getReverse());
long maxAlloc = 50_000_000;
popConfig.setMaxAllocation(maxAlloc);
assertEquals(ExternalSort.DEFAULT_SORT_ALLOCATION, popConfig.getInitialAllocation());
assertEquals(maxAlloc, popConfig.getMaxAllocation());
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class TestExternalSortExec method testOrdering.
@Test
public void testOrdering() {
assertEquals(Direction.ASCENDING, Ordering.getOrderingSpecFromString(null));
assertEquals(Direction.ASCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_ASC));
assertEquals(Direction.DESCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_DESC));
assertEquals(Direction.ASCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_ASCENDING));
assertEquals(Direction.DESCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_DESCENDING));
assertEquals(Direction.ASCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_ASC.toLowerCase()));
assertEquals(Direction.DESCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_DESC.toLowerCase()));
assertEquals(Direction.ASCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_ASCENDING.toLowerCase()));
assertEquals(Direction.DESCENDING, Ordering.getOrderingSpecFromString(Ordering.ORDER_DESCENDING.toLowerCase()));
try {
Ordering.getOrderingSpecFromString("");
fail();
} catch (DrillRuntimeException e) {
}
try {
Ordering.getOrderingSpecFromString("foo");
fail();
} catch (DrillRuntimeException e) {
}
assertEquals(NullDirection.UNSPECIFIED, Ordering.getNullOrderingFromString(null));
assertEquals(NullDirection.FIRST, Ordering.getNullOrderingFromString(Ordering.NULLS_FIRST));
assertEquals(NullDirection.LAST, Ordering.getNullOrderingFromString(Ordering.NULLS_LAST));
assertEquals(NullDirection.UNSPECIFIED, Ordering.getNullOrderingFromString(Ordering.NULLS_UNSPECIFIED));
assertEquals(NullDirection.FIRST, Ordering.getNullOrderingFromString(Ordering.NULLS_FIRST.toLowerCase()));
assertEquals(NullDirection.LAST, Ordering.getNullOrderingFromString(Ordering.NULLS_LAST.toLowerCase()));
assertEquals(NullDirection.UNSPECIFIED, Ordering.getNullOrderingFromString(Ordering.NULLS_UNSPECIFIED.toLowerCase()));
try {
Ordering.getNullOrderingFromString("");
fail();
} catch (DrillRuntimeException e) {
}
try {
Ordering.getNullOrderingFromString("foo");
fail();
} catch (DrillRuntimeException e) {
}
FieldReference expr = FieldReference.getWithQuotedRef("foo");
// Test all getters
Ordering ordering = new Ordering((String) null, expr, (String) null);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
assertSame(expr, ordering.getExpr());
assertTrue(ordering.nullsSortHigh());
// Test all ordering strings
ordering = new Ordering((String) Ordering.ORDER_ASC, expr, (String) null);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
ordering = new Ordering((String) Ordering.ORDER_ASC.toLowerCase(), expr, (String) null);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
ordering = new Ordering((String) Ordering.ORDER_ASCENDING, expr, (String) null);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
ordering = new Ordering((String) Ordering.ORDER_DESC, expr, (String) null);
assertEquals(Direction.DESCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
ordering = new Ordering((String) Ordering.ORDER_DESCENDING, expr, (String) null);
assertEquals(Direction.DESCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
// Test all null ordering strings
ordering = new Ordering((String) null, expr, Ordering.NULLS_FIRST);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.FIRST, ordering.getNullDirection());
assertFalse(ordering.nullsSortHigh());
ordering = new Ordering((String) null, expr, Ordering.NULLS_FIRST);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.FIRST, ordering.getNullDirection());
assertFalse(ordering.nullsSortHigh());
ordering = new Ordering((String) null, expr, Ordering.NULLS_LAST);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.LAST, ordering.getNullDirection());
assertTrue(ordering.nullsSortHigh());
ordering = new Ordering((String) null, expr, Ordering.NULLS_UNSPECIFIED);
assertEquals(Direction.ASCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
assertTrue(ordering.nullsSortHigh());
// Unspecified order is always nulls high
ordering = new Ordering(Ordering.ORDER_DESC, expr, Ordering.NULLS_UNSPECIFIED);
assertEquals(Direction.DESCENDING, ordering.getDirection());
assertEquals(NullDirection.UNSPECIFIED, ordering.getNullDirection());
assertTrue(ordering.nullsSortHigh());
// Null sort direction reverses with a Desc sort.
ordering = new Ordering(Ordering.ORDER_DESC, expr, Ordering.NULLS_FIRST);
assertEquals(Direction.DESCENDING, ordering.getDirection());
assertEquals(NullDirection.FIRST, ordering.getNullDirection());
assertTrue(ordering.nullsSortHigh());
ordering = new Ordering(Ordering.ORDER_DESC, expr, Ordering.NULLS_LAST);
assertEquals(Direction.DESCENDING, ordering.getDirection());
assertEquals(NullDirection.LAST, ordering.getNullDirection());
assertFalse(ordering.nullsSortHigh());
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class OrderTest method test_Ordering_roundTripDescAndNullsUnspecified.
@Test
public void test_Ordering_roundTripDescAndNullsUnspecified() {
Ordering src = new Ordering(Direction.DESCENDING, null, NullDirection.UNSPECIFIED);
Ordering reconstituted = new Ordering(src.getDirection(), (LogicalExpression) null, src.getNullDirection());
assertThat(reconstituted.getDirection(), equalTo(RelFieldCollation.Direction.DESCENDING));
assertThat(reconstituted.getNullDirection(), equalTo(NullDirection.UNSPECIFIED));
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class OrderTest method test_Ordering_roundTripAscAndNullsFirst.
// ////////
// Order.Ordering tests:
// "Round trip" tests that strings from output work as input:
@Test
public void test_Ordering_roundTripAscAndNullsFirst() {
Ordering src = new Ordering(Direction.ASCENDING, null, NullDirection.FIRST);
Ordering reconstituted = new Ordering(src.getDirection(), (LogicalExpression) null, src.getNullDirection());
assertThat(reconstituted.getDirection(), equalTo(RelFieldCollation.Direction.ASCENDING));
assertThat(reconstituted.getNullDirection(), equalTo(NullDirection.FIRST));
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class MergingRecordBatch method generateComparisons.
private void generateComparisons(final ClassGenerator<?> g, final VectorAccessible batch) throws SchemaChangeException {
g.setMappingSet(MAIN_MAPPING);
for (final Ordering od : popConfig.getOrderings()) {
// first, we rewrite the evaluation stack for each side of the comparison.
final ErrorCollector collector = new ErrorCollectorImpl();
final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
if (collector.hasErrors()) {
throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
}
g.setMappingSet(LEFT_MAPPING);
final HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(RIGHT_MAPPING);
final HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(MAIN_MAPPING);
// next we wrap the two comparison sides and add the expression block for the comparison.
final LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
final HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
final JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
if (od.getDirection() == Direction.ASCENDING) {
jc._then()._return(out.getValue());
} else {
jc._then()._return(out.getValue().minus());
}
}
g.getEvalBlock()._return(JExpr.lit(0));
}
Aggregations