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

aggregation: handle non-integer input for bit related aggregate functions #6994

Merged
merged 4 commits into from
Jul 5, 2018
Merged
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
104 changes: 104 additions & 0 deletions expression/aggregation/aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ func (s *testAggFuncSuit) TestBitAnd(c *C) {
c.Assert(result.GetUint64(), Equals, uint64(0))
partialResult := bitAndFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(0))

// test bit_and( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitAndFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)

result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(math.MaxUint64))

var dec types.MyDecimal
err = dec.FromString([]byte("1.234"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))

err = dec.FromString([]byte("3.012"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))

err = dec.FromString([]byte("2.12345678"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitAndFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitAndFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))
}

func (s *testAggFuncSuit) TestBitOr(c *C) {
Expand Down Expand Up @@ -234,6 +266,46 @@ func (s *testAggFuncSuit) TestBitOr(c *C) {
c.Assert(result.GetUint64(), Equals, uint64(3))
partialResult := bitOrFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(3))

// test bit_or( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitOrFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)

result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))

var dec types.MyDecimal
err = dec.FromString([]byte("12.234"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(12))

err = dec.FromString([]byte("1.012"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(13))
err = dec.FromString([]byte("15.12345678"))
c.Assert(err, IsNil)

row = types.MakeDatums(&dec)
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(15))

err = dec.FromString([]byte("16.00"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitOrFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitOrFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(31))
}

func (s *testAggFuncSuit) TestBitXor(c *C) {
Expand Down Expand Up @@ -277,6 +349,38 @@ func (s *testAggFuncSuit) TestBitXor(c *C) {
c.Assert(result.GetUint64(), Equals, uint64(1))
partialResult := bitXorFunc.GetPartialResult(evalCtx)
c.Assert(partialResult[0].GetUint64(), Equals, uint64(1))

// test bit_xor( decimal )
col.RetType = types.NewFieldType(mysql.TypeNewDecimal)
bitXorFunc.ResetContext(s.ctx.GetSessionVars().StmtCtx, evalCtx)

result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))

var dec types.MyDecimal
err = dec.FromString([]byte("1.234"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(1))

err = dec.FromString([]byte("1.012"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(0))

err = dec.FromString([]byte("2.12345678"))
c.Assert(err, IsNil)
row = types.MakeDatums(&dec)
err = bitXorFunc.Update(evalCtx, s.ctx.GetSessionVars().StmtCtx, types.DatumRow(row))
c.Assert(err, IsNil)
result = bitXorFunc.GetResult(evalCtx)
c.Assert(result.GetUint64(), Equals, uint64(2))
}

func (s *testAggFuncSuit) TestCount(c *C) {
Expand Down
10 changes: 9 additions & 1 deletion expression/aggregation/bit_and.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ func (bf *bitAndFunction) Update(evalCtx *AggEvaluateContext, sc *stmtctx.Statem
return errors.Trace(err)
}
if !value.IsNull() {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() & value.GetUint64())
if value.Kind() == types.KindUint64 {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() & value.GetUint64())
} else {
int64Value, err := value.ToInt64(sc)
if err != nil {
return errors.Trace(err)
}
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() & uint64(int64Value))
}
}
return nil
}
Expand Down
10 changes: 9 additions & 1 deletion expression/aggregation/bit_or.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ func (bf *bitOrFunction) Update(evalCtx *AggEvaluateContext, sc *stmtctx.Stateme
return errors.Trace(err)
}
if !value.IsNull() {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() | value.GetUint64())
if value.Kind() == types.KindUint64 {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() | value.GetUint64())
} else {
int64Value, err := value.ToInt64(sc)
if err != nil {
return errors.Trace(err)
}
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() | uint64(int64Value))
}
}
return nil
}
Expand Down
10 changes: 9 additions & 1 deletion expression/aggregation/bit_xor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ func (bf *bitXorFunction) Update(evalCtx *AggEvaluateContext, sc *stmtctx.Statem
return errors.Trace(err)
}
if !value.IsNull() {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() ^ value.GetUint64())
if value.Kind() == types.KindUint64 {
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() ^ value.GetUint64())
} else {
int64Value, err := value.ToInt64(sc)
if err != nil {
return errors.Trace(err)
}
evalCtx.Value.SetUint64(evalCtx.Value.GetUint64() ^ uint64(int64Value))
}
}
return nil
}
Expand Down