prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Bug in the filter_thread_work algorithm for Postgres backend
  Motivation
  ----------
  The partitioning algorithm sometimes fails with `Details: (psycopg2.errors.NumericValueOutOfRange) integer out of range` for Postgres backend, e.g. : 
  ```
  rucio=> select abs(('x'||md5(requests.id::text))::bit(64)::int) from requests limit 1;
  ERROR:  integer out of range
  rucio=> select abs(('x'||md5(requests.id::text))::bit(64)::bigint) from requests limit 1;
           abs         
  ---------------------
   9096650785057471382
  (1 row)
  ```
  
  
  
  
  
  </issue>
  <code>
  [start of lib/rucio/db/sqla/__init__.py]
  1 # Copyright European Organization for Nuclear Research (CERN)
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # You may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6 
  7 from sqlalchemy.sql.expression import bindparam, text
  8 
  9 
  10 def filter_thread_work(session, query, total_threads, thread_id, hash_variable=None):
  11     """ Filters a query to partition thread workloads based on the thread id and total number of threads """
  12     if thread_id is not None and total_threads is not None and (total_threads - 1) > 0:
  13         if session.bind.dialect.name == 'oracle':
  14             bindparams = [bindparam('thread_id', thread_id), bindparam('total_threads', total_threads - 1)]
  15             if not hash_variable:
  16                 query = query.filter(text('ORA_HASH(id, :total_threads) = :thread_id').bindparams(*bindparams))
  17             else:
  18                 query = query.filter(text('ORA_HASH(%s, :total_threads) = :thread_id' % (hash_variable)).bindparams(*bindparams))
  19         elif session.bind.dialect.name == 'mysql':
  20             if not hash_variable:
  21                 query = query.filter(text('mod(md5(id), %s) = %s' % (total_threads, thread_id)))
  22             else:
  23                 query = query.filter(text('mod(md5(%s), %s) = %s' % (hash_variable, total_threads, thread_id)))
  24         elif session.bind.dialect.name == 'postgresql':
  25             if not hash_variable:
  26                 query = query.filter(text('mod(abs((\'x\'||md5(id::text))::bit(32)::int), %s) = %s' % (total_threads, thread_id)))
  27             else:
  28                 query = query.filter(text('mod(abs((\'x\'||md5(%s::text))::bit(32)::int), %s) = %s' % (hash_variable, total_threads, thread_id)))
  29     return query
  30
  [end of lib/rucio/db/sqla/__init__.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/lib/rucio/db/sqla/__init__.py b/lib/rucio/db/sqla/__init__.py
  --- a/lib/rucio/db/sqla/__init__.py
  +++ b/lib/rucio/db/sqla/__init__.py
  @@ -1,8 +1,24 @@
  -# Copyright European Organization for Nuclear Research (CERN)
  +# -*- coding: utf-8 -*-
  +# Copyright 2013-2022 CERN
   #
   # Licensed under the Apache License, Version 2.0 (the "License");
  -# You may not use this file except in compliance with the License.
  -# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  +# you may not use this file except in compliance with the License.
  +# You may obtain a copy of the License at
  +#
  +#    http://www.apache.org/licenses/LICENSE-2.0
  +#
  +# Unless required by applicable law or agreed to in writing, software
  +# distributed under the License is distributed on an "AS IS" BASIS,
  +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  +# See the License for the specific language governing permissions and
  +# limitations under the License.
  +#
  +# Authors:
  +# - Vincent Garonne <vincent.garonne@cern.ch>, 2013-2015
  +# - Brandon White <bjwhite@fnal.gov>, 2019
  +# - Martin Barisits <martin.barisits@cern.ch>, 2020
  +# - Radu Carpa <radu.carpa@cern.ch>, 2021
  +# - Cedric Serfon <cedric.serfon@cern.ch>, 2022
   
   from sqlalchemy.sql.expression import bindparam, text
   
  @@ -23,7 +39,7 @@
                   query = query.filter(text('mod(md5(%s), %s) = %s' % (hash_variable, total_threads, thread_id)))
           elif session.bind.dialect.name == 'postgresql':
               if not hash_variable:
  -                query = query.filter(text('mod(abs((\'x\'||md5(id::text))::bit(32)::int), %s) = %s' % (total_threads, thread_id)))
  +                query = query.filter(text('mod(abs((\'x\'||md5(id::text))::bit(32)::bigint), %s) = %s' % (total_threads, thread_id)))
               else:
  -                query = query.filter(text('mod(abs((\'x\'||md5(%s::text))::bit(32)::int), %s) = %s' % (hash_variable, total_threads, thread_id)))
  +                query = query.filter(text('mod(abs((\'x\'||md5(%s::text))::bit(32)::bigint), %s) = %s' % (hash_variable, total_threads, thread_id)))
       return query
  
verification_info: {"golden_diff": "diff --git a/lib/rucio/db/sqla/__init__.py b/lib/rucio/db/sqla/__init__.py\n--- a/lib/rucio/db/sqla/__init__.py\n+++ b/lib/rucio/db/sqla/__init__.py\n@@ -1,8 +1,24 @@\n-# Copyright European Organization for Nuclear Research (CERN)\n+# -*- coding: utf-8 -*-\n+# Copyright 2013-2022 CERN\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n-# You may not use this file except in compliance with the License.\n-# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+#    http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+#\n+# Authors:\n+# - Vincent Garonne <vincent.garonne@cern.ch>, 2013-2015\n+# - Brandon White <bjwhite@fnal.gov>, 2019\n+# - Martin Barisits <martin.barisits@cern.ch>, 2020\n+# - Radu Carpa <radu.carpa@cern.ch>, 2021\n+# - Cedric Serfon <cedric.serfon@cern.ch>, 2022\n \n from sqlalchemy.sql.expression import bindparam, text\n \n@@ -23,7 +39,7 @@\n                 query = query.filter(text('mod(md5(%s), %s) = %s' % (hash_variable, total_threads, thread_id)))\n         elif session.bind.dialect.name == 'postgresql':\n             if not hash_variable:\n-                query = query.filter(text('mod(abs((\\'x\\'||md5(id::text))::bit(32)::int), %s) = %s' % (total_threads, thread_id)))\n+                query = query.filter(text('mod(abs((\\'x\\'||md5(id::text))::bit(32)::bigint), %s) = %s' % (total_threads, thread_id)))\n             else:\n-                query = query.filter(text('mod(abs((\\'x\\'||md5(%s::text))::bit(32)::int), %s) = %s' % (hash_variable, total_threads, thread_id)))\n+                query = query.filter(text('mod(abs((\\'x\\'||md5(%s::text))::bit(32)::bigint), %s) = %s' % (hash_variable, total_threads, thread_id)))\n     return query\n", "issue": "Bug in the filter_thread_work algorithm for Postgres backend\nMotivation\r\n----------\r\nThe partitioning algorithm sometimes fails with `Details: (psycopg2.errors.NumericValueOutOfRange) integer out of range` for Postgres backend, e.g. : \r\n```\r\nrucio=> select abs(('x'||md5(requests.id::text))::bit(64)::int) from requests limit 1;\r\nERROR:  integer out of range\r\nrucio=> select abs(('x'||md5(requests.id::text))::bit(64)::bigint) from requests limit 1;\r\n         abs         \r\n---------------------\r\n 9096650785057471382\r\n(1 row)\r\n```\r\n\r\n\r\n\r\n\n", "before_files": [{"content": "# Copyright European Organization for Nuclear Research (CERN)\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# You may not use this file except in compliance with the License.\n# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n\nfrom sqlalchemy.sql.expression import bindparam, text\n\n\ndef filter_thread_work(session, query, total_threads, thread_id, hash_variable=None):\n    \"\"\" Filters a query to partition thread workloads based on the thread id and total number of threads \"\"\"\n    if thread_id is not None and total_threads is not None and (total_threads - 1) > 0:\n        if session.bind.dialect.name == 'oracle':\n            bindparams = [bindparam('thread_id', thread_id), bindparam('total_threads', total_threads - 1)]\n            if not hash_variable:\n                query = query.filter(text('ORA_HASH(id, :total_threads) = :thread_id').bindparams(*bindparams))\n            else:\n                query = query.filter(text('ORA_HASH(%s, :total_threads) = :thread_id' % (hash_variable)).bindparams(*bindparams))\n        elif session.bind.dialect.name == 'mysql':\n            if not hash_variable:\n                query = query.filter(text('mod(md5(id), %s) = %s' % (total_threads, thread_id)))\n            else:\n                query = query.filter(text('mod(md5(%s), %s) = %s' % (hash_variable, total_threads, thread_id)))\n        elif session.bind.dialect.name == 'postgresql':\n            if not hash_variable:\n                query = query.filter(text('mod(abs((\\'x\\'||md5(id::text))::bit(32)::int), %s) = %s' % (total_threads, thread_id)))\n            else:\n                query = query.filter(text('mod(abs((\\'x\\'||md5(%s::text))::bit(32)::int), %s) = %s' % (hash_variable, total_threads, thread_id)))\n    return query\n", "path": "lib/rucio/db/sqla/__init__.py"}]}