github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/aggexec/aggMethod.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 aggexec
    16  
    17  import "github.com/matrixorigin/matrixone/pkg/container/types"
    18  
    19  // the definition of functions to get and set the aggregation result.
    20  type (
    21  	AggSetter[T types.FixedSizeTExceptStrType] func(value T)
    22  	AggBytesSetter                             func(value []byte) error
    23  
    24  	AggGetter[T types.FixedSizeTExceptStrType] func() T
    25  	AggBytesGetter                             func() []byte
    26  )
    27  
    28  // the definition of functions to initialize the aggregation.
    29  type (
    30  	SingleAggInit1[from types.FixedSizeTExceptStrType, to types.FixedSizeTExceptStrType] func(
    31  		exec SingleAggFromFixedRetFixed[from, to], setter AggSetter[to], arg, ret types.Type) error
    32  
    33  	SingleAggInit2[from types.FixedSizeTExceptStrType] func(
    34  		exec SingleAggFromFixedRetVar[from], setter AggBytesSetter, arg, ret types.Type) error
    35  
    36  	SingleAggInit3[to types.FixedSizeTExceptStrType] func(
    37  		exec SingleAggFromVarRetFixed[to], setter AggSetter[to], arg, ret types.Type) error
    38  
    39  	SingleAggInit4 func(
    40  		exec SingleAggFromVarRetVar, setter AggBytesSetter, arg, ret types.Type) error
    41  
    42  	MultiAggInit1[to types.FixedSizeTExceptStrType] func(
    43  		exec MultiAggRetFixed[to], setter AggSetter[to], args []types.Type, ret types.Type)
    44  
    45  	MultiAggInit2 func(
    46  		exec MultiAggRetVar, setter AggBytesSetter, args []types.Type, ret types.Type)
    47  )
    48  
    49  // the definition of functions to fill the aggregation with one value.
    50  type (
    51  	SingleAggFill1[from, to types.FixedSizeTExceptStrType] func(
    52  		exec SingleAggFromFixedRetFixed[from, to], value from, getter AggGetter[to], setter AggSetter[to]) error
    53  
    54  	SingleAggFill2[from types.FixedSizeTExceptStrType] func(
    55  		exec SingleAggFromFixedRetVar[from], value from, getter AggBytesGetter, setter AggBytesSetter) error
    56  
    57  	SingleAggFill3[to types.FixedSizeTExceptStrType] func(
    58  		exec SingleAggFromVarRetFixed[to], value []byte, getter AggGetter[to], setter AggSetter[to]) error
    59  
    60  	SingleAggFill4 func(
    61  		exec SingleAggFromVarRetVar, value []byte, getter AggBytesGetter, setter AggBytesSetter) error
    62  )
    63  
    64  // the definition of functions to fill the aggregation with one null value.
    65  type (
    66  	SingleAggFillNull1[from, to types.FixedSizeTExceptStrType] func(
    67  		exec SingleAggFromFixedRetFixed[from, to], getter AggGetter[to], setter AggSetter[to]) error
    68  
    69  	SingleAggFillNull2[from types.FixedSizeTExceptStrType] func(
    70  		exec SingleAggFromFixedRetVar[from], getter AggBytesGetter, setter AggBytesSetter) error
    71  
    72  	SingleAggFillNull3[to types.FixedSizeTExceptStrType] func(
    73  		exec SingleAggFromVarRetFixed[to], getter AggGetter[to], setter AggSetter[to]) error
    74  
    75  	SingleAggFillNull4 func(
    76  		exec SingleAggFromVarRetVar, getter AggBytesGetter, setter AggBytesSetter) error
    77  )
    78  
    79  // the definition of functions to fill the aggregation with multiple values.
    80  type (
    81  	SingleAggFills1[from, to types.FixedSizeTExceptStrType] func(
    82  		exec SingleAggFromFixedRetFixed[from, to], value from, isNull bool, count int, getter AggGetter[to], setter AggSetter[to]) error
    83  
    84  	SingleAggFills2[from types.FixedSizeTExceptStrType] func(
    85  		exec SingleAggFromFixedRetVar[from], value from, isNull bool, count int, getter AggBytesGetter, setter AggBytesSetter) error
    86  
    87  	SingleAggFills3[to types.FixedSizeTExceptStrType] func(
    88  		exec SingleAggFromVarRetFixed[to], value []byte, isNull bool, count int, getter AggGetter[to], setter AggSetter[to]) error
    89  
    90  	SingleAggFills4 func(
    91  		exec SingleAggFromVarRetVar, value []byte, isNull bool, count int, getter AggBytesGetter, setter AggBytesSetter) error
    92  )
    93  
    94  // the definition of functions to merge two aggregations.
    95  type (
    96  	SingleAggMerge1[from, to types.FixedSizeTExceptStrType] func(
    97  		exec1, exec2 SingleAggFromFixedRetFixed[from, to], getter1, getter2 AggGetter[to], setter AggSetter[to]) error
    98  
    99  	SingleAggMerge2[from types.FixedSizeTExceptStrType] func(
   100  		exec1, exec2 SingleAggFromFixedRetVar[from], getter1, getter2 AggBytesGetter, setter AggBytesSetter) error
   101  
   102  	SingleAggMerge3[to types.FixedSizeTExceptStrType] func(
   103  		exec1, exec2 SingleAggFromVarRetFixed[to], getter1, getter2 AggGetter[to], setter AggSetter[to]) error
   104  
   105  	SingleAggMerge4 func(
   106  		exec1, exec2 SingleAggFromVarRetVar, getter1, getter2 AggBytesGetter, setter AggBytesSetter) error
   107  )
   108  
   109  // the definition of functions to return the final result of the aggregation.
   110  type (
   111  	SingleAggFlush1[from, to types.FixedSizeTExceptStrType] func(
   112  		exec SingleAggFromFixedRetFixed[from, to], getter AggGetter[to], setter AggSetter[to]) error
   113  
   114  	SingleAggFlush2[from types.FixedSizeTExceptStrType] func(
   115  		exec SingleAggFromFixedRetVar[from], getter AggBytesGetter, setter AggBytesSetter) error
   116  
   117  	SingleAggFlush3[to types.FixedSizeTExceptStrType] func(
   118  		exec SingleAggFromVarRetFixed[to], getter AggGetter[to], setter AggSetter[to]) error
   119  
   120  	SingleAggFlush4 func(
   121  		exec SingleAggFromVarRetVar, getter AggBytesGetter, setter AggBytesSetter) error
   122  )
   123  
   124  // the definition of functions used for multi-column agg whose result type is a fixed-length type.
   125  type (
   126  	MultiAggFillNull1[to types.FixedSizeTExceptStrType] func(
   127  		exec MultiAggRetFixed[to]) error
   128  	rowValidForMultiAgg1[to types.FixedSizeTExceptStrType] func(
   129  		exec MultiAggRetFixed[to]) bool
   130  	MultiAggEval1[to types.FixedSizeTExceptStrType] func(
   131  		exec MultiAggRetFixed[to], getter AggGetter[to], setter AggSetter[to]) error
   132  	MultiAggMerge1[to types.FixedSizeTExceptStrType] func(
   133  		exec1, exec2 MultiAggRetFixed[to], getter1, getter2 AggGetter[to], setter AggSetter[to]) error
   134  	MultiAggFlush1[to types.FixedSizeTExceptStrType] func(
   135  		exec MultiAggRetFixed[to], getter AggGetter[to], setter AggSetter[to]) error
   136  )
   137  
   138  // the definition of functions used for multi-column agg whose result type is a var-len type.
   139  type (
   140  	MultiAggFillNull2 func(
   141  		exec MultiAggRetVar) error
   142  	rowValidForMultiAgg2 func(
   143  		exec MultiAggRetVar) bool
   144  	MultiAggEval2 func(
   145  		exec MultiAggRetVar, getter AggBytesGetter, setter AggBytesSetter) error
   146  	MultiAggMerge2 func(
   147  		exec1, exec2 MultiAggRetVar, getter1, getter2 AggBytesGetter, setter AggBytesSetter) error
   148  	MultiAggFlush2 func(
   149  		exec MultiAggRetVar, getter AggBytesGetter, setter AggBytesSetter) error
   150  )
   151  
   152  // AggCanMarshal interface is used for agg structures' multi-node communication.
   153  // each private structure of aggregation should implement the AggCanMarshal interface.
   154  // todo: change to deliver []byte directly, and agg developer choose how to use the []byte.
   155  type AggCanMarshal interface {
   156  	Marshal() []byte
   157  	Unmarshal([]byte)
   158  }
   159  
   160  /*
   161  	All the codes bellow were the interface of aggregations' execute context.
   162  	Each aggregation should implement one of the interfaces.
   163  
   164  	1. SingleAggFromFixedRetFixed: aggregation receives a fixed length type and returns a fixed length type.
   165  	2. SingleAggFromFixedRetVar: aggregation receives a fixed length type and returns a variable length type.
   166  	3. SingleAggFromVarRetFixed: aggregation receives a variable length type and returns a fixed length type.
   167  	4. SingleAggFromVarRetVar: aggregation receives a variable length type and returns a variable length type.
   168  	5. MultiAggRetFixed: aggregation receives multi columns and returns a fixed length type.
   169  	6. MultiAggRetVar: aggregation receives multi columns and returns a variable length type.
   170  
   171  	If the aggregation needn't store any context,
   172  	you can use the EmptyContextOfSingleAggRetFixed or EmptyContextOfSingleAggRetBytes.
   173  	If the aggregation only needs to store a flag to indicate whether it is empty,
   174  	you can use the ContextWithEmptyFlagOfSingleAggRetFixed or ContextWithEmptyFlagOfSingleAggRetBytes.
   175  */
   176  
   177  type SingleAggFromFixedRetFixed[from types.FixedSizeTExceptStrType, to types.FixedSizeTExceptStrType] interface{ AggCanMarshal }
   178  type SingleAggFromFixedRetVar[from types.FixedSizeTExceptStrType] interface{ AggCanMarshal }
   179  type SingleAggFromVarRetFixed[to types.FixedSizeTExceptStrType] interface{ AggCanMarshal }
   180  type SingleAggFromVarRetVar interface{ AggCanMarshal }
   181  type MultiAggRetFixed[to types.FixedSizeTExceptStrType] interface{ AggCanMarshal }
   182  type MultiAggRetVar interface{ AggCanMarshal }