Search in sources :

Example 16 with Ordering

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());
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) ExternalSort(org.apache.drill.exec.physical.config.ExternalSort) Ordering(org.apache.drill.common.logical.data.Order.Ordering) OperatorTest(org.apache.drill.categories.OperatorTest) Test(org.junit.Test) DrillTest(org.apache.drill.test.DrillTest)

Example 17 with Ordering

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());
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) Ordering(org.apache.drill.common.logical.data.Order.Ordering) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) OperatorTest(org.apache.drill.categories.OperatorTest) Test(org.junit.Test) DrillTest(org.apache.drill.test.DrillTest)

Example 18 with Ordering

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));
}
Also used : Ordering(org.apache.drill.common.logical.data.Order.Ordering) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 19 with Ordering

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));
}
Also used : Ordering(org.apache.drill.common.logical.data.Order.Ordering) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 20 with Ordering

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));
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Aggregations

Ordering (org.apache.drill.common.logical.data.Order.Ordering)47 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)23 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)23 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)23 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)23 JConditional (com.sun.codemodel.JConditional)21 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)15 FieldReference (org.apache.drill.common.expression.FieldReference)14 Test (org.junit.Test)12 Sort (org.apache.drill.exec.physical.config.Sort)8 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)4 OperatorTest (org.apache.drill.categories.OperatorTest)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 ValueVector (org.apache.drill.exec.vector.ValueVector)4 BaseTest (org.apache.drill.test.BaseTest)4 DrillTest (org.apache.drill.test.DrillTest)4 VectorContainer (org.apache.drill.exec.record.VectorContainer)3 IOException (java.io.IOException)2 RelNode (org.apache.calcite.rel.RelNode)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2