github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/sort_test.go (about) 1 // Copyright 2020-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 rowexec 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/go-mysql-server/memory" 24 "github.com/dolthub/go-mysql-server/sql" 25 "github.com/dolthub/go-mysql-server/sql/expression" 26 "github.com/dolthub/go-mysql-server/sql/plan" 27 "github.com/dolthub/go-mysql-server/sql/types" 28 ) 29 30 func TestSort(t *testing.T) { 31 schema := sql.NewPrimaryKeySchema(sql.Schema{ 32 {Name: "col1", Type: types.Text, Nullable: true}, 33 {Name: "col2", Type: types.Int32, Nullable: true}, 34 {Name: "col3", Type: types.Float64, Nullable: true}, 35 }) 36 37 type sortTest struct { 38 rows []sql.Row 39 sortFields []sql.SortField 40 expected []sql.Row 41 } 42 43 testCases := []sortTest{ 44 { 45 rows: []sql.Row{ 46 sql.NewRow("c", nil, nil), 47 sql.NewRow("a", int32(3), float64(3.0)), 48 sql.NewRow("b", int32(3), float64(3.0)), 49 sql.NewRow("c", int32(1), float64(1.0)), 50 sql.NewRow(nil, int32(1), nil), 51 }, 52 sortFields: []sql.SortField{ 53 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 54 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Descending, NullOrdering: sql.NullsLast}, 55 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 56 }, 57 expected: []sql.Row{ 58 sql.NewRow("c", nil, nil), 59 sql.NewRow(nil, int32(1), nil), 60 sql.NewRow("c", int32(1), float64(1.0)), 61 sql.NewRow("b", int32(3), float64(3.0)), 62 sql.NewRow("a", int32(3), float64(3.0)), 63 }, 64 }, 65 { 66 rows: []sql.Row{ 67 sql.NewRow("c", int32(3), float64(3.0)), 68 sql.NewRow("c", int32(3), nil), 69 }, 70 sortFields: []sql.SortField{ 71 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 72 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Descending, NullOrdering: sql.NullsLast}, 73 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 74 }, 75 expected: []sql.Row{ 76 sql.NewRow("c", int32(3), nil), 77 sql.NewRow("c", int32(3), float64(3.0)), 78 }, 79 }, 80 { 81 rows: []sql.Row{ 82 sql.NewRow("c", nil, nil), 83 sql.NewRow("a", int32(3), float64(3.0)), 84 sql.NewRow("b", int32(3), float64(3.0)), 85 sql.NewRow("c", int32(1), float64(1.0)), 86 sql.NewRow(nil, int32(1), nil), 87 }, 88 sortFields: []sql.SortField{ 89 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 90 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 91 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsLast}, 92 }, 93 expected: []sql.Row{ 94 sql.NewRow("c", nil, nil), 95 sql.NewRow(nil, int32(1), nil), 96 sql.NewRow("c", int32(1), float64(1.0)), 97 sql.NewRow("a", int32(3), float64(3.0)), 98 sql.NewRow("b", int32(3), float64(3.0)), 99 }, 100 }, 101 { 102 rows: []sql.Row{ 103 sql.NewRow("a", int32(1), float64(2)), 104 sql.NewRow("a", int32(1), float64(1)), 105 }, 106 sortFields: []sql.SortField{ 107 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 108 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 109 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 110 }, 111 expected: []sql.Row{ 112 sql.NewRow("a", int32(1), float64(1)), 113 sql.NewRow("a", int32(1), float64(2)), 114 }, 115 }, 116 { 117 rows: []sql.Row{ 118 sql.NewRow("a", int32(1), float64(2)), 119 sql.NewRow("a", int32(1), float64(1)), 120 sql.NewRow("a", int32(2), float64(2)), 121 sql.NewRow("a", int32(3), float64(1)), 122 sql.NewRow("b", int32(2), float64(2)), 123 sql.NewRow("c", int32(3), float64(1)), 124 }, 125 sortFields: []sql.SortField{ 126 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 127 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Descending, NullOrdering: sql.NullsFirst}, 128 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 129 }, 130 expected: []sql.Row{ 131 sql.NewRow("a", int32(3), float64(1)), 132 sql.NewRow("a", int32(2), float64(2)), 133 sql.NewRow("a", int32(1), float64(1)), 134 sql.NewRow("a", int32(1), float64(2)), 135 sql.NewRow("b", int32(2), float64(2)), 136 sql.NewRow("c", int32(3), float64(1)), 137 }, 138 }, 139 { 140 rows: []sql.Row{ 141 sql.NewRow(nil, nil, float64(2)), 142 sql.NewRow(nil, nil, float64(1)), 143 }, 144 sortFields: []sql.SortField{ 145 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 146 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 147 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 148 }, 149 expected: []sql.Row{ 150 sql.NewRow(nil, nil, float64(1)), 151 sql.NewRow(nil, nil, float64(2)), 152 }, 153 }, 154 { 155 rows: []sql.Row{ 156 sql.NewRow(nil, nil, float64(1)), 157 sql.NewRow(nil, nil, float64(2)), 158 }, 159 sortFields: []sql.SortField{ 160 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Descending, NullOrdering: sql.NullsFirst}, 161 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Descending, NullOrdering: sql.NullsFirst}, 162 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Descending, NullOrdering: sql.NullsFirst}, 163 }, 164 expected: []sql.Row{ 165 sql.NewRow(nil, nil, float64(2)), 166 sql.NewRow(nil, nil, float64(1)), 167 }, 168 }, 169 { 170 rows: []sql.Row{ 171 sql.NewRow(nil, nil, float64(1)), 172 sql.NewRow(nil, nil, nil), 173 }, 174 sortFields: []sql.SortField{ 175 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 176 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 177 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 178 }, 179 expected: []sql.Row{ 180 sql.NewRow(nil, nil, nil), 181 sql.NewRow(nil, nil, float64(1)), 182 }, 183 }, 184 { 185 rows: []sql.Row{ 186 sql.NewRow(nil, nil, nil), 187 sql.NewRow(nil, nil, float64(1)), 188 }, 189 sortFields: []sql.SortField{ 190 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsLast}, 191 {Column: expression.NewGetField(1, types.Int32, "col2", true), Order: sql.Ascending, NullOrdering: sql.NullsLast}, 192 {Column: expression.NewGetField(2, types.Float64, "col3", true), Order: sql.Ascending, NullOrdering: sql.NullsLast}, 193 }, 194 expected: []sql.Row{ 195 sql.NewRow(nil, nil, float64(1)), 196 sql.NewRow(nil, nil, nil), 197 }, 198 }, 199 } 200 201 for i, tt := range testCases { 202 t.Run(fmt.Sprintf("Sort test %d", i), func(t *testing.T) { 203 require := require.New(t) 204 205 db := memory.NewDatabase("test") 206 tbl := memory.NewTable(db, "test", schema, nil) 207 pro := memory.NewDBProvider(db) 208 ctx := newContext(pro) 209 210 for _, row := range tt.rows { 211 require.NoError(tbl.Insert(ctx, row)) 212 } 213 214 sort := plan.NewSort(tt.sortFields, plan.NewResolvedTable(tbl, nil, nil)) 215 216 actual, err := NodeToRows(ctx, sort) 217 require.NoError(err) 218 require.Equal(tt.expected, actual) 219 }) 220 } 221 } 222 223 func TestSortAscending(t *testing.T) { 224 require := require.New(t) 225 226 data := []sql.Row{ 227 sql.NewRow("c"), 228 sql.NewRow("a"), 229 sql.NewRow("d"), 230 sql.NewRow(nil), 231 sql.NewRow("b"), 232 } 233 234 db := memory.NewDatabase("test") 235 schema := sql.NewPrimaryKeySchema(sql.Schema{ 236 {Name: "col1", Type: types.Text, Nullable: true}, 237 }) 238 239 pro := memory.NewDBProvider(db) 240 ctx := newContext(pro) 241 242 child := memory.NewTable(db, "test", schema, nil) 243 for _, row := range data { 244 require.NoError(child.Insert(ctx, row)) 245 } 246 247 sf := []sql.SortField{ 248 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Ascending, NullOrdering: sql.NullsFirst}, 249 } 250 s := plan.NewSort(sf, plan.NewResolvedTable(child, nil, nil)) 251 require.Equal(schema.Schema, s.Schema()) 252 253 expected := []sql.Row{ 254 sql.NewRow(nil), 255 sql.NewRow("a"), 256 sql.NewRow("b"), 257 sql.NewRow("c"), 258 sql.NewRow("d"), 259 } 260 261 actual, err := NodeToRows(ctx, s) 262 require.NoError(err) 263 require.Equal(expected, actual) 264 } 265 266 func TestSortDescending(t *testing.T) { 267 require := require.New(t) 268 269 data := []sql.Row{ 270 sql.NewRow("c"), 271 sql.NewRow("a"), 272 sql.NewRow("d"), 273 sql.NewRow(nil), 274 sql.NewRow("b"), 275 } 276 277 db := memory.NewDatabase("test") 278 schema := sql.NewPrimaryKeySchema(sql.Schema{ 279 {Name: "col1", Type: types.Text, Nullable: true}, 280 }) 281 282 pro := memory.NewDBProvider(db) 283 ctx := newContext(pro) 284 285 child := memory.NewTable(db, "test", schema, nil) 286 for _, row := range data { 287 require.NoError(child.Insert(ctx, row)) 288 } 289 290 sf := []sql.SortField{ 291 {Column: expression.NewGetField(0, types.Text, "col1", true), Order: sql.Descending, NullOrdering: sql.NullsFirst}, 292 } 293 s := plan.NewSort(sf, plan.NewResolvedTable(child, nil, nil)) 294 require.Equal(schema.Schema, s.Schema()) 295 296 expected := []sql.Row{ 297 sql.NewRow("d"), 298 sql.NewRow("c"), 299 sql.NewRow("b"), 300 sql.NewRow("a"), 301 sql.NewRow(nil), 302 } 303 304 actual, err := NodeToRows(ctx, s) 305 require.NoError(err) 306 require.Equal(expected, actual) 307 }