github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/agg/max.go (about)

     1  // Copyright 2022 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  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  type Max[T Compare] struct {
    24  }
    25  type Decimal64Max struct {
    26  }
    27  
    28  type Decimal128Max struct {
    29  }
    30  
    31  type BoolMax struct {
    32  }
    33  
    34  type StrMax struct {
    35  }
    36  
    37  type UuidMax struct {
    38  }
    39  
    40  func MaxReturnType(typs []types.Type) types.Type {
    41  	return typs[0]
    42  }
    43  
    44  func NewMax[T Compare]() *Max[T] {
    45  	return &Max[T]{}
    46  }
    47  
    48  func (m *Max[T]) Grows(_ int) {
    49  }
    50  
    51  func (m *Max[T]) Eval(vs []T) []T {
    52  	return vs
    53  }
    54  
    55  func (m *Max[T]) Fill(_ int64, value T, ov T, _ int64, isEmpty bool, isNull bool) (T, bool) {
    56  	if !isNull {
    57  		if value > ov || isEmpty {
    58  			return value, false
    59  		}
    60  	}
    61  	return ov, isEmpty
    62  }
    63  
    64  func (m *Max[T]) Merge(_ int64, _ int64, x T, y T, xEmpty bool, yEmpty bool, _ any) (T, bool) {
    65  	if !yEmpty {
    66  		if !xEmpty && x > y {
    67  			return x, false
    68  		}
    69  		return y, false
    70  	}
    71  	return x, xEmpty
    72  }
    73  
    74  func (m *Max[T]) MarshalBinary() ([]byte, error) {
    75  	return nil, nil
    76  }
    77  
    78  func (m *Max[T]) UnmarshalBinary(data []byte) error {
    79  	return nil
    80  }
    81  
    82  func NewD64Max() *Decimal64Max {
    83  	return &Decimal64Max{}
    84  }
    85  
    86  func (m *Decimal64Max) Grows(_ int) {
    87  }
    88  
    89  func (m *Decimal64Max) Eval(vs []types.Decimal64) []types.Decimal64 {
    90  	return vs
    91  }
    92  
    93  func (m *Decimal64Max) Fill(_ int64, value types.Decimal64, ov types.Decimal64, _ int64, isEmpty bool, isNull bool) (types.Decimal64, bool) {
    94  	if !isNull {
    95  		if value.Gt(ov) || isEmpty {
    96  			return value, false
    97  		}
    98  	}
    99  	return ov, isEmpty
   100  
   101  }
   102  func (m *Decimal64Max) Merge(_ int64, _ int64, x types.Decimal64, y types.Decimal64, xEmpty bool, yEmpty bool, _ any) (types.Decimal64, bool) {
   103  	if !yEmpty {
   104  		if !xEmpty && x.Gt(y) {
   105  			return x, false
   106  		}
   107  		return y, false
   108  	}
   109  	return x, xEmpty
   110  }
   111  
   112  func (m *Decimal64Max) MarshalBinary() ([]byte, error) {
   113  	return nil, nil
   114  }
   115  
   116  func (m *Decimal64Max) UnmarshalBinary(data []byte) error {
   117  	return nil
   118  }
   119  
   120  func NewD128Max() *Decimal128Max {
   121  	return &Decimal128Max{}
   122  }
   123  
   124  func (m *Decimal128Max) Grows(_ int) {
   125  }
   126  
   127  func (m *Decimal128Max) Eval(vs []types.Decimal128) []types.Decimal128 {
   128  	return vs
   129  }
   130  
   131  func (m *Decimal128Max) Fill(_ int64, value types.Decimal128, ov types.Decimal128, _ int64, isEmpty bool, isNull bool) (types.Decimal128, bool) {
   132  	if !isNull {
   133  		if ov.Le(value) || isEmpty {
   134  			return value, false
   135  		}
   136  	}
   137  	return ov, isEmpty
   138  
   139  }
   140  func (m *Decimal128Max) Merge(_ int64, _ int64, x types.Decimal128, y types.Decimal128, xEmpty bool, yEmpty bool, _ any) (types.Decimal128, bool) {
   141  	if !yEmpty {
   142  		if !xEmpty && x.Gt(y) {
   143  			return x, false
   144  		}
   145  		return y, false
   146  	}
   147  	return x, xEmpty
   148  }
   149  
   150  func (m *Decimal128Max) MarshalBinary() ([]byte, error) {
   151  	return nil, nil
   152  }
   153  
   154  func (m *Decimal128Max) UnmarshalBinary(data []byte) error {
   155  	return nil
   156  }
   157  
   158  func NewBoolMax() *BoolMax {
   159  	return &BoolMax{}
   160  }
   161  
   162  func (m *BoolMax) Grows(_ int) {
   163  }
   164  
   165  func (m *BoolMax) Eval(vs []bool) []bool {
   166  	return vs
   167  }
   168  
   169  func (m *BoolMax) Fill(_ int64, value bool, ov bool, _ int64, isEmpty bool, isNull bool) (bool, bool) {
   170  	if !isNull {
   171  		if isEmpty {
   172  			return value, false
   173  		}
   174  		return value || ov, false
   175  	}
   176  	return ov, isEmpty
   177  
   178  }
   179  func (m *BoolMax) Merge(_ int64, _ int64, x bool, y bool, xEmpty bool, yEmpty bool, _ any) (bool, bool) {
   180  	if !yEmpty {
   181  		if !xEmpty {
   182  			return x || y, false
   183  		}
   184  		return y, false
   185  	}
   186  	return x, xEmpty
   187  }
   188  
   189  func (m *BoolMax) MarshalBinary() ([]byte, error) {
   190  	return nil, nil
   191  }
   192  
   193  func (m *BoolMax) UnmarshalBinary(data []byte) error {
   194  	return nil
   195  }
   196  
   197  func NewStrMax() *StrMax {
   198  	return &StrMax{}
   199  }
   200  
   201  func (m *StrMax) Grows(_ int) {
   202  }
   203  
   204  func (m *StrMax) Eval(vs [][]byte) [][]byte {
   205  	return vs
   206  }
   207  
   208  func (m *StrMax) Fill(_ int64, value []byte, ov []byte, _ int64, isEmpty bool, isNull bool) ([]byte, bool) {
   209  	if !isNull {
   210  		if bytes.Compare(value, ov) > 0 || isEmpty {
   211  			return value, false
   212  		}
   213  	}
   214  	return ov, isEmpty
   215  
   216  }
   217  func (m *StrMax) Merge(_ int64, _ int64, x []byte, y []byte, xEmpty bool, yEmpty bool, _ any) ([]byte, bool) {
   218  	if !yEmpty {
   219  		if !xEmpty && bytes.Compare(x, y) > 0 {
   220  			return x, false
   221  		}
   222  		return y, false
   223  	}
   224  	return x, xEmpty
   225  }
   226  
   227  func (m *StrMax) MarshalBinary() ([]byte, error) {
   228  	return nil, nil
   229  }
   230  
   231  func (m *StrMax) UnmarshalBinary(data []byte) error {
   232  	return nil
   233  }
   234  
   235  func NewUuidMax() *UuidMax {
   236  	return &UuidMax{}
   237  }
   238  
   239  func (m *UuidMax) Grows(_ int) {
   240  }
   241  
   242  func (m *UuidMax) Eval(vs []types.Uuid) []types.Uuid {
   243  	return vs
   244  }
   245  
   246  func (m *UuidMax) Fill(_ int64, value types.Uuid, ov types.Uuid, _ int64, isEmpty bool, isNull bool) (types.Uuid, bool) {
   247  	if !isNull {
   248  		if ov.Le(value) || isEmpty {
   249  			return value, false
   250  		}
   251  	}
   252  	return ov, isEmpty
   253  
   254  }
   255  func (m *UuidMax) Merge(_ int64, _ int64, x types.Uuid, y types.Uuid, xEmpty bool, yEmpty bool, _ any) (types.Uuid, bool) {
   256  	if !yEmpty {
   257  		if !xEmpty && x.Gt(y) {
   258  			return x, false
   259  		}
   260  		return y, false
   261  	}
   262  	return x, xEmpty
   263  }
   264  
   265  func (m *UuidMax) MarshalBinary() ([]byte, error) {
   266  	return nil, nil
   267  }
   268  
   269  func (m *UuidMax) UnmarshalBinary(data []byte) error {
   270  	return nil
   271  }