github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/operator_in.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 function
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    21  	"golang.org/x/exp/constraints"
    22  )
    23  
    24  type TGenericOfIn interface {
    25  	constraints.Integer | constraints.Float | bool | types.Uuid |
    26  		types.Time | types.Timestamp | types.Date | types.Datetime | types.Decimal64 | types.Decimal128
    27  }
    28  
    29  type opOperatorFixedIn[T TGenericOfIn] struct {
    30  	ready bool
    31  	mp    map[T]bool
    32  }
    33  
    34  type opOperatorStrIn struct {
    35  	ready bool
    36  	mp    map[string]bool
    37  }
    38  
    39  func newOpOperatorFixedIn[T TGenericOfIn]() *opOperatorFixedIn[T] {
    40  	op := new(opOperatorFixedIn[T])
    41  	op.ready = false
    42  	return op
    43  }
    44  
    45  func newOpOperatorStrIn() *opOperatorStrIn {
    46  	op := new(opOperatorStrIn)
    47  	op.ready = false
    48  	return op
    49  }
    50  
    51  func (op *opOperatorFixedIn[T]) init(tuple *vector.Vector) {
    52  	op.ready = true
    53  
    54  	if tuple.IsConstNull() {
    55  		op.mp = make(map[T]bool)
    56  		return
    57  	}
    58  	p := vector.GenerateFunctionFixedTypeParameter[T](tuple)
    59  
    60  	if tuple.IsConst() {
    61  		v, _ := p.GetValue(0)
    62  		op.mp = make(map[T]bool, 1)
    63  		op.mp[v] = true
    64  		return
    65  	}
    66  
    67  	op.mp = make(map[T]bool, tuple.Length())
    68  	for i := uint64(0); i < uint64(tuple.Length()); i++ {
    69  		v, null := p.GetValue(i)
    70  		if !null {
    71  			op.mp[v] = true
    72  		}
    73  	}
    74  }
    75  
    76  func (op *opOperatorStrIn) init(tuple *vector.Vector) {
    77  	op.ready = true
    78  
    79  	if tuple.IsConstNull() {
    80  		op.mp = make(map[string]bool)
    81  		return
    82  	}
    83  	p := vector.GenerateFunctionStrParameter(tuple)
    84  
    85  	if tuple.IsConst() {
    86  		v, _ := p.GetStrValue(0)
    87  		op.mp = make(map[string]bool, 1)
    88  		op.mp[string(v)] = true
    89  		return
    90  	}
    91  
    92  	op.mp = make(map[string]bool)
    93  	for i := uint64(0); i < uint64(tuple.Length()); i++ {
    94  		v, null := p.GetStrValue(i)
    95  		if !null {
    96  			op.mp[string(v)] = true
    97  		}
    98  	}
    99  }
   100  
   101  func (op *opOperatorFixedIn[T]) operatorIn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   102  	if !op.ready {
   103  		op.init(parameters[1])
   104  	}
   105  
   106  	p := vector.GenerateFunctionFixedTypeParameter[T](parameters[0])
   107  	rs := vector.MustFunctionResult[bool](result)
   108  	for i := uint64(0); i < uint64(length); i++ {
   109  		v, null := p.GetValue(i)
   110  		if null {
   111  			if err := rs.Append(false, true); err != nil {
   112  				return err
   113  			}
   114  		} else {
   115  			_, ok := op.mp[v]
   116  			if err := rs.Append(ok, false); err != nil {
   117  				return err
   118  			}
   119  		}
   120  	}
   121  	return nil
   122  }
   123  
   124  func (op *opOperatorFixedIn[T]) operatorNotIn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   125  	if !op.ready {
   126  		op.init(parameters[1])
   127  	}
   128  
   129  	p := vector.GenerateFunctionFixedTypeParameter[T](parameters[0])
   130  	rs := vector.MustFunctionResult[bool](result)
   131  	for i := uint64(0); i < uint64(length); i++ {
   132  		v, null := p.GetValue(i)
   133  		if null {
   134  			if err := rs.Append(false, true); err != nil {
   135  				return err
   136  			}
   137  		} else {
   138  			_, ok := op.mp[v]
   139  			if err := rs.Append(!ok, false); err != nil {
   140  				return err
   141  			}
   142  		}
   143  	}
   144  	return nil
   145  }
   146  
   147  func (op *opOperatorStrIn) operatorIn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   148  	if !op.ready {
   149  		op.init(parameters[1])
   150  	}
   151  
   152  	p := vector.GenerateFunctionStrParameter(parameters[0])
   153  	rs := vector.MustFunctionResult[bool](result)
   154  	for i := uint64(0); i < uint64(length); i++ {
   155  		v, null := p.GetStrValue(i)
   156  		if null {
   157  			if err := rs.Append(false, true); err != nil {
   158  				return err
   159  			}
   160  		} else {
   161  			_, ok := op.mp[string(v)]
   162  			if err := rs.Append(ok, false); err != nil {
   163  				return err
   164  			}
   165  		}
   166  	}
   167  	return nil
   168  }
   169  
   170  func (op *opOperatorStrIn) operatorNotIn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
   171  	if !op.ready {
   172  		op.init(parameters[1])
   173  	}
   174  
   175  	p := vector.GenerateFunctionStrParameter(parameters[0])
   176  	rs := vector.MustFunctionResult[bool](result)
   177  	for i := uint64(0); i < uint64(length); i++ {
   178  		v, null := p.GetStrValue(i)
   179  		if null {
   180  			if err := rs.Append(false, true); err != nil {
   181  				return err
   182  			}
   183  		} else {
   184  			_, ok := op.mp[string(v)]
   185  			if err := rs.Append(!ok, false); err != nil {
   186  				return err
   187  			}
   188  		}
   189  	}
   190  	return nil
   191  }