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

plan: detect table alias duplication for 'JOIN' #6716

Merged
merged 14 commits into from
Jun 5, 2018

Conversation

spongedu
Copy link
Contributor

fix #3908

plan/errors.go Outdated
@@ -45,6 +45,7 @@ const (
codeDupFieldName = mysql.ErrDupFieldName
codeNonUpdatableTable = mysql.ErrNonUpdatableTable
codeInternal = mysql.ErrInternal
codeNonUniqTable = terror.ErrCode(mysql.ErrNonuniqTable)
Copy link
Member

Choose a reason for hiding this comment

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

No need to wrap the terror.ErrCode()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need indeed. will fix

plan/errors.go Outdated
@@ -75,6 +76,7 @@ var (
ErrDupFieldName = terror.ClassOptimizer.New(codeDupFieldName, mysql.MySQLErrName[mysql.ErrDupFieldName])
ErrNonUpdatableTable = terror.ClassOptimizer.New(codeNonUpdatableTable, mysql.MySQLErrName[mysql.ErrNonUpdatableTable])
ErrInternal = terror.ClassOptimizer.New(codeInternal, mysql.MySQLErrName[mysql.ErrInternal])
ErrNonUniqTable = terror.ClassOptimizer.New(codeNonUniqTable, "Not unique table/alias: '%s'")
Copy link
Member

Choose a reason for hiding this comment

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

Can we use mysql.MySQLErrName[mysql. ErrNonuniqTable]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep

p.isParentJoin = true
}

func isTableAliasDuplicate(node ast.ResultSetNode, tableAliases map[string]interface{}) error {
Copy link
Member

Choose a reason for hiding this comment

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

How about s/isTableAliasDuplicate/checkDuplicatedTableAlias/, and making it a member function of p?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isTableAliasDuplicate is an utility function, I name it follow the other utility functions in preprocess.go such as isConstraintKeyTp, isInvalidDefaultValue, isIncorrectName. I think it's better to keep this name and make it out of p

Copy link
Member

Choose a reason for hiding this comment

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

ok

if len(ts.AsName.L) != 0 && exists {
return ErrNonUniqTable.GenByArgs(ts.AsName)
}
tableAliases[ts.AsName.L] = nil
Copy link
Member

Choose a reason for hiding this comment

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

I think we should only add ts.AsName.L into tableAliases only when len(ts.AsName.L) != 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter because there's nothing done if len(ts.AsName.L) == 0

case *ast.Join:
if len(p.tableAliasInJoin) > 0 {
p.tableAliasInJoin = p.tableAliasInJoin[:len(p.tableAliasInJoin)-1]
}
Copy link
Member

Choose a reason for hiding this comment

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

Reset isParentJoin to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isParentJoin works only in function Enter, there's no need to clear it in Leave

@@ -45,6 +45,9 @@ type preprocessor struct {
inPrepare bool
// inCreateOrDropTable is true when visiting create/drop table statement.
inCreateOrDropTable bool

tableAliasInJoin []map[string]interface{}
isParentJoin bool
Copy link
Member

Choose a reason for hiding this comment

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

How about s/isParentJoin/parentIsJoin/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -45,6 +45,9 @@ type preprocessor struct {
inPrepare bool
// inCreateOrDropTable is true when visiting create/drop table statement.
inCreateOrDropTable bool

tableAliasInJoin []map[string]interface{}
Copy link
Member

Choose a reason for hiding this comment

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

add a comment about this stack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@zz-jason zz-jason added the contribution This PR is from a community contributor. label Jun 1, 2018
@zz-jason
Copy link
Member

zz-jason commented Jun 4, 2018

LGTM

@zz-jason
Copy link
Member

zz-jason commented Jun 4, 2018

@tiancaiamao PTAL

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 4, 2018
@zz-jason
Copy link
Member

zz-jason commented Jun 4, 2018

/run-all-tests

@zz-jason
Copy link
Member

zz-jason commented Jun 5, 2018

@zimulala @winoros @lamxTyler PTAL


// tableAliasInJoin is a stack that keeps the table alias names for joins.
// len(tableAliasInJoin) may bigger than 1 because the left/right child of join may be subquery that contains `JOIN`
tableAliasInJoin []map[string]interface{}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder that is it really necessary to be a slice of map ?

@tiancaiamao
Copy link
Contributor

LGTM

@tiancaiamao tiancaiamao added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 5, 2018
@tiancaiamao tiancaiamao merged commit 006a628 into pingcap:master Jun 5, 2018
@zz-jason zz-jason added the type/bugfix This PR fixes a bug. label Jun 5, 2018
@spongedu spongedu deleted the 0531 branch June 5, 2018 07:51
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. status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to detect derived table name duplication for UNION statement
3 participants