in data_validation/partition_builder.py [0:0]
def _extract_where(table_expr: "ibis.expr.types.Table") -> str:
"""Given a ibis table expression with a filter (i.e. WHERE) clause, this function extracts the
where clause in plain text.
Returns:
String with the where condition
"""
# This extraction of the where clause is a bit of a hack. To extract it correctly, the SQL table
# expression should be correctly parsed and the where clause extracted. Perhaps use something like Sqlglot.
sql_where_expr = re.split(
r"\sWHERE\s", util.ibis_table_to_sql(table_expr), flags=re.I
)[-1]
sql_string_re = re.compile(r"'(?:''|\\'|[^'])*'")
sql_not_string_re = re.compile(r"[^']+")
sql_where_less_ws = ""
# Remove references to t0 and extra whitespace, but only outside quoted strings.
while sql_where_expr:
if match_obj := sql_not_string_re.match(
sql_where_expr
): # Not a quoted string
repl_str = re.sub(r"\s\s+", r" ", match_obj.group(0)).replace("t0.", "")
else: # quoted strings should not be substituted
repl_str = (match_obj := sql_string_re.match(sql_where_expr)).group(0)
sql_where_less_ws += repl_str
sql_where_expr = sql_where_expr[match_obj.end() :]
return sql_where_less_ws