From 1d6244b29b3b73d51e2ba92c24ddfc0d70a584bf Mon Sep 17 00:00:00 2001
From: jcoleman <jtc331@gmail.com>
Date: Fri, 22 Dec 2023 09:53:44 -0500
Subject: [PATCH v2] WIP: use switch statements

---
 src/backend/optimizer/util/predtest.c | 230 ++++++++++++++++++--------
 1 file changed, 160 insertions(+), 70 deletions(-)

diff --git a/src/backend/optimizer/util/predtest.c b/src/backend/optimizer/util/predtest.c
index fe83e45311..0b75516855 100644
--- a/src/backend/optimizer/util/predtest.c
+++ b/src/backend/optimizer/util/predtest.c
@@ -1132,34 +1132,42 @@ predicate_implied_by_simple_clause(Expr *predicate, Node *clause,
 		return true;
 
 	/* Next see if clause is boolean equality to a constant */
-	if (is_opclause(clause) &&
-		((OpExpr *) clause)->opno == BooleanEqualOperator)
+	switch (nodeTag(clause))
 	{
-		OpExpr	   *op = (OpExpr *) clause;
-		Node	   *rightop;
-
-		Assert(list_length(op->args) == 2);
-		rightop = lsecond(op->args);
-		/* We might never see a null Const here, but better check anyway */
-		if (rightop && IsA(rightop, Const) &&
-			!((Const *) rightop)->constisnull)
-		{
-			Node	   *leftop = linitial(op->args);
-
-			if (DatumGetBool(((Const *) rightop)->constvalue))
-			{
-				/* X = true implies X */
-				if (equal(predicate, leftop))
-					return true;
-			}
-			else
+		case T_OpExpr:
 			{
-				/* X = false implies NOT X */
-				if (is_notclause(predicate) &&
-					equal(get_notclausearg(predicate), leftop))
-					return true;
+				OpExpr	   *op = (OpExpr *) clause;
+				Node	   *rightop;
+
+				if (op->opno == BooleanEqualOperator)
+				{
+					Assert(list_length(op->args) == 2);
+					rightop = lsecond(op->args);
+					/* We might never see a null Const here, but better check anyway */
+					if (rightop && IsA(rightop, Const) &&
+						!((Const *) rightop)->constisnull)
+					{
+						Node	   *leftop = linitial(op->args);
+
+						if (DatumGetBool(((Const *) rightop)->constvalue))
+						{
+							/* X = true implies X */
+							if (equal(predicate, leftop))
+								return true;
+						}
+						else
+						{
+							/* X = false implies NOT X */
+							if (is_notclause(predicate) &&
+								equal(get_notclausearg(predicate), leftop))
+								return true;
+						}
+					}
+				}
 			}
-		}
+			break;
+		default:
+			break;
 	}
 
 	/*
@@ -1169,20 +1177,34 @@ predicate_implied_by_simple_clause(Expr *predicate, Node *clause,
 	 */
 
 	/* Next try the IS NOT NULL case */
-	if (!weak &&
-		predicate && IsA(predicate, NullTest))
+	switch (nodeTag(predicate))
 	{
-		NullTest   *ntest = (NullTest *) predicate;
+		case T_NullTest:
+			{
+				NullTest   *ntest = (NullTest *) predicate;
 
-		/* row IS NOT NULL does not act in the simple way we have in mind */
-		if (ntest->nulltesttype == IS_NOT_NULL &&
-			!ntest->argisrow)
-		{
-			/* strictness of clause for foo implies foo IS NOT NULL */
-			if (clause_is_strict_for(clause, (Node *) ntest->arg, true))
-				return true;
-		}
-		return false;			/* we can't succeed below... */
+				switch (ntest->nulltesttype)
+				{
+					case IS_NOT_NULL:
+						{
+							/* row IS NOT NULL does not act in the simple way
+							 * we have in mind */
+							if (ntest->argisrow)
+								return false;
+
+							/* strictness of clause for foo implies foo IS NOT NULL */
+							if (!weak &&
+								clause_is_strict_for(clause, (Node *) ntest->arg, true))
+								return true;
+						}
+						break;
+					default:
+						break;
+				}
+				return false;			/* we can't succeed below... */
+			}
+		default:
+			break;
 	}
 
 	/* Else try operator-related knowledge */
@@ -1232,52 +1254,120 @@ predicate_refuted_by_simple_clause(Expr *predicate, Node *clause,
 		return false;
 
 	/* Try the predicate-IS-NULL case */
-	if (predicate && IsA(predicate, NullTest) &&
-		((NullTest *) predicate)->nulltesttype == IS_NULL)
+	switch (nodeTag(predicate))
 	{
-		Expr	   *isnullarg = ((NullTest *) predicate)->arg;
+		case T_NullTest:
+			{
+				NullTest   *predntest = (NullTest *) predicate;
 
-		/* row IS NULL does not act in the simple way we have in mind */
-		if (((NullTest *) predicate)->argisrow)
-			return false;
+				switch (predntest->nulltesttype)
+				{
+					case IS_NULL:
+						{
+							Expr   *isnullarg = predntest->arg;
 
-		/* strictness of clause for foo refutes foo IS NULL */
-		if (clause_is_strict_for(clause, (Node *) isnullarg, true))
-			return true;
+							/* row IS NULL does not act in the simple way we have in mind */
+							if (predntest->argisrow)
+								return false;
 
-		/* foo IS NOT NULL refutes foo IS NULL */
-		if (clause && IsA(clause, NullTest) &&
-			((NullTest *) clause)->nulltesttype == IS_NOT_NULL &&
-			!((NullTest *) clause)->argisrow &&
-			equal(((NullTest *) clause)->arg, isnullarg))
-			return true;
+							/* strictness of clause for foo refutes foo IS NULL */
+							if (clause_is_strict_for(clause, (Node *) isnullarg, true))
+								return true;
 
-		return false;			/* we can't succeed below... */
+							switch (nodeTag(clause))
+							{
+								case T_NullTest:
+									{
+										NullTest   *clausentest = (NullTest *) clause;
+
+										switch (clausentest->nulltesttype)
+										{
+											case IS_NOT_NULL:
+												{
+													/* foo IS NOT NULL refutes foo IS NULL */
+													if (!clausentest->argisrow &&
+															equal(clausentest->arg, isnullarg))
+														return true;
+
+												}
+												break;
+											default:
+												break;
+										}
+									}
+									break;
+								default:
+									break;
+							}
+						}
+						return false;			/* we can't succeed below... */
+					default:
+						break;
+				}
+			}
+			break;
+		default:
+			break;
 	}
 
 	/* Try the clause-IS-NULL case */
-	if (clause && IsA(clause, NullTest) &&
-		((NullTest *) clause)->nulltesttype == IS_NULL)
+	switch (nodeTag(clause))
 	{
-		Expr	   *isnullarg = ((NullTest *) clause)->arg;
+		case T_NullTest:
+			{
+				NullTest   *clausentest = (NullTest *) clause;
 
-		/* row IS NULL does not act in the simple way we have in mind */
-		if (((NullTest *) clause)->argisrow)
-			return false;
+				switch (clausentest->nulltesttype)
+				{
+					case IS_NULL:
+						{
+							Expr	   *isnullarg = ((NullTest *) clause)->arg;
 
-		/* foo IS NULL refutes foo IS NOT NULL */
-		if (predicate && IsA(predicate, NullTest) &&
-			((NullTest *) predicate)->nulltesttype == IS_NOT_NULL &&
-			!((NullTest *) predicate)->argisrow &&
-			equal(((NullTest *) predicate)->arg, isnullarg))
-			return true;
+							/* row IS NULL does not act in the simple way we
+							 * have in mind */
+							if (clausentest->argisrow)
+								return false;
 
-		/* foo IS NULL weakly refutes any predicate that is strict for foo */
-		if (weak &&
-			clause_is_strict_for((Node *) predicate, (Node *) isnullarg, true))
-			return true;
+							switch (nodeTag(predicate))
+							{
+								case T_NullTest:
+									{
+										NullTest   *predntest = (NullTest *) predicate;
+
+										switch (predntest->nulltesttype)
+										{
+											case IS_NOT_NULL:
+												{
+													/* foo IS NULL refutes foo IS NOT NULL */
+													if (!predntest->argisrow &&
+														equal(predntest->arg, isnullarg))
+														return true;
+												}
+												break;
+											default:
+												break;
+										}
+									}
+									break;
+								default:
+									break;
+							}
+
+							/* foo IS NULL weakly refutes any predicate that is strict for foo */
+							if (weak &&
+								clause_is_strict_for((Node *) predicate, (Node *) isnullarg, true))
+								return true;
 
-		return false;			/* we can't succeed below... */
+							return false;			/* we can't succeed below... */
+						}
+						break;
+					default:
+						break;
+				}
+			}
+			break;
+		default:
+			break;
 	}
 
 	/* Else try operator-related knowledge */
-- 
2.39.3 (Apple Git-145)

