From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 15 Feb 1999 01:06:59 +0000 (+0000)
Subject: Fix a number of places that made faulty assumptions about
X-Git-Tag: REL6_5~650
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dec354ca9769d21a526867ad7636a078dd6ff386;p=postgresql

Fix a number of places that made faulty assumptions about
what is_opclause will accept.
---

diff --git a/src/backend/optimizer/path/clausesel.c b/src/backend/optimizer/path/clausesel.c
index e4afad6545..18bb69f53c 100644
--- a/src/backend/optimizer/path/clausesel.c
+++ b/src/backend/optimizer/path/clausesel.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.17 1999/02/13 23:16:15 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/clausesel.c,v 1.18 1999/02/15 01:06:57 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -206,7 +206,7 @@ compute_selec(Query *root, List *clauses, List *or_selectivities)
 	Cost		s1 = 0;
 	List	   *clause = lfirst(clauses);
 
-	if (clauses == NULL)
+	if (clause == NULL)
 		s1 = 1.0;
 	else if (IsA(clause, Param))
 	{
@@ -351,7 +351,7 @@ compute_selec(Query *root, List *clauses, List *or_selectivities)
 	 * an 'or' clause, but rather that of the single clause.
 	 */
 
-	if (length(clauses) < 2)
+	if (lnext(clauses) == NIL)
 		return s1;
 	else
 	{
diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index 8b54b66402..efedd76325 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.44 1999/02/13 23:16:16 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/path/indxpath.c,v 1.45 1999/02/15 01:06:57 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -324,25 +324,25 @@ match_index_orclause(RelOptInfo *rel,
 	{
 		clause = lfirst(clist);
 
-		if (is_opclause(clause) &&
-			op_class(((Oper *) ((Expr *) clause)->oper)->opno,
-					 xclass, index->relam) &&
-			((match_index_to_operand(indexkey,
-									 (Expr *) get_leftop((Expr *) clause),
-									 rel,
-									 index) &&
-			  IsA(get_rightop((Expr *) clause), Const)) ||
-			 (match_index_to_operand(indexkey,
-								   (Expr *) get_rightop((Expr *) clause),
-									 rel,
-									 index) &&
-			  IsA(get_leftop((Expr *) clause), Const))))
-			lfirst(matching_indices) = lcons(index, lfirst(matching_indices));
+		if (is_opclause(clause))
+		{
+			Expr   *left = (Expr *) get_leftop((Expr *) clause);
+			Expr   *right = (Expr *) get_rightop((Expr *) clause);
+			if (left && right &&
+				op_class(((Oper *) ((Expr *) clause)->oper)->opno,
+						 xclass, index->relam) &&
+				((IsA(right, Const) &&
+				  match_index_to_operand(indexkey, left, rel, index)) ||
+				 (IsA(left, Const) &&
+				  match_index_to_operand(indexkey, right, rel, index))))
+				lfirst(matching_indices) = lcons(index,
+												 lfirst(matching_indices));
+		}
 
 		matching_indices = lnext(matching_indices);
 	}
-	return index_list;
 
+	return index_list;
 }
 
 /****************************************************************************
@@ -1019,6 +1019,7 @@ clause_pred_clause_test(Expr *predicate, Node *clause)
 	/* Check the basic form; for now, only allow the simplest case */
 	if (!is_opclause(clause) ||
 		!IsA(clause_var, Var) ||
+		clause_const == NULL ||
 		!IsA(clause_const, Const) ||
 		!IsA(predicate->oper, Oper) ||
 		!IsA(pred_var, Var) ||
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index c589b52c15..2559c376a7 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.46 1999/02/13 23:16:27 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.47 1999/02/15 01:06:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -780,6 +780,7 @@ switch_outer(List *clauses)
 	{
 		clause = lfirst(i);
 		op = (Node *) get_rightop(clause);
+		Assert(op != (Node*) NULL);
 		if (IsA(op, ArrayRef))
 			op = ((ArrayRef *) op)->refexpr;
 		Assert(IsA(op, Var));
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index a1762ad8df..e379a2bbed 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.24 1999/02/14 04:56:50 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/initsplan.c,v 1.25 1999/02/15 01:06:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -367,13 +367,30 @@ set_joininfo_mergeable_hashable(List *rel_list)
 static MergeOrder *
 mergejoinop(Expr *clause)
 {
-	Oid			leftOp,
+	Var		   *left,
+			   *right;
+	Oid			opno,
+				leftOp,
 				rightOp;
 	bool		sortable;
 
-	sortable = op_mergejoinable(((Oper *) clause->oper)->opno,
-								(get_leftop(clause))->vartype,
-								(get_rightop(clause))->vartype,
+	if (!is_opclause((Node*) clause))
+		return NULL;
+
+	left = get_leftop(clause);
+	right = get_rightop(clause);
+
+	/* caution: is_opclause accepts more than I do, so check it */
+	if (!right)
+		return NULL;			/* unary opclauses need not apply */
+	if (!IsA(left, Var) || !IsA(right, Var))
+		return NULL;
+
+	opno = ((Oper *) clause->oper)->opno;
+
+	sortable = op_mergejoinable(opno,
+								left->vartype,
+								right->vartype,
 								&leftOp,
 								&rightOp);
 
@@ -381,11 +398,11 @@ mergejoinop(Expr *clause)
 	{
 		MergeOrder *morder = makeNode(MergeOrder);
 
-		morder->join_operator = ((Oper *) clause->oper)->opno;
+		morder->join_operator = opno;
 		morder->left_operator = leftOp;
 		morder->right_operator = rightOp;
-		morder->left_type = (get_leftop(clause))->vartype;
-		morder->right_type = (get_rightop(clause))->vartype;
+		morder->left_type = left->vartype;
+		morder->right_type = right->vartype;
 		return morder;
 	}
 	else
@@ -401,7 +418,22 @@ mergejoinop(Expr *clause)
 static Oid
 hashjoinop(Expr *clause)
 {
-	return (op_hashjoinable(((Oper *) clause->oper)->opno,
-							(get_leftop(clause))->vartype,
-							(get_rightop(clause))->vartype));
+	Var		   *left,
+			   *right;
+
+	if (!is_opclause((Node*) clause))
+		return InvalidOid;
+
+	left = get_leftop(clause);
+	right = get_rightop(clause);
+
+	/* caution: is_opclause accepts more than I do, so check it */
+	if (!right)
+		return InvalidOid;		/* unary opclauses need not apply */
+	if (!IsA(left, Var) || !IsA(right, Var))
+		return InvalidOid;
+
+	return op_hashjoinable(((Oper *) clause->oper)->opno,
+						   left->vartype,
+						   right->vartype);
 }
diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 78d76960f8..e3bfc87586 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.39 1999/02/13 23:16:33 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.40 1999/02/15 01:06:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -290,6 +290,8 @@ replace_clause_joinvar_refs(Expr *clause,
 {
 	List	   *temp = NULL;
 
+	if (clause == NULL)
+		return NULL;
 	if (IsA(clause, Var))
 	{
 		temp = (List *) replace_joinvar_refs((Var *) clause,
@@ -586,6 +588,8 @@ replace_result_clause(Node *clause,
 {
 	List	   *t;
 
+	if (clause == NULL)
+		return;
 	if (IsA(clause, Var))
 	{
 		TargetEntry *subplanVar;
diff --git a/src/backend/optimizer/prep/prepqual.c b/src/backend/optimizer/prep/prepqual.c
index 39f863cde7..cea2939a15 100644
--- a/src/backend/optimizer/prep/prepqual.c
+++ b/src/backend/optimizer/prep/prepqual.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepqual.c,v 1.13 1999/02/13 23:16:37 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepqual.c,v 1.14 1999/02/15 01:06:59 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -106,10 +106,17 @@ find_nots(Expr *qual)
 
 	if (is_opclause((Node *) qual))
 	{
-		return (make_clause(qual->opType, qual->oper,
-							lcons(find_nots((Expr *) get_leftop(qual)),
-							 lcons(find_nots((Expr *) get_rightop(qual)),
-								   NIL))));
+		Expr   *left = (Expr *) get_leftop(qual);
+		Expr   *right = (Expr *) get_rightop(qual);
+		if (right)
+			return make_clause(qual->opType, qual->oper,
+							   lcons(find_nots(left),
+									 lcons(find_nots(right),
+										   NIL)));
+		else
+			return make_clause(qual->opType, qual->oper,
+							   lcons(find_nots(left),
+									 NIL));
 	}
 	else if (and_clause((Node *) qual))
 	{
@@ -155,12 +162,17 @@ normalize(Expr *qual)
 
 	if (is_opclause((Node *) qual))
 	{
-		Expr	   *expr = (Expr *) qual;
-
-		return (make_clause(expr->opType, expr->oper,
-							lcons(normalize((Expr *) get_leftop(qual)),
-							 lcons(normalize((Expr *) get_rightop(qual)),
-								   NIL))));
+		Expr   *left = (Expr *) get_leftop(qual);
+		Expr   *right = (Expr *) get_rightop(qual);
+		if (right)
+			return make_clause(qual->opType, qual->oper,
+							   lcons(normalize(left),
+									 lcons(normalize(right),
+										   NIL)));
+		else
+			return make_clause(qual->opType, qual->oper,
+							   lcons(normalize(left),
+									 NIL));
 	}
 	else if (and_clause((Node *) qual))
 	{
@@ -217,10 +229,17 @@ qual_cleanup(Expr *qual)
 
 	if (is_opclause((Node *) qual))
 	{
-		return ((List *) make_clause(qual->opType, qual->oper,
-							lcons(qual_cleanup((Expr *) get_leftop(qual)),
-						   lcons(qual_cleanup((Expr *) get_rightop(qual)),
-								 NIL))));
+		Expr   *left = (Expr *) get_leftop(qual);
+		Expr   *right = (Expr *) get_rightop(qual);
+		if (right)
+			return (List *) make_clause(qual->opType, qual->oper,
+										lcons(qual_cleanup(left),
+											  lcons(qual_cleanup(right),
+													NIL)));
+		else
+			return (List *) make_clause(qual->opType, qual->oper,
+										lcons(qual_cleanup(left),
+											  NIL));
 	}
 	else if (and_clause((Node *) qual))
 	{
@@ -276,10 +295,17 @@ pull_args(Expr *qual)
 
 	if (is_opclause((Node *) qual))
 	{
-		return (make_clause(qual->opType, qual->oper,
-							lcons(pull_args((Expr *) get_leftop(qual)),
-							 lcons(pull_args((Expr *) get_rightop(qual)),
-								   NIL))));
+		Expr   *left = (Expr *) get_leftop(qual);
+		Expr   *right = (Expr *) get_rightop(qual);
+		if (right)
+			return make_clause(qual->opType, qual->oper,
+							   lcons(pull_args(left),
+									 lcons(pull_args(right),
+										   NIL)));
+		else
+			return make_clause(qual->opType, qual->oper,
+							   lcons(pull_args(left),
+									 NIL));
 	}
 	else if (and_clause((Node *) qual))
 	{
@@ -384,7 +410,7 @@ push_nots(Expr *qual)
 											   0, NULL);
 
 			op->op_fcache = (FunctionCache *) NULL;
-			return (make_opclause(op, get_leftop(qual), get_rightop(qual)));
+			return make_opclause(op, get_leftop(qual), get_rightop(qual));
 		}
 		else
 			return make_notclause(qual);
@@ -511,10 +537,17 @@ remove_ands(Expr *qual)
 		return NIL;
 	if (is_opclause((Node *) qual))
 	{
-		return ((List *) make_clause(qual->opType, qual->oper,
-							lcons(remove_ands((Expr *) get_leftop(qual)),
-						   lcons(remove_ands((Expr *) get_rightop(qual)),
-								 NIL))));
+		Expr   *left = (Expr *) get_leftop(qual);
+		Expr   *right = (Expr *) get_rightop(qual);
+		if (right)
+			return (List *) make_clause(qual->opType, qual->oper,
+										lcons(remove_ands(left),
+											  lcons(remove_ands(right),
+													NIL)));
+		else
+			return (List *) make_clause(qual->opType, qual->oper,
+										lcons(remove_ands(left),
+											  NIL));
 	}
 	else if (and_clause((Node *) qual))
 	{