Sort a column that does not exist

From: Werner Echezuria <wercool(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Sort a column that does not exist
Date: 2009-04-01 02:57:47
Message-ID: 2485a25e0903311957g60f14a4bg3ae70bc4182920e2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I'm in a project that probably some of you have heart about, it is called
PostgreSQLf and I get some help before from this list. The developer team is
been doing some progress about. Now one of us has created an extra column
that is called membership degree, this performs some calculations over some
data that it is store on a data table. We just create a target entry on the
parser and the planner, then in the ExecScan function is where the
calculations performs.

Well, I have been assigned a task. I have to sort that target entry. I
created this target entry on parse_clause and then I do this:

List *
transformSortClause(ParseState *pstate,
List *orderlist,
List **targetlist,
bool resolveUnknown)
{
List *sortlist = NIL;
ListCell *olitem;

foreach(olitem, orderlist)
{
SortBy *sortby = lfirst(olitem);
TargetEntry *tle;
//To find out if it is the membership degree
char *namegrmemb = strVal(linitial(((ColumnRef *)
sortby->node)->fields));

if (strcmp(namegrmemb, "grmemb")==0)
tle = createTargetFuzzyEntry(targetlist);
else
tle = findTargetlistEntry(pstate, sortby->node,
targetlist, ORDER_CLAUSE);

sortlist = addTargetToSortList(pstate, tle,
sortlist, *targetlist,
sortby->sortby_dir,
sortby->sortby_nulls,
sortby->useOp,
resolveUnknown);

}

return sortlist;
}

//To sort the membership degree
TargetEntry *
createTargetFuzzyEntry(List **targetlist){

/*I just have to create the fuzzy target entry right here */
TargetEntry *tfp = makeNode(TargetEntry);
Const *cn = makeNode(Const);
float val = 1.0;
TargetEntry *tlast = list_nth(*targetlist, list_length(*targetlist)-1);

cn = makeConst(700, -1, 4, (Float4GetDatum(val)), false, true);
tfp->resorigtbl=tlast->resorigtbl;
tfp->expr = (Expr *) cn;
tfp->resno = list_length(*targetlist) + 1;
tfp->resname = "grmemb";
tfp->resorigcol = list_length(*targetlist) + 1;
tfp->ressortgroupref = 0;
tfp->resjunk = false;

*targetlist = lappend(*targetlist, tfp);

return tfp;
}
As you can see if someone do this: SELECT * FROM table WHERE
field=some_value ORDER BY grmemb, postgresql creates a new target entry and
then assigned to the targetlist as a sort node. I know that it creates the
node on the parser, but it does not work, it seems the executor don't see
it.

How could I sort a column like this?, I know i'm missing something, but i
just don't see it. What is the process to sort a column?

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2009-04-01 03:09:44 Re: psql \d* and system objects
Previous Message Alvaro Herrera 2009-04-01 02:38:15 Re: WIP: transformation hook modules and JSON support