github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/projection/projection.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 projection
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/colexec"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func String(arg any, buf *bytes.Buffer) {
    26  	n := arg.(*Argument)
    27  	buf.WriteString("projection(")
    28  	for i, e := range n.Es {
    29  		if i > 0 {
    30  			buf.WriteString(",")
    31  		}
    32  		buf.WriteString(e.String())
    33  	}
    34  	buf.WriteString(")")
    35  }
    36  
    37  func Prepare(_ *process.Process, _ any) error {
    38  	return nil
    39  }
    40  
    41  func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    42  	anal := proc.GetAnalyze(idx)
    43  	anal.Start()
    44  	defer anal.Stop()
    45  
    46  	bat := proc.InputBatch()
    47  	if bat == nil {
    48  		proc.SetInputBatch(nil)
    49  		return true, nil
    50  	}
    51  	if bat.Length() == 0 {
    52  		return false, nil
    53  	}
    54  	anal.Input(bat, isFirst)
    55  	ap := arg.(*Argument)
    56  	rbat := batch.NewWithSize(len(ap.Es))
    57  	for i, e := range ap.Es {
    58  		vec, err := colexec.EvalExpr(bat, proc, e)
    59  		if err != nil || vec.ConstExpand(false, proc.Mp()) == nil {
    60  			bat.Clean(proc.Mp())
    61  			rbat.Clean(proc.Mp())
    62  			return false, err
    63  		}
    64  		rbat.Vecs[i] = vec
    65  	}
    66  	for i, vec := range bat.Vecs {
    67  		isSame := false
    68  		for _, rVec := range rbat.Vecs {
    69  			if vec == rVec {
    70  				bat.Vecs[i] = nil
    71  				isSame = true
    72  				break
    73  			}
    74  		}
    75  		if !isSame && vec != nil {
    76  			anal.Alloc(int64(vec.Size()))
    77  		}
    78  	}
    79  	rbat.Zs = bat.Zs
    80  	bat.Zs = nil
    81  	bat.Clean(proc.Mp())
    82  	anal.Output(rbat, isLast)
    83  	proc.SetInputBatch(rbat)
    84  	return false, nil
    85  }