Skip to content

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

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
msirek:94042
Dec 22, 2022
Merged

main: update optgen Op alias to match current memo group member#94112
craig[bot] merged 1 commit intocockroachdb:masterfrom
msirek:94042

Conversation

@msirek
Copy link
Copy Markdown
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
Copy Markdown
Member

This change is Reviewable

@msirek
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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