Skip to content

Commit

Permalink
blend equality and existence optimizations into one
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkramaric committed May 9, 2024
1 parent 6c62af3 commit 95cfdf0
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions daffodil/hstore_predicate.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,34 @@ cdef class HStoreQueryDelegate(BaseDaffodilDelegate):

def optimize_equality_and(self, children, sql_expr):
to_optimize = [child_exp for child_exp in children if child_exp.daff_test == "="]

keys = [child_exp.daff_key for child_exp in to_optimize]
unique_keys = set(keys)

if len(unique_keys) < 2 or len(keys) != len(unique_keys):
return sql_expr

keys_and_values = ", ".join(
f'"{child_exp.daff_key}"=>"{child_exp.daff_val}"'
for child_exp in to_optimize
)
optimization_expr = f"{self.field} @> '{keys_and_values}'"
sql_optimized = f"{self.field} @> '{keys_and_values}'"

sql_expr = f" AND ".join(
f"({child_exp})"
for child_exp in children if child_exp.daff_test != "="
remaining_children = [child_exp for child_exp in children if child_exp.daff_test != "="]
if not len(remaining_children):
# every single expression is an equality test, nothing more to do
return sql_optimized

sql_remaining = f" AND ".join(
f"({child_exp})" for child_exp in remaining_children
)
if sql_expr:
return f"{optimization_expr} AND ({sql_expr})"
else:
return optimization_expr

# Apply existence optimizer if applicable
if not breaks_existence_optimizer(remaining_children):
sql_remaining = self.optimize_existence(remaining_children, sql_remaining, "?&", "AND")

return f"{sql_optimized} AND ({sql_remaining})"



def mk_all(self, children):
if not children or not any(children):
Expand All @@ -141,12 +149,12 @@ cdef class HStoreQueryDelegate(BaseDaffodilDelegate):

sql_expr = " AND ".join(f"({child_exp})" for child_exp in children if child_exp)

if not breaks_existence_optimizer(children):
return self.optimize_existence(children, sql_expr, "?&", "AND")

if not breaks_equality_optimizer(children):
return self.optimize_equality_and(children, sql_expr)

if not breaks_existence_optimizer(children):
return self.optimize_existence(children, sql_expr, "?&", "AND")

return sql_expr

def mk_not_any(self, children):
Expand Down

0 comments on commit 95cfdf0

Please sign in to comment.