github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/window_frame_factory_test.go (about)

     1  package planbuilder
     2  
     3  import (
     4  	"testing"
     5  
     6  	ast "github.com/dolthub/vitess/go/vt/sqlparser"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/dolthub/go-mysql-server/sql"
    10  	"github.com/dolthub/go-mysql-server/sql/expression"
    11  	"github.com/dolthub/go-mysql-server/sql/types"
    12  )
    13  
    14  func TestWindowFrameGetters(t *testing.T) {
    15  	exprTests := []struct {
    16  		Name     string
    17  		Fn       func(*Builder, *scope, *ast.Frame) sql.Expression
    18  		Frame    *ast.Frame
    19  		Expected sql.Expression
    20  	}{
    21  		{
    22  			Name: "start preceding int",
    23  			Fn:   (*Builder).getFrameStartNPreceding,
    24  			Frame: &ast.Frame{
    25  				Extent: &ast.FrameExtent{
    26  					Start: &ast.FrameBound{
    27  						Expr: ast.NewIntVal([]byte("1")),
    28  						Type: ast.ExprPreceding,
    29  					},
    30  				},
    31  			},
    32  			Expected: expression.NewLiteral(int8(1), types.Int8),
    33  		},
    34  		{
    35  			Name: "start preceding nil",
    36  			Fn:   (*Builder).getFrameStartNPreceding,
    37  			Frame: &ast.Frame{
    38  				Extent: &ast.FrameExtent{
    39  					Start: &ast.FrameBound{},
    40  				},
    41  			},
    42  			Expected: nil,
    43  		},
    44  		{
    45  			Name: "end preceding int",
    46  			Fn:   (*Builder).getFrameEndNPreceding,
    47  			Frame: &ast.Frame{
    48  				Extent: &ast.FrameExtent{
    49  					End: &ast.FrameBound{
    50  						Expr: ast.NewIntVal([]byte("1")),
    51  						Type: ast.ExprPreceding,
    52  					},
    53  				},
    54  			},
    55  			Expected: expression.NewLiteral(int8(1), types.Int8),
    56  		},
    57  		{
    58  			Name: "end preceding nil",
    59  			Fn:   (*Builder).getFrameEndNPreceding,
    60  			Frame: &ast.Frame{
    61  				Extent: &ast.FrameExtent{
    62  					End: &ast.FrameBound{},
    63  				},
    64  			},
    65  			Expected: nil,
    66  		},
    67  		{
    68  			Name: "start following int",
    69  			Fn:   (*Builder).getFrameStartNFollowing,
    70  			Frame: &ast.Frame{
    71  				Extent: &ast.FrameExtent{
    72  					Start: &ast.FrameBound{
    73  						Expr: ast.NewIntVal([]byte("1")),
    74  						Type: ast.ExprFollowing,
    75  					},
    76  				},
    77  			},
    78  			Expected: expression.NewLiteral(int8(1), types.Int8),
    79  		},
    80  		{
    81  			Name: "start following nil",
    82  			Fn:   (*Builder).getFrameStartNFollowing,
    83  			Frame: &ast.Frame{
    84  				Extent: &ast.FrameExtent{
    85  					Start: &ast.FrameBound{},
    86  				},
    87  			},
    88  			Expected: nil,
    89  		},
    90  		{
    91  			Name: "end following int",
    92  			Fn:   (*Builder).getFrameEndNFollowing,
    93  			Frame: &ast.Frame{
    94  				Extent: &ast.FrameExtent{
    95  					End: &ast.FrameBound{
    96  						Expr: ast.NewIntVal([]byte("1")),
    97  						Type: ast.ExprFollowing,
    98  					},
    99  				},
   100  			},
   101  			Expected: expression.NewLiteral(int8(1), types.Int8),
   102  		},
   103  		{
   104  			Name: "end following nil",
   105  			Fn:   (*Builder).getFrameEndNFollowing,
   106  			Frame: &ast.Frame{
   107  				Extent: &ast.FrameExtent{
   108  					End: &ast.FrameBound{},
   109  				},
   110  			},
   111  			Expected: nil,
   112  		},
   113  	}
   114  
   115  	boolTests := []struct {
   116  		Name     string
   117  		Fn       func(*Builder, *scope, *ast.Frame) bool
   118  		Frame    *ast.Frame
   119  		Expected bool
   120  	}{
   121  		{
   122  			Name: "start current row is set",
   123  			Fn:   (*Builder).getFrameStartCurrentRow,
   124  			Frame: &ast.Frame{
   125  				Extent: &ast.FrameExtent{
   126  					Start: &ast.FrameBound{
   127  						Type: ast.CurrentRow,
   128  					},
   129  				},
   130  			},
   131  			Expected: true,
   132  		},
   133  		{
   134  			Name: "start current row is not set",
   135  			Fn:   (*Builder).getFrameStartCurrentRow,
   136  			Frame: &ast.Frame{
   137  				Extent: &ast.FrameExtent{
   138  					Start: &ast.FrameBound{
   139  						Type: -1,
   140  					},
   141  				},
   142  			},
   143  			Expected: false,
   144  		},
   145  		{
   146  			Name: "end current row is set",
   147  			Fn:   (*Builder).getFrameEndCurrentRow,
   148  			Frame: &ast.Frame{
   149  				Extent: &ast.FrameExtent{
   150  					End: &ast.FrameBound{
   151  						Type: ast.CurrentRow,
   152  					},
   153  				},
   154  			},
   155  			Expected: true,
   156  		},
   157  		{
   158  			Name: "end current row is not set",
   159  			Fn:   (*Builder).getFrameEndCurrentRow,
   160  			Frame: &ast.Frame{
   161  				Extent: &ast.FrameExtent{
   162  					End: &ast.FrameBound{
   163  						Type: -1,
   164  					},
   165  				},
   166  			},
   167  			Expected: false,
   168  		},
   169  		{
   170  			Name: "unbounded preceding is set",
   171  			Fn:   (*Builder).getFrameUnboundedPreceding,
   172  			Frame: &ast.Frame{
   173  				Extent: &ast.FrameExtent{
   174  					Start: &ast.FrameBound{
   175  						Type: ast.UnboundedPreceding,
   176  					},
   177  				},
   178  			},
   179  			Expected: true,
   180  		},
   181  		{
   182  			Name: "unbounded preceding is not set",
   183  			Fn:   (*Builder).getFrameUnboundedPreceding,
   184  			Frame: &ast.Frame{
   185  				Extent: &ast.FrameExtent{
   186  					Start: &ast.FrameBound{},
   187  				},
   188  			},
   189  			Expected: false,
   190  		},
   191  		{
   192  			Name: "unbounded following is set",
   193  			Fn:   (*Builder).getFrameUnboundedFollowing,
   194  			Frame: &ast.Frame{
   195  				Extent: &ast.FrameExtent{
   196  					End: &ast.FrameBound{
   197  						Type: ast.UnboundedFollowing,
   198  					},
   199  				},
   200  			},
   201  			Expected: true,
   202  		},
   203  		{
   204  			Name: "unbounded following is not set",
   205  			Fn:   (*Builder).getFrameUnboundedFollowing,
   206  			Frame: &ast.Frame{
   207  				Extent: &ast.FrameExtent{
   208  					End: &ast.FrameBound{},
   209  				},
   210  			},
   211  			Expected: false,
   212  		},
   213  	}
   214  
   215  	for _, tt := range exprTests {
   216  		t.Run(tt.Name, func(t *testing.T) {
   217  			b := &Builder{}
   218  			res := tt.Fn(b, &scope{b: b}, tt.Frame)
   219  			require.Equal(t, tt.Expected, res)
   220  		})
   221  	}
   222  
   223  	for _, tt := range boolTests {
   224  		t.Run(tt.Name, func(t *testing.T) {
   225  			b := &Builder{}
   226  			res := tt.Fn(b, &scope{b: b}, tt.Frame)
   227  			require.Equal(t, tt.Expected, res)
   228  		})
   229  	}
   230  }