Repository navigation
feat: rewrite cross-shard HAVING queries and enable post merge filtering #1841
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
Open
+868
−65
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
5363d1e
init
murex971 98291d0
improvements
murex971 718ff44
use arc for having rewrite plan
murex971 9b68185
store rewritten aggreate in post route plan
murex971 f24c437
nits
murex971 467946d
tests
murex971 611347a
use option
murex971 7f92f25
redo argh
murex971 9c6fea4
wrap option
murex971 dbc9596
Merge remote-tracking branch 'origin/project-rewrite-refactor' into m…
murex971 9ac875e
arc clone
murex971 773bb04
remove agg
murex971 516b41c
fix: reject cross-shard HAVING with an explicit error
murex971 9c658b3
Merge branch 'having-reject' into murex-having-support
murex971 f442f6e
cleanup
murex971 5af57ae
Merge remote-tracking branch 'origin/main' into having-reject
murex971 05ae025
Merge remote-tracking branch 'origin/having-reject' into murex-having…
murex971 d9348d5
trim
murex971 ea091cc
Merge remote-tracking branch 'origin/main' into murex-having-support
murex971 8b1d4cc
feat: evaluate HAVING on merged cross-shard rows
murex971 c03b0fe
Merge branch 'having-execution' into murex-having-support
murex971 3d88225
resolve comments
murex971 550fddf
Merge remote-tracking branch 'origin/main' into having-execution
murex971 c35e0f9
fmt
murex971 b74027d
Merge branch 'having-execution' into murex-having-support
murex971 27d0f4b
Merge remote-tracking branch 'origin/main' into murex-having-support
murex971 9c9786a
fixes
murex971 99059ba
helper merge
murex971 31a63ed
refactor
murex971 8796c37
testing
murex971 a06c0ee
more tests
murex971 0b29088
more cases
murex971 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,171 @@ | ||
| use crate::setup::{admin_sqlx, connections_sqlx, connections_tokio}; | ||
| use sqlx::{Executor, Row, postgres::PgPool}; | ||
|
|
||
| #[tokio::test] | ||
| async fn having_filters_merged_groups() -> Result<(), Box<dyn std::error::Error>> { | ||
| let sharded = connections_sqlx().await.get(1).cloned().unwrap(); | ||
| reset_table(&sharded).await?; | ||
|
|
||
| seed( | ||
| &sharded, | ||
| 0, | ||
| "INSERT INTO having_merge_test VALUES (1, 10), (1, 11), (2, 12)", | ||
| ) | ||
| .await?; | ||
| seed( | ||
| &sharded, | ||
| 1, | ||
| "INSERT INTO having_merge_test VALUES (1, 20), (2, 21)", | ||
| ) | ||
| .await?; | ||
|
|
||
| let rows = sharded | ||
| .fetch_all( | ||
| "SELECT customer_id, COUNT(*) AS order_count \ | ||
| FROM having_merge_test \ | ||
| GROUP BY customer_id \ | ||
| HAVING COUNT(*) > 2 \ | ||
|
murex971 marked this conversation as resolved.
Resolved
|
||
| ORDER BY customer_id", | ||
| ) | ||
| .await?; | ||
|
|
||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(rows[0].columns().len(), 2); | ||
| assert_eq!(rows[0].get::<i64, _>("customer_id"), 1); | ||
| assert_eq!(rows[0].get::<i64, _>("order_count"), 3); | ||
|
|
||
| // An untyped literal takes the type of the aggregate it is compared with. | ||
| let rows = sharded | ||
| .fetch_all( | ||
| "SELECT customer_id FROM having_merge_test \ | ||
| GROUP BY customer_id HAVING MAX(order_id) > '20'", | ||
| ) | ||
| .await?; | ||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(rows[0].get::<i64, _>("customer_id"), 2); | ||
|
|
||
| cleanup(&sharded).await; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn prepared_having_parameter_typed_and_untyped() -> Result<(), Box<dyn std::error::Error>> { | ||
| let sharded = connections_sqlx().await.get(1).cloned().unwrap(); | ||
| reset_table(&sharded).await?; | ||
|
|
||
| seed( | ||
| &sharded, | ||
| 0, | ||
| "INSERT INTO having_merge_test VALUES (1, 10), (1, 11)", | ||
| ) | ||
| .await?; | ||
| seed( | ||
| &sharded, | ||
| 1, | ||
| "INSERT INTO having_merge_test VALUES (1, 20), (2, 21)", | ||
| ) | ||
| .await?; | ||
|
|
||
| let rows = sqlx::query( | ||
| "SELECT customer_id, COUNT(*) AS order_count \ | ||
| FROM having_merge_test \ | ||
| GROUP BY customer_id \ | ||
| HAVING COUNT(*) > $1", | ||
| ) | ||
| .bind(2_i64) | ||
| .fetch_all(&sharded) | ||
| .await?; | ||
|
|
||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(rows[0].columns().len(), 2); | ||
| assert_eq!(rows[0].get::<i64, _>("customer_id"), 1); | ||
| assert_eq!(rows[0].get::<i64, _>("order_count"), 3); | ||
|
murex971 marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Parse without parameter types, so PostgreSQL has to infer $1. | ||
| let client = connections_tokio().await.remove(1); | ||
| let statement = client | ||
| .prepare( | ||
| "SELECT customer_id FROM having_merge_test \ | ||
| GROUP BY customer_id HAVING COUNT(*) > $1", | ||
| ) | ||
| .await?; | ||
| let rows = client.query(&statement, &[&2_i64]).await?; | ||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(rows[0].get::<_, i64>(0), 1); | ||
|
|
||
| cleanup(&sharded).await; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn having_rejects_shard_local_steps() -> Result<(), Box<dyn std::error::Error>> { | ||
| let sharded = connections_sqlx().await.get(1).cloned().unwrap(); | ||
| reset_table(&sharded).await?; | ||
| let client = connections_tokio().await.remove(1); | ||
|
|
||
| // Each of these would run on shards before PgDog merges the groups. | ||
| for sql in [ | ||
| "SELECT * FROM having_merge_test GROUP BY customer_id, order_id HAVING COUNT(*) > 0", | ||
| "SELECT customer_id FROM having_merge_test GROUP BY customer_id \ | ||
| HAVING COUNT(*) > 0 ORDER BY customer_id LIMIT 1", | ||
| "SELECT customer_id FROM having_merge_test GROUP BY customer_id \ | ||
| HAVING COUNT(*) > 0 OFFSET 1", | ||
| "SELECT DISTINCT ON (customer_id) customer_id FROM having_merge_test \ | ||
| GROUP BY customer_id HAVING COUNT(*) > 0", | ||
| "SELECT customer_id AS c FROM having_merge_test GROUP BY c HAVING COUNT(*) > 0", | ||
| "SELECT customer_id, COUNT(*) + 1 FROM having_merge_test \ | ||
| GROUP BY customer_id HAVING COUNT(*) > 0", | ||
| "EXECUTE having_prepared(0)", | ||
| ] { | ||
| if sql.starts_with("EXECUTE") { | ||
| client | ||
| .simple_query( | ||
| "PREPARE having_prepared AS SELECT customer_id FROM having_merge_test \ | ||
| GROUP BY customer_id HAVING COUNT(*) > $1", | ||
| ) | ||
| .await?; | ||
| } | ||
| let error = client.simple_query(sql).await.unwrap_err(); | ||
| let message = error.as_db_error().map(|error| error.message()); | ||
| assert!( | ||
| message.is_some_and(|message| message.contains("cross-shard HAVING is unsupported")), | ||
| "{sql}: {error}" | ||
| ); | ||
| } | ||
| client.simple_query("SELECT 1").await?; | ||
|
|
||
| cleanup(&sharded).await; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn reset_table(pool: &PgPool) -> Result<(), sqlx::Error> { | ||
| cleanup(pool).await; | ||
| for shard in [0, 1] { | ||
| pool.execute( | ||
| format!( | ||
| "/* pgdog_shard: {shard} */ \ | ||
| CREATE TABLE having_merge_test(customer_id BIGINT, order_id BIGINT)" | ||
| ) | ||
| .as_str(), | ||
| ) | ||
| .await?; | ||
| } | ||
| admin_sqlx().await.execute("RELOAD").await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn seed(pool: &PgPool, shard: usize, statement: &str) -> Result<(), sqlx::Error> { | ||
| pool.execute(format!("/* pgdog_shard: {shard} */ {statement}").as_str()) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| async fn cleanup(pool: &PgPool) { | ||
| for shard in [0, 1] { | ||
| pool.execute( | ||
| format!("/* pgdog_shard: {shard} */ DROP TABLE IF EXISTS having_merge_test").as_str(), | ||
| ) | ||
| .await | ||
| .ok(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is missing a lot of coverage for other kinds of queries