Example #1
0
 def test_bypass(self):
     ''' Verifica se retorna None quando a agregação está na
         lista de ignore '''
     vlr = 'vl_indicador'
     agr = 'DISTINCT'
     result = QueryBuilder.get_simple_agr_string(agr, vlr)
     self.assertEqual(result, None)
Example #2
0
    def build_std_calcs(self, options):
        """Constrói campos calculados de valor, como min, max e normalizado """
        val_field = getattr(self, 'VAL_FIELD', None)
        if val_field is None or self.get_default_partitioning(options) is None:
            return ''

        # Pega o valor passado ou padrão, para montar a query
        if QueryBuilder.check_params(options, ['valor']):
            val_field = options['valor']

        # Pega o valor do particionamento
        res_partition = None
        if QueryBuilder.check_params(options, ['partition']):
            res_partition = options.get('partition')
        elif self.get_default_partitioning(options) != '':
            res_partition = self.get_default_partitioning(options)

        # Transforma o campo de valor em campo agregado conforme query
        if QueryBuilder.check_params(options, ['agregacao']):
            val_field = QueryBuilder.get_simple_agr_string(
                options['agregacao'][0], options['valor'][0])
            if QueryBuilder.check_params(options, ['pivot']):
                res_partition = self.exclude_from_partition(
                    options['categorias'], options['agregacao'])
        elif isinstance(val_field, list):
            val_field = val_field[0]

        str_res_partition = res_partition
        if res_partition is not None and isinstance(res_partition, list):
            str_res_partition = ",".join(res_partition)

        # Constrói a query
        arr_calcs = []
        for calc in options['calcs']:
            # Always appends min and max when calc is not one of them
            if calc not in ['min_part', 'max_part']:
                pattern = self.replace_partition('min_part', options)
                if str_res_partition is None:
                    pattern = pattern.replace('PARTITION BY {partition}', '')
                arr_calcs.append(
                    pattern.format(val_field=val_field,
                                   partition=str_res_partition,
                                   calc='min_part'))

                pattern = self.replace_partition('max_part', options)
                if str_res_partition is None:
                    pattern = pattern.replace('PARTITION BY {partition}', '')
                arr_calcs.append(
                    pattern.format(val_field=val_field,
                                   partition=str_res_partition,
                                   calc='max_part'))
            # Resumes identification of calc
            pattern = self.replace_partition(calc, options)
            if str_res_partition is None:
                pattern = pattern.replace('PARTITION BY {partition}', '')
            arr_calcs.append(
                pattern.format(val_field=val_field,
                               partition=str_res_partition,
                               calc=calc))
        return ', '.join(arr_calcs)
Example #3
0
 def test_as_is(self):
     ''' Verifica se retorna corretamente uma agregação que
         está na lista as_is '''
     vlr = 'vl_indicador'
     agr = 'sum'
     expected = 'sum(vl_indicador)'
     result = QueryBuilder.get_simple_agr_string(agr, vlr)
     self.assertEqual(result, expected)
Example #4
0
    def build_std_calcs(self, options):
        '''Constrói campos calculados de valor, como min, max e normalizado '''
        if self.VAL_FIELD is None or self.get_default_partitioning(options) is None:
            return ''

        # Pega o valor passado ou padrão, para montar a query
        val_field = self.VAL_FIELD
        if QueryBuilder.check_params(options, ['valor']):
            val_field = options['valor']

        # Pega o valor do particionamento
        if not QueryBuilder.check_params(options, ['partition']):
            if self.get_default_partitioning(options) != '':
                res_partition = self.get_default_partitioning(options)
            else:
                res_partition = "'1'"
        else:
            res_partition = options['partition']

        # Transforma o campo de valor em campo agregado conforme query
        if QueryBuilder.check_params(options, ['agregacao']):
            val_field = QueryBuilder.get_simple_agr_string(
                options['agregacao'][0],
                options['valor'][0]
            )
            if QueryBuilder.check_params(options, ['pivot']):
                res_partition = self.exclude_from_partition(
                    options['categorias'],
                    options['agregacao']
                )

        str_res_partition = res_partition
        if isinstance(res_partition, list):
            str_res_partition = ",".join(res_partition)

        # Constrói a query
        arr_calcs = []
        for calc in options['calcs']:
            # Always appends min and max when calc is not one of them
            if calc not in ['min_part', 'max_part']:
                arr_calcs.append(
                    self.replace_partition('min_part').format(
                        val_field=val_field,
                        partition=str_res_partition,
                        calc='min_part'
                    )
                )
                arr_calcs.append(
                    self.replace_partition('max_part').format(
                        val_field=val_field,
                        partition=str_res_partition,
                        calc='max_part'
                    )
                )
            # Resumes identification of calc
            arr_calcs.append(
                self.replace_partition(calc, options).format(
                    val_field=val_field,
                    partition=str_res_partition,
                    calc=calc
                )
            )
        return ', '.join(arr_calcs)