github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/agg/bit_or.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 RegisterBitOr1(id int64) {
    24  	aggexec.RegisterSingleAggFromFixedToFixed(
    25  		aggexec.MakeSingleAgg1RegisteredInfo(
    26  			aggexec.MakeSingleColumnAggInformation(id, types.T_bit.ToType(), BitOrReturnType, false, true),
    27  			newAggBitOr[uint64],
    28  			InitAggBitOr1[uint64],
    29  			FillAggBitOr1[uint64], nil, FillsAggBitOr1[uint64],
    30  			MergeAggBitOr1[uint64],
    31  			nil,
    32  		))
    33  
    34  	aggexec.RegisterSingleAggFromFixedToFixed(
    35  		aggexec.MakeSingleAgg1RegisteredInfo(
    36  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint8.ToType(), BitOrReturnType, false, true),
    37  			newAggBitOr[uint8],
    38  			InitAggBitOr1[uint8],
    39  			FillAggBitOr1[uint8], nil, FillsAggBitOr1[uint8],
    40  			MergeAggBitOr1[uint8],
    41  			nil,
    42  		))
    43  
    44  	aggexec.RegisterSingleAggFromFixedToFixed(
    45  		aggexec.MakeSingleAgg1RegisteredInfo(
    46  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint16.ToType(), BitOrReturnType, false, true),
    47  			newAggBitOr[uint16],
    48  			InitAggBitOr1[uint16],
    49  			FillAggBitOr1[uint16], nil, FillsAggBitOr1[uint16],
    50  			MergeAggBitOr1[uint16],
    51  			nil,
    52  		))
    53  
    54  	aggexec.RegisterSingleAggFromFixedToFixed(
    55  		aggexec.MakeSingleAgg1RegisteredInfo(
    56  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint32.ToType(), BitOrReturnType, false, true),
    57  			newAggBitOr[uint32],
    58  			InitAggBitOr1[uint32],
    59  			FillAggBitOr1[uint32], nil, FillsAggBitOr1[uint32],
    60  			MergeAggBitOr1[uint32],
    61  			nil,
    62  		))
    63  
    64  	aggexec.RegisterSingleAggFromFixedToFixed(
    65  		aggexec.MakeSingleAgg1RegisteredInfo(
    66  			aggexec.MakeSingleColumnAggInformation(id, types.T_uint64.ToType(), BitOrReturnType, false, true),
    67  			newAggBitOr[uint64],
    68  			InitAggBitOr1[uint64],
    69  			FillAggBitOr1[uint64], nil, FillsAggBitOr1[uint64],
    70  			MergeAggBitOr1[uint64],
    71  			nil,
    72  		))
    73  
    74  	aggexec.RegisterSingleAggFromFixedToFixed(
    75  		aggexec.MakeSingleAgg1RegisteredInfo(
    76  			aggexec.MakeSingleColumnAggInformation(id, types.T_int8.ToType(), BitOrReturnType, false, true),
    77  			newAggBitOr[int8],
    78  			InitAggBitOr1[int8],
    79  			FillAggBitOr1[int8], nil, FillsAggBitOr1[int8],
    80  			MergeAggBitOr1[int8],
    81  			nil,
    82  		))
    83  
    84  	aggexec.RegisterSingleAggFromFixedToFixed(
    85  		aggexec.MakeSingleAgg1RegisteredInfo(
    86  			aggexec.MakeSingleColumnAggInformation(id, types.T_int16.ToType(), BitOrReturnType, false, true),
    87  			newAggBitOr[int16],
    88  			InitAggBitOr1[int16],
    89  			FillAggBitOr1[int16], nil, FillsAggBitOr1[int16],
    90  			MergeAggBitOr1[int16],
    91  			nil,
    92  		))
    93  
    94  	aggexec.RegisterSingleAggFromFixedToFixed(
    95  		aggexec.MakeSingleAgg1RegisteredInfo(
    96  			aggexec.MakeSingleColumnAggInformation(id, types.T_int32.ToType(), BitOrReturnType, false, true),
    97  			newAggBitOr[int32],
    98  			InitAggBitOr1[int32],
    99  			FillAggBitOr1[int32], nil, FillsAggBitOr1[int32],
   100  			MergeAggBitOr1[int32],
   101  			nil,
   102  		))
   103  
   104  	aggexec.RegisterSingleAggFromFixedToFixed(
   105  		aggexec.MakeSingleAgg1RegisteredInfo(
   106  			aggexec.MakeSingleColumnAggInformation(id, types.T_int64.ToType(), BitOrReturnType, false, true),
   107  			newAggBitOr[int64],
   108  			InitAggBitOr1[int64],
   109  			FillAggBitOr1[int64], nil, FillsAggBitOr1[int64],
   110  			MergeAggBitOr1[int64],
   111  			nil,
   112  		))
   113  
   114  	aggexec.RegisterSingleAggFromFixedToFixed(
   115  		aggexec.MakeSingleAgg1RegisteredInfo(
   116  			aggexec.MakeSingleColumnAggInformation(id, types.T_float32.ToType(), BitOrReturnType, false, true),
   117  			newAggBitOr[float32],
   118  			InitAggBitOr1[float32],
   119  			FillAggBitOr1[float32], nil, FillsAggBitOr1[float32],
   120  			MergeAggBitOr1[float32],
   121  			nil,
   122  		))
   123  
   124  	aggexec.RegisterSingleAggFromFixedToFixed(
   125  		aggexec.MakeSingleAgg1RegisteredInfo(
   126  			aggexec.MakeSingleColumnAggInformation(id, types.T_float64.ToType(), BitOrReturnType, false, true),
   127  			newAggBitOr[float64],
   128  			InitAggBitOr1[float64],
   129  			FillAggBitOr1[float64], nil, FillsAggBitOr1[float64],
   130  			MergeAggBitOr1[float64],
   131  			nil,
   132  		))
   133  
   134  	aggexec.RegisterSingleAggFromVarToVar(
   135  		aggexec.MakeSingleAgg4RegisteredInfo(
   136  			aggexec.MakeSingleColumnAggInformation(id, types.T_binary.ToType(), BitOrReturnType, false, true),
   137  			aggexec.GenerateFlagContextFromVarToVar,
   138  			aggexec.InitFlagContextFromVarToVar,
   139  			FillAggBitOrBinary, nil, FillsAggBitOrBinary,
   140  			MergeAggBitOrBinary,
   141  			nil,
   142  		))
   143  
   144  	aggexec.RegisterSingleAggFromVarToVar(
   145  		aggexec.MakeSingleAgg4RegisteredInfo(
   146  			aggexec.MakeSingleColumnAggInformation(id, types.T_varbinary.ToType(), BitOrReturnType, false, true),
   147  			aggexec.GenerateFlagContextFromVarToVar,
   148  			aggexec.InitFlagContextFromVarToVar,
   149  			FillAggBitOrBinary, nil, FillsAggBitOrBinary,
   150  			MergeAggBitOrBinary,
   151  			nil,
   152  		))
   153  }
   154  
   155  var BitOrReturnType = BitAndReturnType
   156  
   157  type aggBitOr[T numeric] struct{}
   158  
   159  func newAggBitOr[T numeric]() aggexec.SingleAggFromFixedRetFixed[T, uint64] {
   160  	return aggBitOr[T]{}
   161  }
   162  
   163  func (a aggBitOr[T]) Marshal() []byte  { return nil }
   164  func (a aggBitOr[T]) Unmarshal([]byte) {}
   165  
   166  func InitAggBitOr1[from numeric](
   167  	exec aggexec.SingleAggFromFixedRetFixed[from, uint64], setter aggexec.AggSetter[uint64], arg, ret types.Type) error {
   168  	setter(0)
   169  	return nil
   170  }
   171  func FillAggBitOr1[from numeric](
   172  	exec aggexec.SingleAggFromFixedRetFixed[from, uint64], value from, getter aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   173  	vv := float64(value)
   174  	if vv > math.MaxUint64 {
   175  		setter(math.MaxInt64)
   176  		return nil
   177  	}
   178  	if vv < 0 {
   179  		setter(uint64(int64(value)) | getter())
   180  		return nil
   181  	}
   182  	setter(uint64(value) | getter())
   183  	return nil
   184  }
   185  func FillsAggBitOr1[from numeric](
   186  	exec aggexec.SingleAggFromFixedRetFixed[from, uint64],
   187  	value from, isNull bool, count int, getter aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   188  	if !isNull {
   189  		return FillAggBitOr1(exec, value, getter, setter)
   190  	}
   191  	return nil
   192  }
   193  func MergeAggBitOr1[from numeric](
   194  	exec1, exec2 aggexec.SingleAggFromFixedRetFixed[from, uint64],
   195  	getter1, getter2 aggexec.AggGetter[uint64], setter aggexec.AggSetter[uint64]) error {
   196  	setter(getter1() | getter2())
   197  	return nil
   198  }
   199  
   200  func FillAggBitOrBinary(
   201  	exec aggexec.SingleAggFromVarRetVar, value []byte, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   202  	a := exec.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   203  	if a.IsEmpty {
   204  		a.IsEmpty = false
   205  		return setter(value)
   206  	}
   207  	v := getter()
   208  	types.BitOr(v, v, value)
   209  	return nil
   210  }
   211  func FillsAggBitOrBinary(
   212  	exec aggexec.SingleAggFromVarRetVar,
   213  	value []byte, isNull bool, count int, getter aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   214  	if !isNull {
   215  		return FillAggBitOrBinary(exec, value, getter, setter)
   216  	}
   217  	return nil
   218  }
   219  func MergeAggBitOrBinary(
   220  	exec1, exec2 aggexec.SingleAggFromVarRetVar,
   221  	getter1, getter2 aggexec.AggBytesGetter, setter aggexec.AggBytesSetter) error {
   222  	a1 := exec1.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   223  	a2 := exec2.(*aggexec.ContextWithEmptyFlagOfSingleAggRetBytes)
   224  	if a2.IsEmpty {
   225  		return nil
   226  	}
   227  	if a1.IsEmpty {
   228  		a1.IsEmpty = false
   229  		return setter(getter2())
   230  	}
   231  	v1, v2 := getter1(), getter2()
   232  	types.BitOr(v1, v1, v2)
   233  	return nil
   234  }