Skip to content
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

feat(optimizer): improve scalar subqueries optimization time #16966

Merged
merged 4 commits into from
May 29, 2024

Conversation

chenzl25
Copy link
Contributor

I hereby agree to the terms of the RisingWave Labs, Inc. Contributor License Agreement.

What's changed and what's your intention?

  • Resolve Improve: The optimization time grows exponentially as the number of subqueries increases #16952
  • Multi-scalar subqueries are very common queries written by users. However, in some special cases, it can lead to an exponential increase in optimization time as the number of scalar subqueries grows. The root cause is that multi-scalar subqueries are constructed as a chain of Apply. As a result, it is hard to find the original domain for the upper Apply operator. In this PR, we try to translate the Apply in a top-down order and simplify the domain by extracting the domain from the apply input. In this way, optimization time could linearly increase as the number of scalar subqueries grows.

Checklist

  • I have written necessary rustdoc comments
  • I have added necessary unit tests and integration tests
  • I have added test labels as necessary. See details.
  • I have added fuzzing tests or opened an issue to track them. (Optional, recommended for new SQL features Sqlsmith: Sql feature generation #7934).
  • My PR contains breaking changes. (If it deprecates some features, please create a tracking issue to remove them in the future).
  • All checks passed in ./risedev check (or alias, ./risedev c)
  • My PR changes performance-critical code. (Please run macro/micro-benchmarks and show the results.)
  • My PR contains critical fixes that are necessary to be merged into the latest release. (Please check out the details)

Documentation

  • My PR needs documentation updates. (Please use the Release note section below to summarize the impact on users)

Release note

If this PR includes changes that directly affect users or other significant modifications relevant to the community, kindly draft a release note to provide a concise summary of these changes. Please prioritize highlighting the impact these changes will have on users.

Comment on lines 313 to 339
if let Some(join) = plan.as_logical_join() {
Self::rewrite_join(
join,
left_idxs,
offset,
index_mapping,
data_types,
index,
)
} else if let Some(apply) = plan.as_logical_apply() {
Self::rewrite_apply(
apply,
left_idxs,
offset,
index_mapping,
data_types,
index,
)
} else if let Some(scan) = plan.as_logical_scan() {
Self::rewrite_scan(
scan,
left_idxs,
offset,
index_mapping,
data_types,
index,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these lines duplicated with the lambda rewrite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me refactor that. Thanks for pointing it out.

Copy link
Member

@fuyufjh fuyufjh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

└─BatchExchange { order: [], dist: HashShard(t.arr) }
└─BatchScan { table: t, columns: [t.arr], distribution: SomeShard }
└─BatchExchange { order: [], dist: HashShard(t.arr) }
└─BatchScan { table: t, columns: [t.arr], distribution: SomeShard }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why this plan gets improved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the initial logical plan of this case is:

 LogicalProject { exprs: [t.x, t.arr, unnest, ordinality, unnest, ordinality] }
 └─LogicalApply { type: Inner, on: true, correlated_id: 1 }
   ├─LogicalApply { type: Inner, on: true, correlated_id: 2 }
   │ ├─LogicalScan { table: t, columns: [x, arr, _row_id] }
   │ └─LogicalTableFunction { table_function: Unnest(CorrelatedInputRef { index: 1, correlated_id: 2 }) }
   └─LogicalTableFunction { table_function: Unnest(CorrelatedInputRef { index: 1, correlated_id: 1 }) }

It is the same shape as the multi scalar subqueries.

@chenzl25 chenzl25 enabled auto-merge May 29, 2024 05:22
@chenzl25 chenzl25 added this pull request to the merge queue May 29, 2024
Merged via the queue into main with commit bd6454e May 29, 2024
27 of 28 checks passed
@chenzl25 chenzl25 deleted the dylan/improve_scalar_subquery_optimization_time branch May 29, 2024 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve: The optimization time grows exponentially as the number of subqueries increases
2 participants