-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
array parser? #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Absolutely. If you could gist an example or better yet submit a failing test & I'll get to work on it. |
SELECT ARRAY['key', 'value', 'key key', 'value value']; |
thanks. I've implemented a test - wondering what you would expect to be logged when you logged that row in your example? Something like this:
?? |
Yes, I would expect exactly this. |
I've used this function (copied idea from perl DBI::Pg library):
|
I've pushed an initial pass at this. I've exposed a place on the Query object where you can extend and write custom type parsers (as postgres will allow you to define custom types) Check out lib/query.js for an example, but basically you can do the following:
Where Kaiko - I've used your convert function for starters, but it does not convert the following properly:
I'm holding off on doing another npm version until I have an opportunity to regression test changes against 9.x versions of PostgreSQL and make the converting more robust. |
This works better (still only for 1-dimension arrays):
|
cool - want me to add it to the repo & test it out? You're also welcome to fork & do a pull request as long as the tests still pass. |
Alexander is working on a binary parser. I think it's the best way to do it. Check out https://github.com/AlexanderS/node-postgres Perhaps this could be integrated somehow? Perhaps we have to add support for other data types and we should write some tests :) |
yeah he sent a pull request, but it has no tests written. Once test are written for the binary stuff, I'd be more than happy to pull. |
Hi! What's the status of the subj? TIA |
The status of the array parser? The array parser implementation can be "swapped out" by the above mentioned method. I've not received any pull requests in regards to a better implementation. If you have one (with tests) I'd be happy to merge it in. Or of Alexander's changes? Alexander's changes are still lacking any test coverage unfortunately. :( |
I see. I just yesterday discovered this project and am way immature to offer. So far just pulling the info and exploring the minefield ;) |
We will work on tests in the next 4 weeks... |
TIA. BTW, how at all arrays are useful comparing to normalized out data? I'm a noob to postgres -- what pros/cons do you see? |
Hi, I am one of the co-authors of libpqtypes (http://libpqtypes.esilo.com/) and have been really been playing with incorporating similar concepts into a node.js postgresql driver. Not so much the printf/scanf stuff, but: *) binary protocol message handling *) arrays and composites in both directions *) composite type discovery *) full type implementation interested? if done properly it should really nail the array issue. Iooked at the various node.js pg drivers and this one seems the best suited to do this type of thing. maybe we can put our heads together and get something done. not sure if its better to tackle the native driver, c++, or both :(. |
@dvv:
arrays (and composite types) in postgres are useful in the following scenarios: *) function arguments: postgresql functions can't take a set of records as an input argument. you can however sent in a block of records. consider the following code block: create table foo(a int, b text); the argument to add_foos can be parameterized and sent to the server as a single argument from the client. this can be used to do 'nosql' type approaches to send very complex data (composites in postgres can nest) back and forth from the client. |
We are still working on binary support. See our fork https://github.com/AlexanderS/node-postgres for more information. Feel free to contribute! |
I'm interested in adding binary support for both the plain javascript and native bindings. I want to include the work on the AlexandereS branch; however, no test coverage of the new functionality is a no-go. @merlinm I think the libpqtypes library looks great. Looking at libpqtypes I'm not sure just including it and calling it's functions would work properly as node-postgres native bindings yield to the node (libev) event loop and do no select/poll of their own when executing async commands from libpq. The node-postgres client also does not and will never support synchronous query/command execution (sync command execution would block the event loop in node..big no-no). I would be happy to work with you on including the advanced type stuff into node-postgres...the work you've done looks really valuable As long as a pull request has test coverage I'm happy to help and will include anything that makes node-postgres better for the community! |
OK, took a look at the fork and have some thoughts (it's a great start btw): *) It's probably good to separate the container type parsers (arrays/composites) from the POD parsers (int, float, etc). When you are pulling out container format it turns out you can always break it down to simpler types. *) Aside from the above, I saw a fair amount of optimization potential. For example, when iterating result rows, you don't have to look up conversion functions by oid over and over...just do it once. *) Is it worth doing anything besides the fully native protocol? anybody got some benchmark results handy showing libpq vs native? |
|
@brianc I am suspicious the libpqtypes library will be of much use directly. First of all, it heavily wraps libpq, so it can't support the native driver. It's a also bit of overkill for what is needed here. Maybe some of the type conversion code can be cribbed into a support library that is shared by the native and libpq versions. On top of that, there is a non zero chance that the binary protocol mode is a total non starter for the v8 engine -- just too hard to say right now. All I'm after is to duplicate some of the functionality here -- to fluff arrays and composites into and out of javascript variables primarily. |
Cool man. Was thinking similar things. I think doing the type coercion in the C for the native bindings would be a good start, along with support for more complex types. I am kinda in the dark about how complex types work and their best uses so some integration tests for complex types would be great too. I'm not really sure how fast/slow javascript works with binary manipulation, but the newest versions of node have some more work put into buffers around this issue: https://github.com/joyent/node/blob/master/lib/buffer.js#L506 It's possible this will be moved into their C/C++ layer at some point making binary manipulation perhaps very fast after all. |
Composite types are just a box you can put other types in. create type foo as (a int, b text); Also in postgres all tables are themselves composite types, and composite types can nest, and can be made into arrays. libpqtypes made two innovations to deal with them in C:
I'm imagining that node-postgres will do all this under the hood -- as much as possible, the library will take care of it for you. Since it looks like supporting both the native and libpq protocol modes is baked in (2x performance can't be discarded trivially), hopefully we can keep most of the main logic shared. |
Just want to chime in how awesome this feature would be for both performance and ease of use when dealing with one to many relationships. For example, with the following schema: create table contacts (
id serial,
name text
);
create table contacts_phones (
id serial,
contact_id int references contacts(id),
number text
); Because tables in postgres are composite types, we can: select contacts.*,
(select array(select contacts_phones from contacts_phones where contacts_phones.contact_id = contacts.id))
from contacts Which is much better than having to query contacts, iterate, and then query contacts_phones (or do a join, which still requires iteration to reduce contacts to a single record and phones to an array). But what I like most about being able to use a single query is now this is very friendly for emitter style queries and reduces async clutter big time. plv8 supports this now, so it might be a good reference to see how they are doing it. |
if you're into that type of querying (which I 100% agree with), you need to check out the new json support for postgres. The existence of that is one of the reasons I haven't moved forward with a client side solution. the new json stuff in in-core for 9.2 but can be loaded as an extension for 9.1:
|
@merlinm Agreed the row_to_json stuff is very cool, I actually got a tip from andrew and backported to 9.1 with support for getting the proper column names (instead of 'f1', 'f2', etc...). The only issue I ran into with this approach is you loose some typing, such as dates and a faint whisper in the back of my mind that said "if this was client side, you wouldn't need to call JSON.parse" ;) |
yeah -- good point, and thanks for the tip on the f1/f2 issue. regarding json.parse, so far my usage for node is mostly on data services that i'm pulling from the database and immediately piping to the client so that node isn't handling the json in any way. in other words i'm keeping the node layer incredibly thin as it's just bouncing json up (although data transmission to the database is more complex obviously). i like node, but I'm not comfortable with it enough to put a big logic investment there. that's partially due to lack of experience on my part and partially due to risk of having the platform being forcibly replaced with enterprise java for the application i'm writing, so I have to stay light. anyways, postgres + node + jquery is turning out to be an incredibly productive stack, and I've had zero issues with the node-postgres module. |
All the issues covered here are addressed in either closed pull requests or more current & specific issues. |
Add support for Typescript typings thanks @NoNameProvided !
Are there any plans to parse arrays? We get arrays from our database and would like to work with arrays in nodejs too.
The text was updated successfully, but these errors were encountered: