-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Support \N shortcut for null #3943
Support \N shortcut for null #3943
Conversation
@hanfei1991 @tiancaiamao PTAL. |
parser/lexer.go
Outdated
|
||
if ch0 == '\\' { | ||
// support \N as shortcut for NULL | ||
if s.r.charByIndex(pos.Offset+1) == 'N' { |
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.
Why not use s.r.peek()?
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.
@shenli
Before using s.r.peek()
,s.r inc()
must be called first, because current offset is pointed to '\'.
I want to know if next element is 'N', and don't wanna change offset at the same time .
If next element is not 'N', and s.r.inc()
+ s.r.peek()
are called, we must back to previous offset, it is not convenient .
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.
simplely add initTokenString("\N", null)
in the ruleTable init.
I think you can simply add |
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.
LGTM
server/driver_tidb.go
Outdated
@@ -306,6 +307,10 @@ func (trs *tidbResultSet) Columns() ([]*ColumnInfo, error) { | |||
} | |||
var columns []*ColumnInfo | |||
for _, v := range fields { | |||
if v.ColumnAsName.O == "\\N" { | |||
v.ColumnAsName = model.NewCIStr("NULL") | |||
} |
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.
I think it's ugly to deal with it as a exception here. Would you consider renaming column here https://github.com/pingcap/tidb/blob/master/plan/logical_plan_builder.go#L433 ?
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.
@choleraehyq You are right, doing this work in driver_tidb.go is not good . I have changed it.
plan/logical_plan_builder.go
Outdated
@@ -431,6 +431,11 @@ func (b *planBuilder) buildProjection(p LogicalPlan, fields []*ast.SelectField, | |||
} | |||
} | |||
} | |||
|
|||
if colName.O == "\\N" { |
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.
Could you add a test case for this?
You can add a test case like select \N;
and check the column name and row value.
|
Hi, @yuanwhy
and reset changes in |
@choleraehyq in mysql , like this : mysql column name is "\N", but tidb is N . However, it is another issue , mysql> select "a"; I will add test case for |
@yuanwhy
MySQL: |
…y/tidb into support-shortcut-for-null
1、
this code in
That is, parser doesn't use 2、 I have changed code to fix below issue
And some test cases have been added, like :
|
fields, err = rs.Fields() | ||
c.Check(err, IsNil) | ||
c.Check(len(fields), Equals, 1) | ||
c.Check(fields[0].Column.Name.O, Equals, `NULL`) |
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.
Add a test
select `\N` from test
?
In MySQL this will return \N column.
plan/logical_plan_builder.go
Outdated
@@ -426,8 +426,14 @@ func (b *planBuilder) buildProjection(p LogicalPlan, fields []*ast.SelectField, | |||
if _, ok := innerExpr.(*ast.ValueExpr); ok && innerExpr.Text() != "" { | |||
colName = model.NewCIStr(innerExpr.Text()) | |||
} else { | |||
//Change column name \N to NUll, just when original sql contains \N column |
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.
%s/NUll/NULL
Nice done. |
For this issue #3685
Support
select \N;
, allow \N as shortcut for NULL, this sql will return NULL valueLike this :
tidb> select \N;
+------+
| NULL |
+------+
| NULL |
+------+
1 row in set (0.00 sec)