]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Lazily determine the discrete step, only once
authorLele Gaifax <lele@metapensiero.it>
Wed, 26 Oct 2022 20:23:44 +0000 (22:23 +0200)
committerLele Gaifax <lele@metapensiero.it>
Wed, 26 Oct 2022 20:23:44 +0000 (22:23 +0200)
lib/sqlalchemy/dialects/postgresql/ranges.py

index f5ce56c3f9941c8f6f4370a3b6eecb9244058a02..ce260cac7ae0453d4218a9cda2aada96112e7308 100644 (file)
@@ -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