Changeset: 98cd8480f593 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=98cd8480f593
Modified Files:
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_rdf.c
        sql/backends/monet5/sql_rdf_jgraph.c
        sql/backends/monet5/sql_rdf_jgraph.h
        sql/backends/monet5/sql_scenario.c
        sql/server/rel_optimizer.c
        sql/server/rel_rdfscan.c
        sql/server/rel_rdfscan.h
Branch: rdf
Log Message:

Fix the problem of pushing down topN

+ prepare to add the Select on RDFscan


diffs (truncated from 395 to 300 lines):

diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -139,14 +139,18 @@ sql_symbol2relation(mvc *c, symbol *sym)
                }
 
                if (1){
-               rel_print(c,r,0);
+               //rel_print(c,r,0);
+               printf("BEFORE running rel_optimizer\n");
+               _rel_print(c,r);
                r = rel_optimizer(c, r);
 
                r = rel_distribute(c, r);
                if (rel_is_point_query(r) || rel_need_distinct_query(r))
                        c->point_query = 1;
                
-               rel_print(c,r,0); 
+               //rel_print(c,r,0); 
+               printf("AFTER running rel_optimizer\n");
+               _rel_print(c,r);
                
                }
        }
diff --git a/sql/backends/monet5/sql_rdf.c b/sql/backends/monet5/sql_rdf.c
--- a/sql/backends/monet5/sql_rdf.c
+++ b/sql/backends/monet5/sql_rdf.c
@@ -1790,6 +1790,10 @@ void combine_exception_and_regular_table
                oid sbt = sbatCursor[pos]; 
                int tid = -1; 
                oid tmpS = BUN_NONE; 
+
+               if (sbt == (oid)3835096557682764){
+                       printf("[DEBUG] FOUND THAT SUBJECT HERE\n");
+               }
                getTblIdxFromS(sbt, &tid, &tmpS);
                if (tid != curtid){
                        #if RDF_HANDLING_EXCEPTION_MISSINGPROP_OPT
@@ -1842,6 +1846,9 @@ void combine_exception_and_regular_table
                }
 
                //printf("At row "BUNFMT" of table %d for sbt "BUNFMT"...", 
tmpS, tid, sbt); 
+               if (sbt == (oid)3835096557682764){
+                       printf("[DEBUG2] FOUND THAT SUBJECT HERE\n");
+               }
                accept = 1; 
                #if RDF_HANDLING_EXCEPTION_MISSINGPROP_OPT
                for (j = 0; j < num_mp; j++){
@@ -1898,7 +1905,19 @@ void combine_exception_and_regular_table
        free(regular_obat_cursors); 
 }
 
+static void
+BATprint_topn(BAT *b, int n){
+       BAT *tmp = NULL; 
+       if (BATcount(b) > (oid) n){
+               tmp = BATslice(b, 0, n);
+       } else {
+               tmp = BATslice(b, 0, BATcount(b) - 1); 
+       }
 
+       BATprint(tmp); 
+
+       BBPunfix(tmp->batCacheid); 
+}
 
 /*
  * The input for this pattern should be
@@ -1977,6 +1996,8 @@ SQLrdfScan(Client cntxt, MalBlkPtr mb, M
                str msg; 
                BAT *pso_fullSbat = NULL, *pso_fullObat = NULL;
                clock_t sT1, eT1; 
+               BUN testbun = BUN_NONE; 
+               BUN testoid = 3835096557682764;
                
                sT1 = clock(); 
                rethrow("sql.rdfShred", msg, getSQLContext(cntxt, mb, &m, 
NULL));
@@ -1988,12 +2009,25 @@ SQLrdfScan(Client cntxt, MalBlkPtr mb, M
 
                eT1 = clock(); 
                printf("Step 1 in Handling exception took  %f seconds.\n", 
((float)(eT1 - sT1))/CLOCKS_PER_SEC);
+               
 
+               testbun = BUNfnd(r_sbat, &testoid); 
+               if (testbun == BUN_NONE){
+                       printf("[DEBUG] That subject is not here\n");
+               } else {
+                       printf("[DEBUG] The subject is found at " BUNFMT " 
position\n", testbun);
+               }
                //Step 2. Merge exceptions with Tables
                sT1 = clock();
                
                combine_exception_and_regular_tables(m, &m_sbat, &m_obats, 
r_sbat, r_obats, lstProps, *nP, *nRP);
                
+               printf("Combining exceptions and regular table returns "BUNFMT 
" rows\n", BATcount(m_sbat)); 
+
+               BATprint_topn(m_sbat, 5); 
+               for (i = 0; i < (*nP); i++){
+                       BATprint_topn(m_obats[i], 5); 
+               }
                //BATprint(m_sbat); 
 
                for (i = 0; i < (*nP); i++){
diff --git a/sql/backends/monet5/sql_rdf_jgraph.c 
b/sql/backends/monet5/sql_rdf_jgraph.c
--- a/sql/backends/monet5/sql_rdf_jgraph.c
+++ b/sql/backends/monet5/sql_rdf_jgraph.c
@@ -247,8 +247,8 @@ sql_table *create_dummy_table(mvc *c, st
                        char colname_s[50], colname_o[50]; 
                        sql_subtype tpe; 
                        sql_find_subtype(&tpe, "oid", 31, 0);
-                       sprintf(colname_s, "dummy_col_s_%d", i); 
-                       sprintf(colname_o, "dummy_col_o_%d", i);                
+                       sprintf(colname_s, "dummy_col_%d_s", i); 
+                       sprintf(colname_o, "dummy_col_%d_o", i);                
                        if (mvc_bind_column(c, tbl, colname_s) == NULL){
                                mvc_create_column(c, tbl, colname_s, &tpe);
                        }
@@ -1101,7 +1101,7 @@ void _detect_star_pattern(jgraph *jg, jg
  * a star pattern
  * */
 static 
