sql: Support the && Array intersection operator
What is your situation? User has the following query with correlated subquery (anonymized):
SELECT m.uuid,
m.meta,
m.content,
--following line is a replacement for int[] & int[]
array
(SELECT unnest(m.filter_ids) INTERSECT SELECT f.id) AS filter_ids,
m.end_ts AS TIMESTAMP
FROM t1 AS m,
(SELECT t2.id,
t2.name
FROM t2
WHERE owner_id = 1
AND is_active = TRUE) AS f
--following line is a replacement for int[] && int[]
--WHERE EXISTS(SELECT unnest(m.filter_ids) INTERSECT SELECT unnest(filter_ids))
GROUP BY m.uuid,
m.meta,
m.content,
filter_ids,
f.id,
m.end_ts;
Here is the anonymized schema for both tables used in the query above:
CREATE TABLE t1
(id SERIAL PRIMARY KEY,
name TEXT,
owner_id INTEGER,
regex TEXT,
is_active BOOLEAN,
is_deleted BOOLEAN);
CREATE TABLE t2
(uuid UUID UNIQUE PRIMARY KEY,
meta JSONB,
content TEXT,
filter_ids INT[],
start_ts TIMESTAMP,
end_ts TIMESTAMP);
Since we do not support all types of correlated queries, can we help the user rewrite this query and stop the error from happening.
@roncrdb, the query isn't correct, because t2 does not have an owner_id column. Can you go back and check the original query? I think something got lost in translation when you anonymized.
@andy-kimball
CREATE TABLE filters
(id SERIAL PRIMARY KEY,
name TEXT,
owner_id INTEGER,
regex TEXT,
is_active BOOLEAN,
is_deleted BOOLEAN);
CREATE TABLE matches
(uuid UUID UNIQUE PRIMARY KEY,
meta JSONB,
content TEXT,
filter_ids INT[],
start_ts TIMESTAMP,
end_ts TIMESTAMP);
SELECT m.uuid,
m.meta,
m.content,
--following line is a replacement for int[] & int[], when on postgres, it was
--m.filter_ids & array_agg(f.id) AS filter_ids,
array(SELECT unnest(m.filter_ids) INTERSECT SELECT f.id) AS filter_ids,
m.end_ts AS timestamp
FROM matches as m, (SELECT filters.id,
filters.name
FROM filters
WHERE owner_id = 1
AND is_active = true) as f
--following line is a replacement for int[] && int[], when on postgres, it was
--WHERE m.filter_ids && filter_ids
--WHERE EXISTS(SELECT unnest(m.filter_ids) INTERSECT SELECT unnest(filter_ids))
GROUP BY m.uuid, m.meta, m.content, filter_ids, f.id, m.end_ts;
Is this related? @rytaft https://github.com/cockroachdb/cockroach/issues/31706
Sorry, @jonwalch, I missed that you had posted the original query. The rewritten query is very difficult to decorrelate (and even once we supported it, very slow), so I think that's a dead end. However, I'm still not understanding the query. When I run it on Postgres 10.5, I get an error:
ERROR: operator does not exist: integer[] & integer[]
LINE 5: m.filter_ids & array_agg(f.id) AS filter_ids,
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Also, this line would seem to intersect the m.filter_ids array with itself, which can't be the intention (it'd always be true):
WHERE m.filter_ids && filter_ids
Maybe you intended something like this?
SELECT m.uuid,
m.meta,
m.content,
m.filter_ids && array_agg(f.id) AS filter_ids,
m.end_ts AS timestamp
FROM matches as m, (SELECT filters.id,
filters.name
FROM filters
WHERE owner_id = 1
AND is_active = true) as f
GROUP BY m.uuid, m.meta, m.content, filter_ids, f.id, m.end_ts
HAVING m.filter_ids && array_agg(f.id);
Regardless, I think a much better (and easier) option to support your scenario is to implement the && array intersection operator in CockroachDB. I'm changing the issue accordingly.
@andy-kimball
https://www.postgresql.org/docs/current/intarray.html
In the SELECT clause we intend to return an array that is the intersection (& operator) between m.filter_ids and f.id.
Later in the WHERE, we're checking if the two arrays have any overlap &&. In other similar, but slightly different queries, we need to check if filters provided by the user overlap (&&) with m.filter_ids.
Zendesk ticket #2799 has been linked to this issue.
@andy-kimball I was playing around with this query more:
SELECT array(SELECT unnest(filter_ids)) FROM matches;
pq: could not decorrelate subquery
Surely this query can be decorrelated?
It may look simple, but it's deceiving. It's quite challenging to decorrelate. These are not relational operators, but instead Postgres-specific extensions.
If you know that your filter_ids values are always non-null and non-empty, then you can rewrite that query like this:
SELECT array_agg(filter_id)
FROM
(
SELECT uuid, unnest(filter_ids) AS filter_id
FROM matches
)
GROUP BY uuid
The optimizer can't do that automatically because it has no way to know that the arrays are non-empty.
We're planning to add execution support for correlated subqueries. Once we have that, you'll no longer get "cannot be decorrelated" errors. At that point, if the optimizer can't decorrelate a certain query, it will just pass it off to the execution engine to run it as a naive correlated nested loop.
@andy-kimball sorry for all of these questions (filter_ids are now uuids)
SELECT uuid,
unnest(filter_ids) AS filter_id
FROM matches
WHERE filter_id = uuid 'bd79731c-53da-47d3-b470-a4b7054671f5'
pq: column "filter_id" does not exist
WITH unwrapped_matches as (
SELECT uuid,
unnest(filter_ids) AS filter_id
FROM matches
)
SELECT *
FROM unwrapped_matches
WHERE filter_id = uuid 'bd79731c-53da-47d3-b470-a4b7054671f5'
pq: could not decorrelate subquery
Are these also expected?
The first error is expected, because you can't filter on a projected column.
The second error should be addressed by https://github.com/cockroachdb/cockroach/issues/31706 in 2.1.1 (due out next week).
I investigated supporting the && array operator a bit at https://github.com/mjibson/cockroach/tree/array-overlap. I found it is tricky because that operator is already used by the inet type but coerced into a function call instead. Things broke when I made it a normal op. Dealing with the opt fallout of a new op is an unknown amount of work for me, so I'm tabling this work for now.
My recommendation is to not pursue this further until it's on a roadmap.
+1
We have marked this issue as stale because it has been inactive for 18 months. If this issue is still relevant, removing the stale label or adding a comment will keep it active. Otherwise, we'll close it in 5 days to keep the issue queue tidy. Thank you for your contribution to CockroachDB!
we support array intersection now.