Skip to content

Flag inputs to SELECT queries as monotonic if until is set. - #14607

Closed
frankmcsherry wants to merge 6 commits into
MaterializeInc:mainfrom
frankmcsherry:monotonic_select_inputs
Closed

frankmcsherry wants to merge 6 commits into
MaterializeInc:mainfrom
frankmcsherry:monotonic_select_inputs

Conversation

@frankmcsherry

Copy link
Copy Markdown
Contributor

This PR flags sources and indexes as monotonic if we are in a Peek setting and have successfully set until to be one greater than the query timestamp. In such a setting, we will ingest only the snapshot and no further modifications, which means no retractions. We can use simpler dataflow fragments in cases such as min, max, and top_k.

Motivation

  • This PR adds a feature that has not yet been specified.

One-off SELECT queries use dataflows that are more general than strictly required, and this is one example of how that can be relaxed.

Tips for reviewer

Checklist

@frankmcsherry

Copy link
Copy Markdown
Contributor Author

I have verified that this produces monotonic query plans manually, as I can't think of a good way to introspect on this (the explained plans are with non-monotonic inputs, which seems good as we are plausibly explaining what an installed view would look like). I'm going to try and get materialize to admit that the performance is better by using some otherwise fairly silly queries, and I'll report back about that.

@frankmcsherry

Copy link
Copy Markdown
Contributor Author

Also, just for further commentary, setting the inputs to monotonic is probably not the best answer here, as while true it won't unlock all of the good query plans, as some operators like Reduce with aggregates (i.e. not Distinct) are marked as non-monotonic on monotonic inputs (e.g. the count could increase). However, in a snapshot setting, their outputs would be monotonic (again, as with only one time there are not retractions).

@frankmcsherry

Copy link
Copy Markdown
Contributor Author

@antiguru observes that this may be DOA as we are not assured that the data we get are consolidated, and we cannot afford to consolidate the data as it arrives (real append-only sources benefit by never needing their contents resident at once).

Closing for now!

@frankmcsherry

Copy link
Copy Markdown
Contributor Author

Notes for future selves: it is plausible that an Arrangement could provide consolidated output, using a mechanism other than "decant each batch independently". Instead, given all the batches it could enumerate them as if a Cursor, producing the correct total for each record before emitting it. This is not the current behavior though, so all the more reason not to do this.

@vmarcos

vmarcos commented Mar 21, 2023

Copy link
Copy Markdown
Contributor

This PR can be a good starting point for work in epic MaterializeInc/database-issues#4125, including issue MaterializeInc/database-issues#5030.

@vmarcos vmarcos reopened this Mar 21, 2023
@vmarcos
vmarcos requested a review from a team as a code owner March 21, 2023 17:36
@vmarcos
vmarcos force-pushed the monotonic_select_inputs branch from e6b87f1 to e5dcc02 Compare March 21, 2023 17:37
@vmarcos
vmarcos marked this pull request as draft March 21, 2023 17:39
@vmarcos

vmarcos commented Mar 22, 2023 •

Copy link
Copy Markdown
Contributor

@antiguru observes that this may be DOA as we are not assured that the data we get are consolidated, and we cannot afford to consolidate the data as it arrives (real append-only sources benefit by never needing their contents resident at once).

Closing for now!

Just to make sure this observation gets recorded: We already had consolidate_named calls in the code previously for both min/max and top-1 monotonic renderings. As such, adding a similar consolidation for monotonic top-k maintains consistency for now, but is not the final state we'd like to be in, as observed above. Follow-up work needs to be carried out to take the responsibility of deciding when to consolidate out of rendering and into the planner, e.g., in the context of MaterializeInc/database-issues#5062. This future work can ensure that consolidation is not done for append-only sources, but is inserted for one-off SELECTs.

For this PR, we argue that consolidating in all monotonic variants increases consistency and moves us forward regarding one-off SELECT improvements.

@vmarcos

vmarcos commented Mar 23, 2023 •

Copy link
Copy Markdown
Contributor

An evaluation of the effect of this PR can be found in the linked spreadsheet. The setup is of a local developer machine running the TPC-H load generator without updates at scale factor 1. The queries were designed to hit min/max aggregates, top-1, and top-k. The vast majority of the queries evaluated get meaningful speedups, some of over an order of magnitude, as expected. On average across the 7 queries evaluated, the speedup is around 5x.

frankmcsherry and others added 5 commits March 27, 2023 13:10
This commit moves the code for monotonic top-k into its own method, mirroring what is done for monotonic top-1 and basic top-k. Additionally, some documentation is added to the related functions. This commit should not include any functional changes.
This commit ensures that monotonic top-k processing is resilient to logically monotonic input. This is achieved by consolidating the input to transform (assumed) logical monotonicity into physical monotonicity and ensuring that physical monotonicity was then attained. The code also makes comments related to this step in the rendering more consistent across monotonic top-k, monotonic top-1, and monotonic hierarchical reductions.
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 refactors the if let pattern that computes as_of + 1 in setting the until of a DataflowDescription in the case of one-off SELECT queries. The small refactor avoids explicit array access by going through the as_option method of Antichain.
@vmarcos

vmarcos commented Mar 27, 2023 •

Copy link
Copy Markdown
Contributor

@antiguru and I decided to run the slow SQL Logic Tests on this PR and got this output. Note that the only two tests that failed were due to the changes introduced in PR #18302. After rebasing to catch the changes in the latter PR, one of the tests was removed, and the other passed.

@vmarcos

vmarcos commented Mar 27, 2023

Copy link
Copy Markdown
Contributor

For further validation, I ran also a subset of the nightlies, namely all testdrive and SQLsmith ones along with a couple of others, and got this output. Here, we see that the only relevant errors reported by SQLsmith are instances of MaterializeInc/database-issues#5387.

From the test results, it appears that this PR is pretty safe. Additionally, we are using plans that can be produced for append-only sources already, except that we are making this plan usage more frequent by also including one-shot SELECTs. However, this PR might introduce an observability / performance debugging issue as the plans generated cannot be exposed currently by EXPLAIN. So to be conservative, I am going to make further changes here related to gating the generation of monotonic plans for one-off SELECTs behing a feature flag enable_monotonic_oneshot_selects. A discussion regarding the latter can be found in this Slack thread.

@CLAassistant

CLAassistant commented Mar 27, 2023 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This commit introduces the feature flag enable_monotonic_oneshot_selects, turned off by default, and gates the optimization of using 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.
@vmarcos
vmarcos force-pushed the monotonic_select_inputs branch from ca6a0e1 to fd85912 Compare March 28, 2023 10:27
/// Here, we observe that monotonicity implies that we do not need to maintain
/// state proportional to the size of the input collection, as loser rows
/// can never become part of the top-k. We apply an inter-timestamp thinning
/// step that discards losers rows as well as an inter-timestamp thinning step

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be "intra-timestamp", right?

@vmarcos

vmarcos commented Apr 19, 2023

Copy link
Copy Markdown
Contributor

I am closing this PR again, as we have agreed to merge #18546 instead.

@vmarcos vmarcos closed this Apr 19, 2023
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.

5 participants