Coverage for dj/sql/parse.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-17 20:05 -0700

1""" 

2SQL parsing functions. 

3""" 

4 

5from typing import Optional 

6 

7 

8def is_metric(query: Optional[str]) -> bool: 

9 """ 

10 Return if a SQL query defines a metric. 

11 

12 The SQL query should have a single expression in its projections, and it should 

13 be an aggregation function in order for it to be considered a metric. 

14 """ 

15 

16 from dj.sql.parsing.backends.antlr4 import parse # pylint: disable=C0415 

17 

18 if query is None: 

19 return False 

20 

21 tree = parse(query) 

22 

23 # must have a single expression 

24 if len(tree.select.projection) != 1: 

25 return False 

26 

27 return tree.select.projection[0].is_aggregation() # type: ignore