Skip to content

Commit

Permalink
Merge #59266 #59408
Browse files Browse the repository at this point in the history
59266: opt: build scan InvertedConstraints from JSON FetchVal operator r=mgartner a=mgartner

#### opt: rename inverted constraint contains extraction function

The function name now reflects that it specifically deals with the
Contains expression, `@>`.

Release note: None

#### opt: add rule expectations for GenerateInvertedIndexScan tests

Release note: None

#### invertedidx: add convenience function for extracting index column variable

Release note: None

#### opt: build inverted index scan constraints from JSON fetch val operator

GenerateInvertedIndexScans now generates InvertedConstraints from
equality expressions with a JSON FetchVal operator on the left side and
a constant on the right side, like `j->'a' = '1'`. Previously
Constraints were built for these expressions with the `idxconstraint`
package.

By building InvertedConstraints from FetchVal operators, the optimizer
now has the ability to generate inverted index scans with conjunctive
and disjunctive filters containing FetchVal operators. For example, the
optimizer will plan an inverted index scan with the query below.

    CREATE TABLE t (k INT PRIMARY KEY, j JSON, INVERTED INDEX (j))

    SELECT k FROM t WHERE j->'a' = '1' AND j->'b' = 2

This change also brings us a step closer to cleaning up the
`idxconstraint` by removing code related to inverted indexes.

Informs #47340

Release note (performance improvement): The query optimizer now plans
scans over inverted indexes on JSON columns for query filters that
constrain the JSON column with equality and fetch value operators (`->`)
inside conjunctions and disjunctions, like
`j->'a' = '1' AND j->'b' = '2'`.

#### opt: build InvertedConstraints for inverted index scans on computed columns

GenerateInvertedIndexScans now builds InvertedConstraints over inverted
indexes on computed columns when a filter constrains an expression that
is equivalent to the computed column expression. Previously Constraints
were built for these expressions with the `idxconstraint` package.

This change provides no functional change, but brings us a step closer
to cleaning up the `idxconstraint` by removing code related to inverted
indexes.

Release note: None

#### opt: do not build Constraints in GenerateInvertedIndexScans

InvertedConstraints can be built for inverted index scans in all cases
that Constraints could be built. Therefore there is no longer a need to
build Constraints in GenerateInvertedIndexScans. This commit removes the
code related to this.

Release note: None


59408: *:upgrade vendored pgconn to v1.8.0 r=gaoxk a=gaoxk

The old version of pgconn asynchronously cancelled requests to the server. However, .connect synchronously closes connections in responses to errors. This causes race conditions for goroutine leaks.
The newer pgconn synchronously closes connections to the server, ideally eliminating this race condition. See jackc/pgconn@340bfec

Release note (general change): Upgrade to v1.8.0 `pgconn` to patch potential race condition errors.

Co-authored-by: Marcus Gartner <[email protected]>
Co-authored-by: Kristy Gao <[email protected]>
  • Loading branch information
3 people committed Jan 27, 2021
3 parents 9a93daf + 0a19637 + 45704de commit ba82208
Show file tree
Hide file tree
Showing 18 changed files with 542 additions and 166 deletions.
28 changes: 18 additions & 10 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1363,8 +1363,8 @@ def go_deps():
name = "com_github_jackc_pgconn",
build_file_proto_mode = "disable_global",
importpath = "github.com/jackc/pgconn",
sum = "h1:lwofaXKPbIx6qEaK8mNm7uZuOwxHw+PnAFGDsDFpkRI=",
version = "v1.6.1",
sum = "h1:FmjZ0rOyXTr1wfWs45i4a9vjnjWUAGpMuQLD9OSs+lw=",
version = "v1.8.0",
)
go_repository(
name = "com_github_jackc_pgio",
Expand Down Expand Up @@ -1398,15 +1398,15 @@ def go_deps():
name = "com_github_jackc_pgproto3_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/jackc/pgproto3/v2",
sum = "h1:RHkX5ZUD9bl/kn0f9dYUWs1N7Nwvo1wwUYvKiR26Zco=",
version = "v2.0.4",
sum = "h1:6Pwi1b3QdY65cuv6SyVO0FgPd5J3Bl7wf/nQQjinHMA=",
version = "v2.0.7",
)
go_repository(
name = "com_github_jackc_pgservicefile",
build_file_proto_mode = "disable_global",
importpath = "github.com/jackc/pgservicefile",
sum = "h1:Q3tB+ExeflWUW7AFcAhXqk40s9mnNYLk1nOkKNZ5GnU=",
version = "v0.0.0-20200307190119-3430c5407db8",
sum = "h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=",
version = "v0.0.0-20200714003250-2b9c44734f2b",
)
go_repository(
name = "com_github_jackc_pgtype",
Expand Down Expand Up @@ -2915,8 +2915,8 @@ def go_deps():
name = "org_golang_x_crypto",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/crypto",
sum = "h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=",
version = "v0.0.0-20200622213623-75b288015ac9",
sum = "h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=",
version = "v0.0.0-20201221181555-eec23a3978ad",
)
go_repository(
name = "org_golang_x_exp",
Expand Down Expand Up @@ -2988,12 +2988,20 @@ def go_deps():
sum = "h1:wHn06sgWHMO1VsQ8F+KzDJx/JzqfsNLnc+oEi07qD7s=",
version = "v0.0.0-20210108172913-0df2131ae363",
)
go_repository(
name = "org_golang_x_term",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/term",
sum = "h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=",
version = "v0.0.0-20201117132131-f5c789dd3221",
)

