forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
223 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
go/vt/vtgate/planbuilder/operators/predicates/predicate.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Copyright 2025 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package predicates | ||
|
||
import ( | ||
"vitess.io/vitess/go/vt/sqlparser" | ||
"vitess.io/vitess/go/vt/vtgate/semantics" | ||
) | ||
|
||
// Predicate represents a condition relating two parts of the query, | ||
// typically used when expressions span multiple tables. We track it in | ||
// different shapes so we can rewrite or push it differently during plan generation. | ||
type Predicate struct { | ||
ID ID | ||
LHS, RHS semantics.TableSet | ||
tracker *Tracker | ||
} | ||
|
||
var _ sqlparser.Expr = (*Predicate)(nil) | ||
var _ sqlparser.Visitable = (*Predicate)(nil) | ||
|
||
func (j *Predicate) Inputs() []sqlparser.SQLNode { | ||
return []sqlparser.SQLNode{j.current()} | ||
} | ||
|
||
func (j *Predicate) current() sqlparser.Expr { | ||
return j.tracker.Expressions[j.ID] | ||
} | ||
|
||
func (j *Predicate) IsExpr() {} | ||
|
||
func (j *Predicate) Format(buf *sqlparser.TrackedBuffer) { | ||
j.current().Format(buf) | ||
} | ||
|
||
func (j *Predicate) FormatFast(buf *sqlparser.TrackedBuffer) { | ||
j.current().FormatFast(buf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2025 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package predicates | ||
|
||
import ( | ||
"sync" | ||
"vitess.io/vitess/go/vt/sqlparser" | ||
"vitess.io/vitess/go/vt/vtgate/semantics" | ||
) | ||
|
||
type ( | ||
// Tracker manages a global mapping of expression IDs to their "Shape". | ||
// This allows the same logical expression to take different forms (shapes) | ||
// depending on pushdown or join strategies. We lock around 'lastID' to ensure | ||
// unique IDs in concurrent planning contexts. | ||
Tracker struct { | ||
mu sync.Mutex | ||
lastID ID | ||
Expressions map[ID]sqlparser.Expr | ||
} | ||
|
||
// ID is a unique key that references the shape of a single expression. | ||
// We use it so multiple references to an expression can share the same shape entry. | ||
ID int | ||
) | ||
|
||
func NewTracker() *Tracker { | ||
return &Tracker{ | ||
Expressions: make(map[ID]sqlparser.Expr), | ||
} | ||
} | ||
|
||
func (t *Tracker) NewPredicate(org sqlparser.Expr, lhs, rhs semantics.TableSet) *Predicate { | ||
t.Expressions[t.NextID()] = org | ||
return &Predicate{ | ||
ID: t.NextID(), | ||
Original: org, | ||
LHS: lhs, | ||
RHS: rhs, | ||
tracker: t, | ||
} | ||
} | ||
|
||
func (t *Tracker) NextID() ID { | ||
t.mu.Lock() | ||
defer t.mu.Unlock() | ||
id := t.lastID | ||
t.lastID++ | ||
return id | ||
} | ||
|
||
func (t *Tracker) GetPushDownExpression(pred *Predicate) sqlparser.Expr { | ||
return t.Expressions[pred.ID] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
[ | ||
{ | ||
"comment": "Add your test case here for debugging and run go test -run=One.", | ||
"query": "", | ||
"query": "select id from user where someColumn = null", | ||
"plan": { | ||
} | ||
} | ||
"QueryType": "SELECT", | ||
"Original": "select id from user where someColumn = null", | ||
"Instructions": { | ||
"OperatorType": "Route", | ||
"Variant": "None", | ||
"Keyspace": { | ||
"Name": "user", | ||
"Sharded": true | ||
}, | ||
"FieldQuery": "select id from `user` where 1 != 1", | ||
"Query": "select id from `user` where someColumn = null", | ||
"Table": "`user`" | ||
}, | ||
"TablesUsed": [ | ||
"user.user" | ||
] | ||
} } | ||
] |