Skip to content

Instantly share code, notes, and snippets.

@avar
Created May 20, 2022 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avar/6e0735146e53c680102051d16026340b to your computer and use it in GitHub Desktop.
Save avar/6e0735146e53c680102051d16026340b to your computer and use it in GitHub Desktop.
diff --git a/src/include/utils/sortsupport.h b/src/include/utils/sortsupport.h
index 8c36cf8d82..e1d209cb66 100644
--- a/src/include/utils/sortsupport.h
+++ b/src/include/utils/sortsupport.h
@@ -193,15 +193,11 @@ typedef struct SortSupportData
/*
- * Apply a sort comparator function and return a 3-way comparison result.
- * This takes care of handling reverse-sort and NULLs-ordering properly.
+ * The common isNull1 or isNull2 case for all the Apply.*Sort() below.
*/
-static inline int
-ApplySortComparator(Datum datum1, bool isNull1,
- Datum datum2, bool isNull2,
- SortSupport ssup)
+static inline void _SortComparatorNull(bool isNull1, bool isNull2, int *comparep)
{
- int compare;
+ int compare;
if (isNull1)
{
@@ -219,6 +215,30 @@ ApplySortComparator(Datum datum1, bool isNull1,
else
compare = -1; /* NOT_NULL "<" NULL */
}
+ else
+ {
+ abort(); // unreachable
+ }
+
+ *comparep = compare;
+}
+
+
+/*
+ * Apply a sort comparator function and return a 3-way comparison result.
+ * This takes care of handling reverse-sort and NULLs-ordering properly.
+ */
+static inline int
+ApplySortComparator(Datum datum1, bool isNull1,
+ Datum datum2, bool isNull2,
+ SortSupport ssup)
+{
+ int compare;
+
+ if (isNull1 || isNull2)
+ {
+ _SortComparatorNull(isNull1, isNull2, &compare);
+ }
else
{
compare = ssup->comparator(datum1, datum2, ssup);
@@ -236,22 +256,10 @@ ApplyUnsignedSortComparator(Datum datum1, bool isNull1,
{
int compare;
- if (isNull1)
- {
- if (isNull2)
- compare = 0; /* NULL "=" NULL */
- else if (ssup->ssup_nulls_first)
- compare = -1; /* NULL "<" NOT_NULL */
- else
- compare = 1; /* NULL ">" NOT_NULL */
- }
- else if (isNull2)
+ if (isNull1 || isNull2)
{
- if (ssup->ssup_nulls_first)
- compare = 1; /* NOT_NULL ">" NULL */
- else
- compare = -1; /* NOT_NULL "<" NULL */
- }
+ _SortComparatorNull(isNull1, isNull2, &compare);
+ }
else
{
compare = datum1 < datum2 ? -1 : datum1 > datum2 ? 1 : 0;
@@ -305,22 +313,10 @@ ApplyInt32SortComparator(Datum datum1, bool isNull1,
{
int compare;
- if (isNull1)
- {
- if (isNull2)
- compare = 0; /* NULL "=" NULL */
- else if (ssup->ssup_nulls_first)
- compare = -1; /* NULL "<" NOT_NULL */
- else
- compare = 1; /* NULL ">" NOT_NULL */
- }
- else if (isNull2)
+ if (isNull1 || isNull2)
{
- if (ssup->ssup_nulls_first)
- compare = 1; /* NOT_NULL ">" NULL */
- else
- compare = -1; /* NOT_NULL "<" NULL */
- }
+ _SortComparatorNull(isNull1, isNull2, &compare);
+ }
else
{
compare = DatumGetInt32(datum1) < DatumGetInt32(datum2) ? -1 :
@@ -344,22 +340,10 @@ ApplySortAbbrevFullComparator(Datum datum1, bool isNull1,
{
int compare;
- if (isNull1)
+ if (isNull1 || isNull2)
{
- if (isNull2)
- compare = 0; /* NULL "=" NULL */
- else if (ssup->ssup_nulls_first)
- compare = -1; /* NULL "<" NOT_NULL */
- else
- compare = 1; /* NULL ">" NOT_NULL */
- }
- else if (isNull2)
- {
- if (ssup->ssup_nulls_first)
- compare = 1; /* NOT_NULL ">" NULL */
- else
- compare = -1; /* NOT_NULL "<" NULL */
- }
+ _SortComparatorNull(isNull1, isNull2, &compare);
+ }
else
{
compare = ssup->abbrev_full_comparator(datum1, datum2, ssup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment