graemedouglas / LittleD

A relational database for embedded devices and sensors nodes
Apache License 2.0
726 stars 51 forks source link

SELECT not working #1

Closed bradjc closed 8 years ago

bradjc commented 8 years ago

Not sure if I'm doing something wrong here but I'm running this code:

#include "../../dbparser/dbparser.h"

int main(void)
{
     {
        char          memseg[1000];
        db_query_mm_t mm;
        db_op_base_t* root;
        db_tuple_t    tuple;
        init_query_mm(&mm, memseg, 1000);

        // DML commands don't produce operators, they just execute.
        parse("CREATE TABLE sensors (temp int);", &mm);
        parse("INSERT INTO sensors VALUES (221);", &mm);
        parse("INSERT INTO sensors VALUES (89884);", &mm);
        parse("INSERT INTO sensors VALUES (112);", &mm);
        parse("INSERT INTO sensors VALUES (455);", &mm);

        root = parse("SELECT * FROM sensors", &mm);
        if (root == NULL) {
            // Always gets here
            printf("NULL root\n");
        } else {
            init_tuple(&tuple, root->header->tuple_size, root->header->num_attr, &mm);

            while (next(root, &tuple, &mm) == 1) {
            // Do whatever. Check src/dbobjects/tuple.h for how to extract tuple data.
                printf("ok!\n");
            }
        }

    }
    return 0;
}

and the select * returns NULL. If I add another INSERT or remove one INSERT, the SELECT returns correctly.

graemedouglas commented 8 years ago

Brad, which device are you using this on, or just on your development system? Did you pull the latest changes from November 9th, 2015?

If this is just being run on a standard desktop OS (Linux, Mac, Windows), you can try enabling the error messages in here. It should be enabled by default. Can you tell me what the error message was?

EDIT: I've run your code on my system, and everything went fine/as expected. So I am guessing you are running this on device, which is bad because it probably means there is some sort of memory leak I haven't caught with valgrind/others. Have you modified the LittleD code in any other way? Since I likely won't have your specific device available to me, you will unfortunately need to dig into the code. If the parser is returning NULL on the SELECT parse, then dbparser.c is where you need to look, although that can be daunting without a debugger since there are many cases in which NULL is returned.

bradjc commented 8 years ago

I'm testing on Linux.

I think this boils down to a bug in db_op_base_t* parse(char* command, db_query_mm_t* mmp) stemming from the fact that the array of clausenode is initialized with an element:

struct clausenode *clausestack = db_qmm_balloc(mmp, sizeof(struct clausenode));

but that item is neither used nor initialized. In my test, it was unfortanutely getting set with a bcode of 1 (INSERT clause) which was then causing later code to fail because there is obviously no insert in a select *. By adding an init to bcode to make the start of parse look like:

    /* Create the clause stack. Top will start at back and move forwards. */
    struct clausenode *clausestack_bottom = mmp->last_back;
    struct clausenode *clausestack = db_qmm_balloc(mmp,
                        sizeof(struct clausenode));
    struct clausenode *clausestack_top = clausestack;
    clausestack->start = -1;    /* Used to flag first clause found. */
    clausestack->bcode = 0; /* Used to flag first clause found. */

things are working.

I still don't quite understand why there is an extra element at the beginning as it doesn't seem to get used.

graemedouglas commented 8 years ago

OK,

Thanks, thats definitely a problem. Before I simply created a memory allocation spot for a stack (allocated of size 0) and one of the recent changes was to add the item up front to fix a bug during some testing. I obviously didn't turn my brain on and think why I was writing to bad memory in the first place.

I will do some testing and push (properly) fixed code soon.

graemedouglas commented 8 years ago

Sorry that you had to go through the pain in finding that! I've removed the extra allocation and writes completely, all regression tests pass. I've pushed the code up, the issue should now be resolved.

yashpanchal95 commented 5 years ago

Hello, I tried same example. as it is working for store the data and create the table. but when it is query as "select * from table_name;". it is unable to extract the table information.