Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor reducers #1481

Merged
merged 4 commits into from
Oct 15, 2020
Merged

refactor reducers #1481

merged 4 commits into from
Oct 15, 2020

Conversation

mccanne
Copy link
Collaborator

@mccanne mccanne commented Oct 13, 2020

This commit refactors the reducer package to be more go-like and
ergonomic. These changes will make it easier to add the "where"
clause to reducers and add new reducer functions.

The reducer compilation logic moved to groupby, as a temporary stop
on the way to zq/compiler (see issue #1162). reducer/compile is
now gone and reducers don't know about rows.

The multi-type math got moved to zq/anymath, replacing streamfn,
and expr/functions.go now uses this too.

This commit refactors the reducer package to be more go-like and
ergonomic.  These changes will make it easier to add the "where"
clause to reducers and add new reducer functions.

The reducer compilation logic moved to groupby, as a temporary stop
on the way to zq/compiler (see issue #1162).  reducer/compile is
now gone and reducers don't know about rows.

The multi-type math got moved to zq/anymath, replacing streamfn,
and expr/functions.go now uses this too.
@mccanne mccanne requested a review from a team October 13, 2020 06:37
Comment on lines 50 to 51
// Currently,tThe only reducer that supports operator without
// a field is count().
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Currently,tThe only reducer that supports operator without
// a field is count().
// Count is the only reducer that doesn't require an argument.

anymath/math.go Outdated
Comment on lines 5 to 20
type Init struct {
Float64 float64
Int64 int64
Uint64 uint64
}

type Int64 func(int64, int64) int64
type Uint64 func(uint64, uint64) uint64
type Float64 func(float64, float64) float64

type Function struct {
Init
Int64
Uint64
Float64
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIts:

  1. You don't really need the Float64, Int64, and Uint64 types.
  2. Function is more interesting than Init, so lead with it.
  3. Use the same field order (Float64, Int64, Uint64) in Function and Init.
Suggested change
type Init struct {
Float64 float64
Int64 int64
Uint64 uint64
}
type Int64 func(int64, int64) int64
type Uint64 func(uint64, uint64) uint64
type Float64 func(float64, float64) float64
type Function struct {
Init
Int64
Uint64
Float64
}
type Function struct {
Init
Float64 func(float64, float64) float64
Int64 func(int64, int64) int64
Uint64 func(uint64, uint64) uint64
}
type Init struct {
Float64 float64
Int64 int64
Uint64 uint64
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see their use in the math reducers? I prefer the types but if people find it confusing, it's always easy to change.

}, nil
}

func CompileReducer(assignment ast.Assignment) (field.Static, reducer.Maker, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: No need to export this so call it compileReducer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's currently used by reducer_test.go. We can un-import this when we refactor the compiler components as this is all going to move anyway.

Comment on lines 15 to 18
type reducerMaker struct {
name field.Static
create reducer.Maker
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This seems a little out of place here. I'd declare it immediately after type Params in groupby.go.

Comment on lines 172 to 173
instance := r.create()
if _, ok := instance.(reducer.Decomposable); !ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
instance := r.create()
if _, ok := instance.(reducer.Decomposable); !ok {
if _, ok := r.create().(reducer.Decomposable); !ok {

Comment on lines 43 to 44
resolver := col.nameExpr
v, err := resolver.Eval(rec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolver := col.nameExpr
v, err := resolver.Eval(rec)
v, err := col.nameExpr.Eval(rec)

@mccanne mccanne merged commit 51a0a44 into master Oct 15, 2020
@mccanne mccanne deleted the refactor-reducer branch October 15, 2020 02:04
brim-bot pushed a commit to brimdata/zui that referenced this pull request Oct 15, 2020
This is an auto-generated commit with a zq dependency update. The zq PR
brimdata/super#1481, authored by @mccanne,
has been merged.

refactor reducers

This commit refactors the reducer package to be more go-like and
ergonomic.  These changes will make it easier to add the "where"
clause to reducers and add new reducer functions.

The reducer compilation logic moved to groupby, as a temporary stop
on the way to zq/compiler (see issue brimdata/super#1162).  reducer/compile is
now gone and reducers don't know about rows.

The multi-type math got moved to zq/anymath, replacing streamfn,
and expr/functions.go now uses this too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants