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

Monotonic select #18546

Merged
merged 10 commits into from
Apr 19, 2023
Merged

Monotonic select #18546

merged 10 commits into from
Apr 19, 2023

Conversation

frankmcsherry
Copy link
Contributor

Demonstration of one approach to introducing monotonicity for SELECT queries, by informing LIR plans of consolidation that when performed should result in collections with non-negative updates.

Motivation

Tips for reviewer

Checklist

  • This PR has adequate test coverage / QA involvement has been duly considered.
  • This PR has an associated up-to-date design doc, is a design doc (template), or is sufficiently small to not require a design.
  • This PR evolves an existing $T ⇔ Proto$T mapping (possibly in a backwards-incompatible way) and therefore is tagged with a T-proto label.
  • If this PR will require changes to cloud orchestration, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label (example).
  • This PR includes the following user-facing behavior changes:

@CLAassistant
Copy link

CLAassistant commented Apr 1, 2023

CLA assistant check
All committers have signed the CLA.

@frankmcsherry frankmcsherry force-pushed the monotonic_select branch 2 times, most recently from f2ba0d7 to 3755c60 Compare April 1, 2023 19:30
@vmarcos
Copy link
Contributor

vmarcos commented Apr 3, 2023

An evaluation of the first two commits in this PR can be found in this spreadsheet. In contrast to the evaluation of PR #14607, we included here a few additional queries with TopK after Reduce, which this PR can convert to monotonic evaluation while the former one could not. The additional queries were adapted from the definitions in epic #16698. They included some scalar expressions to compute the input to Reduce in them, which we are not so good at, so a second pass at the numbers could be taken where we compute single-attribute aggregates to focus on the speedups obtainable for the core operators in focus here.

@vmarcos
Copy link
Contributor

vmarcos commented Apr 4, 2023

Important note for follow-up work: The plans produced here still sometimes set must_consolidate to true when not strictly necessary. Consider the following plan, obtained by forcing single-timestamp planning in sequence_explain_plan, for a one-shot SELECT:

materialize=> explain physical plan for
select
    sum(revenue), count(l_orderkey)
from (
    select l_orderkey,
           sum(l_extendedprice *
             (1 - l_discount)) as revenue
    from lineitem
    group by l_orderkey
    order by revenue desc 
    limit 10);
                                                    Physical Plan                                                    
