use of org.apache.drill.common.logical.data.Order.Ordering in project drill by axbaretto.
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 axbaretto.
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 axbaretto.
the class OrderTest method testOrdering_nullStrings.
// Defaults-value/null-strings test:
@Test
public void testOrdering_nullStrings() {
Ordering ordering = new Ordering((String) null, (LogicalExpression) null, null);
assertThat(ordering.getDirection(), equalTo(RelFieldCollation.Direction.ASCENDING));
assertThat(ordering.getNullDirection(), equalTo(RelFieldCollation.NullDirection.UNSPECIFIED));
assertThat(ordering.getOrder(), equalTo("ASC"));
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class SortBatch method createNewSorter.
public static Sorter createNewSorter(FragmentContext context, List<Ordering> orderings, VectorAccessible batch, MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping) {
CodeGenerator<Sorter> cg = CodeGenerator.get(Sorter.TEMPLATE_DEFINITION, context.getOptions());
// This operator may be deprecated. No tests exercise it.
// There is no way, at present, to verify if the generated code
// works with Plain-old Java.
// cg.plainOldJavaCapable(true);
// Uncomment out this line to debug the generated code.
// cg.saveCodeForDebugging(true);
ClassGenerator<Sorter> g = cg.getRoot();
g.setMappingSet(mainMapping);
for (Ordering od : orderings) {
// first, we rewrite the evaluation stack for each side of the comparison.
ErrorCollector collector = new ErrorCollectorImpl();
final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
collector.reportErrors(logger);
g.setMappingSet(leftMapping);
HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(rightMapping);
HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(mainMapping);
// next we wrap the two comparison sides and add the expression block for the comparison.
LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
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));
return context.getImplementationClass(cg);
}
use of org.apache.drill.common.logical.data.Order.Ordering in project drill by apache.
the class MergeSortWrapper method createNewMSorter.
private MSorter createNewMSorter(List<Ordering> orderings, MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping) {
CodeGenerator<MSorter> cg = CodeGenerator.get(MSorter.TEMPLATE_DEFINITION, context.getFragmentContext().getOptions());
cg.plainJavaCapable(true);
// Uncomment out this line to debug the generated code.
// cg.saveCodeForDebugging(true);
ClassGenerator<MSorter> g = cg.getRoot();
g.setMappingSet(mainMapping);
for (Ordering od : orderings) {
// first, we rewrite the evaluation stack for each side of the comparison.
ErrorCollector collector = new ErrorCollectorImpl();
final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), destContainer, collector, context.getFragmentContext().getFunctionRegistry());
if (collector.hasErrors()) {
throw UserException.unsupportedError().message("Failure while materializing expression. " + collector.toErrorString()).build(logger);
}
g.setMappingSet(leftMapping);
HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(rightMapping);
HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(mainMapping);
// next we wrap the two comparison sides and add the expression block for the comparison.
LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFragmentContext().getFunctionRegistry());
HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
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.rotateBlock();
}
g.rotateBlock();
g.getEvalBlock()._return(JExpr.lit(0));
return getInstance(cg, logger);
}
Aggregations