Skip to content

Commit

Permalink
Simplify pushdown disabling logic (#2008)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Feb 16, 2023
1 parent 32238ee commit 23c574f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 57 deletions.
5 changes: 4 additions & 1 deletion internal/handlers/pg/msg_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
}

qp.Explain = true
qp.DisablePushdown = h.DisablePushdown

explain, err := common.GetRequiredParam[*types.Document](document, "explain")
if err != nil {
Expand All @@ -71,6 +70,10 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
return nil, lazyerrors.Error(err)
}

if h.DisablePushdown {
qp.Filter = nil
}

var queryPlanner *types.Document
err = dbPool.InTransaction(ctx, func(tx pgx.Tx) error {
var err error
Expand Down
5 changes: 3 additions & 2 deletions internal/handlers/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ type Handler struct {

// NewOpts represents handler configuration.
type NewOpts struct {
PostgreSQLURL string
DisablePushdown bool
PostgreSQLURL string

L *zap.Logger
Metrics *connmetrics.ConnMetrics
StateProvider *state.Provider
DisablePushdown bool
}

// New returns a new handler.
Expand Down
44 changes: 15 additions & 29 deletions internal/handlers/pg/pgdb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ type FetchedDocs struct {
// QueryParam represents options/parameters used for SQL query.
type QueryParam struct {
// Query filter for possible pushdown; may be ignored in part or entirely.
Filter *types.Document
DB string
Collection string
Comment string
Explain bool
DisablePushdown bool
Filter *types.Document
DB string
Collection string
Comment string
Explain bool
}

// Explain returns SQL EXPLAIN results for given query parameters.
Expand Down Expand Up @@ -84,14 +83,8 @@ func Explain(ctx context.Context, tx pgx.Tx, qp *QueryParam) (*types.Document, e

query += ` FROM ` + pgx.Identifier{qp.DB, table}.Sanitize()

var args []any

if qp.Filter != nil && !qp.DisablePushdown {
var where string

where, args = prepareWhereClause(qp.Filter)
query += where
}
where, args := prepareWhereClause(qp.Filter)
query += where

rows, err := tx.Query(ctx, query, args...)
if err != nil {
Expand Down Expand Up @@ -189,12 +182,11 @@ func queryById(ctx context.Context, tx pgx.Tx, schema, table string, id any) (*t

// iteratorParams contains parameters for building an iterator.
type iteratorParams struct {
schema string
table string
comment string
explain bool
disablePushdown bool
filter *types.Document
schema string
table string
comment string
explain bool
filter *types.Document
}

// buildIterator returns an iterator to fetch documents for given iteratorParams.
Expand All @@ -207,7 +199,7 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (iterator.

query += `SELECT _jsonb `

if c := p.comment; c != "" && !p.disablePushdown {
if c := p.comment; c != "" {
// prevent SQL injections
c = strings.ReplaceAll(c, "/*", "/ *")
c = strings.ReplaceAll(c, "*/", "* /")
Expand All @@ -217,14 +209,8 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (iterator.

query += ` FROM ` + pgx.Identifier{p.schema, p.table}.Sanitize()

var args []any

if p.filter != nil && !p.disablePushdown {
var where string

where, args = prepareWhereClause(p.filter)
query += where
}
where, args := prepareWhereClause(p.filter)
query += where

rows, err := tx.Query(ctx, query, args...)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/handlers/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type NewHandlerOpts struct {
Logger *zap.Logger
Metrics *connmetrics.ConnMetrics
StateProvider *state.Provider
DisablePushdown bool // defaults to false
DisablePushdown bool

// for `pg` handler
PostgreSQLURL string
Expand Down Expand Up @@ -85,7 +85,8 @@ func init() {

registry["pg"] = func(opts *NewHandlerOpts) (handlers.Interface, error) {
handlerOpts := &pg.NewOpts{
PostgreSQLURL: opts.PostgreSQLURL,
PostgreSQLURL: opts.PostgreSQLURL,

L: opts.Logger,
Metrics: opts.Metrics,
StateProvider: opts.StateProvider,
Expand Down
16 changes: 9 additions & 7 deletions internal/handlers/registry/tigris.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
func init() {
registry["tigris"] = func(opts *NewHandlerOpts) (handlers.Interface, error) {
handlerOpts := &tigris.NewOpts{
ClientID: opts.TigrisClientID,
ClientSecret: opts.TigrisClientSecret,
Token: opts.TigrisToken,
URL: opts.TigrisURL,
L: opts.Logger,
Metrics: opts.Metrics,
StateProvider: opts.StateProvider,
ClientID: opts.TigrisClientID,
ClientSecret: opts.TigrisClientSecret,
Token: opts.TigrisToken,
URL: opts.TigrisURL,

L: opts.Logger,
Metrics: opts.Metrics,
StateProvider: opts.StateProvider,
DisablePushdown: opts.DisablePushdown,
}
return tigris.New(handlerOpts)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/handlers/tigris/msg_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
return nil, lazyerrors.Error(err)
}

var queryFilter string
if !h.DisablePushdown {
queryFilter = string(tigrisdb.BuildFilter(filter))
if h.DisablePushdown {
filter = nil
}

queryFilter := string(tigrisdb.BuildFilter(filter))

queryPlanner := must.NotFail(types.NewDocument(
"Filter", queryFilter,
))
Expand Down
11 changes: 6 additions & 5 deletions internal/handlers/tigris/tigris.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import (

// NewOpts represents handler configuration.
type NewOpts struct {
ClientID string
ClientSecret string
Token string
URL string
DisablePushdown bool
ClientID string
ClientSecret string
Token string
URL string

L *zap.Logger
Metrics *connmetrics.ConnMetrics
StateProvider *state.Provider
DisablePushdown bool
}

// Handler implements handlers.Interface on top of Tigris.
Expand Down
12 changes: 4 additions & 8 deletions internal/handlers/tigris/tigrisdb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ import (
// QueryParam represents options/parameters used by the fetch/query.
type QueryParam struct {
// Query filter for possible pushdown; may be ignored in part or entirely.
Filter *types.Document
DB string
Collection string
DisablePushdown bool
Filter *types.Document
DB string
Collection string
}

// QueryDocuments fetches documents from the given collection.
Expand Down Expand Up @@ -68,10 +67,7 @@ func (tdb *TigrisDB) QueryDocuments(ctx context.Context, param *QueryParam) (ite
return nil, lazyerrors.Error(err)
}

var filter driver.Filter
if !param.DisablePushdown {
filter = BuildFilter(param.Filter)
}
filter := BuildFilter(param.Filter)

tdb.l.Sugar().Debugf("Read filter: %s", filter)

Expand Down

0 comments on commit 23c574f

Please sign in to comment.