<commit_before>/*
Copyright 2021 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 planbuilder

import (
	"vitess.io/vitess/go/vt/sqlparser"
	"vitess.io/vitess/go/vt/vtgate/planbuilder/abstract"
	"vitess.io/vitess/go/vt/vtgate/semantics"
	"vitess.io/vitess/go/vt/vtgate/vindexes"
)

// queryTree interface and implementations
// These representation helps in optimizing the join planning using tables and predicates.
type (
	queryTree interface {
		// tableID returns the table identifiers that are solved by this plan
		tableID() semantics.TableSet

		// creates a copy of the queryTree that can be updated without changing the original
		clone() queryTree

		// cost is simply the number of routes in the queryTree
		cost() int

		pushOutputColumns([]*sqlparser.ColName, *semantics.SemTable) ([]int, error)
	}
)

// relation interface and implementations
// They are representation of the tables in a routeTree
// When we are able to merge queryTree then it lives as relation otherwise it stays as joinTree
type (
	relation interface {
		tableID() semantics.TableSet
		tableNames() []string
	}

	joinTables struct {
		lhs, rhs relation
		pred     sqlparser.Expr
	}

	routeTable struct {
		qtable *abstract.QueryTable
		vtable *vindexes.Table
	}

	parenTables []relation

	derivedTable struct {
		// tables contains inner tables that are solved by this plan.
		// the tables also contain any predicates that only depend on that particular table
		tables parenTables

		// predicates are the predicates evaluated by this plan
		predicates []sqlparser.Expr

		// leftJoins are the join conditions evaluated by this plan
		leftJoins []*outerTable

		alias string

		query *sqlparser.Select
	}
)

type outerTable struct {
	right relation
	pred  sqlparser.Expr
}

// type assertions
var _ relation = (*routeTable)(nil)
var _ relation = (*joinTables)(nil)
var _ relation = (parenTables)(nil)
var _ relation = (*derivedTable)(nil)

func (d *derivedTable) tableID() semantics.TableSet { return d.tables.tableID() }

func (d *derivedTable) tableNames() []string { return d.tables.tableNames() }<commit_msg>Addition of the vindexTable relation<commit_after>/*
Copyright 2021 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 planbuilder

import (
	"vitess.io/vitess/go/vt/sqlparser"
	"vitess.io/vitess/go/vt/vtgate/planbuilder/abstract"
	"vitess.io/vitess/go/vt/vtgate/semantics"
	"vitess.io/vitess/go/vt/vtgate/vindexes"
)

// queryTree interface and implementations
// These representation helps in optimizing the join planning using tables and predicates.
type (
	queryTree interface {
		// tableID returns the table identifiers that are solved by this plan
		tableID() semantics.TableSet

		// creates a copy of the queryTree that can be updated without changing the original
		clone() queryTree

		// cost is simply the number of routes in the queryTree
		cost() int

		pushOutputColumns([]*sqlparser.ColName, *semantics.SemTable) ([]int, error)
	}
)

// relation interface and implementations
// They are representation of the tables in a routeTree
// When we are able to merge queryTree then it lives as relation otherwise it stays as joinTree
type (
	relation interface {
		tableID() semantics.TableSet
		tableNames() []string
	}

	vindexTable struct {
		table *abstract.VindexTable
	}

	joinTables struct {
		lhs, rhs relation
		pred     sqlparser.Expr
	}

	routeTable struct {
		qtable *abstract.QueryTable
		vtable *vindexes.Table
	}

	parenTables []relation

	derivedTable struct {
		// tables contains inner tables that are solved by this plan.
		// the tables also contain any predicates that only depend on that particular table
		tables parenTables

		// predicates are the predicates evaluated by this plan
		predicates []sqlparser.Expr

		// leftJoins are the join conditions evaluated by this plan
		leftJoins []*outerTable

		alias string

		query *sqlparser.Select
	}
)

type outerTable struct {
	right relation
	pred  sqlparser.Expr
}

// type assertions
var _ relation = (*routeTable)(nil)
var _ relation = (*joinTables)(nil)
var _ relation = (parenTables)(nil)
var _ relation = (*derivedTable)(nil)
var _ relation = (*vindexTable)(nil)

func (v vindexTable) tableID() semantics.TableSet { return v.table.TableID }

func (v vindexTable) tableNames() []string { return []string{v.table.Table.Name.String()} }

func (d *derivedTable) tableID() semantics.TableSet { return d.tables.tableID() }

func (d *derivedTable) tableNames() []string { return d.tables.tableNames() }
{'fraction_non_alphanumeric': 0.08058941240676733, 'fraction_numerical': 0.0029106785519374206, 'mean_word_length': 4.133520074696546, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6944036', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1529, 'n_words': 613}