From 69c72cc491d4fa09e298aa71d872675386e64d8f Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Mon, 2 Oct 2023 14:11:52 -0400 Subject: [PATCH] Replace sourceInfo with Collection --- planner/datasource.go | 16 ++++------------ planner/planner.go | 2 +- planner/select.go | 17 ++++++++--------- planner/type_join.go | 14 ++++++-------- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/planner/datasource.go b/planner/datasource.go index 7a8b0917fe..27b7b2c165 100644 --- a/planner/datasource.go +++ b/planner/datasource.go @@ -15,15 +15,9 @@ import ( "github.com/sourcenetwork/defradb/planner/mapper" ) -// sourceInfo stores info about the data source -type sourceInfo struct { - collectionDescription client.CollectionDescription - // and more -} - type planSource struct { - info sourceInfo - plan planNode + collection client.Collection + plan planNode } func (p *Planner) getSource(parsed *mapper.Select) (planSource, error) { @@ -43,9 +37,7 @@ func (p *Planner) getCollectionScanPlan(parsed *mapper.Select) (planSource, erro } return planSource{ - plan: scan, - info: sourceInfo{ - collectionDescription: col.Description(), - }, + plan: scan, + collection: col, }, nil } diff --git a/planner/planner.go b/planner/planner.go index bcb0653633..295f8a1447 100644 --- a/planner/planner.go +++ b/planner/planner.go @@ -344,7 +344,7 @@ func (p *Planner) expandGroupNodePlan(topNodeSelect *selectTopNode) error { childSelect, pipe, false, - &topNodeSelect.selectNode.sourceInfo, + topNodeSelect.selectNode.collection, ) if err != nil { return err diff --git a/planner/select.go b/planner/select.go index b53288bfb5..dbe8796580 100644 --- a/planner/select.go +++ b/planner/select.go @@ -14,6 +14,7 @@ import ( cid "github.com/ipfs/go-cid" "github.com/sourcenetwork/immutable" + "github.com/sourcenetwork/defradb/client" "github.com/sourcenetwork/defradb/client/request" "github.com/sourcenetwork/defradb/core" "github.com/sourcenetwork/defradb/db/base" @@ -101,9 +102,7 @@ type selectNode struct { // was created origSource planNode - // cache information about the original data source - // collection name, meta-data, etc. - sourceInfo sourceInfo + collection client.Collection // top level filter expression // filter is split between select, scan, and typeIndexJoin. @@ -244,7 +243,7 @@ func (n *selectNode) initSource() ([]aggregateNode, error) { } n.source = sourcePlan.plan n.origSource = sourcePlan.plan - n.sourceInfo = sourcePlan.info + n.collection = sourcePlan.collection // split filter // apply the root filter to the source @@ -278,7 +277,7 @@ func (n *selectNode) initSource() ([]aggregateNode, error) { // instead of a prefix scan + filter via the Primary Index (0), like here: spans := make([]core.Span, len(n.selectReq.DocKeys.Value())) for i, docKey := range n.selectReq.DocKeys.Value() { - dockeyIndexKey := base.MakeDocKey(sourcePlan.info.collectionDescription, docKey) + dockeyIndexKey := base.MakeDocKey(sourcePlan.collection.Description(), docKey) spans[i] = core.NewSpan(dockeyIndexKey, dockeyIndexKey.PrefixEnd()) } origScan.Spans(core.NewSpans(spans...)) @@ -404,7 +403,7 @@ func (p *Planner) SelectFromSource( selectReq *mapper.Select, source planNode, fromCollection bool, - providedSourceInfo *sourceInfo, + collection client.Collection, ) (planNode, error) { s := &selectNode{ planner: p, @@ -419,8 +418,8 @@ func (p *Planner) SelectFromSource( orderBy := selectReq.OrderBy groupBy := selectReq.GroupBy - if providedSourceInfo != nil { - s.sourceInfo = *providedSourceInfo + if collection != nil { + s.collection = collection } if fromCollection { @@ -429,7 +428,7 @@ func (p *Planner) SelectFromSource( return nil, err } - s.sourceInfo = sourceInfo{col.Description()} + s.collection = col } aggregates, err := s.initFields(selectReq) diff --git a/planner/type_join.go b/planner/type_join.go index 12f08e7c24..791fe522e6 100644 --- a/planner/type_join.go +++ b/planner/type_join.go @@ -81,8 +81,7 @@ func (p *Planner) makeTypeIndexJoin( var joinPlan planNode var err error - desc := parent.sourceInfo.collectionDescription - typeFieldDesc, ok := desc.Schema.GetField(subType.Name) + typeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name) if !ok { return nil, client.NewErrFieldNotExist(subType.Name) } @@ -245,7 +244,7 @@ func (p *Planner) makeTypeJoinOne( } // get the correct sub field schema type (collection) - subTypeFieldDesc, ok := parent.sourceInfo.collectionDescription.Schema.GetField(subType.Name) + subTypeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name) if !ok { return nil, client.NewErrFieldNotExist(subType.Name) } @@ -262,7 +261,7 @@ func (p *Planner) makeTypeJoinOne( subTypeSchema := subTypeCollection.Schema() subTypeField, subTypeFieldNameFound := subTypeCollection.Description().GetFieldByRelation( subTypeFieldDesc.RelationName, - parent.sourceInfo.collectionDescription.Name, + parent.collection.Name(), subTypeFieldDesc.Name, &subTypeSchema, ) @@ -369,8 +368,7 @@ func (n *typeJoinOne) valuesPrimary(doc core.Doc) (core.Doc, error) { // create the collection key for the sub doc slct := n.subType.(*selectTopNode).selectNode - desc := slct.sourceInfo.collectionDescription - subKeyIndexKey := base.MakeDocKey(desc, subDocKeyStr) + subKeyIndexKey := base.MakeDocKey(slct.collection.Description(), subDocKeyStr) // reset span n.spans = core.NewSpans(core.NewSpan(subKeyIndexKey, subKeyIndexKey.PrefixEnd())) @@ -477,7 +475,7 @@ func (p *Planner) makeTypeJoinMany( return nil, err } - subTypeFieldDesc, ok := parent.sourceInfo.collectionDescription.Schema.GetField(subType.Name) + subTypeFieldDesc, ok := parent.collection.Schema().GetField(subType.Name) if !ok { return nil, client.NewErrFieldNotExist(subType.Name) } @@ -490,7 +488,7 @@ func (p *Planner) makeTypeJoinMany( subTypeSchema := subTypeCollection.Schema() rootField, rootNameFound := subTypeCollection.Description().GetFieldByRelation( subTypeFieldDesc.RelationName, - parent.sourceInfo.collectionDescription.Name, + parent.collection.Name(), subTypeFieldDesc.Name, &subTypeSchema, )