LCOV - code coverage report
Current view: top level - sql/server - sql_query.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 65 100.0 %
Date: 2021-10-13 02:24:04 Functions: 13 13 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "sql_query.h"
      11             : #include "rel_rel.h"
      12             : 
      13             : static stacked_query *
      14       31284 : sq_create( sql_allocator *sa, sql_rel *rel, int sql_state)
      15             : {
      16       31284 :         stacked_query *q = SA_NEW(sa, stacked_query);
      17             : 
      18       31284 :         assert(rel);
      19       31284 :         *q = (stacked_query) {
      20             :                 .rel = rel,
      21             :                 .sql_state = sql_state,
      22       31284 :                 .grouped = is_groupby(rel->op),
      23             :                 .groupby = 0, /* not used for groupby of inner */
      24             :         };
      25       31284 :         return q;
      26             : }
      27             : 
      28             : sql_query *
      29      540385 : query_create( mvc *sql)
      30             : {
      31      540385 :         sql_query *q = SA_NEW(sql->sa, sql_query);
      32             : 
      33      540385 :         q->sql = sql;
      34      540385 :         q->outer = sql_stack_new(sql->sa, 32);
      35      540385 :         return q;
      36             : }
      37             : 
      38             : void
      39       31284 : query_push_outer(sql_query *q, sql_rel *r, int sql_state)
      40             : {
      41       31284 :         stacked_query *sq = sq_create(q->sql->sa, r, sql_state);
      42       31284 :         assert(sq);
      43       31284 :         sql_stack_push(q->outer, sq);
      44       31284 : }
      45             : 
      46             : sql_rel *
      47       31284 : query_pop_outer(sql_query *q)
      48             : {
      49       31284 :         stacked_query *sq = sql_stack_pop(q->outer);
      50       31284 :         sql_rel *r = sq->rel;
      51       31284 :         return r;
      52             : }
      53             : 
      54             : sql_rel *
      55       12286 : query_fetch_outer(sql_query *q, int i)
      56             : {
      57       12286 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
      58       12286 :         if (!sq)
      59             :                 return NULL;
      60       12270 :         return sq->rel;
      61             : }
      62             : 
      63             : int
      64       11386 : query_fetch_outer_state(sql_query *q, int i)
      65             : {
      66       11386 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
      67       11386 :         if (!sq)
      68             :                 return 0;
      69       11386 :         return sq->sql_state;
      70             : }
      71             : 
      72             : void
      73         244 : query_update_outer(sql_query *q, sql_rel *r, int i)
      74             : {
      75         244 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
      76         244 :         sq->rel = r;
      77         244 :         sq->last_used = NULL;
      78         244 :         sq->used_card = 0;
      79         244 :         sq->grouped = is_groupby(r->op);
      80         244 : }
      81             : 
      82             : int
      83      223380 : query_has_outer(sql_query *q)
      84             : {
      85      223380 :         return sql_stack_top(q->outer);
      86             : }
      87             : 
      88             : int
      89       10698 : query_outer_used_exp(sql_query *q, int i, sql_exp *e, int f)
      90             : {
      91       10698 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
      92             : 
      93       10698 :         if (is_sql_aggr(f) && !is_sql_farg(f) && sq->groupby) /* cannot user outer both for inner groupby and aggregation */
      94             :                 return -1;
      95       10698 :         if (is_sql_groupby(f))
      96          17 :                 sq->groupby = 1;
      97             : 
      98       10698 :         if (is_sql_aggr(f) && !is_sql_farg(f) && sq->groupby && sq->last_used && sq->used_card > CARD_AGGR) /* used full relation */
      99             :                 return -1;
     100       10698 :         if (!is_sql_aggr(f) && sq->grouped && e->card > CARD_AGGR)
     101             :                 return -1;
     102             : 
     103       10698 :         sq->last_used = e;
     104       10698 :         sq->used_card = sq->rel->card;
     105       10698 :         assert( (!is_sql_aggr(f) && sq->grouped == 0 && e->card != CARD_AGGR) || /* outer is a none grouped relation */
     106             :                 (!is_sql_aggr(f) && sq->grouped == 1 && e->card <= CARD_AGGR) || /* outer is groupbed, ie only return aggregations or groupby cols */
     107             :                 (is_sql_aggr(f) && !is_sql_farg(f) && !sq->grouped && e->card != CARD_AGGR) || /* a column/constant to be aggregated */
     108             :                 (is_sql_aggr(f) && !is_sql_farg(f) && sq->grouped && e->card != CARD_AGGR) || /* a column/constant to be aggregated */
     109             :                 (is_sql_aggr(f) && is_sql_farg(f) && sq->grouped && e->card <= CARD_AGGR) ||  /* groupby ( function (group by col)) */
     110             :                 (is_sql_aggr(f) && sq->grouped && e->card <= CARD_AGGR) ); /* nested aggregations is handled later */
     111             :         return 0;
     112             : }
     113             : 
     114             : void
     115           2 : query_outer_pop_last_used(sql_query *q, int i)
     116             : {
     117           2 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
     118             : 
     119           2 :         sq->last_used = NULL;
     120           2 :         sq->used_card = 0;
     121           2 :         sq->sql_state = 0;
     122           2 : }
     123             : 
     124             : int
     125         244 : query_outer_aggregated(sql_query *q, int i, sql_exp *e)
     126             : {
     127         244 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
     128             : 
     129         244 :         assert(sq->grouped);
     130         244 :         sq->last_used = e;
     131         244 :         sq->used_card = sq->rel->card;
     132         244 :         return 0;
     133             : }
     134             : 
     135             : int
     136         275 : query_outer_used_card(sql_query *q, int i)
     137             : {
     138         275 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
     139             : 
     140         275 :         return sq->used_card;
     141             : }
     142             : 
     143             : sql_exp *
     144           4 : query_outer_last_used(sql_query *q, int i)
     145             : {
     146           4 :         stacked_query *sq = sql_stack_fetch(q->outer, i);
     147             : 
     148           4 :         return sq->last_used;
     149             : }

Generated by: LCOV version 1.14