Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 202 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8107,6 +8107,208 @@ where
},
},
},
{
Name: "ntile tests",
Dialect: "mysql",
SetUpScript: []string{
"create table t (i int primary key, j int);",
"insert into t values (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 2), (7, 2), (8, 2), (9, 2), (10, 2);",
},
Assertions: []ScriptTestAssertion{
{
Query: "select i, ntile(null) over() from t;",
ExpectedErr: sql.ErrInvalidArgument,
},
{
Query: "select i, ntile(0) over() from t;",
ExpectedErr: sql.ErrInvalidArgument,
},
{
Query: "select i, ntile(-1) over() from t;",
ExpectedErr: sql.ErrInvalidArgument,
},
{
Query: "select i, ntile(100) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(2)},
{3, uint64(3)},
{4, uint64(4)},
{5, uint64(5)},
{6, uint64(6)},
{7, uint64(7)},
{8, uint64(8)},
{9, uint64(9)},
{10, uint64(10)},
},
},
{
Query: "select i, ntile(10) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(2)},
{3, uint64(3)},
{4, uint64(4)},
{5, uint64(5)},
{6, uint64(6)},
{7, uint64(7)},
{8, uint64(8)},
{9, uint64(9)},
{10, uint64(10)},
},
},
{
Query: "select i, ntile(9) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(2)},
{4, uint64(3)},
{5, uint64(4)},
{6, uint64(5)},
{7, uint64(6)},
{8, uint64(7)},
{9, uint64(8)},
{10, uint64(9)},
},
},
{
Query: "select i, ntile(8) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(2)},
{4, uint64(2)},
{5, uint64(3)},
{6, uint64(4)},
{7, uint64(5)},
{8, uint64(6)},
{9, uint64(7)},
{10, uint64(8)},
},
},
{
Query: "select i, ntile(7) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(2)},
{4, uint64(2)},
{5, uint64(3)},
{6, uint64(3)},
{7, uint64(4)},
{8, uint64(5)},
{9, uint64(6)},
{10, uint64(7)},
},
},
{
Query: "select i, ntile(6) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(2)},
{4, uint64(2)},
{5, uint64(3)},
{6, uint64(3)},
{7, uint64(4)},
{8, uint64(4)},
{9, uint64(5)},
{10, uint64(6)},
},
},
{
Query: "select i, ntile(5) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(2)},
{4, uint64(2)},
{5, uint64(3)},
{6, uint64(3)},
{7, uint64(4)},
{8, uint64(4)},
{9, uint64(5)},
{10, uint64(5)},
},
},
{
Query: "select i, ntile(4) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(1)},
{4, uint64(2)},
{5, uint64(2)},
{6, uint64(2)},
{7, uint64(3)},
{8, uint64(3)},
{9, uint64(4)},
{10, uint64(4)},
},
},
{
Query: "select i, ntile(3) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(1)},
{4, uint64(1)},
{5, uint64(2)},
{6, uint64(2)},
{7, uint64(2)},
{8, uint64(3)},
{9, uint64(3)},
{10, uint64(3)},
},
},
{
Query: "select i, ntile(2) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(1)},
{4, uint64(1)},
{5, uint64(1)},
{6, uint64(2)},
{7, uint64(2)},
{8, uint64(2)},
{9, uint64(2)},
{10, uint64(2)},
},
},
{
Query: "select i, ntile(1) over() from t;",
Expected: []sql.Row{
{1, uint64(1)},
{2, uint64(1)},
{3, uint64(1)},
{4, uint64(1)},
{5, uint64(1)},
{6, uint64(1)},
{7, uint64(1)},
{8, uint64(1)},
{9, uint64(1)},
{10, uint64(1)},
},
},
{
Query: "select i, j, ntile(2) over(partition by j) from t;",
Expected: []sql.Row{
{1, 1, uint64(1)},
{2, 1, uint64(1)},
{3, 1, uint64(1)},
{4, 1, uint64(2)},
{5, 1, uint64(2)},
{6, 2, uint64(1)},
{7, 2, uint64(1)},
{8, 2, uint64(1)},
{9, 2, uint64(2)},
{10, 2, uint64(2)},
},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20250410090211-143e6b272ad4
github.com/dolthub/vitess v0.0.0-20250410233614-8d8c7a5b3d6b
github.com/go-kit/kit v0.10.0
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
github.com/gocraft/dbr/v2 v2.7.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
github.com/dolthub/vitess v0.0.0-20250410090211-143e6b272ad4 h1:LGTt2LtYX8vaai32d+c9L0sMcP+Dg9w1kO6+lbsxxYg=
github.com/dolthub/vitess v0.0.0-20250410090211-143e6b272ad4/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
github.com/dolthub/vitess v0.0.0-20250410233614-8d8c7a5b3d6b h1:2wE+qJwJ5SRIzz+dJQT8XbkpK+g8/pFt34AU/iJ5K+Y=
github.com/dolthub/vitess v0.0.0-20250410233614-8d8c7a5b3d6b/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down
16 changes: 8 additions & 8 deletions optgen/cmd/source/unary_aggs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ unaryAggs:
- name: "Sum"
desc: "returns the sum of expr in all rows"
nullable: false
- name: "StdDevPop"
desc: "returns the population standard deviation of expr"
- name: "StdDevSamp"
desc: "returns the sample standard deviation of expr"
- name: "VarPop"
desc: "returns the population variance of expr"
- name: "VarSamp"
desc: "returns the sample variance of expr"
- name: "StdDevPop"
desc: "returns the population standard deviation of expr"
- name: "StdDevSamp"
desc: "returns the sample standard deviation of expr"
- name: "VarPop"
desc: "returns the population variance of expr"
- name: "VarSamp"
desc: "returns the sample variance of expr"
142 changes: 142 additions & 0 deletions sql/expression/function/aggregation/window/ntile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2025 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package window

import (
"strings"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression/function/aggregation"
"github.com/dolthub/go-mysql-server/sql/types"
)

type NTile struct {
pos uint64
count uint64

bucketExpr sql.Expression
bucketSize uint64

id sql.ColumnId
window *sql.WindowDefinition
}

var _ sql.FunctionExpression = (*NTile)(nil)
var _ sql.WindowAggregation = (*NTile)(nil)
var _ sql.WindowAdaptableExpression = (*NTile)(nil)
var _ sql.CollationCoercible = (*NTile)(nil)

func NewNTile(expr sql.Expression) sql.Expression {
return &NTile{
bucketExpr: expr,
}
}

// Id implements sql.IdExpression
func (n *NTile) Id() sql.ColumnId {
return n.id
}

// WithId implements sql.IdExpression
func (n *NTile) WithId(id sql.ColumnId) sql.IdExpression {
ret := *n
ret.id = id
return &ret
}

// Description implements sql.FunctionExpression
func (n *NTile) Description() string {
return "returns percentage rank value."
}

// Window implements sql.WindowExpression
func (n *NTile) Window() *sql.WindowDefinition {
return n.window
}

func (n *NTile) Resolved() bool {
return windowResolved(n.window)
}

func (n *NTile) String() string {
sb := strings.Builder{}
sb.WriteString("ntile()")
if n.window != nil {
sb.WriteString(" ")
sb.WriteString(n.window.String())
}
return sb.String()
}

func (n *NTile) DebugString() string {
sb := strings.Builder{}
sb.WriteString("ntile()")
if n.window != nil {
sb.WriteString(" ")
sb.WriteString(sql.DebugString(n.window))
}
return sb.String()
}

// FunctionName implements sql.FunctionExpression
func (n *NTile) FunctionName() string {
return "NTILE"
}

// Type implements sql.Expression
func (n *NTile) Type() sql.Type {
return types.Float64
}

// CollationCoercibility implements the interface sql.CollationCoercible.
func (*NTile) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}

// IsNullable implements sql.Expression
func (n *NTile) IsNullable() bool {
return false
}

// Eval implements sql.Expression
func (n *NTile) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, sql.ErrWindowUnsupported.New(n.FunctionName())
}

// Children implements sql.Expression
func (n *NTile) Children() []sql.Expression {
return n.window.ToExpressions()
}

// WithChildren implements sql.Expression
func (n *NTile) WithChildren(children ...sql.Expression) (sql.Expression, error) {
window, err := n.window.FromExpressions(children)
if err != nil {
return nil, err
}

return n.WithWindow(window), nil
}

// WithWindow implements sql.WindowAggregation
func (n *NTile) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression {
nr := *n
nr.window = window
return &nr
}

func (n *NTile) NewWindowFunction() (sql.WindowFunction, error) {
return aggregation.NewNTile(n.bucketExpr), nil
}
Loading
Loading