go_repository(
name = "org_golang_x_text",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/text",
sum = "h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=",
version = "v0.3.3",
sum = "h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=",
version = "v0.3.5",
)
go_repository(
name = "org_golang_x_time",
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ require (
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/ianlancetaylor/cgosymbolizer v0.0.0-20201002210021-dda951febc36 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgconn v1.6.1
github.com/jackc/pgproto3/v2 v2.0.4
github.com/jackc/pgconn v1.8.0
github.com/jackc/pgproto3/v2 v2.0.7
github.com/jackc/pgx v3.6.2+incompatible
github.com/jackc/pgx/v4 v4.6.0
github.com/jaegertracing/jaeger v1.17.0
Expand Down Expand Up @@ -146,15 +146,16 @@ require (
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
github.com/zabawaba99/go-gitignore v0.0.0-20200117185801-39e6bddfb292
go.etcd.io/etcd/raft/v3 v3.0.0-20201109164711-01844fd28560
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/exp v0.0.0-20201229011636-eab1b5eb1a03
golang.org/x/lint v0.0.0-20200130185559-910be7a94367
golang.org/x/net v0.0.0-20201021035429-f5854403a974
golang.org/x/oauth2 v0.0.0-20190115181402-5dab4167f31c
golang.org/x/perf v0.0.0-20180704124530-6e6d33e29852
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363
golang.org/x/text v0.3.3
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221
golang.org/x/text v0.3.5
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee
google.golang.org/api v0.1.0
Expand Down
20 changes: 15 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9
github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE=
github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s=
github.com/jackc/pgconn v1.5.0/go.mod h1:QeD3lBfpTFe8WUnPZWN5KY/mB8FGMIYRdd8P8Jr0fAI=
github.com/jackc/pgconn v1.6.1 h1:lwofaXKPbIx6qEaK8mNm7uZuOwxHw+PnAFGDsDFpkRI=
github.com/jackc/pgconn v1.6.1/go.mod h1:g8mKMqmSUO6AzAvha7vy07g1rbGOlc7iF0nU0ei83hc=
github.com/jackc/pgconn v1.8.0 h1:FmjZ0rOyXTr1wfWs45i4a9vjnjWUAGpMuQLD9OSs+lw=
github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2 h1:JVX6jT/XfzNqIjye4717ITLaNwV9mWbJx0dLCpcRzdA=
Expand All @@ -420,11 +420,14 @@ github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod
github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM=
github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM=
github.com/jackc/pgproto3/v2 v2.0.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.0.2/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.0.4 h1:RHkX5ZUD9bl/kn0f9dYUWs1N7Nwvo1wwUYvKiR26Zco=
github.com/jackc/pgproto3/v2 v2.0.4/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.0.6 h1:b1105ZGEMFe7aCvrT1Cca3VoVb4ZFMaFJLJcg/3zD+8=
github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgproto3/v2 v2.0.7 h1:6Pwi1b3QdY65cuv6SyVO0FgPd5J3Bl7wf/nQQjinHMA=
github.com/jackc/pgproto3/v2 v2.0.7/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8 h1:Q3tB+ExeflWUW7AFcAhXqk40s9mnNYLk1nOkKNZ5GnU=
github.com/jackc/pgservicefile v0.0.0-20200307190119-3430c5407db8/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg=
github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc=
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
Expand Down Expand Up @@ -761,6 +764,8 @@ golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
Expand Down Expand Up @@ -848,6 +853,7 @@ golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -857,10 +863,14 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363 h1:wHn06sgWHMO1VsQ8F+KzDJx/JzqfsNLnc+oEi07qD7s=
golang.org/x/sys v0.0.0-20210108172913-0df2131ae363/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/roachprod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ go_library(
"//pkg/util/flagutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_spf13_cobra//:cobra",
"@org_golang_x_crypto//ssh/terminal",
"@org_golang_x_sys//unix",
"@org_golang_x_term//:term",
],
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/roachprod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/flagutil"
"github.com/cockroachdb/errors"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/sys/unix"
"golang.org/x/term"
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -185,7 +185,7 @@ Available clusters:
c.Tag = "/" + tag
}
c.UseTreeDist = useTreeDist
c.Quiet = quiet || !terminal.IsTerminal(int(os.Stdout.Fd()))
c.Quiet = quiet || !term.IsTerminal(int(os.Stdout.Fd()))
c.MaxConcurrency = maxConcurrency
return c, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/security/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ go_library(
"@com_github_cockroachdb_redact//:redact",
"@org_golang_x_crypto//bcrypt",
"@org_golang_x_crypto//ocsp",
"@org_golang_x_crypto//ssh/terminal",
"@org_golang_x_sync//errgroup",
"@org_golang_x_term//:term",
],
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/security/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/errors"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

// BcryptCost is the cost to use when hashing passwords. It is exposed for
Expand Down Expand Up @@ -69,7 +69,7 @@ func HashPassword(password string) ([]byte, error) {
// This is meant to be used when using a password.
func PromptForPassword() (string, error) {
fmt.Print("Enter password: ")
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,22 @@ SELECT j FROM f@i WHERE j->'a' = '1' ORDER BY k
{"a": 1, "b": 2}
{"a": 1, "c": 3}

query T
SELECT j FROM f@i WHERE j->'a' = '1' OR j->'b' = '2' ORDER BY k
----
{"a": 1}
{"b": 2}
{"a": 1, "b": 2}
{"a": 1, "c": 3}

query T
SELECT j FROM f@i WHERE j->'a' = '1' OR j @> '{"b": 2}' ORDER BY k
----
{"a": 1}
{"b": 2}
{"a": 1, "b": 2}
{"a": 1, "c": 3}

subtest arrays

statement ok
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/testdata/inverted_index
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ vectorized: true
└── • scan
columns: (a)
estimated row count: 110 (missing stats)
estimated row count: 111 (missing stats)
table: d@foo_inv
spans: /"a"/"b"-/"a"/"b"/PrefixEnd

Expand Down Expand Up @@ -399,7 +399,7 @@ vectorized: true
└── • scan
columns: (a)
estimated row count: 110 (missing stats)
estimated row count: 111 (missing stats)
table: d@foo_inv
spans: /"a"/"b"-/"a"/"b"/PrefixEnd

Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/invertedidx/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ go_library(
"//pkg/sql/sem/tree",
"//pkg/sql/types",
"//pkg/util/encoding",
"//pkg/util/json",
"@com_github_cockroachdb_errors//:errors",
"@com_github_golang_geo//r1",
"@com_github_golang_geo//s1",
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/opt/invertedidx/geo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,13 @@ func TestTryFilterGeoIndex(t *testing.T) {
// that is tested elsewhere. This is just testing that we are constraining
// the index when we expect to.
spanExpr, _, remainingFilters, pfState, ok := invertedidx.TryFilterInvertedIndex(
evalCtx, &f, filters, nil /* optionalFilters */, tab, md.Table(tab).Index(tc.indexOrd),
evalCtx,
&f,
filters,
nil, /* optionalFilters */
tab,
md.Table(tab).Index(tc.indexOrd),
nil, /* computedColumns */
)
if tc.ok != ok {
t.Fatalf("expected %v, got %v", tc.ok, ok)
Expand Down
25 changes: 23 additions & 2 deletions pkg/sql/opt/invertedidx/inverted_index_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TryFilterInvertedIndex(
optionalFilters memo.FiltersExpr,
tabID opt.TableID,
index cat.Index,
computedColumns map[opt.ColumnID]opt.ScalarExpr,
) (
spanExpr *invertedexpr.SpanExpression,
constraint *constraint.Constraint,
Expand Down Expand Up @@ -109,8 +110,9 @@ func TryFilterInvertedIndex(
typ = types.Geometry
} else {
filterPlanner = &jsonOrArrayFilterPlanner{
tabID: tabID,
index: index,
tabID: tabID,
index: index,
computedColumns: computedColumns,
}
col := index.VirtualInvertedColumn().InvertedSourceColumnOrdinal()
typ = factory.Metadata().Table(tabID).Column(col).DatumType()
Expand Down Expand Up @@ -497,3 +499,22 @@ func extractInvertedFilterCondition(
return filterPlanner.extractInvertedFilterConditionFromLeaf(evalCtx, filterCond)
}
}

// isIndexColumn returns true if e is an expression that corresponds to an
// inverted index column. The expression can be either:
// - a variable on the index column, or
// - an expression that matches the computed column expression (if the index
// column is computed).
//
func isIndexColumn(
tabID opt.TableID, index cat.Index, e opt.Expr, computedColumns map[opt.ColumnID]opt.ScalarExpr,
) bool {
invertedSourceCol := tabID.ColumnID(index.VirtualInvertedColumn().InvertedSourceColumnOrdinal())
if v, ok := e.(*memo.VariableExpr); ok && v.Col == invertedSourceCol {
return true
}
if computedColumns != nil && e == computedColumns[invertedSourceCol] {
return true
}
return false
}
Loading

0 comments on commit ba82208

Please sign in to comment.