Re: Sort a column that does not exist - Mailing list pgsql-hackers

From Werner Echezuria
Subject Re: Sort a column that does not exist
Date
Msg-id 2485a25e0904020921y187ad0c9u85a31da34a0da749@mail.gmail.com
Whole thread Raw
In response to Re: Sort a column that does not exist  (Hitoshi Harada <umi.tanuki@gmail.com>)
Responses Re: Sort a column that does not exist
List pgsql-hackers

Hi, the problem goes on. I think the problem is in the planner.c on grouping_planner function, because when I do a regular sort it gets to it:

    /*
     * If we were not able to make the plan come out in the right order, add
     * an explicit sort step.
     */
    if (parse->sortClause)
    {
        if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys))
        {
            result_plan = (Plan *) make_sort_from_pathkeys(root,
                                                           result_plan,
                                                           sort_pathkeys,
                                                           limit_tuples);
            current_pathkeys = sort_pathkeys;
        }
    }

and do the make_sort_from_pathkeys, but when I do the sort by grmemb it does not. So I change it in order to pass through make_sort_from_pathkey, but it drops an error like this "invalid attnum", so when I go to heaptuple and force to get to ObjectIdGetDatum in heap_getsysattr, the server hang out. What can I do? How can I assign a valid attrnum?


2009/4/1 Hitoshi Harada <umi.tanuki@gmail.com>
2009/4/1 Werner Echezuria <wercool@gmail.com>:
> 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.

See include/nodes/primnodes.h around line 1075:
       bool            resjunk;                /* set to true to eliminate the attribute from
                                                                * final target list */

If the TargetEntry is set resjunk = false, the final result is
filtered as junk. So more accurately the executor sees but drops 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?
>

Use makeTargetEntry in makefuncs.c
TargetEntry *
makeTargetEntry(Expr *expr,
                               AttrNumber resno,
                               char *resname,
                               bool resjunk)

by the 4th argument you can set resjunk = false if you don't want it
to be in the result.

Regards,


--
Hitoshi Harada

pgsql-hackers by date:

Previous
From: "David E. Wheeler"
Date:
Subject: Re: [GENERAL] string_to_array with empty input
Next
From: Tom Lane
Date:
Subject: Re: questions about not-null constraints and inheritance