Node *
replaceParamsMutator (Node *node, ReplaceParamsContext* context)
{
        if (node == NULL)
                return NULL;

        // replace Param nodes with Vars
        if (IsA(node, Param) && context->touchParams)
        {
                Param *param;
                Node *newExpr;
                TargetEntry *te;

                param = (Param *) node;

                /* find target list entry for param and retrieve expr value */
                te = (TargetEntry *) list_nth(context->sublink->targetList,param->paramid - 1);

                /* if the caller provides an varno value create a new var referencing this RTE */
                if (context->useVarnoValue)
                        newExpr = (Node *) makeVar(context->useVarnoValue, param->paramid, param->paramtype, param->paramtypmod, 0);
                /* else use the expr from the original sublink target entry */
                else
                        newExpr = (Node *) copyObject(te->expr);

                return (Node *) newExpr;
        }
        // adapt varlevelsup for Var nodes
        else if (IsA(node, Var))
        {
                Var *var;

                var = (Var *) node;

                if (context->addVarSublevelsUp)
                        var->varlevelsup = var->varlevelsup + context->addVarSublevelsUp;

                if (context->varSublevelsUp != -1)
                        var->varlevelsup = context->varSublevelsUp;

                return (Node *) var;
        }
        // adapt aggregation varlevels up
        else if (IsA(node, Aggref) && context->touchAggs)
        {
                Aggref *aggref;

                aggref = (Aggref *) node;
                aggref->agglevelsup = context->aggSublevelsUp;

                return expression_tree_mutator(node, replaceParamsMutator, (void *) context);
        }

        // recurse
        return expression_tree_mutator(node, replaceParamsMutator, (void *) context);
}
Node *
increaseSublevelsUpMutator (Node *node, int *context)
{
        if (node == NULL)
                return node;

        if (IsA(node, Var))
        {
                Var *var;

                var = (Var *) node;
                if (var->varlevelsup > *context)
                {
                        var->varlevelsup++;
                }

                return (Node *) var;
        }
        else if (IsA(node, Query))
        {
                int *newContext;

                newContext = (int *) palloc(sizeof(int));
                *newContext = *context + 1;
                return (Node *) query_tree_mutator((Query *) node, increaseSublevelsUpMutator, (void *) newContext, QTW_IGNORE_JOINALIASES);
        }

        return expression_tree_mutator(node, increaseSublevelsUpMutator, (void *) context);
}
Example #3
0
Node * my_mutator (Node *node, context_modifier *context)
  {
    if (node == NULL)
 			return NULL;
    /* elog(LOG,"%d",nodeTag(node)); */
    if(IsA(node,BoolExpr)){
      BoolExpr * bExpr =(BoolExpr *) node;
      if(bExpr->boolop == NOT_EXPR){
        /* elog(LOG,"\nTHERENOT\n"); */
        context->positive = !context->positive;
        Node * retour =  expression_tree_mutator(node, my_mutator, (void *) context);
        context->positive = !context->positive;
        return retour;
      }
      if(bExpr->boolop == OR_EXPR || bExpr->boolop == AND_EXPR){
        /*Pré traitement*/
        List * l_save = context->list_of_not_null_in_current;
        context->list_of_not_null_in_current = list_copy(l_save);
        if(context->positive){
          context->list_of_not_null_in_current = list_concat(context->list_of_not_null_in_current,list_nth(context->list_list_true,context->list_list_true->length - context->where_i_am - 1));
        }
        else{
          context->list_of_not_null_in_current = list_concat(context->list_of_not_null_in_current,list_nth(context->list_list_false,context->list_list_true->length - context->where_i_am - 1));
        }
        /* elog(LOG,"\n LIST_NOT_NULL: %s \n",nodeToString(context->list_of_not_null_in_current)); */
        context->where_i_am ++;
        Node * retour =  expression_tree_mutator(node, my_mutator, (void *) context);
        /*Post traitement*/
        context->list_of_not_null_in_current = l_save;

        return retour;
      }
    }
    if(IsA(node,Query)){
      Query * q = (Query *) node;
      /* context->current_varlevelsup ++; */
      List * save_current_trueVar = context->current_trueVar;
      context->current_trueVar = list_nth(context->trueVars,context->where_i_am_querry);
      context->where_i_am_querry = context->where_i_am_querry + 1;
      q->jointree = expression_tree_mutator((Node *) q->jointree, my_mutator, (void *) context);
      /* context->where_i_am_querry = context->where_i_am_querry - 1; */
      context->current_trueVar = save_current_trueVar;
      /* context->current_varlevelsup --; */
      return node;
    }
    if(IsA(node,OpExpr)){
      /* elog(LOG,"\nTHEREOP = \n"); */
      OpExpr * oExpr = (OpExpr *) node;
      if(context->positive && oExpr->opno == 518){ /*<>*/
        context->ready = true;
      }
      if(!context->positive && oExpr->opno == 96){ /* = */
        context->ready = true;
      }
      /* if(context->positive && oExpr->opno == 521){ #<{(| > |)}># */
      /*   context->ready = true; */
      /* } */
      if(oExpr->opno == 525){ /* >= */
        OpExpr * equa = makeNode(OpExpr);
        equa->opno = 96; /*=*/
        equa->args = list_concat(equa->args,oExpr->args);
        OpExpr * stric = makeNode(OpExpr);
        stric->opno = 521; /*>*/
        stric->args = list_concat(stric->args,oExpr->args);
        List * tmp = NULL;
        tmp = lappend(tmp,equa);
        tmp = lappend(tmp,stric);
        Node * result = expression_tree_mutator((Node *)makeBoolExpr(OR_EXPR,tmp,-1),my_mutator,context);
        return result;

      }
      if(oExpr->opno == 523){ /* <= */
        OpExpr * equa = makeNode(OpExpr);
        equa->opno = 96; /*=*/
        equa->args = list_concat(equa->args,oExpr->args);
        OpExpr * stric = makeNode(OpExpr);
        stric->opno = 97; /*<*/
        stric->args = list_concat(stric->args,oExpr->args);
        List * tmp = NULL;
        tmp = lappend(tmp,equa);
        tmp = lappend(tmp,stric);
        Node * result = expression_tree_mutator((Node *)makeBoolExpr(OR_EXPR,tmp,-1),my_mutator,context);
        return result;
      }
      /* if(context->positive && oExpr->opno == 97){ #<{(| < |)}># */
      /*   context->ready = true; */
      /* } */
      if(!context->positive && oExpr->opno == 1209){ /* LIKE */
        context->ready = true;
      }


      Node * result = expression_tree_mutator(node, my_mutator, (void *) context);
      if(context->constraint_to_add != NULL){
        if(!context->positive /* && oExpr->opno == 518 */){
          context->constraint_to_add = lappend(context->constraint_to_add,result);
          Node * to_return = makeBoolExpr(OR_EXPR,context->constraint_to_add,-1);
          context->constraint_to_add = NULL;
          context->ready = false;
          /* elog(LOG,"inter :%s\n",nodeToString(to_return)); */
          return to_return;
        }
        if(context->positive /* && oExpr->opno == 96 */){
          context->constraint_to_add = lappend(context->constraint_to_add,result);
          Node * to_return = makeBoolExpr(AND_EXPR,context->constraint_to_add,-1);
          context->constraint_to_add = NULL;
          context->ready = false;
          return to_return;
        }
      }
      else
        return result;

    }
 		if(context->ready && IsA(node, Var))
 		{
      /* elog(LOG,"\n LIST_NOT_NULL: %s \n",nodeToString(context->list_of_not_null_in_current)); */
      Var * v = (Var *) node;
      Var * rv = getTrueVar(context->current_trueVar,v);
      /* elog(LOG,"\nTRUE VARS : %s \n",nodeToString(context->current_trueVar)); */
      /* elog(LOG,"\nV check %s \n",nodeToString(v)); */
      /* elog(LOG,"\nRV check %s \n",nodeToString(rv)); */
      /* elog(LOG,"\nNOT NULL CUR: %s \n",nodeToString(context->list_of_not_null_in_current)); */
      if(!context->positive && !isInListTrueVar(context->list_of_not_null_in_current,rv)){
        /* elog(LOG,"\nHERE !\n"); */
        OpExpr * inf0 = makeNode(OpExpr);
        inf0->opno = 97; /*<*/
        inf0->args = lappend(inf0->args,node);
        Const * c0 = makeConst(23,-1,0,4,Int16GetDatum(0),false, true);
        inf0->args = lappend(inf0->args,c0);
        context->constraint_to_add = lappend(context->constraint_to_add,inf0);
      }
      if(context->positive){
        /* elog(LOG,"\nHERE !\n"); */
        OpExpr * inf0 = makeNode(OpExpr);
        inf0->opno = 521; /*>*/
        inf0->args = lappend(inf0->args,node);
        Const * c0 = makeConst(23,-1,0,4,Int16GetDatum(0),false, true);
        inf0->args = lappend(inf0->args,c0);
        context->constraint_to_add = lappend(context->constraint_to_add,inf0);
      }

 		}
 		return expression_tree_mutator(node, my_mutator, (void *) context);
  }