github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/planner/plan.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 planner
    16  
    17  import (
    18  	"strconv"
    19  )
    20  
    21  type Plan interface {
    22  	// ID gets the ID.
    23  	ID() int
    24  	// TP gets the plan type.
    25  	TP() string
    26  	// Columns gets the result columns.
    27  	Columns() ResultColumns
    28  	// SetColumns sets the result columns
    29  	SetColumns(cols ResultColumns)
    30  	// ExplainID gets the ID in explain statement
    31  	ExplainID() string
    32  	// ExplainInfo returns operator information to be explained.
    33  	ExplainInfo() string
    34  }
    35  
    36  type LogicalPlan interface {
    37  	Plan
    38  
    39  	// Children returns all the children.
    40  	Children() []LogicalPlan
    41  	// SetChildren sets the children for the plan.
    42  	SetChildren(...LogicalPlan)
    43  	// SetChild sets the ith child for the plan.
    44  	SetChild(i int, child LogicalPlan)
    45  }
    46  
    47  type PhysicalPlan interface {
    48  	Plan
    49  
    50  	// Children returns all the children.
    51  	Children() []PhysicalPlan
    52  	// SetChildren sets the children for the plan.
    53  	SetChildren(...PhysicalPlan)
    54  	// SetChild sets the ith child for the plan.
    55  	SetChild(i int, child PhysicalPlan)
    56  }
    57  
    58  type basePlan struct {
    59  	tp      string
    60  	id      int
    61  	columns ResultColumns
    62  }
    63  
    64  // ID implements the Plan interface.
    65  func (p *basePlan) ID() int {
    66  	return p.id
    67  }
    68  
    69  // TP implements the Plan interface.
    70  func (p *basePlan) TP() string {
    71  	return p.tp
    72  }
    73  
    74  // Columns implements the Plan.Columns interface.
    75  func (p *basePlan) Columns() ResultColumns {
    76  	return p.columns
    77  }
    78  
    79  func (p *basePlan) SetColumns(cols ResultColumns) {
    80  	p.columns = cols
    81  }
    82  
    83  // ExplainID implements the Plan interface.
    84  func (p *basePlan) ExplainID() string {
    85  	return p.tp + "_" + strconv.Itoa(p.id)
    86  }
    87  
    88  // ExplainInfo implements Plan interface.
    89  func (*basePlan) ExplainInfo() string {
    90  	return "N/A"
    91  }
    92  
    93  type baseLogicalPlan struct {
    94  	basePlan
    95  
    96  	self     LogicalPlan
    97  	children []LogicalPlan
    98  }
    99  
   100  // Children implements LogicalPlan Children interface.
   101  func (p *baseLogicalPlan) Children() []LogicalPlan {
   102  	return p.children
   103  }
   104  
   105  // SetChildren implements LogicalPlan SetChildren interface.
   106  func (p *baseLogicalPlan) SetChildren(children ...LogicalPlan) {
   107  	p.children = children
   108  }
   109  
   110  // SetChild implements LogicalPlan SetChild interface.
   111  func (p *baseLogicalPlan) SetChild(i int, child LogicalPlan) {
   112  	p.children[i] = child
   113  }
   114  
   115  func (p *baseLogicalPlan) Columns() ResultColumns {
   116  	if p.columns == nil && len(p.Children()) == 1 {
   117  		// default implementation for plans has only one child: proprgate child columns.
   118  		// multi-children plans are likely to have particular implementation.
   119  		p.columns = p.Children()[0].Columns()
   120  	}
   121  	return p.columns
   122  }
   123  
   124  type basePhysicalPlan struct {
   125  	basePlan
   126  
   127  	self     PhysicalPlan
   128  	children []PhysicalPlan
   129  }
   130  
   131  // Children implements PhysicalPlan Children interface.
   132  func (p *basePhysicalPlan) Children() []PhysicalPlan {
   133  	return p.children
   134  }
   135  
   136  // SetChildren implements PhysicalPlan SetChildren interface.
   137  func (p *basePhysicalPlan) SetChildren(children ...PhysicalPlan) {
   138  	p.children = children
   139  }
   140  
   141  // SetChild implements PhysicalPlan SetChild interface.
   142  func (p *basePhysicalPlan) SetChild(i int, child PhysicalPlan) {
   143  	p.children[i] = child
   144  }
   145  
   146  func (p *basePhysicalPlan) Columns() ResultColumns {
   147  	if p.columns == nil && len(p.Children()) == 1 {
   148  		// default implementation for plans has only one child: proprgate child columns.
   149  		// multi-children plans are likely to have particular implementation.
   150  		p.columns = p.Children()[0].Columns()
   151  	}
   152  	return p.columns
   153  }
   154  
   155  // LogicalDual represents the plan which returns empty result set.
   156  type LogicalDual struct {
   157  	baseLogicalPlan
   158  }