Skip to content

Commit

Permalink
Lab 1 Writeups
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerie Kwek authored and Valerie Kwek committed Oct 16, 2024
1 parent cf0dea8 commit 6a0a294
Show file tree
Hide file tree
Showing 19 changed files with 285 additions and 215 deletions.
4 changes: 2 additions & 2 deletions godb/agg_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func extractGroupByKeyTuple(a *Aggregator, t *Tuple) (*Tuple, error) {
fields = append(fields, value)
}
groupByKeyTuple := &Tuple{
Desc: t.Desc,
Desc: *a.Descriptor(),
Fields: fields,
Rid: t.Rid,
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func getFinalizedTuplesIterator(a *Aggregator, groupByList []*Tuple, aggState ma
}
curr -= 1
aggTuple.Desc = *a.Descriptor()
return aggTuple, nil // replace me
return aggTuple, nil
}
return nil, nil
}
Expand Down
138 changes: 55 additions & 83 deletions godb/agg_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type SumAggState struct {

func (a *SumAggState) Copy() AggState {
// TODO: some code goes here
return &SumAggState{a.alias, a.expr, a.sum} // replace me
return &SumAggState{a.alias, a.expr, a.sum}
}

func intAggGetter(v DBValue) any {
Expand Down Expand Up @@ -122,7 +122,7 @@ func (a *SumAggState) GetTupleDesc() *TupleDesc {
fts := []FieldType{ft}
td := TupleDesc{}
td.Fields = fts
return &td // replace me
return &td
}

func (a *SumAggState) Finalize() *Tuple {
Expand All @@ -134,40 +134,40 @@ func (a *SumAggState) Finalize() *Tuple {
} else {
f = StringField{a.sum.(string)}
}
return &Tuple{*td, []DBValue{f}, nil} // replace me
return &Tuple{*td, []DBValue{f}, nil}
}

// Implements the aggregation state for AVG
// Note that we always AddTuple() at least once before Finalize()
// so no worries for divide-by-zero
type AvgAggState struct {
// TODO: some code goes here
alias string
expr Expr
total int64
size int64
alias string
expr Expr
sumOfVals int64
numVals int64
}

func (a *AvgAggState) Copy() AggState {
// TODO: some code goes here
return &AvgAggState{a.alias, a.expr, a.total, a.size} // replace me
return &AvgAggState{a.alias, a.expr, 0, 0}
}

func (a *AvgAggState) Init(alias string, expr Expr) error {
// TODO: some code goes here
a.total = 0
a.size = 0
a.expr = expr
a.alias = alias
return nil // replace me
a.expr = expr
a.numVals = 0
a.sumOfVals = 0
return nil
}

func (a *AvgAggState) AddTuple(t *Tuple) {
// TODO: some code goes here
dbVal, err := a.expr.EvalExpr(t)
if err == nil {
a.total += dbVal.(IntField).Value
a.size += 1
dbVal, _ := a.expr.EvalExpr(t)
if intAggGetter(dbVal) != nil {
a.sumOfVals += intAggGetter(dbVal).(int64)
a.numVals += 1
}
}

Expand All @@ -177,144 +177,116 @@ func (a *AvgAggState) GetTupleDesc() *TupleDesc {
fts := []FieldType{ft}
td := TupleDesc{}
td.Fields = fts
return &td // replace me
return &td
}

func (a *AvgAggState) Finalize() *Tuple {
// TODO: some code goes here
td := a.GetTupleDesc()
f := IntField{a.total / a.size}
return &Tuple{*td, []DBValue{f}, nil} // replace me
avg := IntField{a.sumOfVals / a.numVals}
return &Tuple{*td, []DBValue{avg}, nil}
}

// Implements the aggregation state for MAX
// Note that we always AddTuple() at least once before Finalize()
// so no worries for NaN max
type MaxAggState struct {
// TODO: some code goes here
alias string
expr Expr
max any
alias string
expr Expr
maxVal DBValue
}

func (a *MaxAggState) Copy() AggState {
// TODO: some code goes here
return &MaxAggState{a.alias, a.expr, a.max} // replace me
return &MaxAggState{a.alias, a.expr, a.maxVal}
}

func (a *MaxAggState) Init(alias string, expr Expr) error {
// TODO: some code goes here
a.max = nil
a.expr = expr
a.alias = alias
return nil // replace me
a.expr = expr
return nil
}

func (a *MaxAggState) AddTuple(t *Tuple) {
// TODO: some code goes here
dbVal, _ := a.expr.EvalExpr(t)
intValue := intAggGetter(dbVal)
if intValue != nil {
if a.max == nil {
a.max = intValue.(int64)
}
if a.max.(int64) < intValue.(int64) {
a.max = intValue.(int64)
}
} else {
strValue := stringAggGetter(dbVal).(string)
if a.max == nil {
a.max = strValue
}
if a.max.(string) < strValue {
a.max = strValue
}
if a.maxVal == nil {
a.maxVal = dbVal
} else if dbVal.EvalPred(a.maxVal, OpGt) {
a.maxVal = dbVal
}
}

func (a *MaxAggState) GetTupleDesc() *TupleDesc {
// TODO: some code goes here
ft := FieldType{a.alias, "", IntType}
var ft FieldType
if intAggGetter(a.maxVal) != nil {
ft = FieldType{a.alias, "", StringType}
} else {
ft = FieldType{a.alias, "", IntType}
}
fts := []FieldType{ft}
td := TupleDesc{}
td.Fields = fts
return &td // replace me
return &td
}

func (a *MaxAggState) Finalize() *Tuple {
// TODO: some code goes here
td := a.GetTupleDesc()
var f DBValue
if intValue, ok := a.max.(int64); ok {
f = IntField{intValue}
} else {
f = StringField{a.max.(string)}
}
return &Tuple{*td, []DBValue{f}, nil} // replace me
return &Tuple{*td, []DBValue{a.maxVal}, nil}
}

// Implements the aggregation state for MIN
// Note that we always AddTuple() at least once before Finalize()
// so no worries for NaN min
type MinAggState struct {
// TODO: some code goes here
alias string
expr Expr
min any
alias string
expr Expr
minVal DBValue
}

func (a *MinAggState) Copy() AggState {
// TODO: some code goes here
return &MinAggState{a.alias, a.expr, a.min} // replace me
return &MinAggState{a.alias, a.expr, a.minVal}
}

func (a *MinAggState) Init(alias string, expr Expr) error {
// TODO: some code goes here
a.min = nil
a.expr = expr
a.alias = alias
return nil // replace me
a.expr = expr
return nil
}

func (a *MinAggState) AddTuple(t *Tuple) {
// TODO: some code goes here
dbVal, _ := a.expr.EvalExpr(t)
intValue := intAggGetter(dbVal)
if intValue != nil {
if a.min == nil {
a.min = intValue.(int64)
}
if a.min.(int64) > intValue.(int64) {
a.min = intValue.(int64)
}
} else {
strValue := stringAggGetter(dbVal).(string)
if a.min == nil {
a.min = strValue
}
if a.min.(string) > strValue {
a.min = strValue
}
if a.minVal == nil {
a.minVal = dbVal
} else if dbVal.EvalPred(a.minVal, OpLt) {
a.minVal = dbVal
}
}

func (a *MinAggState) GetTupleDesc() *TupleDesc {
// TODO: some code goes here
ft := FieldType{a.alias, "", IntType}
var ft FieldType
if intAggGetter(a.minVal) != nil {
ft = FieldType{a.alias, "", StringType}
} else {
ft = FieldType{a.alias, "", IntType}
}
fts := []FieldType{ft}
td := TupleDesc{}
td.Fields = fts
return &td // replace me
return &td
}

func (a *MinAggState) Finalize() *Tuple {
// TODO: some code goes here
td := a.GetTupleDesc()
var f DBValue
if intValue, ok := a.min.(int64); ok {
f = IntField{intValue}
} else {
f = StringField{a.min.(string)}
}
return &Tuple{*td, []DBValue{f}, nil} // replace me
return &Tuple{*td, []DBValue{a.minVal}, nil}
}
34 changes: 18 additions & 16 deletions godb/delete_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type DeleteOp struct {
// child Operator from the specified DBFile.
func NewDeleteOp(deleteFile DBFile, child Operator) *DeleteOp {
// TODO: some code goes here
return &DeleteOp{deleteFile, child} // replace me
return &DeleteOp{deleteFile: deleteFile, child: child}
}

// The delete TupleDesc is a one column descriptor with an integer field named
// "count".
func (i *DeleteOp) Descriptor() *TupleDesc {
// TODO: some code goes here
return &TupleDesc{
Fields: []FieldType{{Fname: "count", Ftype: IntType}},
} // replace me
Fields: []FieldType{
{Fname: "count", Ftype: IntType},
},
}
}

// Return an iterator that deletes all of the tuples from the child iterator
Expand All @@ -28,31 +30,31 @@ func (i *DeleteOp) Descriptor() *TupleDesc {
// Tuples should be deleted using the [DBFile.deleteTuple] method.
func (dop *DeleteOp) Iterator(tid TransactionID) (func() (*Tuple, error), error) {
// TODO: some code goes here
childIter, err := dop.child.Iterator(tid)
if err != nil {
return nil, err
}
insertions := int64(0)
deletions := int64(0)
return func() (*Tuple, error) {
childIterator, err := dop.child.Iterator(tid)
if err != nil {
return nil, err
}

for {
tuple, err := childIter()
tuple, err := childIterator()
if err != nil {
return nil, err
}

if tuple == nil {
break
}
err = dop.deleteFile.deleteTuple(tuple, tid)
if err != nil {
return nil, err
}
insertions++

dop.deleteFile.deleteTuple(tuple, tid)
deletions += 1
}
return &Tuple{
Desc: *dop.Descriptor(),
Fields: []DBValue{
IntField{insertions},
IntField{deletions},
},
}, nil
}, nil // replace me
}, nil
}
22 changes: 11 additions & 11 deletions godb/filter_op.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package godb

// import (
// "fmt"
// "go/types"
// )

type Filter struct {
op BoolOp
left Expr
Expand All @@ -29,30 +24,35 @@ func (f *Filter) Descriptor() *TupleDesc {
// HINT: you can use [types.evalPred] to compare two values.
func (f *Filter) Iterator(tid TransactionID) (func() (*Tuple, error), error) {
// TODO: some code goes here
childIter, err := f.child.Iterator(tid)
childIteratorFunc, err := f.child.Iterator(tid)
if err != nil {
return nil, err
}

return func() (*Tuple, error) {
for {
tuple, err := childIter()
tuple, err := childIteratorFunc()
if err != nil {
return nil, err
}

if tuple == nil {
return nil, nil
}
leftFieldVal, err := f.left.EvalExpr(tuple)

leftVal, err := f.left.EvalExpr(tuple)
if err != nil {
return nil, err
}
rightFieldVal, err := f.right.EvalExpr(tuple)

rightVal, err := f.right.EvalExpr(tuple)
if err != nil {
return nil, err
}
if leftFieldVal.EvalPred(rightFieldVal, f.op) {

if leftVal.EvalPred(rightVal, f.op) {
return tuple, nil
}
}
}, nil // replace me
}, nil
}
Loading

0 comments on commit 6a0a294

Please sign in to comment.