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

[SQL] Support MySQL window function. #688

Merged
merged 41 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8ea27df
add node config support (#464)
csynineyang Nov 2, 2022
78728b4
Merge branch 'arana-db:master' into master
csynineyang Nov 4, 2022
4fe1362
Merge branch 'arana-db:master' into master
csynineyang Nov 7, 2022
3eb3735
Merge branch 'arana-db:master' into master
csynineyang Nov 19, 2022
71ad2e5
Merge branch 'arana-db:master' into master
csynineyang Nov 23, 2022
9c56b98
Support MySQL CAST_CHAR function.
csynineyang Nov 23, 2022
a7ab678
Merge branch 'arana-db:master' into master
csynineyang Nov 28, 2022
fac5028
format style
csynineyang Nov 28, 2022
0545aee
Merge branch 'arana-db:master' into master
csynineyang Dec 9, 2022
78e61c1
Merge branch 'arana-db:master' into master
csynineyang Dec 19, 2022
c05d948
Support MySQL CAST_TIME function. (#570)
csynineyang Dec 19, 2022
97ac206
Support MySQL CAST_DATE function. (#569)
csynineyang Dec 20, 2022
b688837
Merge branch 'arana-db:master' into master
csynineyang Dec 26, 2022
e55e00f
Support MySQL CAST_DATETIME function. (#568)
csynineyang Dec 26, 2022
6416c50
Merge branch 'master' of github.com:csynineyang/arana
csynineyang Dec 26, 2022
a99781d
Merge branch 'arana-db:master' into master
csynineyang Jan 17, 2023
9ca3805
Support MySQL CAST_TIME/CAST_DATE/CAST_DATETIME function
csynineyang Jan 17, 2023
d91d12c
Merge branch 'arana-db:master' into master
csynineyang Feb 2, 2023
9c083ca
Resolve Conversation
csynineyang Feb 2, 2023
d4425dc
Merge branch 'arana-db:master' into master
csynineyang Feb 15, 2023
9cce43c
Merge branch 'arana-db:master' into master
csynineyang Mar 18, 2023
7a64890
Support CREATE TABLE
csynineyang Mar 18, 2023
70c31f5
Merge branch 'arana-db:master' into master
csynineyang Mar 22, 2023
d02656f
add: IfNotExists
csynineyang Mar 22, 2023
002330a
fix: reformat imports
csynineyang Mar 22, 2023
4d9bf3f
Resolve Conversation
csynineyang Apr 17, 2023
82d600c
Merge branch 'arana-db:master' into master
csynineyang Apr 19, 2023
742659f
Support window function: CUME_DIST
csynineyang May 18, 2023
c090c4f
Support window function: PERCENT_RANK
csynineyang May 21, 2023
80124ff
Support window function: RANK
csynineyang May 21, 2023
3dd8864
Support window function: DENSE_RANK
csynineyang May 21, 2023
77489e3
Merge branch 'arana-db:master' into master
csynineyang May 21, 2023
92100a7
Merge branch 'master' of github.com:csynineyang/arana
csynineyang May 21, 2023
74c5d30
Support window function: FIRST_VALUE/LAST_VALUE/LAG/LEAD
csynineyang Jun 10, 2023
bc4d13f
Merge branch 'arana-db:master' into master
csynineyang Jun 10, 2023
3dd8d97
Merge branch 'master' of github.com:csynineyang/arana
csynineyang Jun 10, 2023
9eb15ee
Support window function: NTH_VALUE/NTILE/ROW_NUMBER
csynineyang Jun 14, 2023
a6c7ea0
support argument(n) in LAG/LEAD
csynineyang Jun 14, 2023
c0321c7
convert Int64 to Float64 in test case
csynineyang Jun 14, 2023
1825bff
Merge branch 'arana-db:master' into master
csynineyang Jun 14, 2023
4db0475
Merge branch 'master' of github.com:csynineyang/arana
csynineyang Jun 14, 2023
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
72 changes: 72 additions & 0 deletions pkg/runtime/function/cume_dist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 function

import (
"context"
)

import (
"github.com/pkg/errors"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

// FuncCumeDist is https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
const FuncCumeDist = "CUME_DIST"

var _ proto.Func = (*cumedistFunc)(nil)

func init() {
proto.RegisterFunc(FuncCumeDist, cumedistFunc{})
}

type cumedistFunc struct{}

func (a cumedistFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
first, err := inputs[0].Value(ctx)
if first == nil || err != nil {
return nil, errors.Wrapf(err, "cannot eval %s", FuncCumeDist)
}
firstDec, _ := first.Float64()
firstNum := 0

for _, it := range inputs[1:] {
val, err := it.Value(ctx)
if val == nil || err != nil {
return nil, errors.Wrapf(err, "cannot eval %s", FuncCumeDist)
}
valDec, _ := val.Float64()

if valDec <= firstDec {
firstNum++
}
}

r := 0.0
if len(inputs) > 1 {
r = float64(firstNum) / float64(len(inputs)-1)
}
return proto.NewValueFloat64(r), nil
}

func (a cumedistFunc) NumInput() int {
return 0
}
127 changes: 127 additions & 0 deletions pkg/runtime/function/cume_dist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 function

import (
"context"
"fmt"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

func TestFuncCumeDist(t *testing.T) {
fn := proto.MustGetFunc(FuncCumeDist)
type tt struct {
inputs []proto.Value
want string
}
for _, v := range []tt{
{
[]proto.Value{
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"0.2222222222222222",
},
{
[]proto.Value{
proto.NewValueInt64(2),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"0.3333333333333333",
},
{
[]proto.Value{
proto.NewValueInt64(3),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"0.6666666666666666",
},
{
[]proto.Value{
proto.NewValueInt64(4),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"0.8888888888888888",
},
{
[]proto.Value{
proto.NewValueInt64(5),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"1",
},
} {
t.Run(v.want, func(t *testing.T) {
var inputs []proto.Valuer
for i := range v.inputs {
inputs = append(inputs, proto.ToValuer(v.inputs[i]))
}
out, err := fn.Apply(context.Background(), inputs...)
assert.NoError(t, err)
assert.Equal(t, v.want, fmt.Sprint(out))
})
}
}
70 changes: 70 additions & 0 deletions pkg/runtime/function/dense_rank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 function

import (
"context"
)

import (
"github.com/pkg/errors"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

// FuncDenseRank is https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
const FuncDenseRank = "DENSE_RANK"

var _ proto.Func = (*denserankFunc)(nil)

func init() {
proto.RegisterFunc(FuncDenseRank, denserankFunc{})
}

type denserankFunc struct{}

func (a denserankFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
first, err := inputs[0].Value(ctx)
if first == nil || err != nil {
return nil, errors.Wrapf(err, "cannot eval %s", FuncCumeDist)
}
firstDec, _ := first.Float64()
secondDec := firstDec
firstNum := 0

for _, it := range inputs[1:] {
val, err := it.Value(ctx)
if val == nil || err != nil {
return nil, errors.Wrapf(err, "cannot eval %s", FuncCumeDist)
}
valDec, _ := val.Float64()

if valDec < firstDec && valDec != secondDec {
firstNum++
secondDec = valDec
}
}

return proto.NewValueInt64(int64(firstNum) + 1), nil
}

func (a denserankFunc) NumInput() int {
return 0
}
127 changes: 127 additions & 0 deletions pkg/runtime/function/dense_rank_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 function

import (
"context"
"fmt"
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/proto"
)

func TestFuncDenseRankt(t *testing.T) {
fn := proto.MustGetFunc(FuncDenseRank)
type tt struct {
inputs []proto.Value
want string
}
for _, v := range []tt{
{
[]proto.Value{
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"1",
},
{
[]proto.Value{
proto.NewValueInt64(2),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"2",
},
{
[]proto.Value{
proto.NewValueInt64(3),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"3",
},
{
[]proto.Value{
proto.NewValueInt64(4),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"4",
},
{
[]proto.Value{
proto.NewValueInt64(5),
proto.NewValueInt64(1),
proto.NewValueInt64(1),
proto.NewValueInt64(2),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(3),
proto.NewValueInt64(4),
proto.NewValueInt64(4),
proto.NewValueInt64(5),
},
"5",
},
} {
t.Run(v.want, func(t *testing.T) {
var inputs []proto.Valuer
for i := range v.inputs {
inputs = append(inputs, proto.ToValuer(v.inputs[i]))
}
out, err := fn.Apply(context.Background(), inputs...)
assert.NoError(t, err)
assert.Equal(t, v.want, fmt.Sprint(out))
})
}
}
Loading