---------------------------------------------------------------------------------------------------------------------
 Explained Query:                                                                                                   +
   Return                                                                                                           +
     Union                                                                                                          +
       ArrangeBy                                                                                                    +
         input_key=[]                                                                                               +
         raw=true                                                                                                   +
         Get::PassArrangements l0                                                                                   +
           raw=false                                                                                                +
           arrangements[0]={ key=[], permutation=id, thinning=(#0, #1) }                                            +
       Mfp                                                                                                          +
         project=(#0, #1)                                                                                           +
         map=(null, 0)                                                                                              +
         Union                                                                                                      +
           Negate                                                                                                   +
             Get::Arrangement l0                                                                                    +
               project=()                                                                                           +
               key=                                                                                                 +
               raw=false                                                                                            +
               arrangements[0]={ key=[], permutation=id, thinning=(#0, #1) }                                        +
           Constant                                                                                                 +
             - ()                                                                                                   +
   With                                                                                                             +
     cte l0 =                                                                                                       +
       Reduce::Accumulable                                                                                          +
         simple_aggrs[0]=(0, 0, sum(#0))                                                                            +
         simple_aggrs[1]=(1, 1, count(*))                                                                           +
         val_plan                                                                                                   +
           project=(#0, #1)                                                                                         +
           map=(true)                                                                                               +
         key_plan                                                                                                   +
           project=()                                                                                               +
         TopK::MonotonicTopK order_by=[#0 desc nulls_first] limit=10        must_consolidate                        +
                                                                                                                    +
           Mfp                                                                                                      +
             project=(#1)                                                                                           +
             input_key=#0                                                                                           +
             Reduce::Accumulable                                                                                    +
               simple_aggrs[0]=(0, 0, sum((#1 * (1 - #2))))                                                         +
               val_plan                                                                                             +
                 project=(#3)                                                                                       +
                 map=((#1 * (1 - #2)))                                                                              +
               key_plan                                                                                             +
                 project=(#0)                                                                                       +
               Get::Arrangement tpch.public.lineitem                                                                +
                 project=(#0, #5, #6)                                                                               +
                 key=#0, #3                                                                                         +
                 raw=false                                                                                          +
                 arrangements[0]={ key=[#0, #3], permutation={#1: #2, #2: #3, #3: #1}, thinning=(#1, #2, #4..=#15) }+
                                                                                                                    +
 Used Indexes:                                                                                                      +
   - tpch.public.pk_lineitem_orderkey_linenumber                                                                    +
 
(1 row)

Above, we see that must_consolidate is set for the TopK::MonotonicTopK operator even though its input is coming from a Reduce::Accumulable. Since the latter already requires arranged data, we would not need to reconsolidate the data in the former.

@vmarcos vmarcos force-pushed the monotonic_select branch 2 times, most recently from 99302cb to 16933dd Compare April 5, 2023 12:05
@vmarcos
Copy link
Contributor

vmarcos commented Apr 5, 2023

I tried to schedule SQL-related nightly runs for this PR; however, all of the errors found are also happening in main at the moment, and none appear to be related to the PR.

@vmarcos vmarcos force-pushed the monotonic_select branch 3 times, most recently from e0f5221 to 8895d80 Compare April 6, 2023 10:18
Copy link
Member

@antiguru antiguru left a comment

Choose a reason for hiding this comment

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

Suggesting changes for an explain oversight!

src/compute-client/src/explain/text.rs Outdated Show resolved Hide resolved
src/compute-client/src/explain/text.rs Outdated Show resolved Hide resolved
src/compute-client/src/explain/text.rs Outdated Show resolved Hide resolved
@vmarcos vmarcos force-pushed the monotonic_select branch 2 times, most recently from b09d5c8 to 24d6932 Compare April 6, 2023 16:21
@vmarcos
Copy link
Contributor

vmarcos commented Apr 6, 2023

@aalexandrov @teskje @jkosh44 I am tagging you as reviewers for this PR. The PR is an MVP for the feature of exploiting monotonic operator variants in single-time queries (i.e., one-shot SELECTs currently). There are a few items that I'd like to explicitly leave to follow-up work, if you agree, namely:

  1. Setting must_consolidate to false in one-shot SELECTs where consolidation in the monotonic operator is unnecessary (for an example, see Monotonic select #18546 (comment)).
  2. Extended validation and improvements of EXPLAIN output. Now, EXPLAIN PHYSICAL PLAN will not show the plan produced for a one-shot SELECT with the monotonic operators. Even when that is fixed, MIR-level EXPLAIN will for TopK say that monotonic=false, but the physical plan will differ in the actual operator choice. This follow-up work should be enabled by EXPLAIN pipeline should reflect different optimization paths #18089.
  3. Fixing of our tests to ensure that we cover both non-monotonic and monotonic operator choices in CI. Right now, the PR should be inert in that respect since we are gating the changes behind a feature flag, turned off by default. But if we ever decide to flip this feature flag on and eventually retire it, we need a permanent solution for ensuring that our tests exercise the different operator variants.

I can create follow-up issues for (1) and (3) above if you are in agreement. Even in its current version, we can already measure quite sizable query processing times gains for some queries (when the feature flag is turned on).

@frankmcsherry Hope that it is OK with you to initiate review for this PR; otherwise, please let us know what other strategy to take!

@vmarcos vmarcos marked this pull request as ready for review April 6, 2023 17:13
@vmarcos vmarcos requested a review from a team as a code owner April 6, 2023 17:13
@vmarcos vmarcos requested a review from a team April 6, 2023 17:13
Copy link
Contributor

@jkosh44 jkosh44 left a comment

Choose a reason for hiding this comment

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

Surfaces changes LGTM

@vmarcos
Copy link
Contributor

vmarcos commented Apr 10, 2023

Just to be on the safe side, I scheduled a slow SQL Logic Test run on commit 8895d80, prior to introducing the feature flag. No errors were reported, so it looks like doing operator selection for one-shot SELECTs as in this PR passes this first level of validation: https://buildkite.com/materialize/sql-logic-tests/builds/5507.

@frankmcsherry
Copy link
Contributor Author

I've been told this may be blocked on me! It shouldn't; if it's good to go, go for it!

Copy link
Contributor

@teskje teskje left a comment

Choose a reason for hiding this comment

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

LGTM! I was wondering about the risk involved with removing those "always consolidates". But with the ensure_consolidates we won't produce incorrect data, so that seems fine.

}
}
let plan = self.finalize_dataflow(dataflow, instance)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

It feels very brittle that we have to rely on nobody modifying the finalized plan anymore for correctness. The "lock down the COMPUTE interface" part of #18496 can solve this, but I wonder if there is a way to short-term ensure that plan is not modified accidentally.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree this is something to give some thought to as part of #18496, but I am afraid that trying to address this locally might exceed the scope of this PR. Still, we are introducing an unwritten API requirement here that until needs to be assigned prior to calling Coordinator::finalize_dataflow, which was not previously present. I wrote down this extra requirement now in the docstring of Coordinator::finalize_dataflow.

src/compute-client/src/plan/mod.rs Show resolved Hide resolved
Copy link
Contributor

@aalexandrov aalexandrov left a comment

Choose a reason for hiding this comment

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

Looks good overall, I have some minor questions / nits about naming and code organization.

src/compute-client/src/plan/top_k.proto Show resolved Hide resolved
src/compute-client/src/plan/top_k.rs Outdated Show resolved Hide resolved
src/compute-client/src/plan/mod.rs Show resolved Hide resolved
src/compute-client/src/plan/mod.rs Outdated Show resolved Hide resolved
src/compute-client/src/plan/mod.rs Outdated Show resolved Hide resolved
}

let debug_name = self.debug_name.to_string();
let (collection, errs) = collection.ensure_monotonic(move |data, diff| {
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought that this check was added in #18415.

Copy link
Contributor

Choose a reason for hiding this comment

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

The check was added by #18140 to both monotonic top-1 and to monotonic reduce; however, it was not added there to monotonic top-k. This is because monotonic top-k does not need to use explode_one, and adding explode_one to the other two cases was the intended change of #18140.

So the present PR complements that work and makes sure that we have an ensure_monotonic check in all of our monotonic rendering paths.

src/compute/src/render/top_k.rs Outdated Show resolved Hide resolved
.map_err(AdapterError::Internal)
Plan::<mz_repr::Timestamp>::finalize_dataflow(
dataflow,
self.catalog()
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess without tweaking the dataflow construction logic this is not sufficient for triggering the refine_single_time_dataflow call.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is correct. I did not change the setting of until here for dataflow, because otherwise, we'd show one-shot SELECT plans on EXPLAIN PHYSICAL PLAN when the feature flag is enabled. I propose that we leave figuring out the right output to produce for EXPLAIN as part of #18089.

Frank McSherry and others added 9 commits April 18, 2023 22:18
This commit fixes the invalid accumulation tests in the mzcompose cluster test workflows to use non-monotonic rendering whenever a min/max aggregate is employed. This is ensured by use of materialized views instead of one-off SELECTs in the tests, since the latter now exploit logical monotonicity.
This commit fixes the negative multiplicities tests in testdrive to use non-monotonic rendering whenever a min/max aggregate is employed. This is ensured by use of indexed views instead of one-off SELECTs in the tests, since the latter now exploit logical monotonicity.
This commit adds a monotonicity check with the EnsureMonotonic operator to the rendering of monotonic top-k. This step increases the consistency of the rendering code across monotonic top-k, monotonic top-1, and monotonic hierarchical reductions.
This commit introduces the feature flag enable_monotonic_oneshot_selects, turned off by default, and gates single-time operator selection to use monotonic plans for one-shot SELECTs behind this flag. As a consequence, after this commit, all tests in CI will by default execute against non-monotonic plans, as previously. The commit enables us to carefully evaluate when to make the functionality of monotonic plans for one-shot SELECTs available by default.
This commit includes code improvements to refactor plan refinements performed at the end of Plan::finalize_dataflow into separate functions, improving readability. In addition, the commit: (a) makes explicit in the documentation of the API of Coordinator::finalize_dataflow the new requirement to set the until field of the dataflow, and (b) adds a method to DataflowDescription to test if a dataflow refers to a single timestamp.
This commit refactors the method flag_snapshot added in connection with single-time dataflow refinement to a more general as_monotonic conversion method. The as_monotonic name reflects better the intent to upgrade plans to monotonic, but can also be used uniformly to set consolidation requirements. The commit also includes an as_monotonic method in CollationPlan, making the usage in single-time dataflow refinement more consistent.
This commit introduces the consolidate_named_if method in CollectionExt that allows us to conditionally consolidate a differential dataflow collection. The method is used in rendering in monotonic operators, which take a must_consolidate flag to indicate whether their input requires a consolidation step to turn logical into physical monotonicity. The method allows the uses in rendering to reduce code duplication and avoid mutability in collection assignments.
Copy link
Contributor

@teskje teskje left a comment

Choose a reason for hiding this comment

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

Still LGTM! Thanks for the readability improvements!

src/compute-client/src/plan/mod.rs Outdated Show resolved Hide resolved
src/compute-client/src/plan/mod.rs Show resolved Hide resolved
This commit adds instrumentation to finalize_dataflow so that lowering from MIR to LIR as well as subsequent plan refinements can be observed through EXPLAIN OPTIMIZER TRACE. As part of the commit, the lowering step is abstracted into a separate function and the physical plan explain stage is renamed to finalize_dataflow, where mir_to_lir is but one of its components.
Copy link
Contributor

@ggevay ggevay left a comment

Choose a reason for hiding this comment

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

LGTM!

@vmarcos
Copy link
Contributor

vmarcos commented Apr 19, 2023

Thank you all for the reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants