• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Morry98 / sql-query-parser / 8096033456

25 Apr 2022 04:52PM UTC coverage: 54.854%. Remained the same
8096033456

push

github

Morry98
code-checks add workflow_dispatch

(cherry picked from commit 54d394370)

469 of 855 relevant lines covered (54.85%)

0.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sql_query_parser/functions/count.py
1
from typing import Tuple, Optional
×
2
import re
×
3

4
from sql_query_parser.table import Table
×
5
from sql_query_parser.configurations import Configurations
×
6

7
REGEX_PATTERN = re.compile(r"count\((.*)\)")
×
8

9

10
def compute(
×
11
        word: str,
12
        config: Configurations
13
) -> Tuple[bool, Configurations]:
14
    if len(config.keywords) > 0 and config.keywords[-1] == "function_count":
×
15
        if "as" in word:
×
16
            config.add_keyword("as")
×
17
            return True, config
×
18
        if "from" in word:  # TODO Can be found a better solution
×
19
            config.pop_last_keyword()
×
20
            config.pop_last_parsing_value()
×
21
            return False, config
×
22
    elif len(config.keywords) > 1 and config.keywords[-2] == "function_count":
×
23
        if config.keywords[-1] == "as":
×
24
            if len(config.parsing_value) != 1:
×
25
                raise Exception(f"Function count support only one column, {len(config.parsing_value)} found")
×
26
            add_to_query(alias=word, config=config)
×
27
            config.pop_last_keyword()
×
28
            config.pop_last_keyword()
×
29
            return True, config
×
30
        raise Exception(f'{config.keywords[-1]} keyword not supported in count function')
×
31
    else:
32
        if "count" in word:
×
33
            matches = REGEX_PATTERN.findall(word)
×
34
            if len(matches) == 1:
×
35
                config.add_keyword("function_count")
×
36
                config.add_parsing_value(parsing_value=(matches[0], None))
×
37
                add_to_query(config=config)
×
38
                if "," in word:
×
39
                    config.pop_last_keyword()
×
40
                    config.pop_last_parsing_value()
×
41
            return True, config
×
42
    return False, config
×
43

44

45
def add_to_query(
×
46
        config: Configurations,
47
        alias: Optional[str] = None
48
) -> None:
49
    parsing_column = config.pop_last_parsing_value()
×
50
    column = parsing_column[0]
×
51
    if alias is None:
×
52
        config.add_parsing_value(parsing_value=parsing_column)
×
53
        alias = parsing_column[1]
×
54
    elif parsing_column[1] is not None:
×
55
        raise Exception(f"Double alias for {column} column: {parsing_column[1]} and {alias}")
×
56
    split_parsing_column = column.split(".")
×
57
    table_str: str = "" if len(split_parsing_column) <= 1 else split_parsing_column[0]
×
58
    table = config.query.get_table_by_name_or_alias(table_str)
×
59
    if table is None:
×
60
        table = Table(alias=table_str)
×
61
        config.query.add_table(table)
×
62
    alias = alias.replace(",", "") if alias is not None else None
×
63
    table.add_function(function="count(" + column + ")", alias=alias)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc