From: Lele Gaifax Date: Wed, 26 Oct 2022 20:23:44 +0000 (+0200) Subject: Lazily determine the discrete step, only once X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=faf358f4090572ac6373acdb7eb53d19eb5b2e9e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Lazily determine the discrete step, only once --- diff --git a/lib/sqlalchemy/dialects/postgresql/ranges.py b/lib/sqlalchemy/dialects/postgresql/ranges.py index f5ce56c3f9..ce260cac7a 100644 --- a/lib/sqlalchemy/dialects/postgresql/ranges.py +++ b/lib/sqlalchemy/dialects/postgresql/ranges.py @@ -136,19 +136,16 @@ class Range(Generic[_T]): if other.empty: return False - slower = self.lower - slower_inc = self.bounds[0] == "[" - supper = self.upper - supper_inc = self.bounds[1] == "]" olower = other.lower - olower_inc = other.bounds[0] == "[" oupper = other.upper - oupper_inc = other.bounds[1] == "]" # A bilateral unbound range contains any other range if olower is oupper is None: return True + slower = self.lower + supper = self.upper + # A lower-bound range cannot contain a lower-unbound range if slower is None and olower is not None: return False @@ -157,19 +154,27 @@ class Range(Generic[_T]): if supper is None and oupper is not None: return False + slower_inc = self.bounds[0] == "[" + supper_inc = self.bounds[1] == "]" + olower_inc = other.bounds[0] == "[" + oupper_inc = other.bounds[1] == "]" + # Check the lower end + step = -1 if slower is not None and olower is not None: lside = olower < slower if not lside: if not slower_inc or olower_inc: lside = olower == slower if not lside: - step = self._get_discrete_step() - if step is not None: - # Cover (1,x] vs [2,x) and (0,x] vs [1,x) - if not slower_inc and olower_inc and slower < olower: + # Cover (1,x] vs [2,x) and (0,x] vs [1,x) + if not slower_inc and olower_inc and slower < olower: + step = self._get_discrete_step() + if step is not None: lside = olower == (slower + step) - elif slower_inc and not olower_inc and slower > olower: + elif slower_inc and not olower_inc and slower > olower: + step = self._get_discrete_step() + if step is not None: lside = (olower + step) == slower if not lside: return False @@ -185,12 +190,16 @@ class Range(Generic[_T]): if not supper_inc or oupper_inc: uside = oupper == supper if not uside: - step = self._get_discrete_step() - if step is not None: - # Cover (x,2] vs [x,3) and (x,1] vs [x,2) - if supper_inc and not oupper_inc and supper < oupper: + # Cover (x,2] vs [x,3) and (x,1] vs [x,2) + if supper_inc and not oupper_inc and supper < oupper: + if step == -1: + step = self._get_discrete_step() + if step is not None: uside = oupper == (supper + step) - elif not supper_inc and oupper_inc and supper > oupper: + elif not supper_inc and oupper_inc and supper > oupper: + if step == -1: + step = self._get_discrete_step() + if step is not None: uside = (oupper + step) == supper return uside