github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/executor/executor.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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 executor
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/vescale/zgraph/datum"
    21  	"github.com/vescale/zgraph/planner"
    22  	"github.com/vescale/zgraph/stmtctx"
    23  )
    24  
    25  // Executor is the physical implementation of an algebra operator.
    26  type Executor interface {
    27  	base() *baseExecutor
    28  	Columns() planner.ResultColumns
    29  	Open(context.Context) error
    30  	Next(context.Context) (datum.Row, error)
    31  	Close() error
    32  }
    33  
    34  type baseExecutor struct {
    35  	sc       *stmtctx.Context
    36  	id       int
    37  	columns  planner.ResultColumns // output columns
    38  	children []Executor
    39  }
    40  
    41  func newBaseExecutor(sc *stmtctx.Context, cols planner.ResultColumns, id int, children ...Executor) baseExecutor {
    42  	e := baseExecutor{
    43  		children: children,
    44  		sc:       sc,
    45  		id:       id,
    46  		columns:  cols,
    47  	}
    48  	return e
    49  }
    50  
    51  // base returns the baseExecutor of an executor, don't override this method!
    52  func (e *baseExecutor) base() *baseExecutor {
    53  	return e
    54  }
    55  
    56  // Open initializes children recursively and "childrenResults" according to children's schemas.
    57  func (e *baseExecutor) Open(ctx context.Context) error {
    58  	for _, child := range e.children {
    59  		err := child.Open(ctx)
    60  		if err != nil {
    61  			return err
    62  		}
    63  	}
    64  	return nil
    65  }
    66  
    67  // Next fills multiple rows into a chunk.
    68  func (e *baseExecutor) Next(context.Context) (datum.Row, error) {
    69  	return nil, nil
    70  }
    71  
    72  // Close closes all executors and release all resources.
    73  func (e *baseExecutor) Close() error {
    74  	var firstErr error
    75  	for _, src := range e.children {
    76  		if err := src.Close(); err != nil && firstErr == nil {
    77  			firstErr = err
    78  		}
    79  	}
    80  	return firstErr
    81  }
    82  
    83  func (e *baseExecutor) Columns() planner.ResultColumns {
    84  	return e.columns
    85  }