github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/planner/optimizer.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 // Optimize optimizes the plan to the optimal physical plan. 18 func Optimize(plan LogicalPlan) Plan { 19 switch p := plan.(type) { 20 case *LogicalMatch: 21 return optimizeMatch(p) 22 case *LogicalProjection: 23 return optimizeProjection(p) 24 case *LogicalSelection: 25 return optimizeSelection(p) 26 } 27 return plan 28 } 29 30 func optimizeMatch(plan *LogicalMatch) Plan { 31 result := &PhysicalMatch{} 32 result.SetColumns(plan.Columns()) 33 result.Subgraph = plan.Subgraph 34 return result 35 } 36 37 func optimizeProjection(plan *LogicalProjection) Plan { 38 result := &PhysicalProjection{} 39 result.SetColumns(plan.Columns()) 40 result.Exprs = plan.Exprs 41 childPlan := Optimize(plan.Children()[0]) 42 result.SetChildren(childPlan.(PhysicalPlan)) 43 return result 44 } 45 46 func optimizeSelection(plan *LogicalSelection) Plan { 47 result := &PhysicalSelection{} 48 result.SetColumns(plan.Columns()) 49 result.Condition = plan.Condition 50 childPlan := Optimize(plan.Children()[0]) 51 result.SetChildren(childPlan.(PhysicalPlan)) 52 return result 53 }