From 27dbe7ce0d3cb02ccc79d2ab7353824f590b1b3a Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Fri, 11 Nov 2022 13:13:27 -0300 Subject: [PATCH] Queries: Added LIKE --- data_diff/sqeleton/databases/base.py | 3 +-- data_diff/sqeleton/queries/ast_classes.py | 14 ++++++++------ data_diff/sqeleton/utils.py | 3 ++- tests/test_query.py | 12 +++++++++++- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/data_diff/sqeleton/databases/base.py b/data_diff/sqeleton/databases/base.py index cbe63693..1d62390f 100644 --- a/data_diff/sqeleton/databases/base.py +++ b/data_diff/sqeleton/databases/base.py @@ -287,8 +287,7 @@ def enable_interactive(self): self._interactive = True def select_table_schema(self, path: DbPath) -> str: - """Provide SQL for selecting the table schema as (name, type, date_prec, num_prec) - """ + """Provide SQL for selecting the table schema as (name, type, date_prec, num_prec)""" schema, table = self._normalize_table_path(path) return ( diff --git a/data_diff/sqeleton/queries/ast_classes.py b/data_diff/sqeleton/queries/ast_classes.py index c42405eb..c10189f2 100644 --- a/data_diff/sqeleton/queries/ast_classes.py +++ b/data_diff/sqeleton/queries/ast_classes.py @@ -228,6 +228,9 @@ def __and__(self, other): def is_distinct_from(self, other): return IsDistinctFrom(self, other) + def like(self, other): + return BinBoolOp("LIKE", [self, other]) + def sum(self): return Func("SUM", [self]) @@ -493,11 +496,11 @@ def compile(self, parent_c: Compiler) -> str: @classmethod def make(cls, table: ITable, distinct: bool = SKIP, **kwargs): - assert 'table' not in kwargs + assert "table" not in kwargs if not isinstance(table, cls): # If not Select if distinct is not SKIP: - kwargs['distinct'] = distinct + kwargs["distinct"] = distinct return cls(table, **kwargs) # We can safely assume isinstance(table, Select) @@ -505,7 +508,7 @@ def make(cls, table: ITable, distinct: bool = SKIP, **kwargs): if distinct is not SKIP: if distinct == False and table.distinct: return cls(table, **kwargs) - kwargs['distinct'] = distinct + kwargs["distinct"] = distinct if table.limit_expr or table.group_by_exprs: return cls(table, **kwargs) @@ -513,14 +516,13 @@ def make(cls, table: ITable, distinct: bool = SKIP, **kwargs): # Fill in missing attributes, instead of creating a new instance. for k, v in kwargs.items(): if getattr(table, k) is not None: - if k == 'where_exprs': # Additive attribute + if k == "where_exprs": # Additive attribute kwargs[k] = getattr(table, k) + v - elif k == 'distinct': + elif k == "distinct": pass else: raise ValueError(k) - return table.replace(**kwargs) diff --git a/data_diff/sqeleton/utils.py b/data_diff/sqeleton/utils.py index 2ad02b8c..14486629 100644 --- a/data_diff/sqeleton/utils.py +++ b/data_diff/sqeleton/utils.py @@ -8,6 +8,7 @@ # -- Common -- + def join_iter(joiner: Any, iterable: Iterable) -> Iterable: it = iter(iterable) try: @@ -35,7 +36,6 @@ def is_uuid(u): return True - def match_regexps(regexps: Dict[str, Any], s: str) -> Sequence[tuple]: for regexp, v in regexps.items(): m = re.match(regexp + "$", s) @@ -47,6 +47,7 @@ def match_regexps(regexps: Dict[str, Any], s: str) -> Sequence[tuple]: V = TypeVar("V") + class CaseAwareMapping(MutableMapping[str, V]): @abstractmethod def get_key(self, key: str) -> str: diff --git a/tests/test_query.py b/tests/test_query.py index 32192b54..96a1d3c9 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -181,7 +181,7 @@ def test_select_distinct(self): assert q == "SELECT DISTINCT b FROM a" # selects merge - q = c.compile(t.where(this.b>10).select(this.b, distinct=True)) + q = c.compile(t.where(this.b > 10).select(this.b, distinct=True)) self.assertEqual(q, "SELECT DISTINCT b FROM a WHERE (b > 10)") # selects stay apart @@ -198,3 +198,13 @@ def test_union(self): q = c.compile(a.union(b)) assert q == "SELECT x FROM a UNION SELECT y FROM b" + + def test_ops(self): + c = Compiler(MockDatabase()) + t = table("a") + + q = c.compile(t.select(this.b + this.c)) + self.assertEqual(q, "SELECT (b + c) FROM a") + + q = c.compile(t.select(this.b.like(this.c))) + self.assertEqual(q, "SELECT (b LIKE c) FROM a")