github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/count_distinct.go (about) 1 // Copyright 2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aggregation 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql/expression" 21 "github.com/dolthub/go-mysql-server/sql/transform" 22 "github.com/dolthub/go-mysql-server/sql/types" 23 24 "github.com/dolthub/go-mysql-server/sql" 25 ) 26 27 type CountDistinct struct { 28 expression.NaryExpression 29 window *sql.WindowDefinition 30 typ sql.Type 31 id sql.ColumnId 32 } 33 34 var _ sql.FunctionExpression = (*CountDistinct)(nil) 35 var _ sql.Aggregation = (*CountDistinct)(nil) 36 var _ sql.WindowAdaptableExpression = (*CountDistinct)(nil) 37 var _ sql.CollationCoercible = (*CountDistinct)(nil) 38 39 func NewCountDistinct(exprs ...sql.Expression) *CountDistinct { 40 return &CountDistinct{ 41 NaryExpression: expression.NaryExpression{ChildExpressions: exprs}, 42 } 43 } 44 45 // Type implements the Expression interface. 46 func (a *CountDistinct) Type() sql.Type { 47 return types.Int64 48 } 49 50 // CollationCoercibility implements the interface sql.CollationCoercible. 51 func (*CountDistinct) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 52 return sql.Collation_binary, 5 53 } 54 55 // IsNullable implements the Expression interface. 56 func (a *CountDistinct) IsNullable() bool { 57 return false 58 } 59 60 // Id implements the Aggregation interface 61 func (a *CountDistinct) Id() sql.ColumnId { 62 return a.id 63 } 64 65 // WithId implements the Aggregation interface 66 func (a *CountDistinct) WithId(id sql.ColumnId) sql.IdExpression { 67 ret := *a 68 ret.id = id 69 return &ret 70 } 71 72 // Eval implements the Expression interface. 73 func (a *CountDistinct) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 74 return nil, ErrEvalUnsupportedOnAggregation.New("CountDistinct") 75 } 76 77 // Children implements the Expression interface. 78 func (a *CountDistinct) Children() []sql.Expression { 79 return a.ChildExpressions 80 } 81 82 // WithChildren implements the Expression interface. 83 func (a *CountDistinct) WithChildren(children ...sql.Expression) (sql.Expression, error) { 84 return NewCountDistinct(children...), nil 85 } 86 87 // FunctionName implements the FunctionExpression interface. 88 func (a *CountDistinct) FunctionName() string { 89 return "CountDistinct" 90 } 91 92 // Description implements the FunctionExpression interface. 93 func (a *CountDistinct) Description() string { 94 return "returns the number of distinct values in a result set." 95 } 96 97 // NewBuffer implements the Aggregation interface. 98 func (a *CountDistinct) NewBuffer() (sql.AggregationBuffer, error) { 99 exprs := make([]sql.Expression, len(a.ChildExpressions)) 100 for i, expr := range a.ChildExpressions { 101 child, err := transform.Clone(expr) 102 if err != nil { 103 return nil, err 104 } 105 exprs[i] = child 106 } 107 return NewCountDistinctBuffer(exprs), nil 108 } 109 110 // WithWindow implements the Aggregation interface. 111 func (a *CountDistinct) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression { 112 na := *a 113 na.window = window 114 return &na 115 } 116 117 // Window implements the Aggregation interface. 118 func (a *CountDistinct) Window() *sql.WindowDefinition { 119 return a.window 120 } 121 122 // String implements the ValueStats interface. 123 func (a *CountDistinct) String() string { 124 return fmt.Sprintf("COUNTDISTINCT(%s)", a.ChildExpressions) 125 } 126 127 // NewWindowFunction implements the WindowAdaptableExpression interface. 128 func (a *CountDistinct) NewWindowFunction() (sql.WindowFunction, error) { 129 child, err := transform.Clone(a.NaryExpression.ChildExpressions[0]) 130 if err != nil { 131 return nil, err 132 } 133 return NewCountDistinctAgg(child).WithWindow(a.Window()) 134 }