-
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
aggfuncs: implement bit-or with new aggregation framework #6975
Conversation
executor/aggfuncs/func_bit_or.go
Outdated
baseAggFunc | ||
} | ||
|
||
type result4BitOrUint64 struct { |
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/result4BitOrUint64/partialResult4BitFunc/, so we can reuse it in other bit aggregate functions
executor/aggfuncs/builder.go
Outdated
base := baseAggFunc{ | ||
args: aggFuncDesc.Args, | ||
ordinal: ordinal, | ||
} |
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.
need to handle the function which has the distinct
property.
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 bit-or need to care distinct
property?
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.
consider this query: select bit_or(distinct a) from t;
we only calculate the distinct values of column a
in this kind of query.
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.
It doesn't matter, because bit_or(distinct a) = bit_or(a)
, bit_and
same too, except bit_xor
.
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.
ok
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.
please add a comment here to statement that function bitor
no need to consider the distinct
property
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.
done
executor/aggfuncs/func_bit_or.go
Outdated
baseAggFunc | ||
} | ||
|
||
type partialResult4BitFunc struct { |
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.
type partialResult4BitFunc uint64
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.
done
executor/aggfuncs/func_bit_or.go
Outdated
} | ||
|
||
func (e *bitOrUint64) AllocPartialResult() PartialResult { | ||
return PartialResult(&partialResult4BitFunc{}) |
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.
return PartialResult(&uint64)
executor/aggfuncs/func_bit_or.go
Outdated
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
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.
remove this line
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.
done
executor/aggfuncs/func_bit_or.go
Outdated
"github.com/pingcap/tidb/util/chunk" | ||
) | ||
|
||
type bitOrUint64 struct { |
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.
type baseBitAggFunc struct{
baseAggFunc
}
type bitOrUint64 struct{
baseBitAggFunc
}
thus baseBitAggFunc can be reused.
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.
done~
executor/aggfuncs/func_bit_or.go
Outdated
func (e *bitOrUint64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error { | ||
p := (*partialResult4BitFunc)(pr) | ||
for _, row := range rowsInGroup { | ||
inputValue, isNull, err := e.args[0].EvalInt(sctx, row) |
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.
Should we wrap a cast as uint in typeInfer4BitFuncs
,
or bit_or(varchar) may fail?
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.
bit_or( varchar ) will return 0.
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.
actually, we need to add a cast, consider this case:
drop table if exists t;
create table t(a decimal(10, 4));
insert into t values(12.2);
select bit_or(a) from (select * from t union all select * from t) tmp;
TiDB(localhost:4000) > desc select bit_or(a) from (select * from t union all select * from t) tmp;
+--------------------------+------+----------------------------------------------+----------+
| id | task | operator info | count |
+--------------------------+------+----------------------------------------------+----------+
| StreamAgg_13 | root | funcs:bit_or(tmp.a) | 1.00 |
| └─Union_21 | root | | 20000.00 |
| ├─TableReader_24 | root | data:TableScan_23 | 10000.00 |
| │ └─TableScan_23 | cop | table:t, range:[-inf,+inf], keep order:false | 10000.00 |
| └─TableReader_27 | root | data:TableScan_26 | 10000.00 |
| └─TableScan_26 | cop | table:t, range:[-inf,+inf], keep order:false | 10000.00 |
+--------------------------+------+----------------------------------------------+----------+
6 rows in set (0.00 sec)
The above StreamAgg_13
directly handles the original data instead of another aggregate operator's partial result, which is guaranteed to be uint64. This PR may failed on this query if we don't wrap a cast on it's parameter.
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.
Ye, I'll fix it
executor/aggfuncs/func_bit_or.go
Outdated
|
||
type partialResult4BitFunc = uint64 | ||
|
||
func (e *bitOrUint64) AllocPartialResult() PartialResult { |
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.
This can be a member function of *baseBitAggFunc
.
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.
great suggestion.
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 take back the last sentence. 😂
We should not make the result value to be a member of baseBitAggFunc
, Because this will make The baseBitAggFunc
to be Stateful.
Consider another scenario, If we have many groups to be aggregated, if the AggFunc
is statefull, we have to create many aggFunc
to handle this.
But if AggFunc
is not statefull, we can only create one AggFunc
and many partialResult4BitFunc
, this will reduce go GC pressure.
( This is @zz-jason told me. Thanks very much~ )
executor/aggfuncs/func_bit_or.go
Outdated
return PartialResult(new(partialResult4BitFunc)) | ||
} | ||
|
||
func (e *bitOrUint64) ResetPartialResult(pr PartialResult) { |
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.
ditto
executor/aggfuncs/func_bit_or.go
Outdated
*p = 0 | ||
} | ||
|
||
func (e *bitOrUint64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error { |
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.
ditto
executor/aggfuncs/func_bit_or.go
Outdated
@@ -0,0 +1,60 @@ | |||
// Copyright 2018 PingCAP, Inc. |
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.
rename the filename as func_bitfuncs.go
executor/aggfuncs/func_bit_or.go
Outdated
|
||
type baseBitAggFunc struct { | ||
baseAggFunc | ||
value uint64 |
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.
do not store the partial result into a aggregate function. aggregate functions should be stateless.
0ec3f50
to
0b00dc8
Compare
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
executor/aggfuncs/builder.go
Outdated
@@ -120,6 +120,15 @@ func buildGroupConcat(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc | |||
|
|||
// buildCount builds the AggFunc implementation for function "BIT_OR". | |||
func buildBitOr(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc { | |||
// BIT_OR doesn't need to handle the distinct property. | |||
switch aggFuncDesc.Args[0].GetType().Tp { |
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.
switch aggFuncDesc.Args[0].GetType().EvalType(){
case types.ETInt:
xxxx
}
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.
Done. PTAL
/run-all-tests |
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
@crazycs520 This PR can be merged after the checks finish. |
What have you changed? (mandatory)
This PR implements bit-or with new aggregation framework
What are the type of the changes (mandatory)?
improvement
How has this PR been tested (mandatory)?
the existing test cases
Does this PR affect documentation (docs/docs-cn) update? (optional)
No
Refer to a related PR or issue link (optional)
#6952 #6852