From d83151b492860d9c92f6c2a88f89e23cefcbc4d9 Mon Sep 17 00:00:00 2001 From: miaobingjie Date: Tue, 19 Jul 2022 14:38:38 -0600 Subject: [PATCH] MB-53028 Do not generate index filters if _EMPTY_SPAN is used As a special case, if an index scan is using _EMPTY_SPAN, then nothing will be returned from the index scan, and in such cases there is no need to generate index filters as well as other optimizations based on index keys such as early order and bloom filters. Change-Id: Ib9dc24532b1432a406fd55aac1d31cdd234e0d3e Reviewed-on: https://review.couchbase.org/c/query/+/177756 Reviewed-by: Sitaram Vemulapalli Tested-by: Bingjie Miao --- planner/build_scan_secondary.go | 5 +++++ planner/spans_term.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/planner/build_scan_secondary.go b/planner/build_scan_secondary.go index feb71038b..1b2a34487 100644 --- a/planner/build_scan_secondary.go +++ b/planner/build_scan_secondary.go @@ -1027,6 +1027,11 @@ func (this *builder) eqJoinFilter(fl *base.Filter, alias string) (bool, expressi func (this *builder) getIndexFilters(entry *indexEntry, node *algebra.KeyspaceTerm, baseKeyspace *base.BaseKeyspace, id expression.Expression) (err error) { + // special case, if the span is an empty span, no need to proceed + if isSpecialSpan(entry.spans, plan.RANGE_EMPTY_SPAN) { + return nil + } + alias := node.Alias() useCBO := this.useCBO && this.keyspaceUseCBO(alias) advisorValidate := this.advisorValidate() diff --git a/planner/spans_term.go b/planner/spans_term.go index 53fbbc6f1..0f4da33cb 100644 --- a/planner/spans_term.go +++ b/planner/spans_term.go @@ -554,6 +554,19 @@ func setSpecialSpan(rg *plan.Range2) { } } +func isSpecialSpan(sspans SargSpans, flag uint32) bool { + if tspans, ok := sspans.(*TermSpans); ok { + spans := tspans.spans + if len(spans) == 1 { + rgs := spans[0].Ranges + if len(rgs) == 1 { + return (rgs[0].Flags & flag) != 0 + } + } + } + return false +} + func ConvertSpans2ToSpan(spans2 plan.Spans2, total int) (plan.Spans, bool) { exact := true spans := make(plan.Spans, 0, len(spans2))