jt2594838 commented on code in PR #19035:
URL: https://github.com/apache/iotdb/pull/19035#discussion_r4478034876


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/sorter/InsertEventSorter.java:
##########
@@ -166,4 +168,57 @@ private int getLastNonnullIndex(
     }
     return index[lastNonnullIndex];
   }
+
+  /** Sort by time only, keeping the relative order of rows that share a 
timestamp. */
+  public void sortByTimestampIfNecessary() {
+    if (dataAdapter == null || dataAdapter.getRowSize() == 0) {
+      return;
+    }
+
+    final long[] timestamps = dataAdapter.getTimestamps();
+    final int rowSize = dataAdapter.getRowSize();
+    for (int i = 1; i < rowSize; ++i) {
+      if (timestamps[i] < timestamps[i - 1]) {
+        isSorted = false;
+        break;
+      }
+    }
+
+    if (isSorted) {
+      return;
+    }
+
+    index = new Integer[rowSize];
+    for (int i = 0; i < rowSize; i++) {
+      index[i] = i;
+    }
+    sortTimestamps();
+
+    deDuplicatedIndex = null;
+    deDuplicatedSize = rowSize;
+    sortAndMayDeduplicateValuesAndBitMaps();
+  }
+
+  /**
+   * The permutation the last sort applied: the row at position {@code i} came 
from row {@code
+   * permutation[i]} of the input. {@code null} when the rows were already in 
order and nothing was
+   * moved.
+   */
+  public int[] getRowPermutation() {
+    if (index == null) {
+      return null;
+    }
+    final int[] permutation = new int[index.length];
+    for (int i = 0; i < index.length; i++) {
+      permutation[i] = index[i];
+    }
+    return permutation;
+  }
+
+  protected void sortTimestamps() {
+    // Index is sorted stably because it is Integer[]
+    Arrays.sort(this.index, 
Comparator.comparingLong(dataAdapter::getTimestamp));
+    final long[] timestamps = dataAdapter.getTimestamps();
+    Arrays.sort(timestamps, 0, dataAdapter.getRowSize());

Review Comment:
   Maybe we can rearrange timestamps according to the index?
   It will be O(N) instead of O(NlogN).



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/sorter/InsertEventSorter.java:
##########
@@ -166,4 +168,57 @@ private int getLastNonnullIndex(
     }
     return index[lastNonnullIndex];
   }
+
+  /** Sort by time only, keeping the relative order of rows that share a 
timestamp. */
+  public void sortByTimestampIfNecessary() {
+    if (dataAdapter == null || dataAdapter.getRowSize() == 0) {
+      return;
+    }
+
+    final long[] timestamps = dataAdapter.getTimestamps();
+    final int rowSize = dataAdapter.getRowSize();
+    for (int i = 1; i < rowSize; ++i) {
+      if (timestamps[i] < timestamps[i - 1]) {
+        isSorted = false;
+        break;
+      }
+    }
+
+    if (isSorted) {
+      return;
+    }
+
+    index = new Integer[rowSize];
+    for (int i = 0; i < rowSize; i++) {
+      index[i] = i;
+    }
+    sortTimestamps();
+
+    deDuplicatedIndex = null;
+    deDuplicatedSize = rowSize;
+    sortAndMayDeduplicateValuesAndBitMaps();
+  }
+
+  /**
+   * The permutation the last sort applied: the row at position {@code i} came 
from row {@code
+   * permutation[i]} of the input. {@code null} when the rows were already in 
order and nothing was
+   * moved.
+   */
+  public int[] getRowPermutation() {
+    if (index == null) {
+      return null;
+    }
+    final int[] permutation = new int[index.length];
+    for (int i = 0; i < index.length; i++) {
+      permutation[i] = index[i];
+    }
+    return permutation;

Review Comment:
   Why not use array copy?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/event/common/tablet/parser/TabletInsertionEventParser.java:
##########
@@ -443,9 +443,16 @@ private List<Integer> generateRowIndexList(final long[] 
originTimestampColumn) {
     }
 
     final List<Integer> rowIndexList = new ArrayList<>();
-    // We assume that `originTimestampColumn` is ordered.
-    if (originTimestampColumn[originTimestampColumn.length - 1] < 
sourceEvent.getStartTime()
-        || originTimestampColumn[0] > sourceEvent.getEndTime()) {
+    // The timestamps are not necessarily ordered: the rows of a table-model 
tablet are grouped by
+    // device, so its first and last timestamp are not the minimum and the 
maximum. The event can
+    // only be skipped without looking at the rows when all of them are 
outside the time range.
+    long minTimestamp = Long.MAX_VALUE;
+    long maxTimestamp = Long.MIN_VALUE;
+    for (final long timestamp : originTimestampColumn) {
+      minTimestamp = Math.min(minTimestamp, timestamp);
+      maxTimestamp = Math.max(maxTimestamp, timestamp);
+    }
+    if (maxTimestamp < sourceEvent.getStartTime() || minTimestamp > 
sourceEvent.getEndTime()) {

Review Comment:
   Why do you think "the rows of a table-model tablet are grouped by device"?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to