Skip to content

When two result columns have the same name, one is dropped #280

Closed
@davidcrawford

Description

@davidcrawford

This most commonly happens when issuing queries with functions. If you don't explicitly alias the result, the column is named after the function. For instance:

select origin, avg(delay), avg(distance) 
from flights
group by 1
limit 10;

 origin |       avg        |       avg        
--------+------------------+------------------
 SAN    | 10.3516289991195 | 475.812298209569
 SLC    | 6.66094166419899 | 579.881551673083
 SJC    | 11.6489714664897 | 394.197909754479
 BWI    | 8.25543071161049 | 615.350280898876
 ELP    | 6.72572101236021 |  496.80665097116
 HOU    | 7.62836264353971 |  413.10896972617
 BNA    | 5.42857142857143 | 749.543639921722
 MSY    | 7.46954113924051 | 506.503362341772
 SEA    | 5.51911027568922 | 665.635964912281
 IAH    | 4.94343065693431 |              217
(10 rows)

As far as I can tell, node-postgres only returns rows as objects (not arrays, e.g.). So the second 'avg' overwrites the first one in the object.

That's a great default, but since I'm using node-postgres to build a web client for sql analytic queries, I have to expect my users to write queries like this.

I'd like to propose an addition to the API of an option to emit rows as arrays (or maybe even just a separate event like 'row-array'). I'd love your feedback on whether this is something you'd be interested in pulling into the library, and how you'd recommend doing it.

One consideration is that if you send back an array, you don't get column names. I would propose a 'columns' event that fires before any row events and sends the column information for rows to come. This would be one way of addressing issue #209.

I'm happy to help out on this, though I have no familiarity with the V8 engine so it would be great to get pointers to good docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions