Skip to content

Commit 5f22be8

Browse files
committed
opt: index accelerate @> operator for ltree
The @> operator is accelerated by restricting the spans to be the union of equality spans for all sub-ltrees rooted at ''. For example, a query with predicate `WHERE a @> 'A.B'` would create spans [/'' - /''], [/'A' - /'A'], [/'A.B' - /'A.B']. Informs: #44657 Epic: CRDB-148 Release note (performance improvement): LTREE is now index accelerated with the `@>` operator.
1 parent 1aabeeb commit 5f22be8

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

pkg/sql/opt/exec/execbuilder/testdata/select_index

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,3 +1904,26 @@ vectorized: true
19041904
missing stats
19051905
table: t4@t4_pkey
19061906
spans: [/1/1 - /1/1] [/1/5 - /1/5] [/5/1 - /5/1] [/5/5 - /5/5]
1907+
1908+
# ------------------------------------------------------------------------------
1909+
# Tests for index acceleration on ancestry operators (@> and <@) using
1910+
# LTREE columns
1911+
# ------------------------------------------------------------------------------
1912+
1913+
statement ok
1914+
CREATE TABLE t5 (a LTREE)
1915+
1916+
statement ok
1917+
CREATE INDEX t5_a_idx ON t5 (a)
1918+
1919+
query T
1920+
EXPLAIN SELECT a FROM t5 WHERE a @> 'A.B.C'
1921+
----
1922+
distribution: local
1923+
vectorized: true
1924+
·
1925+
• scan
1926+
missing stats
1927+
table: t5@t5_a_idx
1928+
spans: [/'' - /''] [/'A' - /'A'] [/'A.B' - /'A.B'] [/'A.B.C' - /'A.B.C']
1929+

pkg/sql/opt/idxconstraint/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_library(
1616
"//pkg/sql/sem/tree",
1717
"//pkg/sql/types",
1818
"//pkg/util",
19+
"//pkg/util/ltree",
1920
"@com_github_cockroachdb_errors//:errors",
2021
],
2122
)

pkg/sql/opt/idxconstraint/index_constraints.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
2121
"github.com/cockroachdb/cockroach/pkg/sql/types"
2222
"github.com/cockroachdb/cockroach/pkg/util"
23+
"github.com/cockroachdb/cockroach/pkg/util/ltree"
2324
"github.com/cockroachdb/errors"
2425
)
2526

@@ -338,6 +339,39 @@ func (c *indexConstraintCtx) makeSpansForSingleColumnDatum(
338339
return complete
339340
}
340341
}
342+
343+
case opt.ContainsOp:
344+
if l, ok := datum.(*tree.DLTree); ok {
345+
var spans constraint.Spans
346+
// We need to create an equality span for each subtree of the LTree that
347+
// is rooted from the root, including the empty ltree.
348+
spans.Alloc(l.LTree.Len() + 1)
349+
keyCtx := &c.keyCtx[offset]
350+
for i := 0; i <= l.LTree.Len(); i++ {
351+
var subLTree ltree.T
352+
if l.LTree.Compare(ltree.Empty) != 0 {
353+
var err error
354+
subLTree, err = l.LTree.SubPath(0 /* offset */, i /* length */)
355+
if err != nil {
356+
panic(err)
357+
}
358+
} else {
359+
// SubPath is not graceful with empty ltree, thus we handle it here.
360+
subLTree = ltree.Empty
361+
}
362+
key := constraint.MakeKey(tree.NewDLTree(subLTree))
363+
var sp constraint.Span
364+
sp.Init(key, includeBoundary, key, includeBoundary)
365+
spans.Append(&sp)
366+
}
367+
// Given how we've constructed the spans, they already are ordered and
368+
// unique, but we choose to call SortAndMerge for symmetry with other
369+
// expressions and as a sanity check (the function exits quickly if the
370+
// ordering is already correct).
371+
spans.SortAndMerge(keyCtx)
372+
out.Init(keyCtx, &spans)
373+
return true
374+
}
341375
}
342376
c.unconstrained(offset, out)
343377
return false
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
index-constraints vars=(a ltree) index=a
2+
a @> '-'
3+
----
4+
[/'' - /'']
5+
[/'-' - /'-']
6+
7+
index-constraints vars=(a ltree) index=a
8+
a @> 'A.B.C'
9+
----
10+
[/'' - /'']
11+
[/'A' - /'A']
12+
[/'A.B' - /'A.B']
13+
[/'A.B.C' - /'A.B.C']
14+
15+
index-constraints vars=(a ltree) index=a
16+
a @> ''
17+
----
18+
[/'' - /'']
19+
20+
index-constraints vars=(a ltree) index=a
21+
a @> NULL
22+
----
23+

0 commit comments

Comments
 (0)