Skip to content

Commit

Permalink
MB-51817 Add index condition
Browse files Browse the repository at this point in the history
Previous implementation only included index keys if an index
key is needed for filter evaluation. For consistency and
other potential uses, also include index conditions. Similar
to using "_index_key(expr)" instead of "cover(expr)" for
index key expressions, use "_index_condition(expr)" instead
of "cover(expr)" for index condition expressions.

Change-Id: Id0d5d9699828332c0259d71169b8c65903a95ee8
Reviewed-on: https://review.couchbase.org/c/query/+/174772
Reviewed-by: Sitaram Vemulapalli <sitaram.vemulapalli@couchbase.com>
Reviewed-by: Marco Greco <marco.greco@couchbase.com>
Tested-by: Bingjie Miao <bingjie.miao@couchbase.com>
  • Loading branch information
miaobingjie committed May 12, 2022
1 parent 4d6db6f commit 3d7e315
Show file tree
Hide file tree
Showing 12 changed files with 1,063 additions and 250 deletions.
2 changes: 1 addition & 1 deletion execution/scan_index3.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (this *IndexScan3) RunOnce(context *Context, parent value.Value) {
av := this.newEmptyDocumentWithKey(entry.PrimaryKey, scope_value, context)
if lcovers > 0 {

for c, v := range this.plan.FilterCovers() {
for c, v := range this.plan.AllFilterCovers() {
av.SetCover(c.Text(), v)
}

Expand Down
47 changes: 40 additions & 7 deletions expression/index_cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import (

type Covers []*Cover

const (
_COVER_NONE = int32(iota)
_COVER_FULL
_COVER_KEY
_COVER_COND
)

/*
Internal Expression to support covering indexing.
*/
type Cover struct {
ExpressionBase
covered Expression
text string
fullCover bool
coverType int32
}

func NewCover(covered Expression) *Cover {
Expand All @@ -34,7 +41,7 @@ func NewCover(covered Expression) *Cover {
rv := &Cover{
covered: covered,
text: covered.String(),
fullCover: true,
coverType: _COVER_FULL,
}

rv.expr = rv
Expand All @@ -50,7 +57,23 @@ func NewIndexKey(covered Expression) *Cover {
rv := &Cover{
covered: covered,
text: covered.String(),
fullCover: false,
coverType: _COVER_KEY,
}

rv.expr = rv
return rv
}

func NewIndexCondition(covered Expression) *Cover {
switch covered := covered.(type) {
case *Cover:
return covered
}

rv := &Cover{
covered: covered,
text: covered.String(),
coverType: _COVER_COND,
}

rv.expr = rv
Expand Down Expand Up @@ -115,7 +138,7 @@ func (this *Cover) EquivalentTo(other Expression) bool {
}

oc, ok := other.(*Cover)
return ok && this.covered.EquivalentTo(oc.covered)
return ok && this.covered.EquivalentTo(oc.covered) && this.coverType == oc.coverType
}

func (this *Cover) DependsOn(other Expression) bool {
Expand All @@ -142,10 +165,12 @@ func (this *Cover) MapChildren(mapper Mapper) error {

func (this *Cover) Copy() Expression {
var rv *Cover
if this.fullCover {
if this.coverType == _COVER_FULL {
rv = NewCover(this.covered.Copy())
} else {
} else if this.coverType == _COVER_KEY {
rv = NewIndexKey(this.covered.Copy())
} else if this.coverType == _COVER_COND {
rv = NewIndexCondition(this.covered.Copy())
}
rv.BaseCopy(this)
return rv
Expand All @@ -160,5 +185,13 @@ func (this *Cover) Text() string {
}

func (this *Cover) FullCover() bool {
return this.fullCover
return this.coverType == _COVER_FULL
}

func (this *Cover) IsIndexKey() bool {
return this.coverType == _COVER_KEY
}

func (this *Cover) IsIndexCond() bool {
return this.coverType == _COVER_COND
}
6 changes: 5 additions & 1 deletion expression/stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,12 @@ func (this *Stringer) VisitCover(expr *Cover) (interface{}, error) {
var buf bytes.Buffer
if expr.FullCover() {
buf.WriteString("cover (")
} else {
} else if expr.IsIndexKey() {
buf.WriteString("_index_key (")
} else if expr.IsIndexCond() {
buf.WriteString("_index_condition (")
} else {
return nil, fmt.Errorf("VisitCover: unexpected cover type")
}
buf.WriteString(expr.Text())
buf.WriteString(")")
Expand Down
1 change: 1 addition & 0 deletions parser/n1ql/n1ql.nex
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
/;/ { yylex.logToken(yylex.Text(), "SEMI"); return SEMI }
/\!/ { yylex.logToken(yylex.Text(), "NOT_A_TOKEN"); return NOT_A_TOKEN }

/[_][iI][nN][dD][eE][xX][_][cC][oO][nN][dD][iI][tT][iI][oO][nN]/ { yylex.logToken(yylex.Text(), "_INDEX_CONDITION"); return _INDEX_CONDITION }
/[_][iI][nN][dD][eE][xX][_][kK][eE][yY]/ { yylex.logToken(yylex.Text(), "_INDEX_KEY"); return _INDEX_KEY }
/[aA][dD][vV][iI][sS][eE]/ {
yylex.logToken(yylex.Text(), "ADVISE")
Expand Down
Loading

0 comments on commit 3d7e315

Please sign in to comment.