我有两个具有范围类型的字段。
这两个where
表达式会给出相同的结果吗?
where range1 && range2
where not isempty( range1 * range2 )
答案 0 :(得分:0)
他们会的。
重叠算子(&&
)和交点(*
)都可以正确评估,包括2个范围具有公共边界及其交点的情况:
以下查询几乎测试了所有情况(=大于1点,正好1点,0点但“接近”,“真正” 0点以及所有上下限包含的组合的交集):
WITH r(range) AS (
VALUES (numrange(0,1,'[]')), (numrange(1,2,'[]')), (numrange(0,2,'[]')), (numrange(5,6, '[]')),
(numrange(0,1,'[)')), (numrange(1,2,'[)')), (numrange(0,2,'[)')), (numrange(5,6, '[)')),
(numrange(0,1,'(]')), (numrange(1,2,'(]')), (numrange(0,2,'(]')), (numrange(5,6, '(]')),
(numrange(0,1,'()')), (numrange(1,2,'()')), (numrange(0,2,'()')), (numrange(5,6, '()'))
)
SELECT * FROM (
SELECT r1.range, r2.range, r1.range && r2.range AS UsingOverlap, NOT isempty(r1.range * r2.range) AS UsingIntersect
FROM r r1, r r2
) T
随时添加WHERE UsingOverlap <> UsingIntersect
。