aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer/util/ordering.c
blob: 855f2f7274928afa4431ee1be55b26c7580911f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*-------------------------------------------------------------------------
 *
 * ordering.c--
 *	  Routines to manipulate and compare merge and path orderings
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/Attic/ordering.c,v 1.6 1997/09/08 21:45:52 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <sys/types.h>

#include "postgres.h"

#include "optimizer/internal.h"
#include "optimizer/ordering.h"

static bool equal_sortops_order(Oid *ordering1, Oid *ordering2);

/*
 * equal-path-path-ordering--
 *	  Returns t iff two path orderings are equal.
 *
 */
bool
equal_path_path_ordering(PathOrder *path_ordering1,
						 PathOrder *path_ordering2)
{
	if (path_ordering1 == path_ordering2)
		return true;

	if (!path_ordering1 || !path_ordering2)
		return false;

	if (path_ordering1->ordtype == MERGE_ORDER &&
		path_ordering2->ordtype == MERGE_ORDER)
	{

		return equal(path_ordering1->ord.merge, path_ordering2->ord.merge);

	}
	else if (path_ordering1->ordtype == SORTOP_ORDER &&
			 path_ordering2->ordtype == SORTOP_ORDER)
	{

		return
			(equal_sortops_order(path_ordering1->ord.sortop,
								 path_ordering2->ord.sortop));
	}
	else if (path_ordering1->ordtype == MERGE_ORDER &&
			 path_ordering2->ordtype == SORTOP_ORDER)
	{

		return (path_ordering2->ord.sortop &&
				(path_ordering1->ord.merge->left_operator ==
				 path_ordering2->ord.sortop[0]));
	}
	else
	{

		return (path_ordering1->ord.sortop &&
				(path_ordering1->ord.sortop[0] ==
				 path_ordering2->ord.merge->left_operator));
	}
}

/*
 * equal-path-merge-ordering--
 *	  Returns t iff a path ordering is usable for ordering a merge join.
 *
 * XXX	Presently, this means that the first sortop of the path matches
 *		either of the merge sortops.  Is there a "right" and "wrong"
 *		sortop to match?
 *
 */
bool
equal_path_merge_ordering(Oid *path_ordering,
						  MergeOrder *merge_ordering)
{
	if (path_ordering == NULL || merge_ordering == NULL)
		return (false);

	if (path_ordering[0] == merge_ordering->left_operator ||
		path_ordering[0] == merge_ordering->right_operator)
		return (true);
	else
		return (false);
}

/*
 * equal-merge-merge-ordering--
 *	  Returns t iff two merge orderings are equal.
 *
 */
bool
equal_merge_merge_ordering(MergeOrder *merge_ordering1,
						   MergeOrder *merge_ordering2)
{
	return (equal(merge_ordering1, merge_ordering2));
}

/*****************************************************************************
 *
 *****************************************************************************/

/*
 * equal_sort_ops_order -
 *	  Returns true iff the sort operators are in the same order.
 */
static bool
equal_sortops_order(Oid *ordering1, Oid *ordering2)
{
	int			i = 0;

	if (ordering1 == NULL || ordering2 == NULL)
		return (ordering1 == ordering2);

	while (ordering1[i] != 0 && ordering2[i] != 0)
	{
		if (ordering1[i] != ordering2[i])
			break;
		i++;
	}

	return (ordering1[i] == 0 && ordering2[i] == 0);
}