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: rewrite builtin function: INSERT #4414

Merged
merged 4 commits into from Sep 7, 2017

Conversation

Projects
None yet
3 participants
@zz-jason
Member

zz-jason commented Sep 4, 2017

to #4080

@zz-jason zz-jason referenced this pull request Sep 4, 2017

Closed

rewrite builtin functions #4080

150 of 150 tasks complete
Show outdated Hide outdated expression/builtin_string.go
Show outdated Hide outdated expression/builtin_string.go
Show outdated Hide outdated expression/builtin_string.go
sig := &builtinInsertFuncSig{newBaseBuiltinFunc(args, ctx)}
bf := newBaseBuiltinFuncWithTp(args, ctx, tpString, tpString, tpInt, tpInt, tpString)
bf.tp.Flen = mysql.MaxBlobWidth
SetBinFlagOrBinStr(args[0].GetType(), bf.tp)

This comment has been minimized.

@XuHuaiyu

XuHuaiyu Sep 5, 2017

Contributor

why?

@XuHuaiyu

XuHuaiyu Sep 5, 2017

Contributor

why?

This comment has been minimized.

@zz-jason

zz-jason Sep 5, 2017

Member

this is a bug fix: for INSERT(str,pos,len,newstr), if str is a binary string, we should treat it like a byte array, otherwise we should treat it like a code point array. see builtinInsertBinarySig and builtinInsertSig for more detail

@zz-jason

zz-jason Sep 5, 2017

Member

this is a bug fix: for INSERT(str,pos,len,newstr), if str is a binary string, we should treat it like a byte array, otherwise we should treat it like a code point array. see builtinInsertBinarySig and builtinInsertSig for more detail

This comment has been minimized.

@XuHuaiyu

XuHuaiyu Sep 5, 2017

Contributor

If we use args[0].GetType() after newBaseBuiltinFunc,
args[0] has already been wrapped with cast.

@XuHuaiyu

XuHuaiyu Sep 5, 2017

Contributor

If we use args[0].GetType() after newBaseBuiltinFunc,
args[0] has already been wrapped with cast.

This comment has been minimized.

@zz-jason

zz-jason Sep 5, 2017

Member

when arg[0] is a binary string, it will not be wrapped with a cast.

btw, I think wrapped cast functions should guarantee the correct type inference, and every function should do its type inference based on its children, no matter what expression the children is

@zz-jason

zz-jason Sep 5, 2017

Member

when arg[0] is a binary string, it will not be wrapped with a cast.

btw, I think wrapped cast functions should guarantee the correct type inference, and every function should do its type inference based on its children, no matter what expression the children is

@zz-jason

This comment has been minimized.

Show comment
Hide comment
@zz-jason
Member

zz-jason commented Sep 6, 2017

if length > runeLength-pos+1 || length < 0 {
return string(runes[0:pos-1]) + newstr, false, nil
}
return string(runes[0:pos-1]) + newstr + string(runes[pos+length-1:]), false, nil

This comment has been minimized.

@jackysp

jackysp Sep 6, 2017

Member

Use fmt.Sprintf instead?

@jackysp

jackysp Sep 6, 2017

Member

Use fmt.Sprintf instead?

This comment has been minimized.

@zz-jason

zz-jason Sep 6, 2017

Member
package main

import "fmt"
import "time"

func main() {
        str := ""
        str1 := "aaaa"
        str2 := "bbbb"
        str3 := "cccc"

        start := time.Now()
        for i, round := 0, 1000000; i < round; i++ {
                str = str1 + str2 + str3
        }
        fmt.Printf("%vns\n", time.Now().Sub(start).Nanoseconds())
        fmt.Printf("%s\n", str)

        start = time.Now()
        for i, round := 0, 1000000; i < round; i++ {
                str = fmt.Sprintf("%s%s%s", str1, str2, str3)
        }
        fmt.Printf("%vns\n", time.Now().Sub(start).Nanoseconds())
        fmt.Printf("%s\n", str)
}
$go build a.go
$./a
56394494ns
aaaabbbbcccc
279845006ns
aaaabbbbcccc

It seems fmt.Sprintf() has less efficiency.

@zz-jason

zz-jason Sep 6, 2017

Member
package main

import "fmt"
import "time"

func main() {
        str := ""
        str1 := "aaaa"
        str2 := "bbbb"
        str3 := "cccc"

        start := time.Now()
        for i, round := 0, 1000000; i < round; i++ {
                str = str1 + str2 + str3
        }
        fmt.Printf("%vns\n", time.Now().Sub(start).Nanoseconds())
        fmt.Printf("%s\n", str)

        start = time.Now()
        for i, round := 0, 1000000; i < round; i++ {
                str = fmt.Sprintf("%s%s%s", str1, str2, str3)
        }
        fmt.Printf("%vns\n", time.Now().Sub(start).Nanoseconds())
        fmt.Printf("%s\n", str)
}
$go build a.go
$./a
56394494ns
aaaabbbbcccc
279845006ns
aaaabbbbcccc

It seems fmt.Sprintf() has less efficiency.

@XuHuaiyu XuHuaiyu added the status/LGT1 label Sep 6, 2017

@zz-jason

This comment has been minimized.

Show comment
Hide comment
@zz-jason
Member

zz-jason commented Sep 7, 2017

@jackysp PTAL

@jackysp

jackysp approved these changes Sep 7, 2017

LGTM

@zz-jason zz-jason merged commit d82a36b into master Sep 7, 2017

4 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
jenkins-ci-tidb/build Jenkins job succeeded.
Details
license/cla Contributor License Agreement is signed.
Details

@zz-jason zz-jason deleted the zz-jason/expression/string/insert branch Sep 7, 2017

mccxj added a commit to mccxj/tidb that referenced this pull request Sep 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment