<commit_before>/*
Copyright 2019 The Vitess Authors.

Licensed 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 engine

import (
	"fmt"
	"io"

	"vitess.io/vitess/go/vt/vtgate/evalengine"

	"vitess.io/vitess/go/sqltypes"

	querypb "vitess.io/vitess/go/vt/proto/query"
)

var _ Primitive = (*Limit)(nil)

// Limit is a primitive that performs the LIMIT operation.
type Limit struct {
	Count  sqltypes.PlanValue
	Offset sqltypes.PlanValue
	Input  Primitive
}

// RouteType returns a description of the query routing type used by the primitive
func (l *Limit) RouteType() string {
	return l.Input.RouteType()
}

// GetKeyspaceName specifies the Keyspace that this primitive routes to.
func (l *Limit) GetKeyspaceName() string {
	return l.Input.GetKeyspaceName()
}

// GetTableName specifies the table that this primitive routes to.
func (l *Limit) GetTableName() string {
	return l.Input.GetTableName()
}

// Execute satisfies the Primtive interface.
func (l *Limit) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
	count, err := l.fetchCount(bindVars)
	if err != nil {
		return nil, err
	}
	offset, err := l.fetchOffset(bindVars)
	if err != nil {
		return nil, err
	}
	// When offset is present, we hijack the limit value so we can calculate
	// the offset in memory from the result of the scatter query with count + offset.
	bindVars["__upper_limit"] = sqltypes.Int64BindVariable(int64(count + offset))

	result, err := l.Input.Execute(vcursor, bindVars, wantfields)
	if err != nil {
		return nil, err
	}

	// There are more rows in the response than limit + offset
	if count+offset <= len(result.Rows) {
		result.Rows = result.Rows[offset : count+offset]
		result.RowsAffected = uint64(count)
		return result, nil
	}
	// Remove extra rows from response
	if offset <= len(result.Rows) {
		result.Rows = result.Rows[offset:]
		result.RowsAffected = uint64(len(result.Rows))
		return result, nil
	}
	// offset is beyond the result set
	result.Rows = nil
	result.RowsAffected = 0
	return result, nil
}

// StreamExecute satisfies the Primtive interface.
func (l *Limit) StreamExecute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
	count, err := l.fetchCount(bindVars)
	if err != nil {
		return err
	}
	offset, err := l.fetchOffset(bindVars)
	if err != nil {
		return err
	}

	bindVars["__upper_limit"] = sqltypes.Int64BindVariable(int64(count))

	err = l.Input.StreamExecute(vcursor, bindVars, wantfields, func(qr *sqltypes.Result) error {
		if len(qr.Fields) != 0 {
			if err := callback(&sqltypes.Result{Fields: qr.Fields}); err != nil {
				return err
			}<commit_msg>pass limit+offset to each shard to have enough records to work on offset inmemory<commit_after>/*
Copyright 2019 The Vitess Authors.

Licensed 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 engine

import (
	"fmt"
	"io"

	"vitess.io/vitess/go/vt/vtgate/evalengine"

	"vitess.io/vitess/go/sqltypes"

	querypb "vitess.io/vitess/go/vt/proto/query"
)

var _ Primitive = (*Limit)(nil)

// Limit is a primitive that performs the LIMIT operation.
type Limit struct {
	Count  sqltypes.PlanValue
	Offset sqltypes.PlanValue
	Input  Primitive
}

// RouteType returns a description of the query routing type used by the primitive
func (l *Limit) RouteType() string {
	return l.Input.RouteType()
}

// GetKeyspaceName specifies the Keyspace that this primitive routes to.
func (l *Limit) GetKeyspaceName() string {
	return l.Input.GetKeyspaceName()
}

// GetTableName specifies the table that this primitive routes to.
func (l *Limit) GetTableName() string {
	return l.Input.GetTableName()
}

// Execute satisfies the Primtive interface.
func (l *Limit) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
	count, err := l.fetchCount(bindVars)
	if err != nil {
		return nil, err
	}
	offset, err := l.fetchOffset(bindVars)
	if err != nil {
		return nil, err
	}
	// When offset is present, we hijack the limit value so we can calculate
	// the offset in memory from the result of the scatter query with count + offset.
	bindVars["__upper_limit"] = sqltypes.Int64BindVariable(int64(count + offset))

	result, err := l.Input.Execute(vcursor, bindVars, wantfields)
	if err != nil {
		return nil, err
	}

	// There are more rows in the response than limit + offset
	if count+offset <= len(result.Rows) {
		result.Rows = result.Rows[offset : count+offset]
		result.RowsAffected = uint64(count)
		return result, nil
	}
	// Remove extra rows from response
	if offset <= len(result.Rows) {
		result.Rows = result.Rows[offset:]
		result.RowsAffected = uint64(len(result.Rows))
		return result, nil
	}
	// offset is beyond the result set
	result.Rows = nil
	result.RowsAffected = 0
	return result, nil
}

// StreamExecute satisfies the Primtive interface.
func (l *Limit) StreamExecute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
	count, err := l.fetchCount(bindVars)
	if err != nil {
		return err
	}
	offset, err := l.fetchOffset(bindVars)
	if err != nil {
		return err
	}

	// When offset is present, we hijack the limit value so we can calculate
	// the offset in memory from the result of the scatter query with count + offset.
	bindVars["__upper_limit"] = sqltypes.Int64BindVariable(int64(count + offset))

	err = l.Input.StreamExecute(vcursor, bindVars, wantfields, func(qr *sqltypes.Result) error {
		if len(qr.Fields) != 0 {
			if err := callback(&sqltypes.Result{Fields: qr.Fields}); err != nil {
				return err
			}
{'fraction_non_alphanumeric': 0.09972760290556901, 'fraction_numerical': 0.006658595641646489, 'mean_word_length': 4.175411119812059, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2857479', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2031, 'n_words': 729}