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

     1  // Copyright 2021 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 restrict
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/sql/colexec"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  )
    27  
    28  func String(arg any, buf *bytes.Buffer) {
    29  	ap := arg.(*Argument)
    30  	buf.WriteString(fmt.Sprintf("filter(%s)", ap.E))
    31  }
    32  
    33  func Prepare(_ *process.Process, _ any) error {
    34  	return nil
    35  }
    36  
    37  func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    38  	bat := proc.InputBatch()
    39  	if bat == nil {
    40  		return true, nil
    41  	}
    42  	if bat.Length() == 0 {
    43  		return false, nil
    44  	}
    45  	ap := arg.(*Argument)
    46  	anal := proc.GetAnalyze(idx)
    47  	anal.Start()
    48  	defer anal.Stop()
    49  	anal.Input(bat, isFirst)
    50  	vec, err := colexec.EvalExpr(bat, proc, ap.E)
    51  	if err != nil {
    52  		bat.Clean(proc.Mp())
    53  		return false, err
    54  	}
    55  	defer vec.Free(proc.Mp())
    56  	if proc.OperatorOutofMemory(int64(vec.Size())) {
    57  		return false, moerr.NewOOM(proc.Ctx)
    58  	}
    59  	anal.Alloc(int64(vec.Size()))
    60  	if !vec.GetType().IsBoolean() {
    61  		return false, moerr.NewInvalidInput(proc.Ctx, "filter condition is not boolean")
    62  	}
    63  	bs := vector.GetColumn[bool](vec)
    64  	if vec.IsScalar() {
    65  		if !bs[0] {
    66  			bat.Shrink(nil)
    67  		}
    68  	} else {
    69  		sels := proc.Mp().GetSels()
    70  		for i, b := range bs {
    71  			if b {
    72  				sels = append(sels, int64(i))
    73  			}
    74  		}
    75  		bat.Shrink(sels)
    76  		proc.Mp().PutSels(sels)
    77  	}
    78  	anal.Output(bat, isLast)
    79  	proc.SetInputBatch(bat)
    80  	return false, nil
    81  }