Thread: To know what a macro does

To know what a macro does

From
Werner Echezuria
Date:
Hi, I've been trying to sort a column that performs some calculations, but postgres says this: ERROR:  invalid attnum:
-12851.I was searching on the source code, and I guess the error araises around this macro: <br />/*<br />      * Copy
thegiven tuple into memory we control, and decrease availMem.<br />     * Then call the common code.<br />     */<br
/>   COPYTUP(state, &stup, (void *) slot);<br /><br />So, I'd like to know what COPYTUP does?, what is availMem?,
Howcan I tell postgres it is a valid attnum?<br /><br />Thanks for any help!!!!<br /> 

Re: To know what a macro does

From
Martijn van Oosterhout
Date:
On Sun, Apr 26, 2009 at 04:20:41PM -0430, Werner Echezuria wrote:
> Hi, I've been trying to sort a column that performs some calculations, but
> postgres says this: ERROR:  invalid attnum: -12851. I was searching on the
> source code, and I guess the error araises around this macro:

I'm pretty sure this is a "not supposed to happen" thing. Do you have a
repoducable test case? Also, what version of postgres?

> /*
>      * Copy the given tuple into memory we control, and decrease availMem.
>      * Then call the common code.
>      */
>     COPYTUP(state, &stup, (void *) slot);
>
> So, I'd like to know what COPYTUP does?, what is availMem?, How can I tell
> postgres it is a valid attnum?

COPYTUP does exactly what the comment says it does. I'm guessing this
is in the sort code somewhere? An "attnum" is a column number, like the
first column is attnum 1. Attnum -12851 is definitly bogus.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

Re: To know what a macro does

From
Werner Echezuria
Date:
Well, I do a query like this: "SELECT * FROM historial WHERE id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in
transformSortClauseI know it this way:<br /><br /> /*<br /> * transformSortClause -<br /> *      transform an ORDER BY
clause<br/>  *<br /> * ORDER BY items will be added to the targetlist (as resjunk columns)<br /> * if not already
present,so the targetlist must be passed by reference.<br /> */<br />List *<br />transformSortClause(ParseState
*pstate,<br/>                    List *orderlist,<br />                     List **targetlist,<br />                   
boolresolveUnknown)<br />{<br />    List       *sortlist = NIL;<br />    ListCell   *olitem;<br /><br />   
foreach(olitem,orderlist)<br />    {<br />        SortBy       *sortby = lfirst(olitem);<br />         TargetEntry
*tle;<br/>        //To find out if it is the membership degree<br />        char       *namegrmemb =
strVal(linitial(((ColumnRef*) sortby->node)->fields));<br /><br />        <b>if (strcmp(namegrmemb,
"grmemb")==0)<br/></b>            tle = createTargetFuzzyEntry(targetlist);<br />        else<br />            tle =
findTargetlistEntry(pstate,sortby->node,<br />                                      targetlist, ORDER_CLAUSE);<br
/><br/>        sortlist = addTargetToSortList(pstate, tle,<br />                                        sortlist,
*targetlist,<br/>                                       sortby->sortby_dir,<br />                                   
  sortby->sortby_nulls,<br />                                       sortby->useOp,<br />                        
              resolveUnknown);<br />        <br />        <br /><br />    }<br /><br />    return sortlist;<br />}<br
/><br
/>|***************************************************************************************************************************|<br
/><br/>Then I created a function that includes a new target entry in the targetlist:<br /><br />//To sort the
membershipdegree<br />TargetEntry *<br />createTargetFuzzyEntry(List **targetlist){<br />    <br />    /*I just have to
createthe fuzzy target entry right here */<br />     TargetEntry    *tfp = makeNode(TargetEntry);<br />    Const   *cn
=makeNode(Const);<br />    float    val = 1.0;<br />    TargetEntry *tlast = list_nth(*targetlist,
list_length(*targetlist)-1);<br/><br />    cn = makeConst(700, -1, 4, (Float4GetDatum(val)), false, true); <br />    
tfp->resorigtbl=tlast->resorigtbl;<br/>    tfp->expr = (Expr *) cn;<br />    tfp->resno =
list_length(*targetlist)+ 1;<br />    tfp->resname = "grmemb";<br />    tfp->resorigcol =
list_length(*targetlist)+ 1;<br />     tfp->ressortgroupref = 0;<br />    tfp->resjunk = false;<br /><br />   
*targetlist= lappend(*targetlist, tfp);<br /><br />    return tfp;<br />}<br /><br
/>|*************************************************************************************************************************|<br
/><br/>Later in planner.c on grouping_planner function I do something like this:<br /><br />/*<br />     * If we were
notable to make the plan come out in the right order, add<br />     * an explicit sort step.<br />     */<br />    if
(parse->sortClause)<br/>     {<br />        if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys) ||
parse->hasGrMemb)<br/>        {<br />            result_plan = (Plan *) make_sort_from_pathkeys(root,<br />       
                                                  result_plan,<br />                                                
          sort_pathkeys,<br />                                                           limit_tuples);<br />       
   current_pathkeys = sort_pathkeys;<br />        }<br />    }<br /><br /><br /><div class="gmail_quote">2009/4/26
