Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sql_builder.AfterPrevValues. #1480

Merged
merged 4 commits into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert sql_builder.TupleCompare to use AND/ORs.
The MySQL optimizer does always find good indexes for:

   ('x', 'y') < (3, 4).

Therefore, while keeping the name TupleCompare, I am reverting
the SQL back to:

  x = 3 AND y < 4 OR x < 3.
  • Loading branch information
dumbunny committed Feb 8, 2016
commit 9b240ee8885e5d009dae70d77a1aa645dc114112
47 changes: 28 additions & 19 deletions py/vtdb/sql_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,12 @@ class TupleCompare(BaseWhereExpr):
Example: If reading values ordered by columns (x, y, z) starting after
the point (3, 5, 7), build the SQL:

"(x, y, z) > (3, 5, 7)".
"x = 3 AND (y = 5 AND z > 7 OR y > 5) OR x > 3".

NOTE: MySQL supports "(x, y, z) > (3, 5, 7)" to do the same thing,
but we have found that the optimizer frequently does not recognize
this as an indexed, range query. Surprisingly, it has a better chance
of efficiently using with the above inequality.
"""

def __init__(self, starting_point, asc=True, inclusive=False):
Expand Down Expand Up @@ -994,27 +999,31 @@ def build_where_sql(self, column_name, counter):
"""
if column_name:
raise ValueError('column_name should be None.')
where_clause = None
is_complex = False
bind_vars = {}
column_list = []
token_list = []
for column, value in self.starting_point:
column_list.append(column)
bind_var = choose_bind_name(column, counter)
update_bind_vars(bind_vars, {bind_var: value})
if isinstance(value, (list, tuple, set)):
for column, prev_value in reversed(self.starting_point):
bind_name = choose_bind_name(column, counter)
if isinstance(prev_value, (list, tuple, set)):
raise ValueError(
'Column=%s value=%s should be a single value.' %
(column, value))
token_list.append('%%(%s)s' % (bind_var,))

parts = ['(%s)' % ', '.join(column_list)]
if self.asc:
op = '>=' if self.inclusive else '>'
else:
op = '<=' if self.inclusive else '<'
parts.append(op)
parts.append('(%s)' % ', '.join(token for token in token_list))
where_clause = ' '.join(parts)
(column, prev_value))
update_bind_vars(bind_vars, {bind_name: prev_value})
eq_part = '%s = %%(%s)s' % (column, bind_name)
if where_clause is None and self.inclusive:
op = '>=' if self.asc else '<='
else:
op = '>' if self.asc else '<'
ineq_part = '%s %s %%(%s)s' % (column, op, bind_name)
if where_clause:
if is_complex:
where_clause = '%s AND (%s) OR %s' % (
eq_part, where_clause, ineq_part)
else:
where_clause = '%s AND %s OR %s' % (eq_part, where_clause, ineq_part)
is_complex = True
else:
where_clause = ineq_part
return where_clause, bind_vars


Expand Down
20 changes: 12 additions & 8 deletions test/sql_builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,23 +559,27 @@ def test_or_exprs(self):
def test_tuple_compare(self):
self._check_build_where_sql(
sql_builder.TupleGreater([('x', 3), ('y', 5), ('z', 7)]),
'(x, y, z) > (%(x_3)s, %(y_4)s, %(z_5)s)',
dict(x_3=3, y_4=5, z_5=7),
'x = %(x_5)s AND '
'(y = %(y_4)s AND z > %(z_3)s OR y > %(y_4)s) OR x > %(x_5)s',
dict(x_5=3, y_4=5, z_3=7),
column_name=None)
self._check_build_where_sql(
sql_builder.TupleGreaterEqual([('x', 3), ('y', 5), ('z', 7)]),
'(x, y, z) >= (%(x_3)s, %(y_4)s, %(z_5)s)',
dict(x_3=3, y_4=5, z_5=7),
'x = %(x_5)s AND '
'(y = %(y_4)s AND z >= %(z_3)s OR y > %(y_4)s) OR x > %(x_5)s',
dict(x_5=3, y_4=5, z_3=7),
column_name=None)
self._check_build_where_sql(
sql_builder.TupleLess([('x', 3), ('y', 5), ('z', 7)]),
'(x, y, z) < (%(x_3)s, %(y_4)s, %(z_5)s)',
dict(x_3=3, y_4=5, z_5=7),
'x = %(x_5)s AND '
'(y = %(y_4)s AND z < %(z_3)s OR y < %(y_4)s) OR x < %(x_5)s',
dict(x_5=3, y_4=5, z_3=7),
column_name=None)
self._check_build_where_sql(
sql_builder.TupleLessEqual([('x', 3), ('y', 5), ('z', 7)]),
'(x, y, z) <= (%(x_3)s, %(y_4)s, %(z_5)s)',
dict(x_3=3, y_4=5, z_5=7),
'x = %(x_5)s AND '
'(y = %(y_4)s AND z <= %(z_3)s OR y < %(y_4)s) OR x < %(x_5)s',
dict(x_5=3, y_4=5, z_3=7),
column_name=None)

def test_greater_than(self):
Expand Down