Coverage for mindsdb / integrations / utilities / handlers / query_utilities / base_query_utilities.py: 0%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import pandas as pd 

2from typing import Text, List 

3from mindsdb_sql_parser import ast 

4from abc import ABC, abstractmethod 

5from mindsdb.integrations.utilities.sql_utils import extract_comparison_conditions, filter_dataframe 

6 

7 

8class BaseQueryParser(ABC): 

9 """ 

10 Parses a SQL query into its component parts. 

11 

12 Parameters 

13 ---------- 

14 query : ast 

15 Given SQL query. 

16 """ 

17 

18 def __init__(self, query: ast): 

19 self.query = query 

20 

21 @abstractmethod 

22 def parse_query(self): 

23 """ 

24 Parses a SQL statement into its components. 

25 """ 

26 pass 

27 

28 def parse_where_clause(self) -> List[List[Text]]: 

29 """ 

30 Parses the WHERE clause of the query. 

31 """ 

32 where_conditions = extract_comparison_conditions(self.query.where) 

33 return where_conditions 

34 

35 

36class BaseQueryExecutor(): 

37 """ 

38 Executes a SQL query. 

39 

40 Parameters 

41 ---------- 

42 query : ast 

43 Given SQL query. 

44 """ 

45 

46 def __init__(self, df: pd.DataFrame, where_conditions: List[List[Text]]): 

47 self.df = df 

48 self.where_conditions = where_conditions 

49 

50 def execute_query(self): 

51 """ 

52 Executes the SQL query. 

53 """ 

54 self.execute_where_clause() 

55 

56 return self.df 

57 

58 def execute_where_clause(self): 

59 """ 

60 Executes the where clause of the query. 

61 """ 

62 if len(self.where_conditions) > 0: 

63 self.df = filter_dataframe(self.df, self.where_conditions)