michaelwittig / node-q

Q interfacing with Node.js
MIT License
52 stars 14 forks source link

Queries with 'by' clauses do not return results #18

Closed savvione closed 8 years ago

savvione commented 8 years ago

Hi Michael,

Great work on the module! I've run into an issue - it seems like 'by' clauses confuse node-q. It doesn't throw an exception, I just get an empty-ish result:

q)\c 10 150
q)numtests:100; timerange:10D; freq:0D00:05;
testid:(til numtests)!000000999999+numtests?20;
fcn:numtests*fc:`long$timerange%freq;
tests:([]time:(-0D00:00:10 + fcn?0D00:00:20)+fcn#(.z.p - timerange)+freq*til fc; test:raze fc#'key testid; testin:fcn?16741128383987; testout:fcn?16741128383987)

q)select min testin by test from tests
test| testin    
----| ----------
0   | 8564163863
1   | 2297114685
2   | 1001314197
3   | 4260343635
4   | 4177822301
..
q)

var nodeq = require("node-q");
nodeq.connect({host: "localhost", port: 4000}, function(err, con) {
        if (err) throw err;
        console.log("connected");
        console.time("query");
        con.k("select min testin by test from tests", function(err, res) {
                if (err) throw err;
                console.timeEnd("query");
                console.log("test(" + res.length + " samples)");
                console.log(res);
                console.log("first", res[0]);
                console.log(" last", res[res.length-1]);
        });
});

connected
query: 5ms
test(2 samples)
[ ,  ]
first undefined
 last undefined
^C

var nodeq = require("node-q");
nodeq.connect({host: "localhost", port: 4000}, function(err, con) {
    if (err) throw err;
    console.log("connected");
    console.time("query");
    con.k("select min testin from tests", function(err, res) {
        if (err) throw err;
        console.timeEnd("query");
        console.log("test(" + res.length + " samples)");
        console.log(res);
        console.log("first", res[0]);
        console.log(" last", res[res.length-1]);
    });
});

connected
query: 3ms
test(1 samples)
[ { testin: 40638960 } ]
first { testin: 40638960 }
 last { testin: 40638960 }
^C
daveonhols commented 8 years ago

I just discovered this so I haven't tried it, but just to speculate. The results of a "by clause" query will be a keyed table, not a normal table, so maybe node-q doesn't handle that properly. Have you tried something as simple as putting "0!" in front of the query to remove the key - it will make the result a normal table which node-q may have more luck with.

A keyed table in Q is actually represented as a dictionary, the key of which is a table representing the cols / rows of the key, the value of the result dictionary is also a table, representing the values on the keyed table.

michaelwittig commented 8 years ago

I fixed one bug associated with this (use version 1.1.1). A keyed table is now (as c.js does) turned into an Array of size 2 where the first element contains the keys and the second contains the values. Each is an array of objects.

But the question that remains open is how should a keyed table be turned into JavaScript because we don't have a very useful data structure to use. Any ideas on that?