github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/aggregation/window_func.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package aggregation
    15  
    16  import (
    17  	"strings"
    18  
    19  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    20  	"github.com/whtcorpsinc/milevadb/memex"
    21  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    22  )
    23  
    24  // WindowFuncDesc describes a window function signature, only used in causet.
    25  type WindowFuncDesc struct {
    26  	baseFuncDesc
    27  }
    28  
    29  // NewWindowFuncDesc creates a window function signature descriptor.
    30  func NewWindowFuncDesc(ctx stochastikctx.Context, name string, args []memex.Expression) (*WindowFuncDesc, error) {
    31  	switch strings.ToLower(name) {
    32  	case ast.WindowFuncNthValue:
    33  		val, isNull, ok := memex.GetUint64FromConstant(args[1])
    34  		// nth_value does not allow `0`, but allows `null`.
    35  		if !ok || (val == 0 && !isNull) {
    36  			return nil, nil
    37  		}
    38  	case ast.WindowFuncNtile:
    39  		val, isNull, ok := memex.GetUint64FromConstant(args[0])
    40  		// ntile does not allow `0`, but allows `null`.
    41  		if !ok || (val == 0 && !isNull) {
    42  			return nil, nil
    43  		}
    44  	case ast.WindowFuncLead, ast.WindowFuncLag:
    45  		if len(args) < 2 {
    46  			break
    47  		}
    48  		_, isNull, ok := memex.GetUint64FromConstant(args[1])
    49  		if !ok || isNull {
    50  			return nil, nil
    51  		}
    52  	}
    53  	base, err := newBaseFuncDesc(ctx, name, args)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	return &WindowFuncDesc{base}, nil
    58  }
    59  
    60  // noFrameWindowFuncs is the functions that operate on the entire partition,
    61  // they should not have frame specifications.
    62  var noFrameWindowFuncs = map[string]struct{}{
    63  	ast.WindowFuncCumeDist:    {},
    64  	ast.WindowFuncDenseRank:   {},
    65  	ast.WindowFuncLag:         {},
    66  	ast.WindowFuncLead:        {},
    67  	ast.WindowFuncNtile:       {},
    68  	ast.WindowFuncPercentRank: {},
    69  	ast.WindowFuncRank:        {},
    70  	ast.WindowFuncEventNumber:   {},
    71  }
    72  
    73  // NeedFrame checks if the function need frame specification.
    74  func NeedFrame(name string) bool {
    75  	_, ok := noFrameWindowFuncs[strings.ToLower(name)]
    76  	return !ok
    77  }