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

expression: add builtinValuesBitSig #40994

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,11 @@ func (c *valuesFunctionClass) getFunction(ctx sessionctx.Context, args []Express
}
switch c.tp.EvalType() {
case types.ETInt:
sig = &builtinValuesIntSig{bf, c.offset}
if c.tp.GetType() == mysql.TypeBit {
sig = &builtinValuesBitSig{bf, c.offset}
} else {
sig = &builtinValuesIntSig{bf, c.offset}
}
case types.ETReal:
sig = &builtinValuesRealSig{bf, c.offset}
case types.ETDecimal:
Expand Down Expand Up @@ -1221,6 +1225,43 @@ func (b *builtinValuesIntSig) evalInt(_ chunk.Row) (int64, bool, error) {
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}

type builtinValuesBitSig struct {
baseBuiltinFunc
offset int
}

func (b *builtinValuesBitSig) Clone() builtinFunc {
newSig := &builtinValuesBitSig{offset: b.offset}
newSig.cloneFrom(&b.baseBuiltinFunc)
return newSig
}

// evalInt evals a builtinValuesBitSig.
// See https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_values
func (b *builtinValuesBitSig) evalInt(_ chunk.Row) (int64, bool, error) {
row := b.ctx.GetSessionVars().CurrInsertValues
if row.IsEmpty() {
return 0, true, nil
}
if b.offset < row.Len() {
if row.IsNull(b.offset) {
return 0, true, nil
}
// For BinaryLiteral, see issue #15310
val := row.GetRaw(b.offset)
if len(val) > 8 {
return 0, true, errors.New("Session current insert values is too long")
}
var binary types.BinaryLiteral = val
v, err := binary.ToInt(b.ctx.GetSessionVars().StmtCtx)
if err != nil {
return 0, true, errors.Trace(err)
}
return int64(v), false, nil
}
return 0, true, errors.Errorf("Session current insert values len %d and column's offset %v don't match", row.Len(), b.offset)
}

type builtinValuesRealSig struct {
baseBuiltinFunc

Expand Down
13 changes: 13 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4303,6 +4303,19 @@ func TestValuesForBinaryLiteral(t *testing.T) {
tk.MustExec("drop table testValuesBinary;")
}

func TestValuesForBinaryLiteral2(t *testing.T) {
// See issue #40653
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists testValuesBit")
tk.MustExec("create table testValuesBit (id bigint(20) NOT NULL AUTO_INCREMENT, test_bit bit(64) DEFAULT NULL, PRIMARY KEY(id))")
tk.MustExec("insert into testValuesBit (id,test_bit) values(1,x'AAFF')")
tk.MustExec("insert into testValuesBit (id,test_bit) values(1,x'AAFF') on duplicate key update test_bit=values(test_bit)")
tk.MustQuery("select hex(test_bit) from testValuesBit").Check(testkit.Rows("AAFF"))
}

func TestIssue14159(t *testing.T) {
store := testkit.CreateMockStore(t)

Expand Down