<commit_before>/*
Copyright 2020 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 (
	"fmt"
	"strings"

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

	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
	"vitess.io/vitess/go/vt/vterrors"

	"vitess.io/vitess/go/vt/key"
	"vitess.io/vitess/go/vt/sqlparser"
	"vitess.io/vitess/go/vt/vtgate/engine"
	"vitess.io/vitess/go/vt/vtgate/evalengine"
)

type planFunc = func(expr *sqlparser.SetExpr, vschema ContextVSchema) (engine.SetOp, error)

var sysVarPlanningFunc = map[string]planFunc{}

var notSupported = []string{
	"auto_increment_increment",
	"auto_increment_offset",
	"binlog_direct_non_transactional_updates",
	"innodb_ft_enable_stopword",
	"innodb_ft_user_stopword_table",
	"max_points_in_geometry",
	"max_sp_recursion_depth",
	"myisam_repair_threads",
	"myisam_sort_buffer_size",
	"myisam_stats_method",
	"ndb_allow_copying_alter_table",
	"ndb_autoincrement_prefetch_sz",
	"ndb_blob_read_batch_bytes",
	"ndb_blob_write_batch_bytes",
	"ndb_deferred_constraints",
	"ndb_force_send",
	"ndb_fully_replicated",
	"ndb_index_stat_enable",
	"ndb_index_stat_option",
	"ndb_join_pushdown",
	"ndb_log_bin",
	"ndb_log_exclusive_reads",
	"ndb_row_checksum",
	"ndb_use_exact_count",
	"ndb_use_transactions",
	"ndbinfo_max_bytes",
	"ndbinfo_max_rows",
	"ndbinfo_show_hidden",
	"ndbinfo_table_prefix",
	"old_alter_table",
	"preload_buffer_size",
	"rbr_exec_mode",
	"sql_log_off",
	"transaction_write_set_extraction",
	"audit_log_read_buffer_size",
}

var ignoreThese = []string{
	"big_tables",
	"bulk_insert_buffer_size",
	"debug",
	"default_storage_engine",
	"default_tmp_storage_engine",
	"innodb_strict_mode",
	"innodb_support_xa",
	"innodb_table_locks",
	"innodb_tmpdir",
	"join_buffer_size",
	"keep_files_on_create",
	"lc_messages",
	"long_query_time",
	"low_priority_updates",
	"max_delayed_threads",
	"max_insert_delayed_threads",
	"multi_range_count",
	"net_buffer_length",
	"new",
	"query_cache_type",
	"query_cache_wlock_invalidate",
	"query_prealloc_size",
	"sql_buffer_result",
	"transaction_alloc_block_size",
	"wait_timeout",
}

var saveSettingsToSession = []string{
	"sql_mode",
	"sql_safe_updates",
}

var allowSetIfValueAlreadySet = []string{}

var vitessShouldBeAwareOf = []string{
	"block_encryption_mode",
	"character_set_client",
	"character_set_connection",
	"character_set_database",
	"character_set_filesystem",
	"character_set_server",
	"collation_connection",
	"collation_database",
	"collation_server",
	"completion_type",
	"div_precision_increment",
	"innodb_lock_wait_timeout",
	"interactive_timeout",
	"lc_time_names",
	"lock_wait_timeout",
	"max_allowed_packet",
	"max_error_count",
	"max_execution_time",
	"max_join_size",
	"max_length_for_sort_data",
	"max_sort_length",
	"max_user_connections",
	"session_track_gtids",
	"session_track_schema",
	"session_track_state_change",
	"session_track_system_variables",
	"session_track_transaction_info",
	"time_zone",
	"transaction_isolation",
	"version_tokens_session",
	"sql_auto_is_null",
}

func init() {
	forSettings(ignoreThese, buildSetOpIgnore)
	forSettings(saveSettingsToSession, buildSetOpVarSet)
	forSettings(allowSetIfValueAlreadySet, buildSetOpCheckAndIgnore)
	forSettings(vitessShouldBeAwareOf, buildSetOpCheckAndIgnore)
	forSettings(notSupported, buildNotSupported)
}

func forSettings(settings []string, f planFunc) {
	for _, setting := range settings {
		if _, alreadyExists := sysVarPlanningFunc[setting]; alreadyExists {
			panic("bug in set plan init - " + setting + " aleady configured")<commit_msg>Add more system settings<commit_after>/*
Copyright 2020 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 (
	"fmt"
	"strings"

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

	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
	"vitess.io/vitess/go/vt/vterrors"

	"vitess.io/vitess/go/vt/key"
	"vitess.io/vitess/go/vt/sqlparser"
	"vitess.io/vitess/go/vt/vtgate/engine"
	"vitess.io/vitess/go/vt/vtgate/evalengine"
)

type planFunc = func(expr *sqlparser.SetExpr, vschema ContextVSchema) (engine.SetOp, error)

var sysVarPlanningFunc = map[string]planFunc{}

var notSupported = []string{
	"audit_log_read_buffer_size",
	"auto_increment_increment",
	"auto_increment_offset",
	"binlog_direct_non_transactional_updates",
	"binlog_row_image",
	"binlog_rows_query_log_events",
	"innodb_ft_enable_stopword",
	"innodb_ft_user_stopword_table",
	"max_points_in_geometry",
	"max_sp_recursion_depth",
	"myisam_repair_threads",
	"myisam_sort_buffer_size",
	"myisam_stats_method",
	"ndb_allow_copying_alter_table",
	"ndb_autoincrement_prefetch_sz",
	"ndb_blob_read_batch_bytes",
	"ndb_blob_write_batch_bytes",
	"ndb_deferred_constraints",
	"ndb_force_send",
	"ndb_fully_replicated",
	"ndb_index_stat_enable",
	"ndb_index_stat_option",
	"ndb_join_pushdown",
	"ndb_log_bin",
	"ndb_log_exclusive_reads",
	"ndb_row_checksum",
	"ndb_use_exact_count",
	"ndb_use_transactions",
	"ndbinfo_max_bytes",
	"ndbinfo_max_rows",
	"ndbinfo_show_hidden",
	"ndbinfo_table_prefix",
	"old_alter_table",
	"preload_buffer_size",
	"rbr_exec_mode",
	"sql_log_off",
	"thread_pool_high_priority_connection",
	"thread_pool_prio_kickup_timer",
	"transaction_write_set_extraction",
}

var ignoreThese = []string{
	"big_tables",
	"bulk_insert_buffer_size",
	"debug",
	"default_storage_engine",
	"default_tmp_storage_engine",
	"innodb_strict_mode",
	"innodb_support_xa",
	"innodb_table_locks",
	"innodb_tmpdir",
	"join_buffer_size",
	"keep_files_on_create",
	"lc_messages",
	"long_query_time",
	"low_priority_updates",
	"max_delayed_threads",
	"max_insert_delayed_threads",
	"multi_range_count",
	"net_buffer_length",
	"new",
	"query_cache_type",
	"query_cache_wlock_invalidate",
	"query_prealloc_size",
	"sql_buffer_result",
	"transaction_alloc_block_size",
	"wait_timeout",
}

var useReservedConn = []string{
	"default_week_format",
	"end_markers_in_json",
	"eq_range_index_dive_limit",
	"explicit_defaults_for_timestamp",
	"foreign_key_checks",
	"group_concat_max_len",
	"max_heap_table_size",
	"max_seeks_for_key",
	"max_tmp_tables",
	"min_examined_row_limit",
	"old_passwords",
	"optimizer_prune_level",
	"optimizer_search_depth",
	"optimizer_switch",
	"optimizer_trace",
	"optimizer_trace_features",
	"optimizer_trace_limit",
	"optimizer_trace_max_mem_size",
	"transaction_isolation",
	"tx_isolation",
	"optimizer_trace_offset",
	"parser_max_mem_size",
	"profiling",
	"profiling_history_size",
	"query_alloc_block_size",
	"range_alloc_block_size",
	"range_optimizer_max_mem_size",
	"read_buffer_size",
	"read_rnd_buffer_size",
	"show_create_table_verbosity",
	"show_old_temporals",
	"sort_buffer_size",
	"sql_big_selects",
	"sql_mode",
	"sql_notes",
	"sql_quote_show_create",
	"sql_safe_updates",
	"sql_warnings",
	"tmp_table_size",
	"transaction_prealloc_size",
	"unique_checks",
	"updatable_views_with_limit",
}

// TODO: Most of these settings should be moved into SysSetOpAware, and change Vitess behaviour.
// Until then, SET statements against these settings are allowed
// as long as they have the same value as the underlying database
var checkAndIgnore = []string{
	"binlog_format",
	"block_encryption_mode",
	"character_set_client",
	"character_set_connection",
	"character_set_database",
	"character_set_filesystem",
	"character_set_server",
	"collation_connection",
	"collation_database",
	"collation_server",
	"completion_type",
	"div_precision_increment",
	"innodb_lock_wait_timeout",
	"interactive_timeout",
	"lc_time_names",
	"lock_wait_timeout",
	"max_allowed_packet",
	"max_error_count",
	"max_execution_time",
	"max_join_size",
	"max_length_for_sort_data",
	"max_sort_length",
	"max_user_connections",
	"net_read_timeout",
	"net_retry_count",
	"net_write_timeout",
	"session_track_gtids",
	"session_track_schema",
	"session_track_state_change",
	"session_track_system_variables",
	"session_track_transaction_info",
	"sql_auto_is_null",
	"time_zone",
	"version_tokens_session",
}

func init() {
	forSettings(ignoreThese, buildSetOpIgnore)
	forSettings(useReservedConn, buildSetOpVarSet)
	forSettings(checkAndIgnore, buildSetOpCheckAndIgnore)
	forSettings(notSupported, buildNotSupported)
}

func forSettings(settings []string, f planFunc) {
	for _, setting := range settings {
		if _, alreadyExists := sysVarPlanningFunc[setting]; alreadyExists {
			panic("bug in set plan init - " + setting + " aleady configured")
{'fraction_non_alphanumeric': 0.11252404359905963, 'fraction_numerical': 0.0017097670442402222, 'mean_word_length': 7.754911131898971, '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': '3689785', 'n_tokens_mistral': 3633, 'n_tokens_neox': 3532, 'n_words': 577}