Martijnvan Oosterhout <span dir="ltr"><<a href="mailto:kleptog@svana.org">kleptog@svana.org</a>></span><br
/><blockquoteclass="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex;
padding-left:1ex;"><div class="im">On Sun, Apr 26, 2009 at 04:20:41PM -0430, Werner Echezuria wrote:<br /> > Hi,
I'vebeen trying to sort a column that performs some calculations, but<br /> > postgres says this: ERROR:  invalid
attnum:-12851. I was searching on the<br /> > source code, and I guess the error araises around this macro:<br /><br
/></div>I'mpretty sure this is a "not supposed to happen" thing. Do you have a<br /> repoducable test case? Also, what
versionof postgres?<br /><div class="im"><br /> > /*<br /> >      * Copy the given tuple into memory we control,
anddecrease availMem.<br /> >      * Then call the common code.<br /> >      */<br /> >     COPYTUP(state,
&stup,(void *) slot);<br /> ><br /> > So, I'd like to know what COPYTUP does?, what is availMem?, How can I
tell<br/> > postgres it is a valid attnum?<br /><br /></div>COPYTUP does exactly what the comment says it does. I'm
guessingthis<br /> is in the sort code somewhere? An "attnum" is a column number, like the<br /> first column is attnum
1.Attnum -12851 is definitly bogus.<br /><br /> Have a nice day,<br /><font color="#888888">--<br /> Martijn van
Oosterhout  <<a href="mailto:kleptog@svana.org">kleptog@svana.org</a>>   <a href="http://svana.org/kleptog/"
target="_blank">http://svana.org/kleptog/</a><br/> > Please line up in a tree and maintain the heap invariant
while<br/> > boarding. Thank you for flying nlogn airlines.<br /></font><br />-----BEGIN PGP SIGNATURE-----<br />
Version:GnuPG v1.4.9 (GNU/Linux)<br /><br /> iD8DBQFJ9M7xIB7bNG8LQkwRAviuAJ9F9GeldnVLInum3ZaT0IKTNvk3dACdElyo<br />
7VZ7cLAgu1q4PncHS8rVYJU=<br/> =Y5Qw<br /> -----END PGP SIGNATURE-----<br /><br /></blockquote></div><br /> 

Re: To know what a macro does

From
Martijn van Oosterhout
Date:
On Sun, Apr 26, 2009 at 08:33:42PM -0430, Werner Echezuria wrote:
> Well, I do a query like this: "SELECT * FROM historial WHERE
> id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in transformSortClause
> I know it this way:

Ok, this is way over my head. But really, it would be helpful to know
what you're trying to achieve. Why are you changing the source code?

Maybe someone else can answer your question specifically.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

Re: To know what a macro does

From
Werner Echezuria
Date:
Well, I'm in a project called PostgreSQLf and we're trying to include fuzzy logic inside PostgreSQL. Now I've been thinking this is getting too hard, do you know if I can just sort the results with something like this : Sort(ResultSlot,column)?, I mean without the Order By clause?

2009/4/28 Martijn van Oosterhout <kleptog@svana.org>
On Sun, Apr 26, 2009 at 08:33:42PM -0430, Werner Echezuria wrote:
> Well, I do a query like this: "SELECT * FROM historial WHERE
> id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in transformSortClause
> I know it this way:

Ok, this is way over my head. But really, it would be helpful to know
what you're trying to achieve. Why are you changing the source code?

Maybe someone else can answer your question specifically.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iD8DBQFJ9U7jIB7bNG8LQkwRAhDPAJ9+q/XSGmUo0t2jxdWHtvVKhPt/mACdEoOn
g9bLCsAzjy0oFBMsjvi7Rfg=
=Mofr
-----END PGP SIGNATURE-----


Re: To know what a macro does

From
Tom Lane
Date:
Werner Echezuria <wercool@gmail.com> writes:
> Later in planner.c on grouping_planner function I do something like this:

Well, you've omitted showing us the code where the problem is likely to
be, but I am kinda thinking that you've shot yourself in the foot by
trying to represent your special ordering clause as a simple constant.
The planner is quite smart enough to throw away "order by constant"
as a no-op.  By the time you get down to the pathkey logic it's just
going to be ignoring that clause entirely; and if you try to brute-force
it you're more than likely going to break something.

Rather than kluging up any of this code, I wonder whether you couldn't
represent your fuzzy sorting requirement as ORDER BY some_function(...)
and put all the smarts into that function.
        regards, tom lane


Re: To know what a macro does

From
Werner Echezuria
Date:
2009/4/28 Tom Lane <tgl@sss.pgh.pa.us>
Well, you've omitted showing us the code where the problem is likely to
be, but I am kinda thinking that you've shot yourself in the foot by
trying to represent your special ordering clause as a simple constant.
The planner is quite smart enough to throw away "order by constant"
as a no-op.  By the time you get down to the pathkey logic it's just
going to be ignoring that clause entirely; and if you try to brute-force
it you're more than likely going to break something.

Well, I know I'm breaking something, because when I execute "\d" in psql, the server hangs out.

When I performs the order by clause, I call a function in transformSelectStmt on analyze.c (This is parse->hasGrMemb, that I call in grouping_planner  ) :

qry->hasGrMemb = hassSortByGrMemb(stmt->sortClause);

|****************************************************************************************************|

Then the function is this (on parse_clause.c):

//To know if the query has an ORDER BY grmemb
bool
hassSortByGrMemb(List *orderlist){

    ListCell   *olitem;
    bool        result;
   
    result=false;
    foreach(olitem, orderlist)
    {
        SortBy       *sortby = lfirst(olitem);
        char       *namegrmemb = strVal(linitial(((ColumnRef *) sortby->node)->fields));
        
        if (strcmp(namegrmemb, "grmemb")==0)
            result=true;
    }

    return result;

}


Rather than kluging up any of this code, I wonder whether you couldn't
represent your fuzzy sorting requirement as ORDER BY some_function(...)
and put all the smarts into that function.

Well, the project force me to include all the source in the core. That is why I wanna know if there's any chance that I could sort the final resultSlot?.

regards