-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
expression: implement the Coercibility() function #14739
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d87af2f
coercibility
qw4990 1a4c85f
Merge remote-tracking branch 'upstream/master' into coercibility
qw4990 86f3320
format
qw4990 690ee6c
make ci happy
qw4990 49cc0d1
make linter happy
qw4990 23ebafb
Merge remote-tracking branch 'upstream/master' into coercibility
qw4990 02f5aa7
add more test cases
qw4990 3947ef8
address comments
qw4990 c9ef591
Merge branch 'master' into coercibility
sre-bot 64117c8
Merge branch 'master' into coercibility
qw4990 3100a92
Merge branch 'master' into coercibility
qw4990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2020 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package expression | ||
|
||
import ( | ||
"github.com/pingcap/parser/ast" | ||
"github.com/pingcap/tidb/types" | ||
) | ||
|
||
type coercibility struct { | ||
val Coercibility | ||
init bool | ||
} | ||
|
||
func (c *coercibility) hasCoercibility() bool { | ||
return c.init | ||
} | ||
|
||
func (c *coercibility) value() Coercibility { | ||
return c.val | ||
} | ||
|
||
// SetCoercibility implements CollationExpr SetCoercibility interface. | ||
func (c *coercibility) SetCoercibility(val Coercibility) { | ||
c.val = val | ||
c.init = true | ||
} | ||
|
||
// CollationExpr contains all interfaces about dealing with collation. | ||
type CollationExpr interface { | ||
// Coercibility returns the coercibility value which is used to check collations. | ||
Coercibility() Coercibility | ||
|
||
// SetCoercibility sets a specified coercibility for this expression. | ||
SetCoercibility(val Coercibility) | ||
} | ||
|
||
// Coercibility values are used to check whether the collation of one item can be coerced to | ||
// the collation of other. See https://dev.mysql.com/doc/refman/8.0/en/charset-collation-coercibility.html | ||
type Coercibility int | ||
|
||
const ( | ||
// CoercibilityExplicit is derived from an explicit COLLATE clause. | ||
CoercibilityExplicit Coercibility = 0 | ||
// CoercibilityNone is derived from the concatenation of two strings with different collations. | ||
CoercibilityNone Coercibility = 1 | ||
// CoercibilityImplicit is derived from a column or a stored routine parameter or local variable. | ||
CoercibilityImplicit Coercibility = 2 | ||
// CoercibilitySysconst is derived from a “system constant” (the string returned by functions such as USER() or VERSION()). | ||
CoercibilitySysconst Coercibility = 3 | ||
// CoercibilityCoercible is derived from a literal. | ||
CoercibilityCoercible Coercibility = 4 | ||
// CoercibilityNumeric is derived from a numeric or temporal value. | ||
CoercibilityNumeric Coercibility = 5 | ||
// CoercibilityIgnorable is derived from NULL or an expression that is derived from NULL. | ||
CoercibilityIgnorable Coercibility = 6 | ||
) | ||
|
||
var ( | ||
sysConstFuncs = map[string]struct{}{ | ||
ast.User: {}, | ||
ast.Version: {}, | ||
ast.Database: {}, | ||
ast.CurrentRole: {}, | ||
ast.CurrentUser: {}, | ||
} | ||
) | ||
|
||
func deriveCoercibilityForScarlarFunc(sf *ScalarFunction) Coercibility { | ||
if _, ok := sysConstFuncs[sf.FuncName.L]; ok { | ||
return CoercibilitySysconst | ||
} | ||
if !types.IsString(sf.RetType.Tp) { | ||
return CoercibilityNumeric | ||
} | ||
coer := CoercibilityIgnorable | ||
for _, arg := range sf.GetArgs() { | ||
if arg.Coercibility() < coer { | ||
coer = arg.Coercibility() | ||
} | ||
} | ||
return coer | ||
} | ||
|
||
func deriveCoercibilityForConstant(c *Constant) Coercibility { | ||
if c.Value.IsNull() { | ||
return CoercibilityIgnorable | ||
} else if !types.IsString(c.RetType.Tp) { | ||
return CoercibilityNumeric | ||
} | ||
return CoercibilityCoercible | ||
} | ||
|
||
func deriveCoercibilityForColumn(c *Column) Coercibility { | ||
if !types.IsString(c.RetType.Tp) { | ||
return CoercibilityNumeric | ||
} | ||
return CoercibilityImplicit | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
Bytes
, the collation isbinary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After testing, it seems ok to use this function since the
Tp
ofBytes(binary)
inTiDB
is 254(TypeString
) now.