# LogicTest: local

# SRID of the geometry column is unspecified, so default index bounds are used.
statement ok
CREATE TABLE geo_table(
  k int primary key,
  geom geometry,
  INVERTED INDEX geom_index(geom)
)

# Shapes with SRID 26918. We've taken small X, Y values and added 400,000 to the X coordinate
# and 4,000,000 to the Y coordinate to place them inside the bounds of SRID 26918.
statement ok
INSERT INTO geo_table VALUES
  (1, 'SRID=26918;POINT(400001 4000001)'),
  (2, 'SRID=26918;LINESTRING(400001 4000001, 400002 4000002)'),
  (3, 'SRID=26918;POINT(400003 4000003)'),
  (4, 'SRID=26918;LINESTRING(400004 4000004, 400005 4000005)'),
  (5, 'SRID=26918;LINESTRING(400040 4000040, 400041 4000041)'),
  (6, 'SRID=26918;POLYGON((400001 4000001, 400005 4000001, 400005 4000005, 400001 4000005, 400001 4000001))')

query T
EXPLAIN ANALYZE (DISTSQL) SELECT k FROM geo_table WHERE ST_Intersects('SRID=26918;POINT(400003 4000003)'::geometry, geom) ORDER BY k
----
planning time: 10µs
execution time: 100µs
distribution: <hidden>
vectorized: <hidden>
plan type: custom
rows decoded from KV: 6 (48 B, 12 KVs, 6 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: serializable
priority: normal
quality of service: regular
·
• sort
│ sql nodes: <hidden>
│ regions: <hidden>
│ actual row count: 2
│ estimated max memory allocated: 0 B
│ estimated max sql temp disk usage: 0 B
│ order: +k
│
└── • filter
    │ sql nodes: <hidden>
    │ regions: <hidden>
    │ actual row count: 2
    │ filter: st_intersects('010100002026690000000000000C6A18410000008081844E41', geom)
    │
    └── • index join (streamer)
        │ sql nodes: <hidden>
        │ kv nodes: <hidden>
        │ regions: <hidden>
        │ actual row count: 2
        │ KV time: 0µs
        │ KV contention time: 0µs
        │ KV rows decoded: 2
        │ KV pairs read: 4
        │ KV bytes read: 16 B
        │ KV gRPC calls: 2
        │ estimated max memory allocated: 0 B
        │ estimated max sql temp disk usage: 0 B
        │ table: geo_table@geo_table_pkey
        │
        └── • inverted filter
            │ sql nodes: <hidden>
            │ regions: <hidden>
            │ actual row count: 2
            │ estimated max memory allocated: 0 B
            │ estimated max sql temp disk usage: 0 B
            │ inverted column: geom_inverted_key
            │ num spans: 31
            │
            └── • scan
                  sql nodes: <hidden>
                  kv nodes: <hidden>
                  regions: <hidden>
                  actual row count: 4
                  KV time: 0µs
                  KV contention time: 0µs
                  KV rows decoded: 4
                  KV pairs read: 8
                  KV bytes read: 32 B
                  KV gRPC calls: 4
                  estimated max memory allocated: 0 B
                  missing stats
                  table: geo_table@geom_index
                  spans: 31 spans
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJzMVu9u4kYQ_96nGM2XJKqj7NoO5baqREO4lrY5IkBXnWqENvaEWNhe3-66IYp4rL5An6xaO0QBAnf0j3r-sDCzsz_PzG9-LI9oPmYocNT7pdcdwxzeDgdXMCM1tfImI_j1x96wB8ZO08KSNhRbc3w0GvYvv_Nbb3j72-tB_934OGSMsQDqDxacHAnxQ29w1RsPP3gOKz-BwfCyN4SLDzBHDwuV0DuZk0HxG3KceFhqFZMxSjvXYx3QTxYomIdpUVbWuScexkoTike0qc0IBY5djkOSCekzhh4mZGWa1bDPJXRcAtO0SGiBHnZVVuWFETBvMkMPR6V0jrMIL6JocZtE0YKzKFqwTy14eugZHiHIIoGAgbJ3pA16-PN7cOUaAQVvTE2zVLmULBnbuGyakwD25x9PJ2JVWCpsqoqtLa3uDSQUq4QSAWHjvHmwZECTTAQEPlw03tnwuguxzDLzHFjKVK8C2-jh1ftuF4ylEmJVFRaOaWHP0sKeCGB1y5sAovmugFwuIKdc6QeQWaZiaV1erM7hRtr4jgyoypaVFeDi6_xXjhAnSw8b62kGjJUzQsFfDE3_EgVbep8_N_3id9KWkrdpZkmTPuPrw7Pa7y1KDaqADhdg3JiAsVJbUdMefHMeRcxnUcTYpxYEKpJDj7lp2RqXgWtLx-VbF1jT3bDX2MbKLFsfClpQXG3Pyj5i3J75mIGlvIQkNXOojJzRZ_Pm7-TN3-CNH8LbTyotnuTu75F7821azunhdcn_t5rztzXHW69pzt_WXPivaK4ylICxmmRO-n-jOtig2j-E6mdpButEN36xeSUxzri7fHzmt1pv2Mun2_qet0PeGG3W5u0w7IX8SLy8pTr-yU5x-X9DXP-gbeFG24JD2jZS2pI-C9eb1uFff2m_A-cbVYaHVDkkU6rC0FqVu97ENt50ypcTDymZUfNnw6hKx3StVVzHNuagBqodCRnb7PLG6BerrUZiz9fPSyS-F8nfjcQ3kfy9SMFuJH8TKdiLFO5GCjaRwr1I5_v6NPHwNlP30zRBgSuNnr6yrB50B-TMuAEY3an7Gnb8UDr6bmVmyMMrOadLsqTztEiNTWMUVle0XH71VwAAAP__rWx18g==

statement ok
DROP TABLE geo_table

# SRID of the geometry column is specified, so SRID specific bounds are used.
statement ok
CREATE TABLE geo_table(
  k int primary key,
  geom geometry(geometry, 26918),
  INVERTED INDEX geom_index(geom)
)

# Same shapes.
statement ok
INSERT INTO geo_table VALUES
  (1, 'SRID=26918;POINT(400001 4000001)'),
  (2, 'SRID=26918;LINESTRING(400001 4000001, 400002 4000002)'),
  (3, 'SRID=26918;POINT(400003 4000003)'),
  (4, 'SRID=26918;LINESTRING(400004 4000004, 400005 4000005)'),
  (5, 'SRID=26918;LINESTRING(400040 4000040, 400041 4000041)'),
  (6, 'SRID=26918;POLYGON((400001 4000001, 400005 4000001, 400005 4000005, 400001 4000005, 400001 4000001))')

# The InvertedFilterer stats show "rows read: 2" since all the above shapes are within the index
# bounds.
query T
EXPLAIN ANALYZE (DISTSQL) SELECT k FROM geo_table WHERE ST_Intersects('SRID=26918;POINT(400003 4000003)'::geometry, geom) ORDER BY k
----
planning time: 10µs
execution time: 100µs
distribution: <hidden>
vectorized: <hidden>
plan type: generic, re-optimized
rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: serializable
priority: normal
quality of service: regular
·
• sort
│ sql nodes: <hidden>
│ regions: <hidden>
│ actual row count: 2
│ estimated max memory allocated: 0 B
│ estimated max sql temp disk usage: 0 B
│ order: +k
│
└── • filter
    │ sql nodes: <hidden>
    │ regions: <hidden>
    │ actual row count: 2
    │ filter: st_intersects('010100002026690000000000000C6A18410000008081844E41', geom)
    │
    └── • index join (streamer)
        │ sql nodes: <hidden>
        │ kv nodes: <hidden>
        │ regions: <hidden>
        │ actual row count: 2
        │ KV time: 0µs
        │ KV contention time: 0µs
        │ KV rows decoded: 2
        │ KV pairs read: 4
        │ KV bytes read: 16 B
        │ KV gRPC calls: 2
        │ estimated max memory allocated: 0 B
        │ estimated max sql temp disk usage: 0 B
        │ table: geo_table@geo_table_pkey
        │
        └── • inverted filter
            │ sql nodes: <hidden>
            │ regions: <hidden>
            │ actual row count: 2
            │ estimated max memory allocated: 0 B
            │ estimated max sql temp disk usage: 0 B
            │ inverted column: geom_inverted_key
            │ num spans: 31
            │
            └── • scan
                  sql nodes: <hidden>
                  kv nodes: <hidden>
                  regions: <hidden>
                  actual row count: 2
                  KV time: 0µs
                  KV contention time: 0µs
                  KV rows decoded: 2
                  KV pairs read: 4
                  KV bytes read: 16 B
                  KV gRPC calls: 2
                  estimated max memory allocated: 0 B
                  missing stats
                  table: geo_table@geom_index
                  spans: 31 spans
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJzcVuFu4kYQ_t-nGM2fJKqj7NoO5baqREO4lrY5IkBXnQpCG3tCLOxd3-66IYp4rL5An6xaO0SBHLSoJ7U6_1iY2dnPM_N9w_KI9mOOAke9X3rdMSzg7XBwBXPSMydvcoJff-wNe2DdLFOOjKXE2eOj0bB_-V3YesPb314P-u_GxzFjjEVQf7Do5EiIH3qDq954-CHwWMUJDIaXvSFcfIAFBqh0Su9kQRbFb8hxGmBpdELWauNdj3VAP12iYAFmqqycd08DTLQhFI_oMpcTChz7HIckUzJnDANMycksr2GfS-j4BGaZSmmJAXZ1XhXKClg0mWGAo1J6x9kELyaT5W06mSxZ5Bf2NwueHnqGTxCkSiFioN0dGYsB_vwefLlWgOKNaWieaZ-SI-sal8sKEsD-_OPpRKKVI-UyrV5tGX1vIaVEp5QKCBvnzYMjC4ZkKoC34KLxzofXXUhkntvnwFJmZh0YY4BX77tdsI5KSHSlHBzT0p1lyp0IYHXLmwCixa6AQi6hoEKbB5B5rhPpfF6szuFGuuSOLOjKlZUT4OPr_NeOEKerABvrSQPWyTmh4C9E079EwVbBP9dNX_1OxlH6NssdGTJnfFM86_3esjSgFXS4AOtlAtZJ40RNe_TN-WTCPO3Ms7t3QSCVHnrMq-WVXAa-LR2fb11gTXfDXmNbJ_N8UxS0pKR6rZV9xPg9-zEHR0UJaWYXUFk5p8_AW7jFGz-Et590pp7GPdwz7s23Wbmgh0-P_Jc-c5WlFKwzJAsy_xnV0RbV4SFUP49mtEl04xfbVxLjjPvLJ2Rhq_WGvXy6re95O-aN0WZt3o7jXsyPxMtbqhOefNbh-hdti7faFh3StpE2jsxZvNm0Dv_6__Y7cL5VZXxIlUOypVaWNqrc9Sa29aZTvpoGSOmcmj8bVlcmoWujkzq2MQc1UO1IybpmlzdGX623mhF7vn5eIvG9SOFuJL6NFO5FinYjhdtI0V6keDdStI0U70U639enaYC3ub6fZSkKXM_o6SeW9YP-gJxbL4DRnb6vYccPpafvVuaWArySC7okR6bIVGZdlqBwpqLV6qu_AgAA__-Pi3Xu

# Also works when creating an index.
statement ok
DROP INDEX geo_table@geom_index

statement ok
CREATE INVERTED INDEX geom_index ON geo_table(geom)

query T
EXPLAIN ANALYZE (DISTSQL) SELECT k FROM geo_table WHERE ST_Intersects('SRID=26918;POINT(400003 4000003)'::geometry, geom) ORDER BY k
----
planning time: 10µs
execution time: 100µs
distribution: <hidden>
vectorized: <hidden>
plan type: generic, re-optimized
rows decoded from KV: 4 (32 B, 8 KVs, 4 gRPC calls)
maximum memory usage: <hidden>
network usage: <hidden>
regions: <hidden>
isolation level: serializable
priority: normal
quality of service: regular
·
• sort
│ sql nodes: <hidden>
│ regions: <hidden>
│ actual row count: 2
│ estimated max memory allocated: 0 B
│ estimated max sql temp disk usage: 0 B
│ order: +k
│
└── • filter
    │ sql nodes: <hidden>
    │ regions: <hidden>
    │ actual row count: 2
    │ filter: st_intersects('010100002026690000000000000C6A18410000008081844E41', geom)
    │
    └── • index join (streamer)
        │ sql nodes: <hidden>
        │ kv nodes: <hidden>
        │ regions: <hidden>
        │ actual row count: 2
        │ KV time: 0µs
        │ KV contention time: 0µs
        │ KV rows decoded: 2
        │ KV pairs read: 4
        │ KV bytes read: 16 B
        │ KV gRPC calls: 2
        │ estimated max memory allocated: 0 B
        │ estimated max sql temp disk usage: 0 B
        │ table: geo_table@geo_table_pkey
        │
        └── • inverted filter
            │ sql nodes: <hidden>
            │ regions: <hidden>
            │ actual row count: 2
            │ estimated max memory allocated: 0 B
            │ estimated max sql temp disk usage: 0 B
            │ inverted column: geom_inverted_key
            │ num spans: 31
            │
            └── • scan
                  sql nodes: <hidden>
                  kv nodes: <hidden>
                  regions: <hidden>
                  actual row count: 2
                  KV time: 0µs
                  KV contention time: 0µs
                  KV rows decoded: 2
                  KV pairs read: 4
                  KV bytes read: 16 B
                  KV gRPC calls: 2
                  estimated max memory allocated: 0 B
                  missing stats
                  table: geo_table@geom_index
                  spans: 31 spans
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJzcVuFu4kYQ_t-nGM2fJKqj7NoO5baqREO4lrY5IkBXnQpCG3tCLOxd3-66IYp4rL5An6xaO0SBHLSoJ7U6_1iY2dnPM_N9w_KI9mOOAke9X3rdMSzg7XBwBXPSMydvcoJff-wNe2DdLFOOjKXE2eOj0bB_-V3YesPb314P-u_GxzFjjEVQf7Do5EiIH3qDq954-CHwWMUJDIaXvSFcfIAFBqh0Su9kQRbFb8hxGmBpdELWauNdj3VAP12iYAFmqqycd08DTLQhFI_oMpcTChz7HIckUzJnDANMycksr2GfS-j4BGaZSmmJAXZ1XhXKClg0mWGAo1J6x9kELyaT5W06mSxZ5Bf2NwueHnqGTxCkSiFioN0dGYsB_vwefLlWgOKNaWieaZ-SI-sal8sKEsD-_OPpRKKVI-UyrV5tGX1vIaVEp5QKCBvnzYMjC4ZkKoC34KLxzofXXUhkntvnwFJmZh0YY4BX77tdsI5KSHSlHBzT0p1lyp0IYHXLmwCixa6AQi6hoEKbB5B5rhPpfF6szuFGuuSOLOjKlZUT4OPr_NeOEKerABvrSQPWyTmh4C9E079EwVbBP9dNX_1OxlH6NssdGTJnfFM86_3esjSgFXS4AOtlAtZJ40RNe_TN-WTCPO3Ms7t3QSCVHnrMq-WVXAa-LR2fb11gTXfDXmNbJ_N8UxS0pKR6rZV9xPg9-zEHR0UJaWYXUFk5p8_AW7jFGz-Et590pp7GPdwz7s23Wbmgh0-P_Jc-c5WlFKwzJAsy_xnV0RbV4SFUP49mtEl04xfbVxLjjPvLJ2Rhq_WGvXy6re95O-aN0WZt3o7jXsyPxMtbqhOefNbh-hdti7faFh3StpE2jsxZvNm0Dv_6__Y7cL5VZXxIlUOypVaWNqrc9Sa29aZTvpoGSOmcmj8bVlcmoWujkzq2MQc1UO1IybpmlzdGX623mhF7vn5eIvG9SOFuJL6NFO5FinYjhdtI0V6keDdStI0U70U639enaYC3ub6fZSkKXM_o6SeW9YP-gJxbL4DRnb6vYccPpafvVuaWArySC7okR6bIVGZdlqBwpqLV6qu_AgAA__-Pi3Xu
