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

main: update optgen Op alias to match current memo group member #94112

Merged
merged 1 commit into from
Dec 22, 2022

Conversation

msirek
Copy link
Contributor

@msirek msirek commented Dec 21, 2022

Fixes #94042

This fixes a slightly confusing feature of optgen where an alias in a match rule is assigned to the first expression in the group, and may be of a different expression type than the allowed Ops in the match rule.

Example:

[GenerateStreamingGroupByLimitOrderingHint, Explore]
(Limit
    $aggregation:(GroupBy | DistinctOn

Given this match rule snippet, it looks like $aggregation can only be a GroupBy or DistinctOn expression. But in #94042, it is sometimes a Select expression. The variable is later used to generate a new expression with the assumption it is one of the allowed Op types, causing a panic.

This is fixed by updating the optgen rule gen code to keep the alias up-to-date with the current memo group member being examined in the match rule, as it cycles through all members in the group.

Release note: None

@msirek msirek requested a review from DrewKimball December 21, 2022 23:25
@msirek msirek requested a review from a team as a code owner December 21, 2022 23:25
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@msirek
Copy link
Contributor Author

msirek commented Dec 21, 2022

The generated code looks like this with the fix:

	// [GenerateStreamingGroupByLimitOrderingHint]
	{
		_partlyExplored := _rootOrd < _rootState.start
		aggregation := _root.Input
		_state := _e.lookupExploreState(aggregation)
		if !_state.fullyExplored {
			_fullyExplored = false
		}
		var _member memo.RelExpr
		for _ord := 0; _ord < _state.end; _ord++ {
			if _member == nil {
				_member = aggregation.FirstExpr()
			} else {
				_member = _member.NextExpr()
			}
			aggregation = _member
			if !_partlyExplored || _ord >= _state.start {
				if _member.Op() == opt.GroupByOp || _member.Op() == opt.DistinctOnOp {
...
...

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice fix!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

Fixes cockroachdb#94042

This fixes a slightly confusing feature of optgen where an alias in
a match rule is assigned to the first expression in the group, and
may be of a different expression type than the allowed Ops in the
match rule.

Example:
```
[GenerateStreamingGroupByLimitOrderingHint, Explore]
(Limit
    $aggregation:(GroupBy | DistinctOn
```
Given this match rule, it looks like `$aggregation` can only be a
GroupBy or DistinctOn expression. But in cockroachdb#94042, it is sometimes a
Select expression. The variable is later used to generate a new
expression with the assumption it is one of the allowed Op types,
causing a panic.

This is fixed by updating the optgen rule gen code to keep the alias
up-to-date with the current memo group member being examined in the
match rule, as it cycles through all members in the group.

Release note: None
Copy link
Contributor Author

@msirek msirek left a comment

Choose a reason for hiding this comment

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

TFTR!
bors r=DrewKimball

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale)

@craig
Copy link
Contributor

craig bot commented Dec 22, 2022

Build succeeded:

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.

roachtest: activerecord failed
3 participants