LCOV - code coverage report
Current view: top level - sql/server - rel_partition.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 99 116 85.3 %
Date: 2024-11-15 19:37:45 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "sql_query.h"
      15             : #include "rel_partition.h"
      16             : #include "rel_exp.h"
      17             : #include "rel_prop.h"
      18             : #include "rel_dump.h"
      19             : #include "rel_select.h"
      20             : 
      21             : static lng
      22       80938 : rel_getcount(mvc *sql, sql_rel *rel)
      23             : {
      24       80938 :         if (!sql->session->tr)
      25             :                 return 0;
      26             : 
      27       80938 :         switch(rel->op) {
      28       80938 :         case op_basetable: {
      29       80938 :                 sql_table *t = rel->l;
      30             : 
      31       80938 :                 if (t && isTable(t)) {
      32       80938 :                         sqlstore *store = sql->session->tr->store;
      33       80938 :                         return (lng)store->storage_api.count_col(sql->session->tr, ol_first_node(t->columns)->data, 0);
      34             :                 }
      35             :                 return 0;
      36             :         }
      37             :         default:
      38           0 :                 assert(0);
      39             :                 return 0;
      40             :         }
      41             : }
      42             : 
      43             : static void
      44       84300 : find_basetables(mvc *sql, sql_rel *rel, list *tables )
      45             : {
      46      206184 :         if (mvc_highwater(sql)) {
      47           0 :                 (void) sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
      48           0 :                 return;
      49             :         }
      50             : 
      51      206184 :         if (!rel)
      52             :                 return;
      53      206184 :         switch (rel->op) {
      54       81013 :         case op_basetable: {
      55       81013 :                 sql_table *t = rel->l;
      56             : 
      57       81013 :                 if (t && isTable(t))
      58       80938 :                         append(tables, rel);
      59             :                 break;
      60             :         }
      61         192 :         case op_table:
      62         192 :                 if (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION)
      63         192 :                         if (rel->l)
      64             :                                 find_basetables(sql, rel->l, tables);
      65             :                 break;
      66       63377 :         case op_join:
      67             :         case op_left:
      68             :         case op_right:
      69             :         case op_full:
      70             :         case op_union:
      71             :         case op_inter:
      72             :         case op_except:
      73             :         case op_insert:
      74             :         case op_update:
      75             :         case op_delete:
      76             :         case op_merge:
      77       63377 :                 if (rel->l)
      78       63377 :                         find_basetables(sql, rel->l, tables);
      79       63377 :                 if (rel->r)
      80             :                         find_basetables(sql, rel->r, tables);
      81             :                 break;
      82         693 :         case op_munion:
      83         693 :                 assert(rel->l);
      84        2601 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next)
      85        1908 :                         find_basetables(sql, n->data, tables);
      86             :                 break;
      87       60909 :         case op_semi:
      88             :         case op_anti:
      89             :         case op_groupby:
      90             :         case op_project:
      91             :         case op_select:
      92             :         case op_topn:
      93             :         case op_sample:
      94             :         case op_truncate:
      95       60909 :                 if (rel->l)
      96             :                         find_basetables(sql, rel->l, tables);
      97             :                 break;
      98           0 :         case op_ddl:
      99           0 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq/* || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view*/) {
     100           0 :                         if (rel->l)
     101             :                                 find_basetables(sql, rel->l, tables);
     102           0 :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     103           0 :                         if (rel->l)
     104           0 :                                 find_basetables(sql, rel->l, tables);
     105           0 :                         if (rel->r)
     106             :                                 find_basetables(sql, rel->r, tables);
     107             :                 }
     108             :                 break;
     109             :         }
     110             : }
     111             : 
     112             : static sql_rel *
     113       19015 : _rel_partition(mvc *sql, sql_rel *rel)
     114             : {
     115       19015 :         list *tables = sa_list(sql->sa);
     116             :         /* find basetable relations */
     117             :         /* mark one (largest) with REL_PARTITION */
     118       19015 :         find_basetables(sql, rel, tables);
     119       19015 :         if (list_length(tables)) {
     120       18735 :                 sql_rel *r;
     121       18735 :                 node *n;
     122       18735 :                 int i, mi = 0;
     123       18735 :                 lng *sizes = SA_NEW_ARRAY(sql->sa, lng, list_length(tables)), m = 0;
     124             : 
     125       99673 :                 for(i=0, n = tables->h; n; i++, n = n->next) {
     126       80938 :                         r = n->data;
     127       80938 :                         sizes[i] = rel_getcount(sql, r);
     128       80938 :                         if (sizes[i] > m) {
     129       23988 :                                 m = sizes[i];
     130       23988 :                                 mi = i;
     131             :                         }
     132             :                 }
     133       29823 :                 for(i=0, n = tables->h; i<mi; i++, n = n->next)
     134             :                         ;
     135       18735 :                 r = n->data;
     136             :                 /*  TODO, we now pick first (okay?)! In case of self joins we need to pick the correct table */
     137       18735 :                 r->flag = REL_PARTITION;
     138             :         }
     139       19015 :         return rel;
     140             : }
     141             : 
     142             : static int
     143      155056 : has_groupby(sql_rel *rel)
     144             : {
     145      236363 :         if (!rel)
     146             :                 return 0;
     147             : 
     148      233221 :         switch (rel->op) {
     149             :                 case op_groupby:
     150             :                         return 1;
     151       51102 :                 case op_join:
     152             :                 case op_left:
     153             :                 case op_right:
     154             :                 case op_full:
     155             : 
     156             :                 case op_semi:
     157             :                 case op_anti:
     158             : 
     159             :                 case op_union:
     160             :                 case op_inter:
     161             :                 case op_except:
     162             : 
     163             :                 case op_merge:
     164       51102 :                         return has_groupby(rel->l) || has_groupby(rel->r);
     165        1278 :                 case op_munion:
     166        3317 :                         for (node *n = ((list*)rel->l)->h; n; n = n->next)
     167        2584 :                                 if (has_groupby(n->data))
     168             :                                         return 1;
     169             :                         return 0;
     170       81060 :                 case op_project:
     171             :                 case op_select:
     172             :                 case op_topn:
     173             :                 case op_sample:
     174       81060 :                         return has_groupby(rel->l);
     175           0 :                 case op_insert:
     176             :                 case op_update:
     177             :                 case op_delete:
     178             :                 case op_truncate:
     179           0 :                         return has_groupby(rel->r);
     180           0 :                 case op_ddl:
     181           0 :                         if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view)
     182           0 :                                 return has_groupby(rel->l);
     183             :                         if (rel->flag == ddl_list || rel->flag == ddl_exception)
     184           0 :                                 return has_groupby(rel->l) || has_groupby(rel->r);
     185             :                         return 0;
     186         247 :                 case op_table:
     187         247 :                         if (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION)
     188         247 :                                 return has_groupby(rel->l);
     189             :                         return 0;
     190             :                 case op_basetable:
     191             :                         return 0;
     192             :         }
     193             :         return 0;
     194             : }
     195             : 
     196             : sql_rel *
     197      895536 : rel_partition(mvc *sql, sql_rel *rel)
     198             : {
     199      895536 :         if (mvc_highwater(sql))
     200         108 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
     201             : 
     202      895610 :         switch (rel->op) {
     203       90031 :         case op_basetable:
     204             :         case op_sample:
     205       90031 :                 rel->flag = REL_PARTITION;
     206       90031 :                 break;
     207      364057 :         case op_project:
     208             :         case op_select:
     209             :         case op_groupby:
     210             :         case op_topn:
     211      364057 :                 if (rel->l)
     212      241096 :                         rel_partition(sql, rel->l);
     213             :                 break;
     214        5106 :         case op_semi:
     215             :         case op_anti:
     216             : 
     217             :         case op_union:
     218             :         case op_inter:
     219             :         case op_except:
     220             : 
     221             :         case op_merge:
     222        5106 :                 if (rel->l)
     223        5106 :                         rel_partition(sql, rel->l);
     224        5106 :                 if (rel->r)
     225        5106 :                         rel_partition(sql, rel->r);
     226             :                 break;
     227        3895 :         case op_munion:
     228       13969 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next)
     229       10074 :                         rel_partition(sql, n->data);
     230             :                 break;
     231      108213 :         case op_insert:
     232             :         case op_update:
     233             :         case op_delete:
     234             :         case op_truncate:
     235      108213 :                 if (rel->r && rel->card <= CARD_AGGR)
     236       60762 :                         rel_partition(sql, rel->r);
     237             :                 break;
     238       28065 :         case op_join:
     239             :         case op_left:
     240             :         case op_right:
     241             :         case op_full:
     242       28065 :                 if (has_groupby(rel->l) || has_groupby(rel->r)) {
     243        9050 :                         if (rel->l)
     244        9050 :                                 rel_partition(sql, rel->l);
     245        9050 :                         if (rel->r)
     246        9050 :                                 rel_partition(sql, rel->r);
     247             :                 } else {
     248       19015 :                         _rel_partition(sql, rel);
     249             :                 }
     250             :                 break;
     251      295224 :         case op_ddl:
     252      295224 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     253       32731 :                         if (rel->l)
     254       32371 :                                 rel_partition(sql, rel->l);
     255             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     256         722 :                         if (rel->l)
     257         565 :                                 rel_partition(sql, rel->l);
     258         722 :                         if (rel->r)
     259         688 :                                 rel_partition(sql, rel->r);
     260             :                 }
     261             :                 break;
     262        1019 :         case op_table:
     263        1019 :                 if ((IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION) && rel->l)
     264         283 :                         rel_partition(sql, rel->l);
     265             :                 break;
     266             :         default:
     267           0 :                 assert(0);
     268             :                 break;
     269             :         }
     270             :         return rel;
     271             : }

Generated by: LCOV version 1.14