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

Add COUNT(DISTINCT ...), SUM(DISTINCT ...), AVG(DISTINCT ...) #2568

Merged
merged 2 commits into from
Sep 21, 2015

Conversation

vivekmenezes
Copy link
Contributor

closes #2248

@@ -125,6 +130,19 @@ func (n *groupNode) Next() bool {
for n.plan.Next() {
values := n.plan.Values()
for i, f := range n.funcs {
if f.val.Distinct {
encoded := make([]byte, 0, 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

pretty sure in this case, you're just as well passing nil

@tamird
Copy link
Contributor

tamird commented Sep 19, 2015

typo in commit message: s/ADD/SUM/

@@ -99,6 +103,7 @@ type groupNode struct {
render []parser.Expr
funcs []*aggregateFunc
needGroup bool
seen []map[string]struct{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than a separate seen slice that parallels the funcs slice, could you place this map inside aggregateFunc?

@petermattis
Copy link
Collaborator

LGTM

Seems like there are some opportunities to optimize distinct processing given the ordering, similar to what is done for SELECT DISTINCT.

@vivekmenezes
Copy link
Contributor Author

I've incorporated most of your feedback and added another commit that encodes tuples for SELECT DISTINCT. Thanks.

@@ -196,11 +196,17 @@ func (v *extractAggregatesVisitor) Visit(expr parser.Expr, pre bool) (parser.Vis
break
}
if impl, ok := aggregates[strings.ToLower(string(t.Name.Base))]; ok {
seen := make(map[string]struct{})
Copy link
Contributor

Choose a reason for hiding this comment

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

This unnecessarily allocates a map even when it's unused. How about:

f := &aggregateFunc{
    val: aggregateValue{
        FuncExpr: t,
    },
    impl: impl.New(),
}
if t.Distinct {
    f.seen = make(map[string]struct{})
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

On Mon, Sep 21, 2015 at 3:12 PM Tamir Duberstein notifications@github.com
wrote:

In sql/group.go
#2568 (comment):

@@ -196,11 +196,17 @@ func (v *extractAggregatesVisitor) Visit(expr parser.Expr, pre bool) (parser.Vis
break
}
if impl, ok := aggregates[strings.ToLower(string(t.Name.Base))]; ok {

  •       seen := make(map[string]struct{})
    

This unnecessarily allocates a map even when it's unused. How about:

f := &aggregateFunc{
val: aggregateValue{
FuncExpr: t,
},
impl: impl.New(),
}if t.Distinct {
f.seen = make(map[string]struct{})
}


Reply to this email directly or view it on GitHub
https://github.com/cockroachdb/cockroach/pull/2568/files#r40012456.

@tamird
Copy link
Contributor

tamird commented Sep 21, 2015

There's still a typo in the commit message.

Whoops, no there isn't!

LGTM modulo the extra map allocation.

@vivekmenezes vivekmenezes changed the title Add COUNT(DISTINCT ...), ADD(DISTINCT ...), AVG(DISTINCT ...) Add COUNT(DISTINCT ...), SUM(DISTINCT ...), AVG(DISTINCT ...) Sep 21, 2015
@petermattis
Copy link
Collaborator

LGTM

@vivekmenezes
Copy link
Contributor Author

I was struggling to find a query that will return a tuple and not return an error. The test I have used to return a different error before the change.

SELECT (2, 3) IN (SELECT DISTINCT (y, z) FROM xyz) doesn't work
nor does
SELECT ((2, 3)) IN (SELECT DISTINCT (y, z) FROM xyz)

If we don't have a query available, I'll create a bug report and fix it in a different PR.

@vivekmenezes vivekmenezes force-pushed the vivek/distinct branch 2 times, most recently from 0e0a484 to f2ce174 Compare September 21, 2015 19:34
@petermattis
Copy link
Collaborator

Hmm, SELECT (2, 3) IN (SELECT DISTINCT (y, z) FROM xyz) should work. I'll take a look once this is merged.

@vivekmenezes
Copy link
Contributor Author

Looks like I messed up the commits. Let me put the right changes in the right commits

@tamird
Copy link
Contributor

tamird commented Sep 21, 2015

I think you can just squash these to one.

vivekmenezes added a commit that referenced this pull request Sep 21, 2015
Add COUNT(DISTINCT ...), SUM(DISTINCT ...), AVG(DISTINCT ...)
@vivekmenezes vivekmenezes merged commit 52c505e into master Sep 21, 2015
@vivekmenezes vivekmenezes deleted the vivek/distinct branch September 21, 2015 20:26
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.

sql: add support for AGG_FUNC(DISTINCT ...)
3 participants