Skip to content

Commit

Permalink
invertedidx: add convenience function for extracting index column var…
Browse files Browse the repository at this point in the history
…iable

Release note: None
  • Loading branch information
mgartner committed Jan 26, 2021
1 parent 5f4f0d9 commit f236d53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
18 changes: 18 additions & 0 deletions pkg/sql/opt/invertedidx/inverted_index_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,21 @@ func extractInvertedFilterCondition(
return filterPlanner.extractInvertedFilterConditionFromLeaf(evalCtx, filterCond)
}
}

// indexColumnVariable returns a variable expression (a type-asserted form of e)
// and ok=true if e is a variable expression that corresponds to the inverted
// index column.
func indexColumnVariable(
tabID opt.TableID, index cat.Index, e opt.Expr,
) (_ *memo.VariableExpr, ok bool) {
variable, ok := e.(*memo.VariableExpr)
if !ok {
return nil, false
}
invertedIndexCol := tabID.ColumnID(index.VirtualInvertedColumn().InvertedSourceColumnOrdinal())
if variable.Col != invertedIndexCol {
// The column does not match the index column.
return nil, false
}
return variable, true
}
15 changes: 2 additions & 13 deletions pkg/sql/opt/invertedidx/json_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ func (j *jsonOrArrayJoinPlanner) canExtractJSONOrArrayJoinCondition(
) bool {
// The first argument should be a variable corresponding to the index
// column.
variable, ok := left.(*memo.VariableExpr)
variable, ok := indexColumnVariable(j.tabID, j.index, left)
if !ok {
return false
}
if variable.Col != j.tabID.ColumnID(
j.index.VirtualInvertedColumn().InvertedSourceColumnOrdinal(),
) {
// The column does not match the index column.
return false
}
Expand Down Expand Up @@ -300,16 +295,10 @@ func (j *jsonOrArrayFilterPlanner) extractJSONOrArrayContainsCondition(
) invertedexpr.InvertedExpression {
// The first argument should be a variable corresponding to the index
// column.
variable, ok := left.(*memo.VariableExpr)
variable, ok := indexColumnVariable(j.tabID, j.index, left)
if !ok {
return invertedexpr.NonInvertedColExpression{}
}
if variable.Col != j.tabID.ColumnID(
j.index.VirtualInvertedColumn().InvertedSourceColumnOrdinal(),
) {
// The column does not match the index column.
return invertedexpr.NonInvertedColExpression{}
}

// The second argument should be a constant.
if !memo.CanExtractConstDatum(right) {
Expand Down

0 comments on commit f236d53

Please sign in to comment.