All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.druid.frame.key.ByteRowKeyComparator Maven / Gradle / Ivy

There is a newer version: 30.0.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.druid.frame.key;

import com.google.common.primitives.Ints;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import org.apache.druid.frame.read.FrameReaderUtils;
import org.apache.druid.java.util.common.IAE;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * Comparator for byte arrays from {@link RowKey#key} instances.
 *
 * Comparison logic in this class is very similar to {@link FrameComparisonWidget}, but is different because it works
 * on byte[] instead of Frames.
 */
public class ByteRowKeyComparator implements Comparator
{
  private final int firstFieldPosition;
  private final int[] ascDescRunLengths;

  private ByteRowKeyComparator(
      final int firstFieldPosition,
      final int[] ascDescRunLengths
  )
  {
    this.firstFieldPosition = firstFieldPosition;
    this.ascDescRunLengths = ascDescRunLengths;
  }

  public static ByteRowKeyComparator create(final List keyColumns)
  {
    return new ByteRowKeyComparator(
        computeFirstFieldPosition(keyColumns.size()),
        computeAscDescRunLengths(keyColumns)
    );
  }

  /**
   * Compute the offset into each key where the first field starts.
   *
   * Public so {@link FrameComparisonWidgetImpl} can use it.
   */
  public static int computeFirstFieldPosition(final int fieldCount)
  {
    return Ints.checkedCast((long) fieldCount * Integer.BYTES);
  }

  /**
   * Given a list of sort columns, compute an array of the number of ascending fields in a run, followed by number of
   * descending fields in a run, followed by ascending, etc. For example: ASC, ASC, DESC, ASC would return [2, 1, 1]
   * and DESC, DESC, ASC would return [0, 2, 1].
   *
   * Public so {@link FrameComparisonWidgetImpl} can use it.
   */
  public static int[] computeAscDescRunLengths(final List keyColumns)
  {
    final IntList ascDescRunLengths = new IntArrayList(4);

    KeyOrder order = KeyOrder.ASCENDING;
    int runLength = 0;

    for (final KeyColumn column : keyColumns) {
      if (column.order() == KeyOrder.NONE) {
        throw new IAE("Key must be sortable");
      }

      if (column.order() != order) {
        ascDescRunLengths.add(runLength);
        runLength = 0;

        // Invert "order".
        order = order == KeyOrder.ASCENDING ? KeyOrder.DESCENDING : KeyOrder.ASCENDING;
      }

      runLength++;
    }

    if (runLength > 0) {
      ascDescRunLengths.add(runLength);
    }

    return ascDescRunLengths.toIntArray();
  }

  @Override
  @SuppressWarnings("SubtractionInCompareTo")
  public int compare(final byte[] keyArray1, final byte[] keyArray2)
  {
    // Similar logic to FrameComparaisonWidgetImpl, but implementation is different enough that we need our own.
    // Major difference is Frame v. Frame instead of byte[] v. byte[].

    int comparableBytesStartPosition1 = firstFieldPosition;
    int comparableBytesStartPosition2 = firstFieldPosition;

    boolean ascending = true;
    int field = 0;

    for (int numFields : ascDescRunLengths) {
      if (numFields > 0) {
        final int nextField = field + numFields;
        final int comparableBytesEndPosition1 = RowKeyReader.fieldEndPosition(keyArray1, nextField - 1);
        final int comparableBytesEndPosition2 = RowKeyReader.fieldEndPosition(keyArray2, nextField - 1);

        int cmp = FrameReaderUtils.compareByteArraysUnsigned(
            keyArray1,
            comparableBytesStartPosition1,
            comparableBytesEndPosition1 - comparableBytesStartPosition1,
            keyArray2,
            comparableBytesStartPosition2,
            comparableBytesEndPosition2 - comparableBytesStartPosition2
        );

        if (cmp != 0) {
          return ascending ? cmp : -cmp;
        }

        field = nextField;
        comparableBytesStartPosition1 = comparableBytesEndPosition1;
        comparableBytesStartPosition2 = comparableBytesEndPosition2;
      }

      ascending = !ascending;
    }

    return 0;
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ByteRowKeyComparator that = (ByteRowKeyComparator) o;
    return firstFieldPosition == that.firstFieldPosition
           && Arrays.equals(ascDescRunLengths, that.ascDescRunLengths);
  }

  @Override
  public int hashCode()
  {
    int result = Objects.hash(firstFieldPosition);
    result = 31 * result + Arrays.hashCode(ascDescRunLengths);
    return result;
  }

  @Override
  public String toString()
  {
    return "ByteRowKeyComparator{" +
           "firstFieldPosition=" + firstFieldPosition +
           ", ascDescRunLengths=" + Arrays.toString(ascDescRunLengths) +
           '}';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy