From 912a5d76107fc2597b951d2e2f3c22400a6c0cf6 Mon Sep 17 00:00:00 2001
From: "Matwey V. Kornilov" <matwey.kornilov@gmail.com>
Date: Fri, 1 Feb 2019 12:14:18 +0300
Subject: [PATCH v2 2/3] Introduce spg_quad_inner_consistent_box_helper() in
 spgquadtreeproc.c

This helper function makes spg_quad_inner_consistent() more readable when
changes from the next commit is introduced.

Signed-off-by: Matwey V. Kornilov <matwey.kornilov@gmail.com>
---
 src/backend/access/spgist/spgquadtreeproc.c | 64 ++++++++++++++---------------
 1 file changed, 32 insertions(+), 32 deletions(-)

diff --git a/src/backend/access/spgist/spgquadtreeproc.c b/src/backend/access/spgist/spgquadtreeproc.c
index f2e980b758..41e7fe5c81 100644
--- a/src/backend/access/spgist/spgquadtreeproc.c
+++ b/src/backend/access/spgist/spgquadtreeproc.c
@@ -112,6 +112,37 @@ getQuadrantArea(BOX *bbox, Point *centroid, int quadrant)
 	return result;
 }
 
+static int
+spg_quad_inner_consistent_box_helper(ScanKey sk, Point *centroid)
+{
+	/*
+	 * For this operator, the query is a box not a point.  We
+	 * cheat to the extent of assuming that DatumGetPointP won't
+	 * do anything that would be bad for a pointer-to-box.
+	 */
+	BOX *boxQuery = DatumGetBoxP(sk->sk_argument);
+	Point p;
+	int r = 0;
+
+	if (DatumGetBool(DirectFunctionCall2(box_contain_pt, PointerGetDatum(boxQuery), PointerGetDatum(centroid))))
+	{
+		/* centroid is in box, so all quadrants are OK */
+		return (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
+	}
+
+	/* identify quadrant(s) containing all corners of box */
+	p = boxQuery->low;
+	r |= 1 << getQuadrant(centroid, &p);
+	p.y = boxQuery->high.y;
+	r |= 1 << getQuadrant(centroid, &p);
+	p = boxQuery->high;
+	r |= 1 << getQuadrant(centroid, &p);
+	p.x = boxQuery->low.x;
+	r |= 1 << getQuadrant(centroid, &p);
+
+	return r;
+}
+
 Datum
 spg_quad_choose(PG_FUNCTION_ARGS)
 {
@@ -302,7 +333,6 @@ spg_quad_inner_consistent(PG_FUNCTION_ARGS)
 	{
 		const ScanKey sk = in->scankeys + i;
 		Point	   *query = DatumGetPointP(sk->sk_argument);
-		BOX		   *boxQuery;
 
 		switch (sk->sk_strategy)
 		{
@@ -326,37 +356,7 @@ spg_quad_inner_consistent(PG_FUNCTION_ARGS)
 					which &= (1 << 1) | (1 << 4);
 				break;
 			case RTContainedByStrategyNumber:
-
-				/*
-				 * For this operator, the query is a box not a point.  We
-				 * cheat to the extent of assuming that DatumGetPointP won't
-				 * do anything that would be bad for a pointer-to-box.
-				 */
-				boxQuery = DatumGetBoxP(sk->sk_argument);
-
-				if (DatumGetBool(DirectFunctionCall2(box_contain_pt,
-													 PointerGetDatum(boxQuery),
-													 PointerGetDatum(centroid))))
-				{
-					/* centroid is in box, so all quadrants are OK */
-				}
-				else
-				{
-					/* identify quadrant(s) containing all corners of box */
-					Point		p;
-					int			r = 0;
-
-					p = boxQuery->low;
-					r |= 1 << getQuadrant(centroid, &p);
-					p.y = boxQuery->high.y;
-					r |= 1 << getQuadrant(centroid, &p);
-					p = boxQuery->high;
-					r |= 1 << getQuadrant(centroid, &p);
-					p.x = boxQuery->low.x;
-					r |= 1 << getQuadrant(centroid, &p);
-
-					which &= r;
-				}
+				which &= spg_quad_inner_consistent_box_helper(sk, centroid);
 				break;
 			default:
 				elog(ERROR, "unrecognized strategy number: %d", sk->sk_strategy);
-- 
2.13.7

