Skip to content

Commit

Permalink
statistics VIndexScan
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaiba committed Mar 27, 2024
1 parent ca3cc09 commit 807b10c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions internal/engine/cost/datatypes/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (f *Field) QualifiedColumn() *ColumnDef {

type Schema struct {
Fields []Field
// index
}

func NewSchema(fields ...Field) *Schema {
Expand Down
55 changes: 45 additions & 10 deletions internal/engine/cost/virtual_plan/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,74 @@ import (
ds "github.com/kwilteam/kwil-db/internal/engine/cost/datasource"
)

type VScanOp struct {
type VTableScanOp struct {
ds ds.DataSource
projection []string
}

func (s *VScanOp) String() string {
return fmt.Sprintf("VScan: schema=%s, projection=%s",
func (s *VTableScanOp) String() string {
return fmt.Sprintf("VTableScan: schema=%s, projection=%s",
s.ds.Schema(), s.projection)
}

func (s *VScanOp) Schema() *datatypes.Schema {
func (s *VTableScanOp) Schema() *datatypes.Schema {
return s.ds.Schema().Project(s.projection...)
}

func (s *VScanOp) Inputs() []VirtualPlan {
func (s *VTableScanOp) Inputs() []VirtualPlan {
return []VirtualPlan{}
}

func (s *VScanOp) Execute() *ds.Result {
func (s *VTableScanOp) Execute() *ds.Result {
return s.ds.Scan(s.projection...)
}

func (s *VScanOp) Statistics() *datatypes.Statistics {
func (s *VTableScanOp) Statistics() *datatypes.Statistics {
return &datatypes.Statistics{}
}

func (s *VScanOp) Cost() int64 {
func (s *VTableScanOp) Cost() int64 {
return 0
}

func VScan(datasource ds.SchemaSource, projection ...string) VirtualPlan {
func VTableScan(datasource ds.SchemaSource, projection ...string) VirtualPlan {
ds := ds.SchemaSourceToDataSource(datasource)
return &VScanOp{ds: ds, projection: projection}
return &VTableScanOp{ds: ds, projection: projection}
}

type VIndexScanOp struct {
ds ds.DataSource
projection []string
}

func (s *VIndexScanOp) String() string {
return fmt.Sprintf("VIndexScan: schema=%s, projection=%s",
s.ds.Schema(), s.projection)
}

func (s *VIndexScanOp) Schema() *datatypes.Schema {
return s.ds.Schema().Project(s.projection...)
}

func (s *VIndexScanOp) Inputs() []VirtualPlan {
return []VirtualPlan{}
}

func (s *VIndexScanOp) Execute() *ds.Result {
return s.ds.Scan(s.projection...)
}

func (s *VIndexScanOp) Statistics() *datatypes.Statistics {
return &datatypes.Statistics{}
}

func (s *VIndexScanOp) Cost() int64 {
return 0
}

func VIndexScan(datasource ds.SchemaSource, projection ...string) VirtualPlan {
ds := ds.SchemaSourceToDataSource(datasource)
return &VIndexScanOp{ds: ds, projection: projection}
}

type VProjectionOp struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/engine/cost/virtual_plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type VirtualPlan interface {
Schema() *datatypes.Schema
Inputs() []VirtualPlan

// Execute executes the plan and returns the result
// This for testing purposes
Execute() *datasource.Result
Statistics() *datatypes.Statistics
Cost() int64
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/cost/virtual_plan/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewPlanner() *defaultVirtualPlanner {
func (q *defaultVirtualPlanner) ToPlan(logicalPlan logical_plan.LogicalPlan) VirtualPlan {
switch p := logicalPlan.(type) {
case *logical_plan.ScanOp:
return VScan(p.DataSource(), p.Projection()...)
return VTableScan(p.DataSource(), p.Projection()...)
case *logical_plan.ProjectionOp:
input := q.ToPlan(p.Inputs()[0])
selectExprs := make([]VirtualExpr, 0, len(p.Exprs()))
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/cost/virtual_plan/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ func Example_QueryPlanner_CreateVirtualPlan() {
//
// VProjection: [state@1 username@2]
// VSelection: age@0 = 20
// VScan: schema=[id/int, username/string, age/int, state/string, wallet/string], projection=[age state username]
// VTableScan: schema=[id/int, username/string, age/int, state/string, wallet/string], projection=[age state username]
}

0 comments on commit 807b10c

Please sign in to comment.