]> git.openstreetmap.org Git - nominatim.git/blobdiff - src/nominatim_api/sql/sqlalchemy_functions.py
Merge pull request #3702 from lonvia/remove-tokenizer-dir
[nominatim.git] / src / nominatim_api / sql / sqlalchemy_functions.py
index 094c33dc2995be898f98669b252474276c3a2bd9..00830f33b025db286e570792541ff3f3a3ec345e 100644 (file)
@@ -15,7 +15,6 @@ from sqlalchemy.ext.compiler import compiles
 
 from ..typing import SaColumn
 
-# pylint: disable=all
 
 class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[Any]):
     """ Check for conditions that allow partial index use on
@@ -69,8 +68,8 @@ def default_reverse_place_diameter(element: IntersectsReverseDistance,
            f" AND {table}.name is not null"\
            f" AND {table}.linked_place_id is null"\
            f" AND {table}.osm_type = 'N'" + \
-           " AND ST_Buffer(%s, reverse_place_diameter(%s)) && %s)" \
-               tuple(map(lambda c: compiler.process(c, **kw), element.clauses))
+           " AND ST_Buffer(%s, reverse_place_diameter(%s)) && %s)" \
+        % tuple(map(lambda c: compiler.process(c, **kw), element.clauses))
 
 
 @compiles(IntersectsReverseDistance, 'sqlite')
@@ -79,17 +78,17 @@ def sqlite_reverse_place_diameter(element: IntersectsReverseDistance,
     geom1, rank, geom2 = list(element.clauses)
     table = element.tablename
 
-    return (f"({table}.rank_address between 4 and 25"\
-            f" AND {table}.type != 'postcode'"\
-            f" AND {table}.name is not null"\
-            f" AND {table}.linked_place_id is null"\
-            f" AND {table}.osm_type = 'N'"\
-             " AND MbrIntersects(%s, ST_Expand(%s, 14.0 * exp(-0.2 * %s) - 0.03))"\
-            f" AND {table}.place_id IN"\
-             " (SELECT place_id FROM placex_place_node_areas"\
-             "  WHERE ROWID IN (SELECT ROWID FROM SpatialIndex"\
-             "  WHERE f_table_name = 'placex_place_node_areas'"\
-             "  AND search_frame = %s)))") % (
+    return (f"({table}.rank_address between 4 and 25"
+            f" AND {table}.type != 'postcode'"
+            f" AND {table}.name is not null"
+            f" AND {table}.linked_place_id is null"
+            f" AND {table}.osm_type = 'N'"
+            "  AND MbrIntersects(%s, ST_Expand(%s, 14.0 * exp(-0.2 * %s) - 0.03))"
+            f" AND {table}.place_id IN"
+            "  (SELECT place_id FROM placex_place_node_areas"
+            "   WHERE ROWID IN (SELECT ROWID FROM SpatialIndex"
+            "   WHERE f_table_name = 'placex_place_node_areas'"
+              AND search_frame = %s)))") % (
                 compiler.process(geom1, **kw),
                 compiler.process(geom2, **kw),
                 compiler.process(rank, **kw),
@@ -123,15 +122,18 @@ class IsAddressPoint(sa.sql.functions.GenericFunction[Any]):
 
     def __init__(self, table: sa.Table) -> None:
         super().__init__(table.c.rank_address,
-                         table.c.housenumber, table.c.name)
+                         table.c.housenumber, table.c.name, table.c.address)
 
 
 @compiles(IsAddressPoint)
 def default_is_address_point(element: IsAddressPoint,
                              compiler: 'sa.Compiled', **kw: Any) -> str:
-    rank, hnr, name = list(element.clauses)
-    return "(%s = 30 AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % (
+    rank, hnr, name, address = list(element.clauses)
+    return "(%s = 30 AND (%s IS NULL OR NOT %s ? '_inherited')" \
+           " AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % (
                 compiler.process(rank, **kw),
+                compiler.process(address, **kw),
+                compiler.process(address, **kw),
                 compiler.process(hnr, **kw),
                 compiler.process(name, **kw))
 
@@ -139,9 +141,11 @@ def default_is_address_point(element: IsAddressPoint,
 @compiles(IsAddressPoint, 'sqlite')
 def sqlite_is_address_point(element: IsAddressPoint,
                             compiler: 'sa.Compiled', **kw: Any) -> str:
-    rank, hnr, name = list(element.clauses)
-    return "(%s = 30 AND coalesce(%s, json_extract(%s, '$.addr:housename')) IS NOT NULL)" % (
+    rank, hnr, name, address = list(element.clauses)
+    return "(%s = 30 AND json_extract(%s, '$._inherited') IS NULL" \
+           " AND coalesce(%s, json_extract(%s, '$.addr:housename')) IS NOT NULL)" % (
                 compiler.process(rank, **kw),
+                compiler.process(address, **kw),
                 compiler.process(hnr, **kw),
                 compiler.process(name, **kw))
 
@@ -153,6 +157,7 @@ class CrosscheckNames(sa.sql.functions.GenericFunction[Any]):
     name = 'CrosscheckNames'
     inherit_cache = True
 
+
 @compiles(CrosscheckNames)
 def compile_crosscheck_names(element: CrosscheckNames,
                              compiler: 'sa.Compiled', **kw: Any) -> str:
@@ -188,7 +193,6 @@ def sqlite_json_array_each(element: JsonArrayEach, compiler: 'sa.Compiled', **kw
     return "json_each(%s)" % compiler.process(element.clauses, **kw)
 
 
-
 class Greatest(sa.sql.functions.GenericFunction[Any]):
     """ Function to compute maximum of all its input parameters.
     """
@@ -201,7 +205,6 @@ def sqlite_greatest(element: Greatest, compiler: 'sa.Compiled', **kw: Any) -> st
     return "max(%s)" % compiler.process(element.clauses, **kw)
 
 
-
 class RegexpWord(sa.sql.functions.GenericFunction[Any]):
     """ Check if a full word is in a given string.
     """
@@ -212,10 +215,12 @@ class RegexpWord(sa.sql.functions.GenericFunction[Any]):
 @compiles(RegexpWord, 'postgresql')
 def postgres_regexp_nocase(element: RegexpWord, compiler: 'sa.Compiled', **kw: Any) -> str:
     arg1, arg2 = list(element.clauses)
-    return "%s ~* ('\\m(' || %s  || ')\\M')::text" % (compiler.process(arg2, **kw), compiler.process(arg1, **kw))
+    return "%s ~* ('\\m(' || %s  || ')\\M')::text" \
+        % (compiler.process(arg2, **kw), compiler.process(arg1, **kw))
 
 
 @compiles(RegexpWord, 'sqlite')
 def sqlite_regexp_nocase(element: RegexpWord, compiler: 'sa.Compiled', **kw: Any) -> str:
     arg1, arg2 = list(element.clauses)
-    return "regexp('\\b(' || %s  || ')\\b', %s)" % (compiler.process(arg1, **kw), compiler.process(arg2, **kw))
+    return "regexp('\\b(' || %s  || ')\\b', %s)"\
+        % (compiler.process(arg1, **kw), compiler.process(arg2, **kw))