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

planner: fix union statements order #8214

Merged
merged 2 commits into from
Nov 7, 2018
Merged

planner: fix union statements order #8214

merged 2 commits into from
Nov 7, 2018

Conversation

mtunique
Copy link
Contributor

@mtunique mtunique commented Nov 7, 2018

What problem does this PR solve?

Fix #8202 union statements order because union result schema info isn't correct.

What is changed and how it works?

Because union schema is the same to its first children. MySQL UNION Syntax

func (b *PlanBuilder) buildProjection4Union(u *LogicalUnionAll) {
	unionCols := make([]*expression.Column, 0, u.children[0].Schema().Len())

	// Infer union result types by its children's schema.
	for i, col := range u.children[0].Schema().Columns {
		resultTp := col.RetType
		for j := 1; j < len(u.children); j++ {
			childTp := u.children[j].Schema().Columns[i].RetType
			resultTp = unionJoinFieldType(resultTp, childTp)
		}
		unionCols = append(unionCols, &expression.Column{
			ColName:  col.ColName,
			TblName:  col.TblName,
			RetType:  resultTp,
			UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(),
		})
	}

But when mix UNION ALL and UNION DISTINCT in the same query, allSelectPlans is in front of unionDistinctPlan. When call buildUnionAll , the schema become incorrect. We should change the order, it should be same to SQL.

func (b *PlanBuilder) buildUnion(union *ast.UnionStmt) (LogicalPlan, error) {
	distinctSelectPlans, allSelectPlans, err := b.divideUnionSelectPlans(union.SelectList.Selects)
	if err != nil {
		return nil, errors.Trace(err)
	}

	unionDistinctPlan := b.buildUnionAll(distinctSelectPlans)
	if unionDistinctPlan != nil {
		unionDistinctPlan = b.buildDistinct(unionDistinctPlan, unionDistinctPlan.Schema().Len())
		if len(allSelectPlans) > 0 {
			allSelectPlans = append(allSelectPlans, unionDistinctPlan)
		}
	}
	unionAllPlan := b.buildUnionAll(allSelectPlans)

Check List

Tests

  • Unit test
    Related changes

  • Need to cherry-pick to the release branch

@sre-bot
Copy link
Contributor

sre-bot commented Nov 7, 2018

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added status/LGT1 Indicates that a PR has LGTM 1. contribution This PR is from a community contributor. type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels Nov 7, 2018
@zz-jason
Copy link
Member

zz-jason commented Nov 7, 2018

@lysu @winoros @eurekaka PTAL

@winoros
Copy link
Member

winoros commented Nov 7, 2018

@mtunique
You can use fix issue link grammer in your pr description so that issue can be auto closed when this pr is merged.
See https://help.github.com/articles/closing-issues-using-keywords/

@zz-jason
Copy link
Member

zz-jason commented Nov 7, 2018

@mtunique Should we cherrypick this PR to the release-2.1 and release-2.0 branch?

planner/core/logical_plan_test.go Show resolved Hide resolved
planner/core/logical_plan_test.go Outdated Show resolved Hide resolved
@mtunique
Copy link
Contributor Author

mtunique commented Nov 7, 2018

@zz-jason Maybe it's necessary.

@mtunique
Copy link
Contributor Author

mtunique commented Nov 7, 2018

@mtunique
You can use fix issue link grammer in your pr description so that issue can be auto closed when this pr is merged.
See https://help.github.com/articles/closing-issues-using-keywords/

Done.

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

lgtm

@winoros
Copy link
Member

winoros commented Nov 7, 2018

/run-all-tests

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@XuHuaiyu XuHuaiyu changed the title planner: Fix union statements order (#8202) planner: fix union statements order Nov 7, 2018
@XuHuaiyu XuHuaiyu merged commit deec48d into pingcap:master Nov 7, 2018
XuHuaiyu pushed a commit to XuHuaiyu/tidb that referenced this pull request Nov 8, 2018
@mtunique mtunique deleted the 8202 branch November 9, 2018 06:39
@XuHuaiyu
Copy link
Contributor

@mtunique
And we should also cherry-pick this to release-2.0.

It should be noted that, the directory structure planner/core in master corresponds to the directory plan in release-2.0.

@mtunique
Copy link
Contributor Author

@mtunique
And we should also cherry-pick this to release-2.0.

It should be noted that, the directory structure planner/core in master corresponds to the directory plan in release-2.0.

Done. #8318

mtunique added a commit to mtunique/tidb that referenced this pull request Nov 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. sig/planner SIG: Planner status/LGT1 Indicates that a PR has LGTM 1. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants