int Key::Compare()

in backend/datamodel/key.cc [94:140]


int Key::Compare(const Key& other) const {
  // Handle infinity keys first.
  if (is_infinity_ || other.is_infinity_) {
    if (is_infinity_ == other.is_infinity_) {
      return 0;
    }
    return other.is_infinity_ ? -1 : 1;
  }

  // Perform left-to-right column comparisons.
  for (int i = 0; i < columns_.size(); ++i) {
    if (i >= other.columns_.size()) {
      // If we reached here, other is a prefix of *this.
      return other.is_prefix_limit_ ? -1 : 1;
    }
    if (columns_[i].is_null() && other.columns_[i].is_null()) {
      continue;
    }
    if (columns_[i].is_null() && !other.columns_[i].is_null()) {
      return is_nulls_last_[i] ? 1 : -1;
    }
    if (other.columns_[i].is_null()) {
      return is_nulls_last_[i] ? -1 : 1;
    }
    if (columns_[i].LessThan(other.columns_[i])) {
      return is_descending_[i] ? 1 : -1;
    }

    if (columns_[i].Equals(other.columns_[i])) {
      continue;
    }

    return is_descending_[i] ? -1 : 1;
  }

  // If we reached here, *this is a prefix of other.
  if (other.columns_.size() > columns_.size()) {
    return is_prefix_limit_ ? 1 : -1;
  }

  // If we reached here, all columns are equal.
  if (is_prefix_limit_ != other.is_prefix_limit_) {
    return is_prefix_limit_ ? 1 : -1;
  }

  return 0;
}