-spProps *init_sp_props(int num){
+spProps *init_sp_props(mvc *c, int num){
        int i; 
        spProps* spprops = NULL; 
        spprops = (spProps*) GDKmalloc(sizeof (spProps) ); 
@@ -1109,11 +1109,13 @@ spProps *init_sp_props(int num){
        spprops->subj = BUN_NONE; 
        spprops->lstProps = (char **) GDKmalloc(sizeof(char *) * num); 
        spprops->lstPropIds = (oid *) GDKmalloc(sizeof(oid) * num); 
+       spprops->lstAlias = (char **) GDKmalloc(sizeof(char *) * num);
        spprops->lst_o_constraints = (o_constraint *) 
GDKmalloc(sizeof(o_constraint) * num); 
        spprops->lstPOs = (sp_po *) GDKmalloc(sizeof(sp_po) * num); 
 
        for (i = 0; i < num; i++){
                spprops->lstProps[i] = NULL; 
+               spprops->lstAlias[i] = NULL; 
                spprops->lstPropIds[i] = BUN_NONE; 
                spprops->lst_o_constraints[i].cmp_type = -1; 
                spprops->lst_o_constraints[i].low = BUN_NONE; 
@@ -1123,6 +1125,9 @@ spProps *init_sp_props(int num){
        }
        spprops->lstctype = (ctype *) GDKmalloc(sizeof(ctype) * num); 
 
+       spprops->exps = new_exp_list(c->sa);
+       (void) c; 
+
        return spprops; 
 }
 
@@ -1130,7 +1135,16 @@ static
 void add_props_and_subj_to_spprops(spProps *spprops, int idx, sp_po po, jgnode 
*node){
 
        if (node->prop){
+               str tmpalias = NULL; 
+               sql_rel *tmprel = (sql_rel*) (node->data);
+               assert(tmprel->op == op_select);
+               assert(((sql_rel*)tmprel->l)->op == op_basetable); 
+
+               tmpalias = get_relname_from_basetable(tmprel->l); 
                spprops->lstProps[idx] = GDKstrdup(node->prop); 
+               spprops->lstAlias[idx] = GDKstrdup(tmpalias); 
+               printf("\nTable alias in spprops is %s\n", 
spprops->lstAlias[idx]);
+
                assert(node->poid != BUN_NONE); 
                spprops->lstPropIds[idx] = node->poid; 
                spprops->lstPOs[idx] = po;
@@ -1175,12 +1189,15 @@ void free_sp_props(spProps *spprops){
        int i; 
        for (i = 0; i < spprops->num; i++){
                if (spprops->lstProps[i]) GDKfree(spprops->lstProps[i]); 
+               if (spprops->lstAlias[i]) GDKfree(spprops->lstAlias[i]);
        }
        GDKfree(spprops->lstProps); 
+       GDKfree(spprops->lstAlias);
        GDKfree(spprops->lstPropIds);
        GDKfree(spprops->lstPOs); 
        GDKfree(spprops->lstctype);
        GDKfree(spprops->lst_o_constraints);
+       list_destroy(spprops->exps); 
        GDKfree(spprops); 
 }
 
@@ -2464,7 +2481,8 @@ sql_rel* build_rdfexception (mvc *c, int
                his[i] = spprops->lst_o_constraints[i].hi;
        }
  
-       rel_rdfscan = rel_rdfscan_func(c, tbl, spprops->num, 
nnodes_per_ijgroup[0], spprops->lstPropIds, los, his); 
+       rel_rdfscan = rel_rdfscan_func(c, tbl, spprops->num, 
nnodes_per_ijgroup[0], spprops->lstPropIds, los, his, spprops->exps); 
+       //rel_rdfscan = rel_rdfscan_func(c, tbl, spprops->num, 
nnodes_per_ijgroup[0], spprops->lstPropIds, los, his, NULL); 
        
        printf("\nRDFSCAN \n");
        _rel_print(c, rel_rdfscan);
@@ -3162,6 +3180,38 @@ sql_rel* union_sp_from_all_matching_tbls
        return rel; 
 }
 
+static void
+add_spprops_exps(mvc *c, list *retexps, list *exps){
+
+       node *en;
+       sql_allocator *sa = c->sa;
+
+       for (en = exps->h; en; en = en->next){
+               sql_exp *tmpexp = (sql_exp *) en->data; 
+               sql_exp *e = (sql_exp *)tmpexp->l; 
+
+               assert(tmpexp->type == e_cmp); //TODO: Handle other exps for 
op_select
+               assert(e->type == e_convert); 
+
+               e = e->l; 
+
+               assert(e->type == e_column); 
+
+               if (strcmp(e->name, "p") == 0){
+                       continue; 
+
+               } else if (strcmp(e->name, "o") == 0){
+                       sql_exp *m_exp = exp_copy(sa, tmpexp);
+                       //append this exp to list
+                       append(retexps, m_exp);
+
+               } else if (strcmp(e->name, "s") == 0){
+                       sql_exp *m_exp = exp_copy(sa, tmpexp);
+                       //append this exp to list
+                       append(retexps, m_exp);
+               }
+       }
+}
 
 static
 void update_RP_and_O_constraint(mvc *c, jgraph *jg, int *ijgroup, int nnode, 
spProps *spprops){
@@ -3178,18 +3228,21 @@ void update_RP_and_O_constraint(mvc *c, 
                assert(((sql_rel*)tmprel->l)->op == op_basetable); 
                
                tmpPropId = tmpnode->poid; 
-
+               
                //Get index of this prop
                for (j = 0; j < spprops->num; j++){
                        if (spprops->lstPropIds[j] == tmpPropId) break; 
                }
                pidx = j; 
                spprops->lstPOs[pidx] = REQUIRED;
+
                assert(j < spprops->num); 
 
                //Check for o_constraint
 
                get_o_constraint(c, &(spprops->lst_o_constraints[pidx]), 
tmprel->exps);
+               add_spprops_exps(c, spprops->exps, tmprel->exps); 
+               (void) c; 
        }
 
 }
@@ -3246,7 +3299,7 @@ sql_rel* _group_star_pattern(mvc *c, jgr
                                        // with ifthenelse statement for 
optional keywords
                int *contain_mv_col = NULL;                     
 
-               spprops = init_sp_props(nnode);         
+               spprops = init_sp_props(c, nnode);      
 
                for (i = 0; i < nnode; i++){
                        jgnode *tmpnode = jg->lstnodes[group[i]]; 
diff --git a/sql/backends/monet5/sql_rdf_jgraph.h 
b/sql/backends/monet5/sql_rdf_jgraph.h
--- a/sql/backends/monet5/sql_rdf_jgraph.h
+++ b/sql/backends/monet5/sql_rdf_jgraph.h
@@ -68,8 +68,10 @@ typedef struct propertyList {
        oid subj;       
        char** lstProps; 
        oid* lstPropIds; 
+       char** lstAlias; //Table alias from original query
        sp_po *lstPOs; 
        ctype *lstctype;
+       list *exps;     //Expression of all 
        o_constraint *lst_o_constraints; 
 } spProps;             //star pattern property list
 
diff --git a/sql/backends/monet5/sql_scenario.c 
b/sql/backends/monet5/sql_scenario.c
--- a/sql/backends/monet5/sql_scenario.c
+++ b/sql/backends/monet5/sql_scenario.c
@@ -1128,7 +1128,6 @@ SQLparser(Client c)
 recompilequery:
                r = sql_symbol2relation(m, m->sym);
                s = sql_relation2stmt(m, r);
-
                //if (m->emode == m_sparql) printf("DUC: ---- Input query 
-----\n %s \n", sql_escape_str(QUERY(m->scanner))); 
 
                if (s == 0 || (err = mvc_status(m) && m->type != Q_TRANS)) {
diff --git a/sql/server/rel_optimizer.c b/sql/server/rel_optimizer.c
--- a/sql/server/rel_optimizer.c
+++ b/sql/server/rel_optimizer.c
@@ -1778,6 +1778,10 @@ rel_push_topn_down(int *changes, mvc *sq
                        if (!is_project(ur->op)) 
                                ur = rel_project(sql->sa, ur, 
                                        rel_projections(sql, ur, NULL, 1, 1));
+                       if (need_distinct(r)) {
+                               set_distinct(ul);
+                               set_distinct(ur);
+                       }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to