]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/db/sqlalchemy_functions.py
Added missing return types to functions
[nominatim.git] / nominatim / db / sqlalchemy_functions.py
index 6a5809bdd3de3df069ef58314b22adc52f04117a..cb04f7626f08b97f2ee602900849e132f65f6272 100644 (file)
@@ -17,14 +17,13 @@ from nominatim.typing import SaColumn
 
 # pylint: disable=all
 
-class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[bool]):
+class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[Any]):
     """ Check for conditions that allow partial index use on
         'idx_placex_geometry_reverse_lookupPolygon'.
 
         Needs to be constant, so that the query planner picks them up correctly
         in prepared statements.
     """
-    type = sa.Boolean()
     name = 'PlacexGeometryReverseLookuppolygon'
     inherit_cache = True
 
@@ -51,8 +50,7 @@ def _sqlite_intersects(element: SaColumn,
             " AND placex.linked_place_id is null)")
 
 
-class IntersectsReverseDistance(sa.sql.functions.GenericFunction[bool]):
-    type = sa.Boolean()
+class IntersectsReverseDistance(sa.sql.functions.GenericFunction[Any]):
     name = 'IntersectsReverseDistance'
     inherit_cache = True
 
@@ -66,12 +64,12 @@ class IntersectsReverseDistance(sa.sql.functions.GenericFunction[bool]):
 def default_reverse_place_diameter(element: SaColumn,
                                    compiler: 'sa.Compiled', **kw: Any) -> str:
     table = element.tablename
-    return f"{table}.rank_address between 4 and 25"\
+    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 ST_Buffer(%s, reverse_place_diameter(%s)) && %s" % \
+           " AND ST_Buffer(%s, reverse_place_diameter(%s)) && %s)" % \
                tuple(map(lambda c: compiler.process(c, **kw), element.clauses))
 
 
@@ -81,7 +79,7 @@ def sqlite_reverse_place_diameter(element: SaColumn,
     geom1, rank, geom2 = list(element.clauses)
     table = element.tablename
 
-    return (f"{table}.rank_address between 4 and 25"\
+    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"\
@@ -91,15 +89,14 @@ def sqlite_reverse_place_diameter(element: SaColumn,
              " (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))") % (
+             "  AND search_frame = %s)))") % (
                 compiler.process(geom1, **kw),
                 compiler.process(geom2, **kw),
                 compiler.process(rank, **kw),
                 compiler.process(geom2, **kw))
 
 
-class IsBelowReverseDistance(sa.sql.functions.GenericFunction[bool]):
-    type = sa.Boolean()
+class IsBelowReverseDistance(sa.sql.functions.GenericFunction[Any]):
     name = 'IsBelowReverseDistance'
     inherit_cache = True
 
@@ -132,11 +129,39 @@ def select_index_placex_geometry_reverse_lookupplacenode(table: str) -> 'sa.Text
                    f" AND {table}.osm_type = 'N'")
 
 
-class CrosscheckNames(sa.sql.functions.GenericFunction[bool]):
+class IsAddressPoint(sa.sql.functions.GenericFunction[Any]):
+    name = 'IsAddressPoint'
+    inherit_cache = True
+
+    def __init__(self, table: sa.Table) -> None:
+        super().__init__(table.c.rank_address, # type: ignore[no-untyped-call]
+                         table.c.housenumber, table.c.name)
+
+
+@compiles(IsAddressPoint) # type: ignore[no-untyped-call, misc]
+def default_is_address_point(element: SaColumn,
+                             compiler: 'sa.Compiled', **kw: Any) -> str:
+    rank, hnr, name = list(element.clauses)
+    return "(%s = 30 AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % (
+                compiler.process(rank, **kw),
+                compiler.process(hnr, **kw),
+                compiler.process(name, **kw))
+
+
+@compiles(IsAddressPoint, 'sqlite') # type: ignore[no-untyped-call, misc]
+def sqlite_is_address_point(element: SaColumn,
+                            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)" % (
+                compiler.process(rank, **kw),
+                compiler.process(hnr, **kw),
+                compiler.process(name, **kw))
+
+
+class CrosscheckNames(sa.sql.functions.GenericFunction[Any]):
     """ Check if in the given list of names in parameters 1 any of the names
         from the JSON array in parameter 2 are contained.
     """
-    type = sa.Boolean()
     name = 'CrosscheckNames'
     inherit_cache = True
 
@@ -175,22 +200,6 @@ def sqlite_json_array_each(element: SaColumn, compiler: 'sa.Compiled', **kw: Any
     return "json_each(%s)" % compiler.process(element.clauses, **kw)
 
 
-class JsonHasKey(sa.sql.functions.GenericFunction[bool]):
-    """ Return elements of a json array as a set.
-    """
-    type = sa.Boolean()
-    name = 'JsonHasKey'
-    inherit_cache = True
-
-
-@compiles(JsonHasKey) # type: ignore[no-untyped-call, misc]
-def compile_json_has_key(element: SaColumn,
-                         compiler: 'sa.Compiled', **kw: Any) -> str:
-    arg1, arg2 = list(element.clauses)
-    return "%s->%s is not null" % (compiler.process(arg1, **kw),
-                                   compiler.process(arg2, **kw))
-
-
 class Greatest(sa.sql.functions.GenericFunction[Any]):
     """ Function to compute maximum of all its input parameters.
     """