-
Notifications
You must be signed in to change notification settings - Fork 28
feat: support partitioned queries through SQL statements #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Support partitioned queries using only SQL statements.
Summary of ChangesHello @olavloite, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the database connector by integrating support for partitioned queries directly into the SQL statement parsing and execution flow. It introduces a new Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for partitioned queries through SQL statements, including a new RUN PARTITIONED QUERY statement and several connection properties (auto_partition_mode, data_boost_enabled, etc.). The changes look good overall, but I've found several issues, including some critical bugs in the tests that would cause compilation failures or incorrect test behavior. I've also identified a functional issue where RUN PARTITIONED QUERY is not self-contained and opportunities for refactoring and improving robustness. Please see my detailed comments.
| "max_partitions", | ||
| "The max partitions hint value to use for partitioned queries. "+ | ||
| "Set to 0 if you do not want to specify a hint.", | ||
| 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| { | ||
| input: "run partitioned query select * from my_table", | ||
| want: ParsedRunPartitionedQueryStatement{ | ||
| statement: " select * from my_table", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The field statement is not a member of ParsedRunPartitionedQueryStatement. The correct field name is Statement (with a capital 'S'). This will cause a compilation error. This error is repeated for all test cases in this test function.
| statement: " select * from my_table", | |
| Statement: " select * from my_table", |
| tests := make([]autoPartitionTest, 0) | ||
| for _, useExecOption := range []bool{true, false} { | ||
| for _, withTx := range []bool{false} { | ||
| for maxResultsPerPartition := range []int{0, 1, 5, 50, 200} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This loop is iterating over the indices of the slice, not its values. maxResultsPerPartition will be 0, 1, 2, 3, 4 instead of 0, 1, 5, 50, 200. This is a common Go pitfall. To iterate over the values, you should use for _, maxResultsPerPartition := range ....
| for maxResultsPerPartition := range []int{0, 1, 5, 50, 200} { | |
| for _, maxResultsPerPartition := range []int{0, 1, 5, 50, 200} { |
| rows, err := db.QueryContext(ctx, "run partitioned query "+testutil.SelectFooFromBar) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for rows.Next() { | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is incomplete and leaks resources.
- It does not verify any results. It should configure the mock server with partitions and results, and then assert that the query returns the expected data.
- The
rowsobject is not closed, which will lead to a resource leak. Adefer rows.Close()should be added after the query execution.
_, allResults, err := setupRandomPartitionResults(server, testutil.SelectFooFromBar, 10)
if err != nil {
t.Fatalf("failed to set up partition results: %v", err)
}
rows, err := db.QueryContext(ctx, "run partitioned query "+testutil.SelectFooFromBar)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var results []int64
for rows.Next() {
var val int64
if err := rows.Scan(&val); err != nil {
t.Fatal(err)
}
results = append(results, val)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if len(results) != len(allResults) {
t.Fatalf("row count mismatch: got %d, want %d", len(results), len(allResults))
}| func (s *executableRunPartitionedQueryStatement) queryContext(ctx context.Context, c *conn, opts *ExecOptions) (driver.Rows, error) { | ||
| args := []driver.NamedValue{{Value: opts}} | ||
| return c.QueryContext(ctx, s.stmt.Statement, args) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RUN PARTITIONED QUERY statement should implicitly trigger a partitioned query. Currently, it relies on the ExecOptions passed to it. To make it self-contained, you should ensure that AutoPartitionQuery is set to true on the options before passing them to c.QueryContext.
It's also good practice to work with a copy of the options to avoid side effects.
func (s *executableRunPartitionedQueryStatement) queryContext(ctx context.Context, c *conn, opts *ExecOptions) (driver.Rows, error) {
newOpts := *opts
newOpts.PartitionedQueryOptions.AutoPartitionQuery = true
args := []driver.NamedValue{{Value: &newOpts}}
return c.QueryContext(ctx, s.stmt.Statement, args)
}| "data_boost_enabled", | ||
| "Enable data boost for all partitioned queries that are executed by this connection. "+ | ||
| "This setting is only used for partitioned queries and is ignored by all other statements. "+ | ||
| "Either set `auto_partition_query=true` or execute a query with `RUN PARTITIONED QUERY SELECT ... FROM ...` "+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo in the description. The connection property is auto_partition_mode, but the description refers to auto_partition_query.
| "Either set `auto_partition_query=true` or execute a query with `RUN PARTITIONED QUERY SELECT ... FROM ...` "+ | |
| "Either set `auto_partition_mode=true` or execute a query with `RUN PARTITIONED QUERY SELECT ... FROM ...` "+ |
| func isRunBatch(parser *StatementParser, query string) bool { | ||
| sp := &simpleParser{sql: []byte(query), statementParser: parser} | ||
| if !sp.eatKeyword("run") { | ||
| return false | ||
| } | ||
| if !sp.hasMoreTokens() { | ||
| // START is a synonym for START TRANSACTION | ||
| return false | ||
| } | ||
| if sp.eatKeyword("batch") { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isRunPartitionedQuery(parser *StatementParser, query string) bool { | ||
| sp := &simpleParser{sql: []byte(query), statementParser: parser} | ||
| if !sp.eatKeyword("run") { | ||
| return false | ||
| } | ||
| if !sp.hasMoreTokens() { | ||
| // START is a synonym for START TRANSACTION | ||
| return false | ||
| } | ||
| if sp.eatKeyword("partitioned") { | ||
| return true | ||
| } | ||
| return false | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions isRunBatch and isRunPartitionedQuery are very similar and contain duplicated code. They can be refactored into a single helper function to improve maintainability.
Additionally, the comment // START is a synonym for START TRANSACTION is misleading in this context as it was likely copied from isStartTransaction. It should be removed.
func isRunCommand(parser *StatementParser, query string, command string) bool {
sp := &simpleParser{sql: []byte(query), statementParser: parser}
if !sp.eatKeyword("run") {
return false
}
if !sp.hasMoreTokens() {
return false
}
return sp.eatKeyword(command)
}
func isRunBatch(parser *StatementParser, query string) bool {
return isRunCommand(parser, query, "batch")
}
func isRunPartitionedQuery(parser *StatementParser, query string) bool {
return isRunCommand(parser, query, "partitioned")
}| execOptions.QueryOptions.DataBoostEnabled = execOptions.QueryOptions.DataBoostEnabled || propertyDataBoostEnabled.GetValueOrDefault(tx.state) | ||
| partitions, err := tx.boTx.PartitionQueryWithOptions(ctx, stmt, partitionOptions, execOptions.QueryOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifying execOptions.QueryOptions.DataBoostEnabled directly can lead to unexpected side effects if the execOptions are reused elsewhere. It's safer to work with a copy of QueryOptions.
queryOptions := execOptions.QueryOptions
queryOptions.DataBoostEnabled = queryOptions.DataBoostEnabled || propertyDataBoostEnabled.GetValueOrDefault(tx.state)
partitions, err := tx.boTx.PartitionQueryWithOptions(ctx, stmt, partitionOptions, queryOptions)
Support partitioned queries using only SQL statements.