public inbox for [email protected]
help / color / mirror / Atom feedFrom: Pierre Ducroquet <[email protected]>
Subject: [PATCH] Simplify AND clauses that have NOT NULL clauses on similar variables
Date: Wed, 6 Nov 2019 18:05:01 +0100
It's not uncommon to have views with several branches, with fields that are null depending on the branch.
If the view is queried with an IS NULL or IS NOT NULL on such a field, huge simplification can be done.
This patch thus looks, when flattening and simplifying AND clauses, for opposite NOT NULL on similar variables.
If this happens, the whole AND clause is discarded and simplified into false.
---
src/backend/optimizer/util/clauses.c | 44 ++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/src/backend/optimizer/util/clauses.c b/src/backend/optimizer/util/clauses.c
index a04b62274d..2ba66a62a5 100644
--- a/src/backend/optimizer/util/clauses.c
+++ b/src/backend/optimizer/util/clauses.c
@@ -3781,6 +3781,7 @@ simplify_and_arguments(List *args,
{
List *newargs = NIL;
List *unprocessed_args;
+ List *null_checks = NIL;
/* See comments in simplify_or_arguments */
unprocessed_args = list_copy(args);
@@ -3844,6 +3845,49 @@ simplify_and_arguments(List *args,
continue;
}
+ if (IsA(arg, NullTest))
+ {
+ NullTest *null_check = (NullTest*) arg;
+
+ /* Only check for $VAR IS NULL, ignore other expressions */
+ if (!IsA(null_check->arg, Var))
+ goto next;
+
+ if (null_checks == NIL)
+ {
+ null_checks = lappend(null_checks, null_check);
+ goto next;
+ }
+ else
+ {
+ Var *null_check_var = (Var *) null_check->arg;
+
+ /* Now, we can check every other null check from our list */
+ ListCell *other_null;
+ foreach (other_null, null_checks)
+ {
+ NullTest *other_null_check = (NullTest *) lfirst(other_null);
+ Var *other_null_var = (Var *) other_null_check->arg;
+
+ if ((other_null_check->nulltesttype != null_check->nulltesttype) &&
+ (other_null_var->varno == null_check_var->varno) &&
+ (other_null_var->varattno == null_check_var->varattno))
+ {
+ *forceFalse = true;
+
+ /*
+ * Once we detect a FALSE result we can just exit the loop
+ * immediately. However, if we ever add a notion of
+ * non-removable functions, we'd need to keep scanning.
+ */
+ return NIL;
+ }
+ }
+ lappend(null_checks, null_check);
+ }
+ }
+
+next:
/* else emit the simplified arg into the result list */
newargs = lappend(newargs, arg);
}
--
2.24.0.rc2
--nextPart4446148.x7eVRbpFO8--
view thread (31+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCH] Simplify AND clauses that have NOT NULL clauses on similar variables
In-Reply-To: <no-message-id-1883335@localhost>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox