Search in sources :

Example 11 with ComparableList

use of org.apache.druid.segment.data.ComparableList in project druid by druid-io.

the class GroupByQuery method compareDimsForLimitPushDown.

private static int compareDimsForLimitPushDown(final IntList fields, final List<Boolean> needsReverseList, final List<ColumnType> dimensionTypes, final List<StringComparator> comparators, final ResultRow lhs, final ResultRow rhs) {
    for (int i = 0; i < fields.size(); i++) {
        final int fieldNumber = fields.getInt(i);
        final StringComparator comparator = comparators.get(i);
        final ColumnType dimensionType = dimensionTypes.get(i);
        final int dimCompare;
        final Object lhsObj = lhs.get(fieldNumber);
        final Object rhsObj = rhs.get(fieldNumber);
        if (dimensionType.isNumeric()) {
            if (comparator.equals(StringComparators.NUMERIC)) {
                dimCompare = DimensionHandlerUtils.compareObjectsAsType(lhsObj, rhsObj, dimensionType);
            } else {
                dimCompare = comparator.compare(String.valueOf(lhsObj), String.valueOf(rhsObj));
            }
        } else if (dimensionType.equals(ColumnType.STRING_ARRAY)) {
            final ComparableStringArray lhsArr = DimensionHandlerUtils.convertToComparableStringArray(lhsObj);
            final ComparableStringArray rhsArr = DimensionHandlerUtils.convertToComparableStringArray(rhsObj);
            dimCompare = Comparators.<Comparable>naturalNullsFirst().compare(lhsArr, rhsArr);
        } else if (dimensionType.equals(ColumnType.LONG_ARRAY) || dimensionType.equals(ColumnType.DOUBLE_ARRAY)) {
            final ComparableList lhsArr = DimensionHandlerUtils.convertToList(lhsObj);
            final ComparableList rhsArr = DimensionHandlerUtils.convertToList(rhsObj);
            dimCompare = Comparators.<Comparable>naturalNullsFirst().compare(lhsArr, rhsArr);
        } else {
            dimCompare = comparator.compare((String) lhsObj, (String) rhsObj);
        }
        if (dimCompare != 0) {
            return needsReverseList.get(i) ? -dimCompare : dimCompare;
        }
    }
    return 0;
}
Also used : ColumnType(org.apache.druid.segment.column.ColumnType) ComparableList(org.apache.druid.segment.data.ComparableList) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) StringComparator(org.apache.druid.query.ordering.StringComparator)

Example 12 with ComparableList

use of org.apache.druid.segment.data.ComparableList in project druid by druid-io.

the class ArrayNumericGroupByColumnSelectorStrategy method processValueFromGroupingKey.

@Override
public void processValueFromGroupingKey(GroupByColumnSelectorPlus selectorPlus, ByteBuffer key, ResultRow resultRow, int keyBufferPosition) {
    final int id = key.getInt(keyBufferPosition);
    // GROUP_BY_MISSING_VALUE is used to indicate empty rows, which are omitted from the result map.
    if (id != GROUP_BY_MISSING_VALUE) {
        final List<T> value = dictionary.get(id);
        resultRow.set(selectorPlus.getResultRowPosition(), new ComparableList(value));
    } else {
        resultRow.set(selectorPlus.getResultRowPosition(), null);
    }
}
Also used : ComparableList(org.apache.druid.segment.data.ComparableList)

Example 13 with ComparableList

use of org.apache.druid.segment.data.ComparableList in project druid by druid-io.

the class ArrayDoubleGroupByColumnSelectorStrategyTest method testAddingInDictionaryWithObjects.

@Test
public void testAddingInDictionaryWithObjects() {
    ColumnValueSelector columnValueSelector = Mockito.mock(ColumnValueSelector.class);
    Mockito.when(columnValueSelector.getObject()).thenReturn(new Object[] { 4.0D, 2.0D });
    Assert.assertEquals(3, strategy.getOnlyValue(columnValueSelector));
    GroupByColumnSelectorPlus groupByColumnSelectorPlus = Mockito.mock(GroupByColumnSelectorPlus.class);
    Mockito.when(groupByColumnSelectorPlus.getResultRowPosition()).thenReturn(0);
    ResultRow row = ResultRow.create(1);
    buffer1.putInt(3);
    strategy.processValueFromGroupingKey(groupByColumnSelectorPlus, buffer1, row, 0);
    Assert.assertEquals(new ComparableList(ImmutableList.of(4.0, 2.0)), row.get(0));
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) ComparableList(org.apache.druid.segment.data.ComparableList) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Test(org.junit.Test)

Example 14 with ComparableList

use of org.apache.druid.segment.data.ComparableList in project druid by druid-io.

the class ArrayDoubleGroupByColumnSelectorStrategyTest method testSanity.

@Test
public void testSanity() {
    ColumnValueSelector columnValueSelector = Mockito.mock(ColumnValueSelector.class);
    Mockito.when(columnValueSelector.getObject()).thenReturn(ImmutableList.of(1.0, 2.0));
    Assert.assertEquals(0, strategy.getOnlyValue(columnValueSelector));
    GroupByColumnSelectorPlus groupByColumnSelectorPlus = Mockito.mock(GroupByColumnSelectorPlus.class);
    Mockito.when(groupByColumnSelectorPlus.getResultRowPosition()).thenReturn(0);
    ResultRow row = ResultRow.create(1);
    buffer1.putInt(0);
    strategy.processValueFromGroupingKey(groupByColumnSelectorPlus, buffer1, row, 0);
    Assert.assertEquals(new ComparableList(ImmutableList.of(1.0, 2.0)), row.get(0));
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) ComparableList(org.apache.druid.segment.data.ComparableList) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Test(org.junit.Test)

Example 15 with ComparableList

use of org.apache.druid.segment.data.ComparableList in project druid by druid-io.

the class ClientQuerySegmentWalkerTest method testGroupByOnArraysLongsAsString.

@Test
public void testGroupByOnArraysLongsAsString() {
    final GroupByQuery query = (GroupByQuery) GroupByQuery.builder().setDataSource(ARRAY).setGranularity(Granularities.ALL).setInterval(Collections.singletonList(INTERVAL)).setDimensions(DefaultDimensionSpec.of("al")).build().withId(DUMMY_QUERY_ID);
    // when we donot define an outputType, convert {@link ComparableList} to a string
    testQuery(query, ImmutableList.of(ExpectedQuery.cluster(query)), ImmutableList.of(new Object[] { new ComparableList(ImmutableList.of(1L, 2L)).toString() }, new Object[] { new ComparableList(ImmutableList.of(2L, 4L)).toString() }, new Object[] { new ComparableList(ImmutableList.of(3L, 6L)).toString() }, new Object[] { new ComparableList(ImmutableList.of(4L, 8L)).toString() }));
}
Also used : GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) ComparableList(org.apache.druid.segment.data.ComparableList) Test(org.junit.Test)

Aggregations

ComparableList (org.apache.druid.segment.data.ComparableList)15 Test (org.junit.Test)13 ResultRow (org.apache.druid.query.groupby.ResultRow)6 ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)6 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)4 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)3 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)3 DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)2 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)2 QueryDataSource (org.apache.druid.query.QueryDataSource)1 StringComparator (org.apache.druid.query.ordering.StringComparator)1 ColumnType (org.apache.druid.segment.column.ColumnType)1 ComparableStringArray (org.apache.druid.segment.data.ComparableStringArray)1