Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
Queries: Added LIKE
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Nov 11, 2022
1 parent 9b91f3a commit 27dbe7c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
3 changes: 1 addition & 2 deletions data_diff/sqeleton/databases/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
14 changes: 8 additions & 6 deletions data_diff/sqeleton/queries/ast_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -493,34 +496,33 @@ 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)

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)

# 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)


Expand Down
3 changes: 2 additions & 1 deletion data_diff/sqeleton/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# -- Common --


def join_iter(joiner: Any, iterable: Iterable) -> Iterable:
it = iter(iterable)
try:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

0 comments on commit 27dbe7c

Please sign in to comment.