github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/planner/match_test.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 //go:build ignore 16 17 package planner_test 18 19 import ( 20 "sync/atomic" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/vescale/zgraph" 25 "github.com/vescale/zgraph/catalog" 26 "github.com/vescale/zgraph/parser" 27 "github.com/vescale/zgraph/parser/model" 28 "github.com/vescale/zgraph/planner" 29 "github.com/vescale/zgraph/stmtctx" 30 ) 31 32 func TestBuilder_BuildMatch(t *testing.T) { 33 assert := assert.New(t) 34 35 db, err := zgraph.Open(t.TempDir(), nil) 36 assert.Nil(err) 37 38 // Prepare mock catalog. 39 mockGraphs := []string{"graph101", "graph102"} 40 mockLabels := []string{"A", "B", "C"} 41 mockProps := []string{"name", "age"} 42 id := atomic.Int64{} 43 for _, g := range mockGraphs { 44 graphID := id.Add(1) 45 db.Catalog().Apply(&catalog.Patch{ 46 Type: catalog.PatchTypeCreateGraph, 47 Data: &model.GraphInfo{ 48 ID: graphID, 49 Name: model.NewCIStr(g), 50 }, 51 }) 52 for _, l := range mockLabels { 53 db.Catalog().Apply(&catalog.Patch{ 54 Type: catalog.PatchTypeCreateLabel, 55 Data: &catalog.PatchLabel{ 56 GraphID: graphID, 57 LabelInfo: &model.LabelInfo{ 58 ID: id.Add(1), 59 Name: model.NewCIStr(l), 60 }, 61 }, 62 }) 63 64 } 65 var properties []*model.PropertyInfo 66 for i, p := range mockProps { 67 properties = append(properties, &model.PropertyInfo{ 68 ID: uint16(i + 1), 69 Name: model.NewCIStr(p), 70 }) 71 } 72 db.Catalog().Apply(&catalog.Patch{ 73 Type: catalog.PatchTypeCreateProperties, 74 Data: &catalog.PatchProperties{ 75 MaxPropID: uint16(len(properties)), 76 GraphID: graphID, 77 Properties: properties, 78 }, 79 }) 80 81 } 82 83 cases := []struct { 84 query string 85 check func(proj *planner.LogicalProjection) 86 }{ 87 // Catalog information refer: initCatalog 88 { 89 query: "SELECT * FROM MATCH (n:A)->(m:B)", 90 check: func(proj *planner.LogicalProjection) { 91 match := proj.Children()[0].(*planner.LogicalMatch) 92 assert.Equal(1, len(match.Subgraphs)) 93 assert.Equal(1, len(match.Subgraphs[0].Paths)) 94 assert.Equal(2, len(match.Subgraphs[0].Paths[0].Vertices)) 95 }, 96 }, 97 { 98 query: "SELECT * FROM MATCH ( (n:A)->(m:B), (c:C)->(m:B) )", 99 check: func(proj *planner.LogicalProjection) { 100 match := proj.Children()[0].(*planner.LogicalMatch) 101 assert.Equal(1, len(match.Subgraphs)) 102 assert.Equal(2, len(match.Subgraphs[0].Paths)) 103 assert.Equal(2, len(match.Subgraphs[0].Paths[0].Vertices)) 104 }, 105 }, 106 { 107 query: "SELECT * FROM MATCH (n:A)->(m:B), MATCH (c:C)->(m:B)", 108 check: func(proj *planner.LogicalProjection) { 109 match := proj.Children()[0].(*planner.LogicalMatch) 110 assert.Equal(2, len(match.Subgraphs)) 111 assert.Equal(1, len(match.Subgraphs[0].Paths)) 112 assert.Equal(2, len(match.Subgraphs[0].Paths[0].Vertices)) 113 }, 114 }, 115 } 116 117 for _, c := range cases { 118 parser := parser.New() 119 stmt, err := parser.ParseOneStmt(c.query) 120 assert.Nil(err, c.query) 121 122 sc := stmtctx.New(db.Store(), db.Catalog()) 123 sc.SetCurrentGraphName("graph101") 124 125 builder := planner.NewBuilder(sc) 126 plan, err := builder.Build(stmt) 127 assert.Nil(err) 128 projection, ok := plan.(*planner.LogicalProjection) 129 assert.True(ok) 130 c.check(projection) 131 } 132 }