77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.41 1999/08/09 06:20:22 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.42 1999/08/12 00:42:43 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -741,7 +741,7 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
741741
742742/* ----------------
743743 *Here we figure out the contents of the index qual.
744- *The usual case is (op var const) or (op const var)
744+ *The usual case is (varop const) or (const op var)
745745 *which means we form a scan key for the attribute
746746 *listed in the var node and use the value of the const.
747747 *
@@ -759,6 +759,19 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
759759 *Hence, we set have_runtime_keys to true and then set
760760 *the appropriate flag in run_keys to LEFT_OP or RIGHT_OP.
761761 *The corresponding scan keys are recomputed at run time.
762+ *
763+ *XXX Although this code *thinks* it can handle an indexqual
764+ *with the indexkey on either side, in fact it cannot.
765+ *Indexscans only work with quals that have the indexkey on
766+ *the left (the planner/optimizer makes sure it never passes
767+ *anything else). The reason: the scankey machinery has no
768+ *provision for distinguishing which side of the operator is
769+ *the indexed attribute and which is the compared-to constant.
770+ *It just assumes that the attribute is on the left :-(
771+ *
772+ *I am leaving this code able to support both ways, even though
773+ *half of it is dead code, on the off chance that someone will
774+ *fix the scankey machinery someday --- tgl 8/11/99.
762775 * ----------------
763776 */
764777
@@ -770,7 +783,9 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
770783 */
771784leftop = (Node * )get_leftop (clause );
772785
773- if (IsA (leftop ,Var )&& var_is_rel ((Var * )leftop ))
786+ Assert (leftop != NULL );
787+
788+ if (IsA (leftop ,Var )&& var_is_rel ((Var * )leftop ))
774789{
775790/* ----------------
776791 *if the leftop is a "rel-var", then it means
@@ -781,6 +796,19 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
781796varattno = ((Var * )leftop )-> varattno ;
782797scanvar = LEFT_OP ;
783798}
799+ else if (is_funcclause (leftop )&&
800+ var_is_rel (lfirst (((Expr * )leftop )-> args )))
801+ {
802+ /* ----------------
803+ *if the leftop is a func node then it means
804+ *it identifies the value to place in our scan key.
805+ *Since functional indices have only one attribute
806+ *the attno must always be set to 1.
807+ * ----------------
808+ */
809+ varattno = 1 ;
810+ scanvar = LEFT_OP ;
811+ }
784812else if (IsA (leftop ,Const ))
785813{
786814/* ----------------
@@ -819,21 +847,6 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
819847run_keys [j ]= NO_OP ;
820848}
821849}
822- else if (leftop != NULL &&
823- is_funcclause (leftop )&&
824- var_is_rel (lfirst (((Expr * )leftop )-> args )))
825- {
826- /* ----------------
827- *if the leftop is a func node then it means
828- *it identifies the value to place in our scan key.
829- *Since functional indices have only one attribute
830- *the attno must always be set to 1.
831- * ----------------
832- */
833- varattno = 1 ;
834- scanvar = LEFT_OP ;
835-
836- }
837850else
838851{
839852/* ----------------
@@ -853,7 +866,9 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
853866 */
854867rightop = (Node * )get_rightop (clause );
855868
856- if (IsA (rightop ,Var )&& var_is_rel ((Var * )rightop ))
869+ Assert (rightop != NULL );
870+
871+ if (IsA (rightop ,Var )&& var_is_rel ((Var * )rightop ))
857872{
858873/* ----------------
859874 *here we make sure only one op identifies the
@@ -872,12 +887,28 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
872887 */
873888varattno = ((Var * )rightop )-> varattno ;
874889scanvar = RIGHT_OP ;
890+ }
891+ else if (is_funcclause (rightop )&&
892+ var_is_rel (lfirst (((Expr * )rightop )-> args )))
893+ {
894+ /* ----------------
895+ *if the rightop is a func node then it means
896+ *it identifies the value to place in our scan key.
897+ *Since functional indices have only one attribute
898+ *the attno must always be set to 1.
899+ * ----------------
900+ */
901+ if (scanvar == LEFT_OP )
902+ elog (ERROR ,"ExecInitIndexScan: %s" ,
903+ "both left and right ops are rel-vars" );
875904
905+ varattno = 1 ;
906+ scanvar = RIGHT_OP ;
876907}
877908else if (IsA (rightop ,Const ))
878909{
879910/* ----------------
880- *if theleftop is a const node then it means
911+ *if therightop is a const node then it means
881912 *it identifies the value to place in our scan key.
882913 * ----------------
883914 */
@@ -912,29 +943,10 @@ ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
912943run_keys [j ]= NO_OP ;
913944}
914945}
915- else if (rightop != NULL &&
916- is_funcclause (rightop )&&
917- var_is_rel (lfirst (((Expr * )rightop )-> args )))
918- {
919- /* ----------------
920- *if the rightop is a func node then it means
921- *it identifies the value to place in our scan key.
922- *Since functional indices have only one attribute
923- *the attno must always be set to 1.
924- * ----------------
925- */
926- if (scanvar == LEFT_OP )
927- elog (ERROR ,"ExecInitIndexScan: %s" ,
928- "both left and right ops are rel-vars" );
929-
930- varattno = 1 ;
931- scanvar = RIGHT_OP ;
932-
933- }
934946else
935947{
936948/* ----------------
937- *otherwise, theleftop contains information usable
949+ *otherwise, therightop contains information usable
938950 *at runtime to figure out the value to place in our
939951 *scan key.
940952 * ----------------