github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/bit_and.go (about)

     1  // Copyright 2024 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package agg
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/sql/colexec/aggexec"
    20  	"math"
    21  )
    22  
    23  func RegisterBitAnd1(id int64) {
    24  	aggexec.RegisterSingleAggFromFixedToFixed(
    25  		aggexec.MakeSingleAgg1RegisteredInfo(
    26  			aggexec.MakeSingleColumnAggInformation(id, types.T_bit.ToType(), BitAndReturnType, false, true),
    27  			newAggBitAnd[uint64],
    28  			InitAggBitAnd1[uint64],
    29  			FillAggBitAnd1[uint64], nil, FillsAggBitAnd1[uint64],
    30  			MergeAggBitAnd1[uint64],
    31  			nil,
    32  		))
    33  
    34  	aggexec.RegisterSingleAggFromFixedToFixed(
    35  		aggexec.MakeSingleAgg1RegisteredInfo(
    36  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint8.ToType(), BitAndReturnType, false, true),
    37  			newAggBitAnd[uint8],
    38  			InitAggBitAnd1[uint8],
    39  			FillAggBitAnd1[uint8], nil, FillsAggBitAnd1[uint8],
    40  			MergeAggBitAnd1[uint8],
    41  			nil,
    42  		))
    43  
    44  	aggexec.RegisterSingleAggFromFixedToFixed(
    45  		aggexec.MakeSingleAgg1RegisteredInfo(
    46  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint16.ToType(), BitAndReturnType, false, true),
    47  			newAggBitAnd[uint16],
    48  			InitAggBitAnd1[uint16],
    49  			FillAggBitAnd1[uint16], nil, FillsAggBitAnd1[uint16],
    50  			MergeAggBitAnd1[uint16],
    51  			nil,
    52  		))
    53  
    54  	aggexec.RegisterSingleAggFromFixedToFixed(
    55  		aggexec.MakeSingleAgg1RegisteredInfo(
    56  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint32.ToType(), BitAndReturnType, false, true),
    57  			newAggBitAnd[uint32],
    58  			InitAggBitAnd1[uint32],
    59  			FillAggBitAnd1[uint32], nil, FillsAggBitAnd1[uint32],
    60  			MergeAggBitAnd1[uint32],
    61  			nil,
    62  		))
    63  
    64  	aggexec.RegisterSingleAggFromFixedToFixed(
    65  		aggexec.MakeSingleAgg1RegisteredInfo(
    66  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), BitAndReturnType, false, true),
    67  			newAggBitAnd[uint64],
    68  			InitAggBitAnd1[uint64],
    69  			FillAggBitAnd1[uint64], nil, FillsAggBitAnd1[uint64],
    70  			MergeAggBitAnd1[uint64],
    71  			nil,
    72  		))
    73  
    74  	aggexec.RegisterSingleAggFromFixedToFixed(
    75  		aggexec.MakeSingleAgg1RegisteredInfo(
    76  			aggexec.MakeSingleColumnAggInformation(id, types.T_int8.ToType(), BitAndReturnType, false, true),
    77  			newAggBitAnd[int8],
    78  			InitAggBitAnd1[int8],
    79  			FillAggBitAnd1[int8], nil, FillsAggBitAnd1[int8],
    80  			MergeAggBitAnd1[int8],
    81  			nil,
    82  		))
    83  
    84  	aggexec.RegisterSingleAggFromFixedToFixed(
    85  		aggexec.MakeSingleAgg1RegisteredInfo(
    86  			aggexec.MakeSingleColumnAggInformation(id, types.T_int16.ToType(), BitAndReturnType, false, true),
    87  			newAggBitAnd[int16],
    88  			InitAggBitAnd1[int16],
    89  			FillAggBitAnd1[int16], nil, FillsAggBitAnd1[int16],
    90  			MergeAggBitAnd1[int16],
    91  			nil,
    92  		))
    93  
    94  	aggexec.RegisterSingleAggFromFixedToFixed(
    95  		aggexec.MakeSingleAgg1RegisteredInfo(
    96  			aggexec.MakeSingleColumnAggInformation(id, types.T_int32.ToType(), BitAndReturnType, false, true),
    97  			newAggBitAnd[int32],
    98  			InitAggBitAnd1[int32],
    99  			FillAggBitAnd1[int32], nil, FillsAggBitAnd1[int32],
   100  			MergeAggBitAnd1[int32],
   101  			nil,
   102  		))
   103  
   104  	aggexec.RegisterSingleAggFromFixedToFixed(
   105  		aggexec.MakeSingleAgg1RegisteredInfo(
   106  			aggexec.MakeSingleColumnAggInformation(id, types.T_int64.ToType(), BitAndReturnType, false, true),
   107  			newAggBitAnd[int64],
   108  			InitAggBitAnd1[int64],
   109  			FillAggBitAnd1[int64], nil, FillsAggBitAnd1[int64],
   110  			MergeAggBitAnd1[int64],
   111  			nil,
   112  		))
   113  
   114  	aggexec.RegisterSingleAggFromFixedToFixed(
   115  		aggexec.MakeSingleAgg1RegisteredInfo(
   116  			aggexec.MakeSingleColumnAggInformation(id, types.T_float32.ToType(), BitAndReturnType, false, true),
   117  			newAggBitAnd[float32],
   118  			InitAggBitAnd1[float32],
   119  			FillAggBitAnd1[float32], nil, FillsAggBitAnd1[float32],
   120  			MergeAggBitAnd1[float32],
   121  			nil,
   122  		))
   123  
   124  	aggexec.RegisterSingleAggFromFixedToFixed(
   125  		aggexec.MakeSingleAgg1RegisteredInfo(
   126  			aggexec.MakeSingleColumnAggInformation(id, types.T_float64.ToType(), BitAndReturnType, false, true),
   127  			newAggBitAnd[float64],
   128  			InitAggBitAnd1[float64],
   129  			FillAggBitAnd1[float64], nil, FillsAggBitAnd1[float64],
   130  			MergeAggBitAnd1[float64],
   131  			nil,
   132  		))
   133  
   134  	aggexec.RegisterSingleAggFromVarToVar(
   135  		aggexec.MakeSingleAgg4RegisteredInfo(
   136  			aggexec.MakeSingleColumnAggInformation(id, types.T_binary.ToType(), BitAndReturnType, false, true),
   137  			aggexec.GenerateFlagContextFromVarToVar,
   138  			aggexec.InitFlagContextFromVarToVar,
   139  			FillAggBitAndBinary, nil, FillsAggBitAndBinary,
   140  			MergeAggBitAndBinary,
   141  			nil,
   142  		))
   143  
   144  	aggexec.RegisterSingleAggFromVarToVar(
   145  		aggexec.MakeSingleAgg4RegisteredInfo(
   146  			aggexec.MakeSingleColumnAggInformation(id, types.T_varbinary.ToType(), BitAndReturnType, false, true),
   147  			aggexec.GenerateFlagContextFromVarToVar,
   148  			aggexec.InitFlagContextFromVarToVar,
   149  			FillAggBitAndBinary, nil, FillsAggBitAndBinary,
   150  			MergeAggBitAndBinary,
   151  			nil,
   152  		))
   153  }
   154  
   155  var BitAndSupportedParameters = []types.T{
   156  	types.T_uint8, types.T_uint16, types.T_uint32, types.T_uint64,
   157  	types.T_int8, types.T_int16, types.T_int32, types.T_int64,
   158  	types.T_float32, types.T_float64,
   159  	types.T_binary, types.T_varbinary,
   160  	types.T_bit,
   161  }
   162  
   163  func BitAndReturnType(typs []types.Type) types.Type {
   164  	if typs[0].Oid == types.T_binary || typs[0].Oid == types.T_varbinary {
   165  		return typs[0]
   166  	}
   167  	return types.T_uint64.ToType()
   168  }
   169  
   170  type aggBitAnd[T numeric] struct{}
   171  
   172  func newAggBitAnd[T numeric]() aggexec.SingleAggFromFixedRetFixed[T, uint64] {
   173  	return aggBitAnd[T]{}
   174  }
   175  
   176  func (a aggBitAnd[T]) Marshal() []byte  { return nil }
   177  func (a aggBitAnd[T]) Unmarshal([]byte) {}
   178  
   179  func InitAggBitAnd1[T numeric](exec aggexec.SingleAggFromFixedRetFixed[T, uint64], set aggexec.AggSetter[uint64], arg types.Type, ret types.Type) error {
   180  	set(^uint64(0))
   181  	return nil
   182  }
   183  func FillAggBitAnd1[from numeric](
   184  	exec aggexec.SingleAggFromFixedRetFixed[from, uint64], value from, getter aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   185  	vv := float64(value)
   186  	if vv > math.MaxUint64 {
   187  		setter(math.MaxInt64 & getter())
   188  		return nil
   189  	}
   190  	if vv < 0 {
   191  		setter(uint64(int64(value)) & getter())
   192  		return nil
   193  	}
   194  	setter(uint64(value) & getter())
   195  	return nil
   196  }
   197  func FillsAggBitAnd1[from numeric](
   198  	exec aggexec.SingleAggFromFixedRetFixed[from, uint64],
   199  	value from, isNull bool, count int, getter aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   200  	if !isNull {
   201  		return FillAggBitAnd1(exec, value, getter, setter)
   202  	}
   203  	return nil
   204  }
   205  func MergeAggBitAnd1[from numeric](
   206  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[from, uint64],
   207  	getter1, getter2 aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   208  	setter(getter1() & getter2())
   209  	return nil
   210  }
   211  
   212  func FillAggBitAndBinary(
   213  	exec aggexec.SingleAggFromVarRetVar, value []byte, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   214  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   215  	if a.IsEmpty {
   216  		a.IsEmpty = false
   217  		return setter(value)
   218  	}
   219  	v := getter()
   220  	types.BitAnd(v, v, value)
   221  	return nil
   222  }
   223  func FillsAggBitAndBinary(
   224  	exec aggexec.SingleAggFromVarRetVar,
   225  	value []byte, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   226  	if !isNull {
   227  		return FillAggBitAndBinary(exec, value, getter, setter)
   228  	}
   229  	return nil
   230  }
   231  func MergeAggBitAndBinary(
   232  	exec1, exec2 aggexec.SingleAggFromVarRetVar,
   233  	getter1, getter2 aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   234  	a1 := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   235  	a2 := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   236  	if a2.IsEmpty {
   237  		return nil
   238  	}
   239  	if a1.IsEmpty {
   240  		a1.IsEmpty = false
   241  		return setter(getter2())
   242  	}
   243  	v1, v2 := getter1(), getter2()
   244  	types.BitAnd(v1, v1, v2)
   245  	return nil
   246  }