github.com/XiaoMi/Gaea@v1.2.5/proxy/plan/decorator_limit.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     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 plan
    16  
    17  import (
    18  	"github.com/XiaoMi/Gaea/parser/ast"
    19  	driver "github.com/XiaoMi/Gaea/parser/tidb-types/parser_driver"
    20  )
    21  
    22  // NeedRewriteLimitOrCreateRewrite check if SelectStmt need rewrite limit clause,
    23  // if need, create a rewritten limit clause.
    24  // count == -1代表没有Limit子句
    25  func NeedRewriteLimitOrCreateRewrite(stmt *ast.SelectStmt) (bool, int64, int64, *ast.Limit) {
    26  	limit := stmt.Limit
    27  	if limit == nil {
    28  		return false, -1, -1, nil
    29  	}
    30  
    31  	count := limit.Count.(*driver.ValueExpr).GetInt64()
    32  
    33  	if limit.Offset == nil {
    34  		return false, 0, count, nil
    35  	}
    36  
    37  	offset := limit.Offset.(*driver.ValueExpr).GetInt64()
    38  
    39  	if offset == 0 {
    40  		return false, 0, count, nil
    41  	}
    42  
    43  	newCount := count + offset
    44  	nv := &driver.ValueExpr{}
    45  	nv.SetInt64(newCount)
    46  	newLimit := &ast.Limit{
    47  		Count: nv,
    48  	}
    49  	return true, offset, count, newLimit
    50  }