├── .dockerignore
├── .editorconfig
├── .github
├── ISSUE_TEMPLATE
│ ├── bug-report.md
│ └── enhancement.md
├── PULL_REQUEST_TEMPLATE.md
├── auto-comment.yml
└── workflows
│ ├── changelog.yml
│ ├── codeql-analysis.yml
│ ├── docker-image.yml
│ ├── go-integration.yml
│ ├── go.yml
│ ├── licence-checker.yml
│ ├── linelint.yml
│ ├── release.yml
│ └── reviewdog.yml
├── .gitignore
├── .golangci.yml
├── .idea
└── icon.png
├── .licenserc.yaml
├── .linelint.yml
├── .pre-commit-config.yaml
├── CHANGELOG.md
├── CONTRIBUTING.md
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── README_CN.md
├── VERSION
├── cliff.toml
├── cmd
├── admin.go
├── arana
│ └── arana.go
├── cmd.go
├── import.go
└── start.go
├── conf
├── bootstrap.docker.yaml
├── bootstrap.local-etcd.yaml
├── bootstrap.yaml
└── config.yaml
├── docker-compose.yaml
├── docs
└── pics
│ ├── arana-architecture.png
│ ├── arana-blue.png
│ ├── arana-db-blue.png
│ ├── arana-db-v0.2.sketch
│ ├── arana-logo.png
│ ├── arana-logo.sketch
│ ├── arana-main.png
│ └── dingtalk-group.jpeg
├── example
├── admin_server
│ └── main.go
├── import_config
│ └── main.go
├── local_server
│ └── main.go
└── service_discovery
│ ├── etcd
│ └── main.go
│ └── nacos
│ └── main.go
├── go.mod
├── go.sum
├── integration_test
├── config
│ ├── db
│ │ ├── config.yaml
│ │ ├── data.yaml
│ │ └── expected.yaml
│ ├── db_tbl
│ │ ├── config.yaml
│ │ ├── data.yaml
│ │ └── expected.yaml
│ ├── db_tbl_rw
│ │ ├── config.yaml
│ │ ├── data.yaml
│ │ └── expected.yaml
│ └── tbl
│ │ ├── config.yaml
│ │ ├── data.yaml
│ │ └── expected.yaml
├── scene
│ ├── db
│ │ └── integration_test.go
│ ├── db_tbl
│ │ └── integration_test.go
│ ├── db_tbl_rw
│ │ └── integration_test.go
│ └── tbl
│ │ └── integration_test.go
├── scripts
│ ├── db
│ │ ├── init.sql
│ │ ├── sequence.sql
│ │ └── sharding.sql
│ ├── db_tbl
│ │ ├── init.sql
│ │ ├── sequence.sql
│ │ └── sharding.sql
│ ├── db_tbl_rw
│ │ ├── init.sql
│ │ ├── sequence.sql
│ │ └── sharding.sql
│ └── tbl
│ │ ├── init.sql
│ │ ├── sequence.sql
│ │ └── sharding.sql
└── testcase
│ └── casetest.yaml
├── justfile
├── pkg
├── admin
│ ├── admin.api.yaml
│ ├── admin.go
│ ├── config.go
│ ├── config_service.go
│ ├── cors.go
│ ├── exception
│ │ └── exception.go
│ ├── jwt.go
│ ├── router
│ │ ├── clusters.go
│ │ ├── db_groups.go
│ │ ├── misc.go
│ │ ├── nodes.go
│ │ ├── services.go
│ │ ├── tables.go
│ │ ├── tenants.go
│ │ └── users.go
│ └── service_discovery.go
├── boot
│ ├── boot.go
│ ├── discovery.go
│ ├── discovery_import.go
│ ├── discovery_test.go
│ ├── discovery_watch.go
│ ├── plugin.go
│ ├── proto.go
│ └── watcher.go
├── config
│ ├── README.md
│ ├── api.go
│ ├── api_test.go
│ ├── boot_misc.go
│ ├── boot_misc_test.go
│ ├── config.go
│ ├── config_reader.go
│ ├── config_test.go
│ ├── config_watcher.go
│ ├── config_writer.go
│ ├── default.go
│ ├── diff.go
│ ├── diff_test.go
│ ├── equals.go
│ ├── equals_test.go
│ ├── etcd
│ │ ├── etcd.go
│ │ ├── etcd_example.md
│ │ └── etcd_test.go
│ ├── event.go
│ ├── event_test.go
│ ├── file
│ │ ├── file.go
│ │ └── file_test.go
│ ├── misc.go
│ ├── misc_test.go
│ ├── mock
│ │ └── mock_config.go
│ ├── model.go
│ ├── model_test.go
│ ├── nacos
│ │ ├── nacos.go
│ │ └── nacos_test.go
│ ├── options.go
│ ├── path.go
│ └── tenant.go
├── constants
│ ├── const.go
│ ├── env.go
│ ├── env_test.go
│ └── mysql
│ │ ├── collations.go
│ │ ├── constants.go
│ │ ├── constants_test.go
│ │ ├── type.go
│ │ └── type_test.go
├── dataset
│ ├── chain.go
│ ├── dataset.go
│ ├── filter.go
│ ├── filter_test.go
│ ├── fuse.go
│ ├── fuse_test.go
│ ├── group_reduce.go
│ ├── group_reduce_test.go
│ ├── ordered.go
│ ├── ordered_test.go
│ ├── parallel.go
│ ├── parallel_test.go
│ ├── priority_queue.go
│ ├── priority_queue_test.go
│ ├── reduce.go
│ ├── reduce_test.go
│ ├── sort_merge_join.go
│ ├── sort_merge_join_test.go
│ ├── transform.go
│ ├── transform_test.go
│ ├── virtual.go
│ └── virtual_test.go
├── executor
│ ├── redirect.go
│ └── redirect_test.go
├── merge
│ ├── aggregator.go
│ ├── aggregator
│ │ ├── add_aggregator.go
│ │ ├── add_aggregator_test.go
│ │ ├── avg_aggregator.go
│ │ ├── avg_aggregator_test.go
│ │ ├── init.go
│ │ ├── load_agg.go
│ │ ├── loag_agg_test.go
│ │ ├── max_aggregator.go
│ │ ├── max_aggregator_test.go
│ │ ├── min_aggregator.go
│ │ └── min_aggregator_test.go
│ ├── merge_rows.go
│ ├── merge_rows_test.go
│ ├── priority_queue.go
│ └── priority_queue_test.go
├── metrics
│ └── metrics.go
├── mysql
│ ├── auth.go
│ ├── auth_test.go
│ ├── client.go
│ ├── client_test.go
│ ├── conn.go
│ ├── conn_test.go
│ ├── encoding.go
│ ├── encoding_test.go
│ ├── errors
│ │ ├── error.go
│ │ └── sql_error.go
│ ├── execute_handle.go
│ ├── fields.go
│ ├── fields_test.go
│ ├── result.go
│ ├── rows.go
│ ├── rows
│ │ ├── codec.go
│ │ ├── codec_test.go
│ │ ├── virtual_row.go
│ │ └── virtual_row_test.go
│ ├── rows_test.go
│ ├── server.go
│ ├── statement.go
│ ├── thead
│ │ └── thead.go
│ ├── utils.go
│ └── utils_test.go
├── proto
│ ├── data.go
│ ├── function.go
│ ├── hint
│ │ ├── hint.go
│ │ └── hint_test.go
│ ├── interface.go
│ ├── rule
│ │ ├── attributes.go
│ │ ├── attributes_test.go
│ │ ├── database_table.go
│ │ ├── database_table_test.go
│ │ ├── range.go
│ │ ├── range_test.go
│ │ ├── rule.go
│ │ ├── rule_test.go
│ │ ├── sequence.go
│ │ ├── shards.go
│ │ ├── shards_test.go
│ │ ├── topology.go
│ │ └── topology_test.go
│ ├── runtime.go
│ ├── schema.go
│ ├── sequence.go
│ ├── stmt.go
│ └── value.go
├── reduce
│ ├── max.go
│ ├── min.go
│ ├── reduce.go
│ ├── reduce_test.go
│ └── sum.go
├── registry
│ ├── base
│ │ └── base.go
│ ├── discovery.go
│ ├── etcd
│ │ ├── discovery.go
│ │ └── registery.go
│ ├── nacos
│ │ ├── client.go
│ │ ├── discovery.go
│ │ └── registry.go
│ ├── registry.go
│ └── store
│ │ ├── etcdv3.go
│ │ └── store.go
├── resultx
│ ├── resultx.go
│ └── resultx_test.go
├── runtime
│ ├── ast
│ │ ├── alter_table.go
│ │ ├── analyze_table.go
│ │ ├── ast.go
│ │ ├── ast_test.go
│ │ ├── check_table.go
│ │ ├── create_index.go
│ │ ├── create_table.go
│ │ ├── delete.go
│ │ ├── describe.go
│ │ ├── drop_index.go
│ │ ├── drop_table.go
│ │ ├── expression.go
│ │ ├── expression_atom.go
│ │ ├── function.go
│ │ ├── insert.go
│ │ ├── join.go
│ │ ├── kill.go
│ │ ├── misc.go
│ │ ├── misc_test.go
│ │ ├── model.go
│ │ ├── optimize_table.go
│ │ ├── predicate.go
│ │ ├── proto.go
│ │ ├── rename_table.go
│ │ ├── repair_table.go
│ │ ├── restore.go
│ │ ├── select.go
│ │ ├── select_element.go
│ │ ├── set_variable.go
│ │ ├── show.go
│ │ ├── table_source.go
│ │ ├── trigger.go
│ │ ├── truncate.go
│ │ ├── union.go
│ │ ├── update.go
│ │ └── visitor.go
│ ├── builtin
│ │ ├── shard_computer_javascript.go
│ │ └── shard_computer_javascript_test.go
│ ├── calc
│ │ ├── calculus.go
│ │ ├── calculus_test.go
│ │ ├── logic
│ │ │ ├── bool.go
│ │ │ ├── logic.go
│ │ │ ├── logic_test.go
│ │ │ └── string.go
│ │ ├── misc.go
│ │ └── misc_test.go
│ ├── cmp
│ │ ├── cmp.go
│ │ └── cmp_test.go
│ ├── conn_pool.go
│ ├── conn_pool_test.go
│ ├── context
│ │ ├── context.go
│ │ └── context_test.go
│ ├── function
│ │ ├── abs.go
│ │ ├── abs_test.go
│ │ ├── acos.go
│ │ ├── acos_test.go
│ │ ├── asin.go
│ │ ├── asin_test.go
│ │ ├── cast.go
│ │ ├── cast_char.go
│ │ ├── cast_char_test.go
│ │ ├── cast_charset.go
│ │ ├── cast_date.go
│ │ ├── cast_date_test.go
│ │ ├── cast_datetime.go
│ │ ├── cast_datetime_test.go
│ │ ├── cast_decimal.go
│ │ ├── cast_decimal_test.go
│ │ ├── cast_nchar.go
│ │ ├── cast_nchar_test.go
│ │ ├── cast_test.go
│ │ ├── cast_time.go
│ │ ├── cast_time_test.go
│ │ ├── ceil.go
│ │ ├── ceil_test.go
│ │ ├── char_charset_test.go
│ │ ├── char_length.go
│ │ ├── char_length_test.go
│ │ ├── concat.go
│ │ ├── concat_test.go
│ │ ├── concat_ws.go
│ │ ├── concat_ws_test.go
│ │ ├── cos.go
│ │ ├── cos_test.go
│ │ ├── cume_dist.go
│ │ ├── cume_dist_test.go
│ │ ├── dense_rank.go
│ │ ├── dense_rank_test.go
│ │ ├── doc.go
│ │ ├── exp.go
│ │ ├── exp_test.go
│ │ ├── first_value.go
│ │ ├── first_value_test.go
│ │ ├── floor.go
│ │ ├── floor_test.go
│ │ ├── format_bytes.go
│ │ ├── format_bytes_test.go
│ │ ├── function_test.go
│ │ ├── if.go
│ │ ├── if_null.go
│ │ ├── if_null_test.go
│ │ ├── if_test.go
│ │ ├── lag.go
│ │ ├── lag_test.go
│ │ ├── last_value.go
│ │ ├── last_value_test.go
│ │ ├── lead.go
│ │ ├── lead_test.go
│ │ ├── left.go
│ │ ├── left_test.go
│ │ ├── length.go
│ │ ├── length_test.go
│ │ ├── lower.go
│ │ ├── lower_test.go
│ │ ├── lpad.go
│ │ ├── lpad_test.go
│ │ ├── ltrim.go
│ │ ├── ltrim_test.go
│ │ ├── md5.go
│ │ ├── md5_test.go
│ │ ├── mod.go
│ │ ├── mod_test.go
│ │ ├── nth_value.go
│ │ ├── nth_value_test.go
│ │ ├── ntile.go
│ │ ├── ntile_test.go
│ │ ├── percent_rank.go
│ │ ├── percent_rank_test.go
│ │ ├── pi.go
│ │ ├── pi_test.go
│ │ ├── power.go
│ │ ├── power_test.go
│ │ ├── rand.go
│ │ ├── rand_test.go
│ │ ├── rank.go
│ │ ├── rank_test.go
│ │ ├── repeat.go
│ │ ├── repeat_test.go
│ │ ├── replace.go
│ │ ├── replace_test.go
│ │ ├── reverse.go
│ │ ├── reverse_test.go
│ │ ├── right.go
│ │ ├── right_test.go
│ │ ├── round.go
│ │ ├── round_test.go
│ │ ├── row_number.go
│ │ ├── row_number_test.go
│ │ ├── rpad.go
│ │ ├── rpad_test.go
│ │ ├── rtrim.go
│ │ ├── rtrim_test.go
│ │ ├── sha1.go
│ │ ├── sha1_test.go
│ │ ├── sin.go
│ │ ├── sin_test.go
│ │ ├── space.go
│ │ ├── space_test.go
│ │ ├── sqrt.go
│ │ ├── sqrt_test.go
│ │ ├── strcmp.go
│ │ ├── strcmp_test.go
│ │ ├── substring.go
│ │ ├── substring_test.go
│ │ ├── tan.go
│ │ ├── tan_test.go
│ │ ├── truncate.go
│ │ ├── truncate_test.go
│ │ ├── upper.go
│ │ └── upper_test.go
│ ├── gtid
│ │ ├── gtid.go
│ │ └── gtid_test.go
│ ├── misc
│ │ ├── escape.go
│ │ ├── escape_test.go
│ │ ├── extvalue
│ │ │ ├── div.go
│ │ │ ├── div_test.go
│ │ │ ├── extvalue.go
│ │ │ ├── extvalue_test.go
│ │ │ └── visitor.go
│ │ ├── like.go
│ │ ├── like_test.go
│ │ ├── other.go
│ │ ├── pair.go
│ │ └── strings.go
│ ├── mock_runtime.go
│ ├── namespace
│ │ ├── command.go
│ │ ├── namespace.go
│ │ └── namespace_test.go
│ ├── optimize
│ │ ├── dal
│ │ │ ├── analyze_table.go
│ │ │ ├── kill.go
│ │ │ ├── show_character_set.go
│ │ │ ├── show_collation.go
│ │ │ ├── show_columns.go
│ │ │ ├── show_create.go
│ │ │ ├── show_create_sequence.go
│ │ │ ├── show_database_rule.go
│ │ │ ├── show_databases.go
│ │ │ ├── show_index.go
│ │ │ ├── show_master_status.go
│ │ │ ├── show_nodes.go
│ │ │ ├── show_open_tables.go
│ │ │ ├── show_process_list.go
│ │ │ ├── show_replica_status.go
│ │ │ ├── show_replicas.go
│ │ │ ├── show_sharding_table.go
│ │ │ ├── show_status.go
│ │ │ ├── show_table_rule.go
│ │ │ ├── show_table_status.go
│ │ │ ├── show_tables.go
│ │ │ ├── show_topology.go
│ │ │ ├── show_users.go
│ │ │ ├── show_variables.go
│ │ │ └── show_warnings.go
│ │ ├── ddl
│ │ │ ├── alter_table.go
│ │ │ ├── check_table.go
│ │ │ ├── create_index.go
│ │ │ ├── create_table.go
│ │ │ ├── drop_index.go
│ │ │ ├── drop_table.go
│ │ │ ├── drop_trigger.go
│ │ │ ├── optimize_table.go
│ │ │ ├── rename_table.go
│ │ │ ├── repair_table.go
│ │ │ ├── set_variable.go
│ │ │ └── truncate.go
│ │ ├── dml
│ │ │ ├── aggregate_visitor.go
│ │ │ ├── aggregate_visitor_test.go
│ │ │ ├── delete.go
│ │ │ ├── ext
│ │ │ │ ├── ext.go
│ │ │ │ ├── select_element.go
│ │ │ │ └── weak_alias.go
│ │ │ ├── insert.go
│ │ │ ├── select.go
│ │ │ ├── select_scanner.go
│ │ │ ├── select_scanner_test.go
│ │ │ └── update.go
│ │ ├── hints.go
│ │ ├── optimizer.go
│ │ ├── optimizer_test.go
│ │ ├── shard_visitor.go
│ │ ├── shard_visitor_test.go
│ │ └── utility
│ │ │ ├── describe.go
│ │ │ └── explain.go
│ ├── plan
│ │ ├── always.go
│ │ ├── dal
│ │ │ ├── analyze_table.go
│ │ │ ├── constant.go
│ │ │ ├── kill.go
│ │ │ ├── show_character_set.go
│ │ │ ├── show_collation.go
│ │ │ ├── show_columns.go
│ │ │ ├── show_create.go
│ │ │ ├── show_create_sequence.go
│ │ │ ├── show_database_rules.go
│ │ │ ├── show_database_rules_test.go
│ │ │ ├── show_databases.go
│ │ │ ├── show_index.go
│ │ │ ├── show_master_status.go
│ │ │ ├── show_master_status_test.go
│ │ │ ├── show_nodes.go
│ │ │ ├── show_open_tables.go
│ │ │ ├── show_process_list.go
│ │ │ ├── show_replica_status.go
│ │ │ ├── show_replicas.go
│ │ │ ├── show_sharding_table.go
│ │ │ ├── show_status.go
│ │ │ ├── show_table_rules.go
│ │ │ ├── show_table_status.go
│ │ │ ├── show_tables.go
│ │ │ ├── show_topology.go
│ │ │ ├── show_users.go
│ │ │ ├── show_variables.go
│ │ │ └── show_warnings.go
│ │ ├── ddl
│ │ │ ├── alter_table.go
│ │ │ ├── check_table.go
│ │ │ ├── create_index.go
│ │ │ ├── create_table.go
│ │ │ ├── drop_index.go
│ │ │ ├── drop_table.go
│ │ │ ├── drop_trigger.go
│ │ │ ├── optimize_table.go
│ │ │ ├── rename_table.go
│ │ │ ├── repair_table.go
│ │ │ ├── set_variable.go
│ │ │ └── truncate.go
│ │ ├── dml
│ │ │ ├── aggregate.go
│ │ │ ├── composite.go
│ │ │ ├── drop_weak.go
│ │ │ ├── group_plan.go
│ │ │ ├── hash_join.go
│ │ │ ├── hash_join_test.go
│ │ │ ├── insert_select.go
│ │ │ ├── limit.go
│ │ │ ├── local_select.go
│ │ │ ├── local_sequence.go
│ │ │ ├── mapping.go
│ │ │ ├── order.go
│ │ │ ├── rename.go
│ │ │ ├── simple_delete.go
│ │ │ ├── simple_insert.go
│ │ │ ├── simple_join.go
│ │ │ ├── simple_select.go
│ │ │ └── update.go
│ │ ├── plan.go
│ │ ├── transparent.go
│ │ └── utility
│ │ │ ├── describle.go
│ │ │ └── explain.go
│ ├── runtime.go
│ ├── runtime_test.go
│ ├── tenant
│ │ └── tenant.go
│ ├── transaction
│ │ ├── deadlock.go
│ │ ├── fault_decision.go
│ │ ├── hook.go
│ │ ├── trx_log.go
│ │ ├── trx_log_test.go
│ │ ├── trx_manager.go
│ │ ├── types.go
│ │ └── xa.go
│ ├── tx.go
│ └── tx_test.go
├── schema
│ ├── loader.go
│ └── loader_test.go
├── security
│ ├── tenant.go
│ └── tenant_test.go
├── selector
│ ├── db_manager.go
│ ├── weight_random.go
│ └── weight_random_test.go
├── sequence
│ ├── group
│ │ ├── group.go
│ │ └── group_test.go
│ ├── sequence.go
│ └── snowflake
│ │ ├── snowflake.go
│ │ └── snowflake_test.go
├── server
│ └── server.go
├── trace
│ ├── jaeger
│ │ ├── jaeger.go
│ │ └── jaeger_test.go
│ ├── trace.go
│ └── trace_test.go
└── util
│ ├── bufferpool
│ ├── bufferpool.go
│ └── bufferpool_test.go
│ ├── bytefmt
│ ├── bytefmt.go
│ └── bytefmt_test.go
│ ├── bytesconv
│ ├── bytesconv.go
│ └── bytesconv_test.go
│ ├── charsetconv
│ ├── charsetconv.go
│ └── charsetconv_test.go
│ ├── config
│ ├── config.go
│ ├── config_test.go
│ ├── nacos.go
│ └── nacos_test.go
│ ├── env
│ ├── env.go
│ └── env_test.go
│ ├── file
│ ├── file.go
│ └── file_test.go
│ ├── identity
│ ├── identity.go
│ └── identity_test.go
│ ├── log
│ └── logging.go
│ ├── match
│ ├── slice_match.go
│ └── slice_match_test.go
│ ├── math
│ ├── abs.go
│ ├── abs_test.go
│ ├── compare.go
│ ├── compare_test.go
│ ├── decimal.go
│ ├── decimal_test.go
│ ├── sal.go
│ └── sal_test.go
│ ├── misc
│ ├── misc.go
│ └── misc_test.go
│ ├── net
│ └── net.go
│ ├── rand2
│ └── rand2.go
│ ├── runes
│ ├── rune.go
│ └── rune_test.go
│ └── tableprint
│ └── print_table.go
├── scripts
├── init.sql
├── sequence.sql
└── sharding.sql
├── staticcheck.conf
├── test
├── dataset.go
├── integration_test.go
├── suite.go
└── testcontainer_mysql.go
├── testdata
├── fake_bootstrap.yaml
├── fake_config.yaml
├── fake_empty_config.yaml
├── mock_data.go
├── mock_interface.go
├── mock_rule.go
├── mock_runtime.go
├── mock_schema.go
├── mock_sequence.go
└── testdata.go
└── third_party
├── base58
├── README.md
├── alphabet.go
├── base58.go
├── base58_test.go
├── base58bench_test.go
├── base58check.go
├── base58check_test.go
├── doc.go
└── example_test.go
├── bucketpool
├── bucketpool.go
└── bucketpool_test.go
├── cache
├── lru_cache.go
├── lru_cache_test.go
└── perf_test.go
├── pools
├── id_pool.go
├── id_pool_test.go
├── numbered.go
├── numbered_test.go
├── resource_pool.go
└── resource_pool_flaky_test.go
├── sync2
├── batcher.go
├── batcher_test.go
├── consolidator.go
├── consolidator_test.go
├── doc.go
├── semaphore.go
└── semaphore_flaky_test.go
└── timer
├── randticker.go
├── randticker_flaky_test.go
├── timer.go
└── timer_flaky_test.go
/.dockerignore:
--------------------------------------------------------------------------------
1 | ### Go template
2 | # Binaries for programs and plugins
3 | *.exe
4 | *.exe~
5 | *.dll
6 | *.so
7 | *.dylib
8 |
9 | # Test binary, built with `go test -c`
10 | *.test
11 |
12 | # Output of the go coverage tool, specifically when used with LiteIDE
13 | *.out
14 |
15 | # Dependency directories (remove the comment below to include it)
16 | bin/
17 | vendor/
18 | dist/
19 |
20 | scripts/
21 | docs/
22 |
23 | /docker-compose.yaml
24 | .golangci.yml
25 | .licenserc.yaml
26 | .editorconfig
27 | .linelint.yml
28 | .pre-commit-config.yaml
29 | staticcheck.conf
30 | .github/
31 | .chglog/
32 | justfile
33 | Makefile
34 | cliff.toml
35 | *.md
36 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | root = true
19 |
20 | # Unix-style newlines with a newline ending every file
21 | [*]
22 | end_of_line = lf
23 | insert_final_newline = true
24 | charset = utf-8
25 | trim_trailing_whitespace = true
26 | indent_style = space
27 | indent_size = 2
28 |
29 | [{Makefile,go.mod,go.sum,*.go,.gitmodules}]
30 | indent_style = tab
31 | indent_size = 4
32 |
33 | [*.md]
34 | indent_size = 4
35 | trim_trailing_whitespace = false
36 |
37 | [Dockerfile]
38 | indent_size = 4
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug-report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug Report
3 | about: Report a bug
4 | labels: kind/bug
5 |
6 | ---
7 |
8 |
11 |
12 |
13 | **What happened**:
14 |
15 | **What you expected to happen**:
16 |
17 | **How to reproduce it (as minimally and precisely as possible)**:
18 |
19 | **Anything else we need to know?**:
20 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/enhancement.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Enhancement Request
3 | about: Suggest an enhancement
4 | labels: kind/feature
5 |
6 | ---
7 |
8 |
9 | **What would you like to be added**:
10 |
11 | **Why is this needed**:
12 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
3 |
4 | **What this PR does**:
5 |
6 | **Which issue(s) this PR fixes**:
7 |
12 | Fixes #
13 |
14 | **Special notes for your reviewer**:
15 |
16 | **Does this PR introduce a user-facing change?**:
17 |
22 | ```release-note
23 |
24 | ```
25 |
--------------------------------------------------------------------------------
/.github/auto-comment.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one
3 | # or more contributor license agreements. See the NOTICE file
4 | # distributed with this work for additional information
5 | # regarding copyright ownership. The ASF licenses this file
6 | # to you under the Apache License, Version 2.0 (the
7 | # "License"); you may not use this file except in compliance
8 | # with the License. You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing,
13 | # software distributed under the License is distributed on an
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | # KIND, either express or implied. See the License for the
16 | # specific language governing permissions and limitations
17 | # under the License.
18 | #
19 |
20 | # Comment to a new issue.
21 | issueOpened: >
22 | Thank your for raising a issue. We will try and get back to you as soon as possible.
23 | Please make sure you have given us as much context as possible.
24 | pullRequestOpened: >
25 | Thank your for raising your pull request.
26 | Please make sure you have followed our contributing guidelines. We will review it as soon as possible
27 |
--------------------------------------------------------------------------------
/.github/workflows/changelog.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one
3 | # or more contributor license agreements. See the NOTICE file
4 | # distributed with this work for additional information
5 | # regarding copyright ownership. The ASF licenses this file
6 | # to you under the Apache License, Version 2.0 (the
7 | # "License"); you may not use this file except in compliance
8 | # with the License. You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing,
13 | # software distributed under the License is distributed on an
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | # KIND, either express or implied. See the License for the
16 | # specific language governing permissions and limitations
17 | # under the License.
18 | #
19 |
20 | name: Changelog
21 | on:
22 | release:
23 | types:
24 | - created
25 | jobs:
26 | changelog:
27 | runs-on: ubuntu-latest
28 | name: auto generate changelog
29 | steps:
30 | - name: Checkout
31 | uses: actions/checkout@v3
32 | with:
33 | fetch-depth: 0
34 |
35 | - name: Generate a changelog
36 | uses: orhun/git-cliff-action@v2
37 | id: git-cliff
38 | with:
39 | config: cliff.toml
40 | args: --verbose --strip 'footer' --exclude-path '.github/**'
41 | env:
42 | OUTPUT: CHANGELOG.md
43 |
44 | - name: Print the changelog
45 | run: cat "${{ steps.git-cliff.outputs.changelog }}"
46 |
--------------------------------------------------------------------------------
/.github/workflows/licence-checker.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one
3 | # or more contributor license agreements. See the NOTICE file
4 | # distributed with this work for additional information
5 | # regarding copyright ownership. The ASF licenses this file
6 | # to you under the Apache License, Version 2.0 (the
7 | # "License"); you may not use this file except in compliance
8 | # with the License. You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing,
13 | # software distributed under the License is distributed on an
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | # KIND, either express or implied. See the License for the
16 | # specific language governing permissions and limitations
17 | # under the License.
18 | #
19 |
20 | name: License checker
21 |
22 | on:
23 | push:
24 | branches:
25 | - master
26 | pull_request:
27 | branches:
28 | - master
29 |
30 | jobs:
31 | check-license:
32 | runs-on: ubuntu-latest
33 | steps:
34 | - uses: actions/checkout@v2
35 |
36 | - name: Check License Header
37 | uses: apache/skywalking-eyes@main
38 | env:
39 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40 | with:
41 | log: info
42 | config: .licenserc.yaml
43 |
--------------------------------------------------------------------------------
/.github/workflows/linelint.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one
3 | # or more contributor license agreements. See the NOTICE file
4 | # distributed with this work for additional information
5 | # regarding copyright ownership. The ASF licenses this file
6 | # to you under the Apache License, Version 2.0 (the
7 | # "License"); you may not use this file except in compliance
8 | # with the License. You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing,
13 | # software distributed under the License is distributed on an
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | # KIND, either express or implied. See the License for the
16 | # specific language governing permissions and limitations
17 | # under the License.
18 | #
19 |
20 | name: linelint
21 | on: [push, pull_request]
22 |
23 | jobs:
24 | linelint:
25 | runs-on: ubuntu-latest
26 | name: Check if all files end in newline
27 | steps:
28 | - name: Checkout
29 | uses: actions/checkout@v2
30 | - name: Linelint
31 | uses: fernandrone/linelint@0.0.4
32 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one
3 | # or more contributor license agreements. See the NOTICE file
4 | # distributed with this work for additional information
5 | # regarding copyright ownership. The ASF licenses this file
6 | # to you under the Apache License, Version 2.0 (the
7 | # "License"); you may not use this file except in compliance
8 | # with the License. You may obtain a copy of the License at
9 | #
10 | # http://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing,
13 | # software distributed under the License is distributed on an
14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | # KIND, either express or implied. See the License for the
16 | # specific language governing permissions and limitations
17 | # under the License.
18 | #
19 |
20 | name: Release
21 | on:
22 | release:
23 | types: [ created ]
24 |
25 | jobs:
26 | releases-matrix:
27 | name: Release Go Binary
28 | runs-on: ubuntu-latest
29 | strategy:
30 | matrix:
31 | # build and publish in parallel: linux/386, linux/amd64, windows/386, windows/amd64, darwin/amd64
32 | goos: [ linux, windows ]
33 | goarch: [ "386", amd64, arm ]
34 | exclude:
35 | - goarch: "arm"
36 | goos: windows
37 |
38 | steps:
39 | - uses: actions/checkout@v2
40 | - uses: wangyoucao577/go-release-action@v1.20
41 | with:
42 | github_token: ${{ secrets.GITHUB_TOKEN }}
43 | goos: ${{ matrix.goos }}
44 | goarch: ${{ matrix.goarch }}
45 | goversion: "https://go.dev/dl/go1.20.11.linux-amd64.tar.gz"
46 | project_path: "./cmd/arana"
47 | binary_name: "arana"
48 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Binaries for programs and plugins
2 | *.exe
3 | *.exe~
4 | *.dll
5 | *.so
6 | *.dylib
7 |
8 | # Test binary, built with `go test -c`
9 | *.test
10 |
11 | # Output of the go coverage tool, specifically when used with LiteIDE
12 | *.out
13 |
14 | /coverage.txt
15 |
16 | # Dependency directories (remove the comment below to include it)
17 | vendor/
18 | .idea/
19 |
20 | .tmp/
21 | /docker/data/
22 | /docker/mysqld/
23 | /dist
24 | /default.etcd
25 |
26 | .codecc
27 | .vscode
28 | .go-version
29 |
30 | log/
31 | slow_log/
32 |
--------------------------------------------------------------------------------
/.idea/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/.idea/icon.png
--------------------------------------------------------------------------------
/.linelint.yml:
--------------------------------------------------------------------------------
1 | # 'true' will fix files
2 | autofix: true
3 |
4 | # list of paths to ignore, uses gitignore syntaxes (executes before any rule)
5 | ignore:
6 | - .git/
7 |
8 | rules:
9 | # checks if file ends in a newline character
10 | end-of-file:
11 | # set to true to enable this rule
12 | enable: true
13 |
14 | # set to true to disable autofix (if enabled globally)
15 | disable-autofix: false
16 |
17 | # if true also checks if file ends in a single newline character
18 | single-new-line: true
19 |
--------------------------------------------------------------------------------
/.pre-commit-config.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | # See https://pre-commit.com for more information
19 | # See https://pre-commit.com/hooks.html for more hooks
20 | # See https://github.com/tekwizely/pre-commit-golang for golang hooks
21 |
22 | repos:
23 | - repo: https://github.com/pre-commit/pre-commit-hooks
24 | rev: v2.3.0
25 | hooks:
26 | - id: end-of-file-fixer
27 | - id: trailing-whitespace
28 | - repo: https://github.com/tekwizely/pre-commit-golang
29 | rev: v1.0.0-rc.1
30 | hooks:
31 | - id: my-cmd-mod
32 | name: imports-formatter
33 | alias: imports-formatter
34 | args: [ imports-formatter ]
35 | - id: my-cmd-mod
36 | name: license-eye
37 | alias: license-eye
38 | args: [ license-eye, header, fix ]
39 | - id: go-mod-tidy
40 | - id: go-staticcheck-mod
41 | - repo: https://github.com/golangci/golangci-lint
42 | rev: v1.53.3
43 | hooks:
44 | - id: golangci-lint
45 |
--------------------------------------------------------------------------------
/VERSION:
--------------------------------------------------------------------------------
1 | 0.2.0
2 |
--------------------------------------------------------------------------------
/cmd/arana/arana.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package main
19 |
20 | import (
21 | "github.com/arana-db/arana/cmd"
22 | )
23 |
24 | func main() {
25 | cmd.Main()
26 | }
27 |
--------------------------------------------------------------------------------
/cmd/cmd.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package cmd
19 |
20 | import (
21 | "github.com/pkg/errors"
22 |
23 | "github.com/spf13/cobra"
24 | )
25 |
26 | var Version = "0.1.0"
27 |
28 | var RootCommand = &cobra.Command{
29 | Use: "arana",
30 | Short: "arana is a db proxy server",
31 | Version: Version,
32 | }
33 |
34 | func Main() error {
35 | return errors.WithStack(RootCommand.Execute())
36 | }
37 |
--------------------------------------------------------------------------------
/conf/bootstrap.docker.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | listeners:
19 | - protocol_type: mysql
20 | server_version: 5.7.0
21 | socket_address:
22 | address: 0.0.0.0
23 | port: 13306
24 |
25 | registry:
26 | enable: false
27 | name: etcd
28 | root_path: arana
29 | options:
30 | endpoints: "http://etcd:2379"
31 |
32 | config:
33 | name: etcd
34 | options:
35 | endpoints: "http://etcd:2379"
36 |
37 | trace:
38 | type: jaeger
39 | address: 'http://jaeger:14268/api/traces'
40 |
41 | supervisor:
42 | username: root
43 | password: root
44 |
45 | logging:
46 | level: INFO
47 | path: /var/log/arana
48 | max_size: 128m
49 | max_backups: 3
50 | max_age: 7
51 | compress: true
52 | console: true
53 |
--------------------------------------------------------------------------------
/conf/bootstrap.local-etcd.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | listeners:
19 | - protocol_type: mysql
20 | server_version: 5.7.0
21 | socket_address:
22 | address: 0.0.0.0
23 | port: 13306
24 |
25 | config:
26 | name: etcd
27 | options:
28 | endpoints: "http://127.0.0.1:2379"
29 |
30 | supervisor:
31 | username: root
32 | password: root
33 |
34 | logging:
35 | level: INFO
36 | path: ~/arana/logs
37 | max_size: 128m
38 | max_backups: 5
39 | max_age: 30
40 | compress: false
41 | console: true
42 |
--------------------------------------------------------------------------------
/docs/pics/arana-architecture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-architecture.png
--------------------------------------------------------------------------------
/docs/pics/arana-blue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-blue.png
--------------------------------------------------------------------------------
/docs/pics/arana-db-blue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-db-blue.png
--------------------------------------------------------------------------------
/docs/pics/arana-db-v0.2.sketch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-db-v0.2.sketch
--------------------------------------------------------------------------------
/docs/pics/arana-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-logo.png
--------------------------------------------------------------------------------
/docs/pics/arana-logo.sketch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-logo.sketch
--------------------------------------------------------------------------------
/docs/pics/arana-main.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/arana-main.png
--------------------------------------------------------------------------------
/docs/pics/dingtalk-group.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arana-db/arana/1e525f416bc58c01df88644eabf18cd151f6b3f8/docs/pics/dingtalk-group.jpeg
--------------------------------------------------------------------------------
/example/admin_server/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package main
19 |
20 | import (
21 | "os"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/cmd"
26 | "github.com/arana-db/arana/testdata"
27 | )
28 |
29 | func init() {
30 | os.Args = append(
31 | os.Args,
32 | "admin",
33 | "-c",
34 | testdata.Path("../conf/bootstrap.local-etcd.yaml"),
35 | "-p",
36 | "8080",
37 | )
38 | }
39 |
40 | func main() {
41 | _ = cmd.Main()
42 | }
43 |
--------------------------------------------------------------------------------
/example/import_config/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package main
19 |
20 | import (
21 | "os"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/cmd"
26 | "github.com/arana-db/arana/testdata"
27 | )
28 |
29 | func init() {
30 | os.Args = append(
31 | os.Args,
32 | "import",
33 | "-c",
34 | testdata.Path("../conf/bootstrap.yaml"),
35 | "-s",
36 | testdata.Path("../conf/config.yaml"),
37 | )
38 | }
39 |
40 | func main() {
41 | _ = cmd.Main()
42 | }
43 |
--------------------------------------------------------------------------------
/example/local_server/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package main
19 |
20 | import (
21 | "os"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/cmd"
26 | "github.com/arana-db/arana/testdata"
27 | )
28 |
29 | func init() {
30 | os.Args = append(os.Args, "start", "-c", testdata.Path("../conf/bootstrap.local-etcd.yaml"))
31 | }
32 |
33 | func main() {
34 | _ = cmd.Main()
35 | }
36 |
--------------------------------------------------------------------------------
/example/service_discovery/nacos/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package main
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/registry"
22 | "github.com/arana-db/arana/pkg/registry/base"
23 | "github.com/arana-db/arana/pkg/util/log"
24 | )
25 |
26 | func main() {
27 | storeType := base.NACOS
28 | options := make(map[string]interface{})
29 | options["endpoints"] = "127.0.0.1:8848"
30 | options["scheme"] = "http"
31 | options["username"] = "nacos"
32 | options["password"] = "nacos"
33 |
34 | nacosDiscovery, err := registry.InitDiscovery(base.NACOS, options)
35 | if err != nil {
36 | log.Fatalf("Init %s discovery err:%v", storeType, err)
37 | return
38 | }
39 |
40 | nacosDiscovery.GetServices()
41 | }
42 |
--------------------------------------------------------------------------------
/integration_test/config/db/data.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: DataSet
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/db/expected.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: excepted
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/db_tbl/data.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: DataSet
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/db_tbl/expected.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: excepted
19 | metadata:
20 | tables:
21 | - name: "sequence"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "sequence"
29 | value:
30 | - ["1", "2"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/db_tbl_rw/data.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: DataSet
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/db_tbl_rw/expected.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: excepted
19 | metadata:
20 | tables:
21 | - name: "sequence"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "sequence"
29 | value:
30 | - ["1", "2"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/tbl/data.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: DataSet
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/config/tbl/expected.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | kind: excepted
19 | metadata:
20 | tables:
21 | - name: "order"
22 | columns:
23 | - name: "name"
24 | type: "string"
25 | - name: "value"
26 | type: "string"
27 | data:
28 | - name: "order"
29 | value:
30 | - ["test", "test1"]
31 |
--------------------------------------------------------------------------------
/integration_test/scripts/db/sequence.sql:
--------------------------------------------------------------------------------
1 | --
2 | -- Licensed to the Apache Software Foundation (ASF) under one or more
3 | -- contributor license agreements. See the NOTICE file distributed with
4 | -- this work for additional information regarding copyright ownership.
5 | -- The ASF licenses this file to You under the Apache License, Version 2.0
6 | -- (the "License"); you may not use this file except in compliance with
7 | -- the License. You may obtain a copy of the License at
8 | --
9 | -- http://www.apache.org/licenses/LICENSE-2.0
10 | --
11 | -- Unless required by applicable law or agreed to in writing, software
12 | -- distributed under the License is distributed on an "AS IS" BASIS,
13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | -- See the License for the specific language governing permissions and
15 | -- limitations under the License.
16 | --
17 |
18 | CREATE DATABASE IF NOT EXISTS employees_0000 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
19 |
20 | CREATE TABLE IF NOT EXISTS `employees_0000`.`sequence`
21 | (
22 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
23 | `name` VARCHAR(64) NOT NULL,
24 | `value` BIGINT NOT NULL,
25 | `step` INT NOT NULL DEFAULT 10000,
26 | `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27 | `modified_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 | PRIMARY KEY (`id`),
29 | UNIQUE KEY `uk_name` (`name`)
30 | ) ENGINE = InnoDB
31 | DEFAULT CHARSET = utf8mb4;
32 |
--------------------------------------------------------------------------------
/integration_test/scripts/db_tbl/sequence.sql:
--------------------------------------------------------------------------------
1 | --
2 | -- Licensed to the Apache Software Foundation (ASF) under one or more
3 | -- contributor license agreements. See the NOTICE file distributed with
4 | -- this work for additional information regarding copyright ownership.
5 | -- The ASF licenses this file to You under the Apache License, Version 2.0
6 | -- (the "License"); you may not use this file except in compliance with
7 | -- the License. You may obtain a copy of the License at
8 | --
9 | -- http://www.apache.org/licenses/LICENSE-2.0
10 | --
11 | -- Unless required by applicable law or agreed to in writing, software
12 | -- distributed under the License is distributed on an "AS IS" BASIS,
13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | -- See the License for the specific language governing permissions and
15 | -- limitations under the License.
16 | --
17 |
18 | CREATE DATABASE IF NOT EXISTS employees_0000 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
19 |
20 | CREATE TABLE IF NOT EXISTS `employees_0000`.`sequence`
21 | (
22 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
23 | `name` VARCHAR(64) NOT NULL,
24 | `value` BIGINT NOT NULL,
25 | `step` INT NOT NULL DEFAULT 10000,
26 | `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27 | `modified_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 | PRIMARY KEY (`id`),
29 | UNIQUE KEY `uk_name` (`name`)
30 | ) ENGINE = InnoDB
31 | DEFAULT CHARSET = utf8mb4;
32 |
--------------------------------------------------------------------------------
/integration_test/scripts/db_tbl_rw/sequence.sql:
--------------------------------------------------------------------------------
1 | --
2 | -- Licensed to the Apache Software Foundation (ASF) under one or more
3 | -- contributor license agreements. See the NOTICE file distributed with
4 | -- this work for additional information regarding copyright ownership.
5 | -- The ASF licenses this file to You under the Apache License, Version 2.0
6 | -- (the "License"); you may not use this file except in compliance with
7 | -- the License. You may obtain a copy of the License at
8 | --
9 | -- http://www.apache.org/licenses/LICENSE-2.0
10 | --
11 | -- Unless required by applicable law or agreed to in writing, software
12 | -- distributed under the License is distributed on an "AS IS" BASIS,
13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | -- See the License for the specific language governing permissions and
15 | -- limitations under the License.
16 | --
17 |
18 | CREATE DATABASE IF NOT EXISTS employees_0000 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
19 |
20 | CREATE TABLE IF NOT EXISTS `employees_0000`.`sequence`
21 | (
22 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
23 | `name` VARCHAR(64) NOT NULL,
24 | `value` BIGINT NOT NULL,
25 | `step` INT NOT NULL DEFAULT 10000,
26 | `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27 | `modified_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 | PRIMARY KEY (`id`),
29 | UNIQUE KEY `uk_name` (`name`)
30 | ) ENGINE = InnoDB
31 | DEFAULT CHARSET = utf8mb4;
32 |
--------------------------------------------------------------------------------
/integration_test/scripts/tbl/sequence.sql:
--------------------------------------------------------------------------------
1 | --
2 | -- Licensed to the Apache Software Foundation (ASF) under one or more
3 | -- contributor license agreements. See the NOTICE file distributed with
4 | -- this work for additional information regarding copyright ownership.
5 | -- The ASF licenses this file to You under the Apache License, Version 2.0
6 | -- (the "License"); you may not use this file except in compliance with
7 | -- the License. You may obtain a copy of the License at
8 | --
9 | -- http://www.apache.org/licenses/LICENSE-2.0
10 | --
11 | -- Unless required by applicable law or agreed to in writing, software
12 | -- distributed under the License is distributed on an "AS IS" BASIS,
13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | -- See the License for the specific language governing permissions and
15 | -- limitations under the License.
16 | --
17 |
18 | CREATE DATABASE IF NOT EXISTS employees_0000 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
19 |
20 | CREATE TABLE IF NOT EXISTS `employees_0000`.`sequence`
21 | (
22 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
23 | `name` VARCHAR(64) NOT NULL,
24 | `value` BIGINT NOT NULL,
25 | `step` INT NOT NULL DEFAULT 10000,
26 | `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27 | `modified_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 | PRIMARY KEY (`id`),
29 | UNIQUE KEY `uk_name` (`name`)
30 | ) ENGINE = InnoDB
31 | DEFAULT CHARSET = utf8mb4;
32 |
--------------------------------------------------------------------------------
/justfile:
--------------------------------------------------------------------------------
1 | set dotenv-load := true
2 |
3 | alias r := run
4 | alias c := cli
5 |
6 | default:
7 | @just --list
8 |
9 | run:
10 | @go run ./example/local_server
11 |
12 | cli:
13 | @mycli -h127.0.0.1 -P13306 -udksl employees -p123456
14 | cli-raw:
15 | @mycli -h127.0.0.1 -uroot employees -p123456
16 |
17 | fix:
18 | @imports-formatter .
19 | @license-eye header fix
20 |
21 |
22 | sysbench MODE="run":
23 | sysbench oltp_read_write --mysql-user=root --mysql-password=123456 --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-db=employees_0000 --histogram=on --report-interval=1 --time=300 --db-ps-mode=disable --threads=64 --tables=250 --table_size=25000 --report-interval=1 --percentile=95 --skip-trx=on --mysql-ignore-errors=1062 --forced-shutdown=1 {{MODE}}
24 |
25 |
26 | sysbench2 MODE="run":
27 | sysbench oltp_read_write --mysql-user=arana --mysql-password=123456 --mysql-host=127.0.0.1 --mysql-port=13306 --mysql-db=employees --histogram=on --report-interval=1 --time=300 --db-ps-mode=disable --threads=8 --tables=250 --table_size=25000 --report-interval=1 --percentile=95 --skip-trx=on --mysql-ignore-errors=1062 --forced-shutdown=1 {{MODE}}
28 |
--------------------------------------------------------------------------------
/pkg/admin/cors.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package admin
19 |
20 | import (
21 | "github.com/gin-gonic/gin"
22 | )
23 |
24 | func CORSMiddleware() gin.HandlerFunc {
25 | return func(c *gin.Context) {
26 | c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
27 | c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
28 | c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
29 | c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, PATCH, DELETE")
30 |
31 | if c.Request.Method == "OPTIONS" {
32 | c.AbortWithStatus(204)
33 | return
34 | }
35 |
36 | c.Next()
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/admin/router/services.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package router
19 |
20 | import (
21 | "net/http"
22 | )
23 |
24 | import (
25 | "github.com/gin-gonic/gin"
26 | )
27 |
28 | import (
29 | "github.com/arana-db/arana/pkg/admin"
30 | )
31 |
32 | func init() {
33 | admin.Register(func(router admin.Router, openRouter admin.Router) {
34 | router.GET("/services", GetServices)
35 | })
36 | }
37 |
38 | func GetServices(c *gin.Context) error {
39 | serviceDiscovery := admin.GetServiceDiscovery(c)
40 | data := serviceDiscovery.ListServices()
41 |
42 | c.JSON(http.StatusOK, data)
43 | return nil
44 | }
45 |
--------------------------------------------------------------------------------
/pkg/admin/service_discovery.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package admin
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/registry/base"
22 | )
23 |
24 | type ServiceDiscovery interface {
25 | ListServices() []*ServiceInstanceDTO
26 | }
27 |
28 | type myServiceDiscovery struct {
29 | serviceDiscovery base.Discovery
30 | }
31 |
32 | func (mysds *myServiceDiscovery) ListServices() []*ServiceInstanceDTO {
33 | var (
34 | services = mysds.serviceDiscovery.GetServices()
35 | srvDTOs = make([]*ServiceInstanceDTO, 0, len(services))
36 | )
37 | for _, srv := range services {
38 | srvDTOs = append(srvDTOs, &ServiceInstanceDTO{
39 | ID: srv.ID,
40 | Name: srv.Name,
41 | Version: srv.Version,
42 | Endpoint: srv.Endpoint,
43 | })
44 | }
45 | return srvDTOs
46 | }
47 |
--------------------------------------------------------------------------------
/pkg/boot/plugin.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package boot
19 |
20 | import (
21 | _ "github.com/arana-db/arana/pkg/config/etcd"
22 | _ "github.com/arana-db/arana/pkg/config/file"
23 | _ "github.com/arana-db/arana/pkg/config/nacos"
24 | _ "github.com/arana-db/arana/pkg/sequence"
25 | _ "github.com/arana-db/arana/pkg/sequence/group"
26 | _ "github.com/arana-db/arana/pkg/sequence/snowflake"
27 | _ "github.com/arana-db/arana/pkg/trace/jaeger"
28 | )
29 |
--------------------------------------------------------------------------------
/pkg/config/boot_misc_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package config
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestParseDatabaseAndTable(t *testing.T) {
29 | type tt struct {
30 | name string
31 | db string
32 | table string
33 | }
34 | for _, it := range []tt{
35 | {"employee.student", "employee", "student"},
36 | {"fake-db.fake_table", "fake-db", "fake_table"},
37 | } {
38 | t.Run(it.name, func(t *testing.T) {
39 | db, table, err := ParseDatabaseAndTable(it.name)
40 | assert.NoError(t, err)
41 | assert.Equal(t, it.db, db)
42 | assert.Equal(t, it.table, table)
43 | })
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/pkg/config/default.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | */
18 |
19 | package config
20 |
21 | import (
22 | "github.com/pkg/errors"
23 | )
24 |
25 | import (
26 | "github.com/arana-db/arana/pkg/util/log"
27 | )
28 |
29 | var ErrorNoStoreOperate = errors.New("no store operate")
30 |
31 | func GetStoreOperate() StoreOperator {
32 | return storeOperate
33 | }
34 |
35 | func Init(options Options, version string) error {
36 | initPath(options.RootPath, version)
37 |
38 | var err error
39 | once.Do(func() {
40 | err = InitStoreOperate(options)
41 | })
42 | return err
43 | }
44 |
45 | func InitStoreOperate(options Options) error {
46 | op, ok := slots[options.StoreName]
47 | if !ok {
48 | return ErrorNoStoreOperate
49 | }
50 | if err := op.Init(options.Options); err != nil {
51 | return err
52 | }
53 | log.Infof("[StoreOperate] use plugin : %s", options.StoreName)
54 | storeOperate = op
55 | return nil
56 | }
57 |
--------------------------------------------------------------------------------
/pkg/constants/const.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package constants
19 |
20 | const (
21 | ConfigPathKey = "config"
22 | ImportConfigPathKey = "source"
23 | )
24 |
25 | const (
26 | SQLShowVariables = "SHOW VARIABLES WHERE Variable_name = '%s'"
27 |
28 | VariableNameMaxAllowedPacket = "max_allowed_packet"
29 |
30 | SlowThreshold = "slow_threshold"
31 | )
32 |
--------------------------------------------------------------------------------
/pkg/constants/env.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package constants
19 |
20 | import (
21 | "os"
22 | "path/filepath"
23 | )
24 |
25 | // Environments
26 | const (
27 | EnvBootstrapPath = "ARANA_BOOTSTRAP_PATH" // bootstrap file path, eg: /etc/arana/bootstrap.yaml
28 | EnvConfigPath = "ARANA_CONFIG_PATH" // config file path, eg: /etc/arana/config.yaml
29 | EnvDevelopEnvironment = "ARANA_DEV" // config dev environment
30 | )
31 |
32 | // GetConfigSearchPathList returns the default search path list of configuration.
33 | func GetConfigSearchPathList() []string {
34 | var dirs []string
35 | dirs = append(dirs, ".", "./conf")
36 | if home, err := os.UserHomeDir(); err == nil {
37 | dirs = append(dirs, filepath.Join(home, ".arana"))
38 | }
39 | dirs = append(dirs, "/etc/arana")
40 | return dirs
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/constants/env_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package constants
19 |
20 | import (
21 | "strings"
22 | "testing"
23 | )
24 |
25 | import (
26 | "github.com/stretchr/testify/assert"
27 | )
28 |
29 | func TestGetConfigSearchPathList(t *testing.T) {
30 | tests := []struct {
31 | name string
32 | want []string
33 | }{
34 | {"Test", []string{".", "./conf", "/Users/dongzonglei/.arana", "/etc/arana"}},
35 | }
36 | for _, tt := range tests {
37 | t.Run(tt.name, func(t *testing.T) {
38 | result := GetConfigSearchPathList()
39 | assert.Equal(t, 4, len(result))
40 | assert.Equal(t, ".", result[0])
41 | assert.Equal(t, "./conf", result[1])
42 | assert.True(t, strings.HasSuffix(result[2], ".arana"))
43 | assert.Equal(t, "/etc/arana", result[3])
44 | })
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/pkg/constants/mysql/constants_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package mysql
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestIsNum(t *testing.T) {
29 | type args struct {
30 | typ uint8
31 | }
32 | tests := []struct {
33 | name string
34 | args args
35 | want bool
36 | }{
37 | {"Test_1", args{9}, true},
38 | {"Test_2", args{8}, true},
39 | {"Test_3", args{7}, false},
40 | {"Test_4", args{13}, true},
41 | {"Test_5", args{246}, true},
42 | }
43 | for _, tt := range tests {
44 | t.Run(tt.name, func(t *testing.T) {
45 | assert.Equalf(t, tt.want, IsNum(tt.args.typ), "IsNum(%v)", tt.args.typ)
46 | })
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/dataset/dataset.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dataset
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/proto"
22 | )
23 |
24 | // PeekableDataset represents a peekable dataset.
25 | type PeekableDataset interface {
26 | proto.Dataset
27 | // Peek peeks the next row, but will not consume it.
28 | Peek() (proto.Row, error)
29 | }
30 |
31 | type RandomAccessDataset interface {
32 | PeekableDataset
33 | // Len returns the length of sub-datasets.
34 | Len() int
35 | // PeekN peeks the next row with specified index.
36 | PeekN(index int) (proto.Row, error)
37 | // SetNextN force sets the next index of row.
38 | SetNextN(index int) error
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/dataset/virtual.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dataset
19 |
20 | import (
21 | "io"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | var _ proto.Dataset = (*VirtualDataset)(nil)
29 |
30 | type VirtualDataset struct {
31 | Columns []proto.Field
32 | Rows []proto.Row
33 | }
34 |
35 | func (cu *VirtualDataset) Close() error {
36 | return nil
37 | }
38 |
39 | func (cu *VirtualDataset) Fields() ([]proto.Field, error) {
40 | return cu.Columns, nil
41 | }
42 |
43 | func (cu *VirtualDataset) Next() (proto.Row, error) {
44 | if len(cu.Rows) < 1 {
45 | return nil, io.EOF
46 | }
47 |
48 | next := cu.Rows[0]
49 |
50 | cu.Rows[0] = nil
51 | cu.Rows = cu.Rows[1:]
52 |
53 | return next, nil
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package merge
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/proto"
22 | )
23 |
24 | type Aggregator interface {
25 | Aggregate(values []proto.Value)
26 | GetResult() (proto.Value, bool)
27 | }
28 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator/add_aggregator.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package aggregator
19 |
20 | import (
21 | "github.com/shopspring/decimal"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | type AddAggregator struct {
29 | count decimal.NullDecimal
30 | }
31 |
32 | func (s *AddAggregator) Aggregate(values []proto.Value) {
33 | if len(values) == 0 {
34 | return
35 | }
36 |
37 | if values[0] == nil {
38 | return
39 | }
40 |
41 | val1, err := values[0].Decimal()
42 | if err != nil {
43 | panic(err.Error())
44 | }
45 |
46 | if !s.count.Valid {
47 | s.count.Valid = true
48 | s.count.Decimal = val1
49 | return
50 | }
51 |
52 | s.count.Decimal = s.count.Decimal.Add(val1)
53 | }
54 |
55 | func (s *AddAggregator) GetResult() (proto.Value, bool) {
56 | if !s.count.Valid {
57 | return nil, false
58 | }
59 |
60 | return proto.NewValueDecimal(s.count.Decimal), true
61 | }
62 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator/init.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package aggregator
19 |
20 | import (
21 | "fmt"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/merge"
26 | )
27 |
28 | var aggregatorMap = make(map[string]func() merge.Aggregator)
29 |
30 | func init() {
31 | aggregatorMap["MAX"] = func() merge.Aggregator { return &MaxAggregator{} }
32 | aggregatorMap["MIN"] = func() merge.Aggregator { return &MinAggregator{} }
33 | aggregatorMap["COUNT"] = func() merge.Aggregator { return &AddAggregator{} }
34 | aggregatorMap["SUM"] = func() merge.Aggregator { return &AddAggregator{} }
35 | }
36 |
37 | func GetAggFromName(name string) func() merge.Aggregator {
38 | if agg, ok := aggregatorMap[name]; ok {
39 | return agg
40 | }
41 | panic(fmt.Errorf("aggregator %s not support yet", name))
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator/load_agg.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package aggregator
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/merge"
22 | "github.com/arana-db/arana/pkg/runtime/ast"
23 | )
24 |
25 | func LoadAggs(fields []ast.SelectElement) map[int]func() merge.Aggregator {
26 | aggMap := make(map[int]func() merge.Aggregator)
27 | enter := func(i int, n *ast.AggrFunction) {
28 | if n == nil {
29 | return
30 | }
31 | aggMap[i] = GetAggFromName(n.Name())
32 | }
33 |
34 | for i, field := range fields {
35 | if field == nil {
36 | continue
37 | }
38 | if f, ok := field.(*ast.SelectElementFunction); ok {
39 | enter(i, f.Function().(*ast.AggrFunction))
40 | }
41 | }
42 |
43 | return aggMap
44 | }
45 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator/max_aggregator.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package aggregator
19 |
20 | import (
21 | "github.com/shopspring/decimal"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | type MaxAggregator struct {
29 | // max decimal.Decimal
30 | max decimal.NullDecimal
31 | }
32 |
33 | func (s *MaxAggregator) Aggregate(values []proto.Value) {
34 | if len(values) == 0 {
35 | return
36 | }
37 |
38 | if values[0] == nil {
39 | return
40 | }
41 |
42 | val, err := values[0].Decimal()
43 | if err != nil {
44 | panic(err.Error())
45 | }
46 |
47 | if !s.max.Valid {
48 | s.max.Valid = true
49 | s.max.Decimal = val
50 | return
51 | }
52 |
53 | if s.max.Decimal.LessThan(val) {
54 | s.max.Decimal = val
55 | }
56 | }
57 |
58 | func (s *MaxAggregator) GetResult() (proto.Value, bool) {
59 | if s.max.Valid {
60 | return proto.NewValueDecimal(s.max.Decimal), true
61 | }
62 | return nil, false
63 | }
64 |
--------------------------------------------------------------------------------
/pkg/merge/aggregator/min_aggregator.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package aggregator
19 |
20 | import (
21 | "github.com/shopspring/decimal"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | type MinAggregator struct {
29 | min decimal.NullDecimal
30 | }
31 |
32 | func (s *MinAggregator) Aggregate(values []proto.Value) {
33 | if len(values) == 0 {
34 | return
35 | }
36 |
37 | if values[0] == nil {
38 | return
39 | }
40 |
41 | val, err := values[0].Decimal()
42 | if err != nil {
43 | panic(err)
44 | }
45 |
46 | if !s.min.Valid {
47 | s.min.Valid = true
48 | s.min.Decimal = val
49 | return
50 | }
51 |
52 | if s.min.Decimal.GreaterThan(val) {
53 | s.min.Decimal = val
54 | }
55 | }
56 |
57 | func (s *MinAggregator) GetResult() (proto.Value, bool) {
58 | if s.min.Valid {
59 | return proto.NewValueDecimal(s.min.Decimal), true
60 | }
61 | return nil, false
62 | }
63 |
--------------------------------------------------------------------------------
/pkg/proto/hint/hint_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package hint
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestParse(t *testing.T) {
29 | type tt struct {
30 | input string
31 | output string
32 | pass bool
33 | }
34 |
35 | for _, next := range []tt{
36 | {"route( foo , bar , qux )", "ROUTE(foo,bar,qux)", true},
37 | {"master", "MASTER()", true},
38 | {"slave", "SLAVE()", true},
39 | {"not_exist_hint(1,2,3)", "", false},
40 | {"route(,,,)", "ROUTE()", true},
41 | {"fullscan()", "FULLSCAN()", true},
42 | {"route(foo=111,bar=222,qux=333,)", "ROUTE(foo=111,bar=222,qux=333)", true},
43 | } {
44 | t.Run(next.input, func(t *testing.T) {
45 | res, err := Parse(next.input)
46 | if next.pass {
47 | assert.NoError(t, err)
48 | assert.Equal(t, next.output, res.String())
49 | } else {
50 | assert.Error(t, err)
51 | }
52 | })
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/proto/rule/sequence.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package rule
19 |
20 | type AutoIncrement struct {
21 | Type string
22 | Option map[string]string
23 | }
24 |
--------------------------------------------------------------------------------
/pkg/proto/rule/shards_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package rule
19 |
20 | import (
21 | "fmt"
22 | "strings"
23 | "testing"
24 | )
25 |
26 | import (
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | func TestShards(t *testing.T) {
31 | s := NewShards()
32 | s.Add(0, 0, 1, 2, 3)
33 | s.Add(1, 4, 5, 6, 7)
34 | s.Add(2, 8, 9, 10, 11)
35 |
36 | assert.Equal(t, 12, s.Len())
37 | t.Log("shards:", s)
38 |
39 | var sb strings.Builder
40 | s.Each(func(db, tb uint32) bool {
41 | sb.WriteString(fmt.Sprintf("%d.%d;", db, tb))
42 | return true
43 | })
44 |
45 | assert.Equal(t, "0.0;0.1;0.2;0.3;1.4;1.5;1.6;1.7;2.8;2.9;2.10;2.11;", sb.String())
46 |
47 | db, tb, ok := s.Min()
48 | assert.True(t, ok)
49 | assert.Equal(t, uint32(0), db)
50 | assert.Equal(t, uint32(0), tb)
51 |
52 | db, tb, ok = s.Max()
53 | assert.True(t, ok)
54 | assert.Equal(t, uint32(2), db)
55 | assert.Equal(t, uint32(11), tb)
56 | }
57 |
--------------------------------------------------------------------------------
/pkg/proto/stmt.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package proto
19 |
20 | import (
21 | "github.com/arana-db/parser/ast"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto/hint"
26 | )
27 |
28 | // Stmt is a buffer used for store prepare statement metadata.
29 | type Stmt struct {
30 | StatementID uint32
31 | PrepareStmt string
32 | ParamsCount uint16
33 | ParamsType []int32
34 | ColumnNames []string
35 | BindVars map[string]Value
36 | Hints []*hint.Hint
37 | StmtNode ast.StmtNode
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/reduce/max.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package reduce
19 |
20 | import (
21 | "time"
22 | )
23 |
24 | import (
25 | "github.com/shopspring/decimal"
26 | )
27 |
28 | var _ Reducer = (*maxReducer)(nil)
29 |
30 | type maxReducer struct{}
31 |
32 | func (m maxReducer) Int64(prev, next int64) (int64, error) {
33 | if next > prev {
34 | return next, nil
35 | }
36 | return prev, nil
37 | }
38 |
39 | func (m maxReducer) Float64(prev, next float64) (float64, error) {
40 | if next > prev {
41 | return next, nil
42 | }
43 | return prev, nil
44 | }
45 |
46 | func (m maxReducer) Decimal(prev, next decimal.Decimal) (decimal.Decimal, error) {
47 | if next.GreaterThan(prev) {
48 | return next, nil
49 | }
50 | return prev, nil
51 | }
52 |
53 | func (m maxReducer) Time(prev, next time.Time) (time.Time, error) {
54 | if next.After(prev) {
55 | return next, nil
56 | }
57 | return prev, nil
58 | }
59 |
--------------------------------------------------------------------------------
/pkg/reduce/min.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package reduce
19 |
20 | import (
21 | "time"
22 | )
23 |
24 | import (
25 | "github.com/shopspring/decimal"
26 | )
27 |
28 | type minReducer struct{}
29 |
30 | func (minReducer) Int64(prev, next int64) (int64, error) {
31 | if next < prev {
32 | return next, nil
33 | }
34 | return prev, nil
35 | }
36 |
37 | func (minReducer) Float64(prev, next float64) (float64, error) {
38 | if next < prev {
39 | return next, nil
40 | }
41 | return prev, nil
42 | }
43 |
44 | func (minReducer) Decimal(prev, next decimal.Decimal) (decimal.Decimal, error) {
45 | if next.LessThan(prev) {
46 | return next, nil
47 | }
48 | return prev, nil
49 | }
50 |
51 | func (minReducer) Time(prev, next time.Time) (time.Time, error) {
52 | if next.Before(prev) {
53 | return next, nil
54 | }
55 | return prev, nil
56 | }
57 |
--------------------------------------------------------------------------------
/pkg/reduce/reduce.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package reduce
19 |
20 | import (
21 | "time"
22 | )
23 |
24 | import (
25 | "github.com/shopspring/decimal"
26 | )
27 |
28 | type Reducer interface {
29 | Int64(prev, next int64) (int64, error)
30 | Float64(prev, next float64) (float64, error)
31 | Decimal(prev, next decimal.Decimal) (decimal.Decimal, error)
32 | Time(prev, next time.Time) (time.Time, error)
33 | }
34 |
35 | func Max() Reducer {
36 | return maxReducer{}
37 | }
38 |
39 | func Min() Reducer {
40 | return minReducer{}
41 | }
42 |
43 | func Sum() Reducer {
44 | return sumReducer{}
45 | }
46 |
--------------------------------------------------------------------------------
/pkg/reduce/sum.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package reduce
19 |
20 | import (
21 | "time"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 |
27 | "github.com/shopspring/decimal"
28 | )
29 |
30 | var _ Reducer = (*sumReducer)(nil)
31 |
32 | type sumReducer struct{}
33 |
34 | func (s sumReducer) Int64(prev, next int64) (int64, error) {
35 | return prev + next, nil
36 | }
37 |
38 | func (s sumReducer) Float64(prev, next float64) (float64, error) {
39 | return prev + next, nil
40 | }
41 |
42 | func (s sumReducer) Decimal(prev, next decimal.Decimal) (decimal.Decimal, error) {
43 | return prev.Add(next), nil
44 | }
45 |
46 | func (s sumReducer) Time(_, _ time.Time) (ret time.Time, err error) {
47 | err = errors.New("time.Time is not supported for SUM")
48 | return
49 | }
50 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/analyze_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | var _ Statement = (*AnalyzeTableStatement)(nil)
29 |
30 | type AnalyzeTableStatement struct {
31 | Tables []*TableName
32 | }
33 |
34 | func NewAnalyzeTableStatement() *AnalyzeTableStatement {
35 | return &AnalyzeTableStatement{}
36 | }
37 |
38 | func (a *AnalyzeTableStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
39 | sb.WriteString("ANALYZE TABLE ")
40 | for index, table := range a.Tables {
41 | if index != 0 {
42 | sb.WriteString(", ")
43 | }
44 | if err := table.Restore(flag, sb, args); err != nil {
45 | return errors.Wrapf(err, "an error occurred while restore AnalyzeTableStatement.Tables[%d]", index)
46 | }
47 | }
48 | return nil
49 | }
50 |
51 | func (a *AnalyzeTableStatement) Mode() SQLType {
52 | return SQLTypeAnalyzeTable
53 | }
54 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/check_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | var _ Statement = (*CheckTableStmt)(nil)
29 |
30 | type CheckTableStmt struct {
31 | Tables []*TableName
32 | }
33 |
34 | func NewCheckTableStmt() *CheckTableStmt {
35 | return &CheckTableStmt{}
36 | }
37 |
38 | func (c *CheckTableStmt) CntParams() int {
39 | return 1
40 | }
41 |
42 | func (c *CheckTableStmt) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
43 | sb.WriteString("CHECK TABLE ")
44 | for index, table := range c.Tables {
45 | if index != 0 {
46 | sb.WriteString(", ")
47 | }
48 | if err := table.Restore(flag, sb, args); err != nil {
49 | return errors.Wrapf(err, "an error occurred while restore AnalyzeTableStatement.Tables[%d]", index)
50 | }
51 | }
52 | return nil
53 | }
54 |
55 | func (c *CheckTableStmt) Mode() SQLType {
56 | return SQLTypeCheckTable
57 | }
58 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/drop_index.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | var (
25 | _ Statement = (*DropIndexStatement)(nil)
26 | _ Restorer = (*DropIndexStatement)(nil)
27 | )
28 |
29 | type DropIndexStatement struct {
30 | IfExists bool
31 | IndexName string
32 | Table TableName
33 | }
34 |
35 | func (d *DropIndexStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
36 | sb.WriteString("DROP INDEX ")
37 | if d.IfExists {
38 | sb.WriteString("IF EXISTS")
39 | }
40 | sb.WriteString(d.IndexName)
41 | if len(d.Table) == 0 {
42 | return nil
43 | }
44 | sb.WriteString(" ON ")
45 | return d.Table.Restore(flag, sb, args)
46 | }
47 |
48 | func (d *DropIndexStatement) Mode() SQLType {
49 | return SQLTypeDropIndex
50 | }
51 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/drop_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | type DropTableStatement struct {
29 | Tables []*TableName
30 | }
31 |
32 | func NewDropTableStatement() *DropTableStatement {
33 | return &DropTableStatement{}
34 | }
35 |
36 | func (d DropTableStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
37 | sb.WriteString("DROP TABLE ")
38 | for index, table := range d.Tables {
39 | if index != 0 {
40 | sb.WriteString(", ")
41 | }
42 | if err := table.Restore(flag, sb, args); err != nil {
43 | return errors.Errorf("An error occurred while restore DropTableStatement.Tables[%d],error:%s", index, err)
44 | }
45 | }
46 | return nil
47 | }
48 |
49 | func (d DropTableStatement) Mode() SQLType {
50 | return SQLTypeDropTable
51 | }
52 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/kill.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strconv"
22 | "strings"
23 | )
24 |
25 | type KillStmt struct {
26 | Query bool
27 | ConnectionID uint64
28 | }
29 |
30 | func (k *KillStmt) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
31 | sb.WriteString("KILL ")
32 | if k.Query {
33 | sb.WriteString("QUERY ")
34 | }
35 | sb.WriteString(strconv.FormatUint(k.ConnectionID, 10))
36 | return nil
37 | }
38 |
39 | func (k *KillStmt) Mode() SQLType {
40 | return SQLTypeKill
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/misc.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/runtime/misc"
26 | )
27 |
28 | func WriteID(sb *strings.Builder, field string) {
29 | sb.WriteByte('`')
30 | for _, r := range field {
31 | switch r {
32 | case '`':
33 | sb.WriteString("``")
34 | default:
35 | sb.WriteRune(r)
36 | }
37 | }
38 | sb.WriteByte('`')
39 | }
40 |
41 | func WriteString(sb *strings.Builder, str string) {
42 | sb.WriteByte('\'')
43 | misc.WriteEscape(sb, str, misc.EscapeSingleQuote)
44 | sb.WriteByte('\'')
45 | }
46 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/misc_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | "testing"
23 | )
24 |
25 | import (
26 | "github.com/stretchr/testify/assert"
27 | )
28 |
29 | func TestWriteID(t *testing.T) {
30 | type tt struct {
31 | input string
32 | expect string
33 | }
34 |
35 | for _, it := range []tt{
36 | {"simple", "`simple`"},
37 | {"你好,世界", "`你好,世界`"},
38 | {"Hello`World", "`Hello``World`"},
39 | {"it's ok", "`it's ok`"},
40 | {`x\y\z`, "`x\\y\\z`"},
41 | } {
42 | t.Run(it.input, func(t *testing.T) {
43 | var sb strings.Builder
44 | WriteID(&sb, it.input)
45 | assert.Equal(t, it.expect, sb.String())
46 | })
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/optimize_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | var _ Statement = (*OptimizeTableStatement)(nil)
29 |
30 | type OptimizeTableStatement struct {
31 | Tables []*TableName
32 | }
33 |
34 | func NewOptimizeTableStatement() *OptimizeTableStatement {
35 | return &OptimizeTableStatement{}
36 | }
37 |
38 | func (a *OptimizeTableStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
39 | sb.WriteString("OPTIMIZE TABLE ")
40 | for index, table := range a.Tables {
41 | if index != 0 {
42 | sb.WriteString(", ")
43 | }
44 | if err := table.Restore(flag, sb, args); err != nil {
45 | return errors.Wrapf(err, "an error occurred while restore OptimizeTableStatement.Tables[%d]", index)
46 | }
47 | }
48 | return nil
49 | }
50 |
51 | func (a *OptimizeTableStatement) Mode() SQLType {
52 | return SQLTypeOptimizeTable
53 | }
54 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/restore.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | const (
25 | RestoreDefault RestoreFlag = 0
26 |
27 | RestoreLowerKeyword RestoreFlag = 1 << iota // force use lower-case keyword
28 | RestoreWithoutAlias
29 | RestoreCompat80
30 | )
31 |
32 | type RestoreFlag uint32
33 |
34 | func (rf RestoreFlag) Has(flag RestoreFlag) bool {
35 | return rf&flag != 0
36 | }
37 |
38 | type Restorer interface {
39 | Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error
40 | }
41 |
--------------------------------------------------------------------------------
/pkg/runtime/ast/trigger.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ast
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | var _ Statement = (*DropTriggerStatement)(nil)
25 |
26 | type DropTriggerStatement struct {
27 | IfExists bool
28 | Table TableName
29 | }
30 |
31 | func (d DropTriggerStatement) Mode() SQLType {
32 | return SQLTypeDropTrigger
33 | }
34 |
35 | func (d DropTriggerStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
36 | sb.WriteString("DROP TRIGGER ")
37 | if d.IfExists {
38 | sb.WriteString("IF EXISTS ")
39 | }
40 | return d.Table.Restore(flag, sb, args)
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/runtime/calc/logic/string.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package logic
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | type String string
25 |
26 | func (s String) Compare(item Item) int {
27 | switch that := item.(type) {
28 | case String:
29 | return strings.Compare(string(s), string(that))
30 | default:
31 | return -1
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/pkg/runtime/conn_pool_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package runtime
19 |
20 | import (
21 | "context"
22 | "fmt"
23 | "testing"
24 | )
25 |
26 | import (
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/mysql"
32 | )
33 |
34 | func TestBackendResourcePool_Get(t *testing.T) {
35 | type args struct {
36 | ctx context.Context
37 | }
38 | tests := []struct {
39 | name string
40 | bcp BackendResourcePool
41 | args args
42 | want *mysql.BackendConnection
43 | wantErr assert.ErrorAssertionFunc
44 | }{
45 | // TODO: Add test cases.
46 | }
47 | for _, tt := range tests {
48 | t.Run(tt.name, func(t *testing.T) {
49 | got, err := tt.bcp.Get(tt.args.ctx)
50 | if !tt.wantErr(t, err, fmt.Sprintf("Get(%v)", tt.args.ctx)) {
51 | return
52 | }
53 | assert.Equalf(t, tt.want, got, "Get(%v)", tt.args.ctx)
54 | })
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/pkg/runtime/function/doc.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | // Package function provides a set of mysql functions implements by Golang.
19 | // Please see: https://dev.mysql.com/doc/refman/8.0/en/built-in-function-reference.html
20 | package function
21 |
--------------------------------------------------------------------------------
/pkg/runtime/function/exp.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "math"
23 | )
24 |
25 | import (
26 | "github.com/pkg/errors"
27 | )
28 |
29 | import (
30 | "github.com/arana-db/arana/pkg/proto"
31 | )
32 |
33 | // FuncExp is https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_exp
34 | const FuncExp = "EXP"
35 |
36 | var _ proto.Func = (*expFunc)(nil)
37 |
38 | func init() {
39 | proto.RegisterFunc(FuncExp, expFunc{})
40 | }
41 |
42 | type expFunc struct{}
43 |
44 | func (a expFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
45 | val, err := inputs[0].Value(ctx)
46 | if err != nil {
47 | return nil, errors.Wrapf(err, "cannot eval '%s'", FuncExp)
48 | }
49 |
50 | if val == nil {
51 | return nil, nil
52 | }
53 |
54 | f, _ := val.Float64()
55 | return proto.NewValueFloat64(math.Exp(f)), nil
56 | }
57 |
58 | func (a expFunc) NumInput() int {
59 | return 1
60 | }
61 |
--------------------------------------------------------------------------------
/pkg/runtime/function/function_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "github.com/shopspring/decimal"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | func mustDecimal(s string) proto.Value {
29 | d, _ := decimal.NewFromString(s)
30 | return proto.NewValueDecimal(d)
31 | }
32 |
--------------------------------------------------------------------------------
/pkg/runtime/function/length.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | import (
29 | "github.com/arana-db/arana/pkg/proto"
30 | )
31 |
32 | const FuncLength = "LENGTH"
33 |
34 | var _ proto.Func = (*lengthFunc)(nil)
35 |
36 | func init() {
37 | proto.RegisterFunc(FuncLength, lengthFunc{})
38 | }
39 |
40 | type lengthFunc struct{}
41 |
42 | func (c lengthFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
43 | val, err := inputs[0].Value(ctx)
44 | if err != nil {
45 | return nil, errors.Wrapf(err, "cannot eval %s", FuncLength)
46 | }
47 |
48 | if val == nil {
49 | return nil, nil
50 | }
51 |
52 | return proto.NewValueInt64(int64(len(val.String()))), nil
53 | }
54 |
55 | func (c lengthFunc) NumInput() int {
56 | return 1
57 | }
58 |
--------------------------------------------------------------------------------
/pkg/runtime/function/lower.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "strings"
23 | )
24 |
25 | import (
26 | "github.com/pkg/errors"
27 | )
28 |
29 | import (
30 | "github.com/arana-db/arana/pkg/proto"
31 | )
32 |
33 | // FuncLower is https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_lower
34 | const FuncLower = "LOWER"
35 |
36 | var _ proto.Func = (*lowerFunc)(nil)
37 |
38 | func init() {
39 | proto.RegisterFunc(FuncLower, lowerFunc{})
40 | }
41 |
42 | type lowerFunc struct{}
43 |
44 | func (c lowerFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
45 | val, err := inputs[0].Value(ctx)
46 | if err != nil {
47 | return nil, errors.WithStack(err)
48 | }
49 |
50 | if val == nil {
51 | return nil, nil
52 | }
53 |
54 | return proto.NewValueString(strings.ToLower(val.String())), nil
55 | }
56 |
57 | func (c lowerFunc) NumInput() int {
58 | return 1
59 | }
60 |
--------------------------------------------------------------------------------
/pkg/runtime/function/ltrim.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "fmt"
23 | "strings"
24 | )
25 |
26 | import (
27 | "github.com/pkg/errors"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/proto"
32 | )
33 |
34 | // FuncLtrim is https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_ltrim
35 | const FuncLtrim = "LTRIM"
36 |
37 | var _ proto.Func = (*ltrimFunc)(nil)
38 |
39 | func init() {
40 | proto.RegisterFunc(FuncLtrim, ltrimFunc{})
41 | }
42 |
43 | type ltrimFunc struct{}
44 |
45 | func (a ltrimFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
46 | val, err := inputs[0].Value(ctx)
47 | if err != nil {
48 | return nil, errors.WithStack(err)
49 | }
50 | noleftSpaceString := strings.TrimLeft(fmt.Sprint(val), " ")
51 |
52 | return proto.NewValueString(noleftSpaceString), nil
53 | }
54 |
55 | func (a ltrimFunc) NumInput() int {
56 | return 1
57 | }
58 |
--------------------------------------------------------------------------------
/pkg/runtime/function/md5.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "crypto/md5"
23 | "fmt"
24 | )
25 |
26 | import (
27 | "github.com/pkg/errors"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/proto"
32 | )
33 |
34 | const FuncMd5 = "MD5"
35 |
36 | var _ proto.Func = (*md5Func)(nil)
37 |
38 | func init() {
39 | proto.RegisterFunc(FuncMd5, md5Func{})
40 | }
41 |
42 | type md5Func struct{}
43 |
44 | func (c md5Func) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
45 | val, err := inputs[0].Value(ctx)
46 | if err != nil {
47 | return nil, errors.Wrapf(err, "cannot eval %s", FuncMd5)
48 | }
49 |
50 | if val == nil {
51 | return nil, nil
52 | }
53 |
54 | h := fmt.Sprintf("%x", md5.Sum([]byte(val.String())))
55 | return proto.NewValueString(h), nil
56 | }
57 |
58 | func (c md5Func) NumInput() int {
59 | return 1
60 | }
61 |
--------------------------------------------------------------------------------
/pkg/runtime/function/pi.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | // FuncPi is https://dev.mysql.com/doc/refman/5.6/en/mathematical-functions.html#function_pi
29 | const FuncPi = "PI"
30 |
31 | const _piDefault = 3.141593
32 |
33 | var _ proto.Func = (*piFunc)(nil)
34 |
35 | func init() {
36 | proto.RegisterFunc(FuncPi, piFunc{})
37 | }
38 |
39 | type piFunc struct{}
40 |
41 | func (p piFunc) Apply(_ context.Context, _ ...proto.Valuer) (proto.Value, error) {
42 | return proto.NewValueFloat64(_piDefault), nil
43 | }
44 |
45 | func (p piFunc) NumInput() int {
46 | return 0
47 | }
48 |
--------------------------------------------------------------------------------
/pkg/runtime/function/pi_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "fmt"
23 | "testing"
24 | )
25 |
26 | import (
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/proto"
32 | )
33 |
34 | func TestPi(t *testing.T) {
35 | fn := proto.MustGetFunc(FuncPi)
36 | assert.Equal(t, 0, fn.NumInput())
37 |
38 | type tt struct {
39 | out string
40 | }
41 |
42 | for _, it := range []tt{
43 | {"3.141593"},
44 | } {
45 | t.Run(it.out, func(t *testing.T) {
46 | out, err := fn.Apply(context.Background())
47 | assert.NoError(t, err)
48 | assert.Equal(t, it.out, fmt.Sprint(out))
49 | })
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/pkg/runtime/function/reverse_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "testing"
23 | )
24 |
25 | import (
26 | "github.com/stretchr/testify/assert"
27 | )
28 |
29 | import (
30 | "github.com/arana-db/arana/pkg/proto"
31 | )
32 |
33 | func TestReverse(t *testing.T) {
34 | fn := proto.MustGetFunc(FuncReverse)
35 | assert.Equal(t, 1, fn.NumInput())
36 |
37 | type tt struct {
38 | in proto.Value
39 | out string
40 | }
41 |
42 | for _, it := range []tt{
43 | {proto.NewValueInt64(1111), "1111"},
44 | {proto.MustNewValueDecimalString("12.23"), "32.21"},
45 | {proto.NewValueString("arana"), "anara"},
46 | {proto.NewValueString("db-mesh"), "hsem-bd"},
47 | {proto.NewValueFloat64(20.22), "22.02"},
48 | } {
49 | t.Run(it.out, func(t *testing.T) {
50 | out, err := fn.Apply(context.Background(), proto.ToValuer(it.in))
51 | assert.NoError(t, err)
52 | assert.Equal(t, it.out, out.String())
53 | })
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/pkg/runtime/function/rtrim.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "fmt"
23 | "strings"
24 | )
25 |
26 | import (
27 | "github.com/pkg/errors"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/proto"
32 | )
33 |
34 | // FuncRtrim is https://dev.mysql.com/doc/refman/5.6/en/string-functions.html#function_rtrim
35 | const FuncRtrim = "RTRIM"
36 |
37 | var _ proto.Func = (*rtrimFunc)(nil)
38 |
39 | func init() {
40 | proto.RegisterFunc(FuncRtrim, rtrimFunc{})
41 | }
42 |
43 | type rtrimFunc struct{}
44 |
45 | func (a rtrimFunc) Apply(ctx context.Context, inputs ...proto.Valuer) (proto.Value, error) {
46 | val, err := inputs[0].Value(ctx)
47 | if err != nil {
48 | return nil, errors.WithStack(err)
49 | }
50 | norightSpaceString := strings.TrimRight(fmt.Sprint(val), " ")
51 |
52 | return proto.NewValueString(norightSpaceString), nil
53 | }
54 |
55 | func (a rtrimFunc) NumInput() int {
56 | return 1
57 | }
58 |
--------------------------------------------------------------------------------
/pkg/runtime/function/space_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package function
19 |
20 | import (
21 | "context"
22 | "fmt"
23 | "testing"
24 | )
25 |
26 | import (
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/proto"
32 | )
33 |
34 | func TestSpace(t *testing.T) {
35 | fn := proto.MustGetFunc(FuncSpace)
36 | assert.Equal(t, 1, fn.NumInput())
37 | type tt struct {
38 | inFirst proto.Value
39 | want string
40 | }
41 | for _, v := range []tt{
42 | {proto.NewValueInt64(9), " "},
43 | {proto.NewValueInt64(5), " "},
44 | {proto.NewValueInt64(-9), ""},
45 | {proto.NewValueFloat64(9.2), "NaN"},
46 | {proto.NewValueString("10"), "NaN"},
47 | } {
48 | t.Run(fmt.Sprint(v.inFirst), func(t *testing.T) {
49 | out, err := fn.Apply(context.Background(), proto.ToValuer(v.inFirst))
50 | assert.NoError(t, err)
51 | assert.Equal(t, v.want, fmt.Sprint(out))
52 | })
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/runtime/gtid/gtid.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package gtid
19 |
20 | import (
21 | "strconv"
22 | "sync"
23 | )
24 |
25 | import (
26 | "github.com/bwmarrin/snowflake"
27 | )
28 |
29 | import (
30 | "github.com/arana-db/arana/pkg/util/identity"
31 | "github.com/arana-db/arana/pkg/util/rand2"
32 | )
33 |
34 | var (
35 | nodeId string
36 | once sync.Once
37 | seqBuilder *snowflake.Node
38 | )
39 |
40 | // ID Gtid
41 | type ID struct {
42 | NodeID string
43 | Seq int64
44 | }
45 |
46 | // NewID generates next Gtid
47 | func NewID() ID {
48 | once.Do(func() {
49 | nodeId = identity.GetNodeIdentity()
50 | seqBuilder, _ = snowflake.NewNode(rand2.Int63n(1024))
51 | })
52 |
53 | return ID{
54 | NodeID: nodeId,
55 | Seq: seqBuilder.Generate().Int64(),
56 | }
57 | }
58 |
59 | // String ID to string
60 | func (i ID) String() string {
61 | return i.NodeID + "-" + strconv.FormatInt(i.Seq, 10)
62 | }
63 |
--------------------------------------------------------------------------------
/pkg/runtime/gtid/gtid_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package gtid
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestGtID(t *testing.T) {
29 | id := NewID()
30 | assert.NotEmpty(t, id.NodeID)
31 | assert.NotEmpty(t, id.Seq)
32 | assert.NotEmpty(t, id.String())
33 | }
34 |
--------------------------------------------------------------------------------
/pkg/runtime/misc/escape_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package misc
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestUnescape(t *testing.T) {
29 | assert.Equal(t, `abc\abc`, Unescape(`abc\\abc`))
30 | assert.Equal(t, "abc\nabc", Unescape(`abc\nabc`))
31 | assert.Equal(t, "\\abc\\\\abc\n\t\rabc\v", Unescape(`\\abc\\\\abc\n\t\rabc\v`))
32 | }
33 |
34 | func TestEscape(t *testing.T) {
35 | assert.Equal(t, `hello\nworld!`, Escape("hello\nworld!", 0))
36 | assert.Equal(t, `{\"age\":18}`, Escape(`{"age":18}`, EscapeDoubleQuote))
37 | assert.Equal(t, `like\%`, Escape(`like\%`, EscapeLike))
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/runtime/misc/extvalue/extvalue.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package extvalue
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | )
28 |
29 | func Compute(ctx context.Context, node ast.Node, args ...proto.Value) (proto.Value, error) {
30 | var vv valueVisitor
31 | vv.Context = ctx
32 | vv.args = args
33 | ret, err := node.Accept(&vv)
34 | if err != nil {
35 | return nil, err
36 | }
37 |
38 | switch val := ret.(type) {
39 | case proto.Value:
40 | return val, nil
41 | default:
42 | return nil, nil
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/pkg/runtime/misc/like.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package misc
19 |
20 | import (
21 | "path/filepath"
22 | "strings"
23 | )
24 |
25 | var replacer = strings.NewReplacer(
26 | "%", "*",
27 | "_", "?",
28 | "*", "\u0000",
29 | "?", "\u0001",
30 | "[", "\u0002",
31 | "]", "\u0003",
32 | "\\", "\u0004",
33 | "/", "\u0005",
34 | )
35 |
36 | type Liker interface {
37 | Like(s string) bool
38 | }
39 |
40 | type liker struct {
41 | pattern string
42 | }
43 |
44 | func NewLiker(pattern string) Liker {
45 | return &liker{pattern: pattern}
46 | }
47 |
48 | func (l *liker) Like(s string) bool {
49 | p := l.pattern
50 | p = replacer.Replace(p)
51 | s = replacer.Replace(s)
52 | flag, _ := filepath.Match(p, s)
53 | return flag
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/runtime/misc/pair.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package misc
19 |
20 | type Pair[L any, R any] struct {
21 | L L
22 | R R
23 | }
24 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/analyze_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeAnalyzeTable, optimizeAnalyzeTable)
34 | }
35 |
36 | func optimizeAnalyzeTable(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | shardsByName := make(map[string]rule.DatabaseTables)
39 |
40 | for _, table := range o.Rule.VTables() {
41 | shards = table.Topology().Enumerate()
42 | shardsByName[table.Name()] = shards
43 | break
44 | }
45 |
46 | stmt := o.Stmt.(*ast.AnalyzeTableStatement)
47 | return dal.NewAnalyzeTablePlan(stmt, shards, shardsByName), nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_character_set.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeShowCharacterSet, optimizeShowCharacterSet)
34 | }
35 |
36 | func optimizeShowCharacterSet(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | stmt := o.Stmt.(*ast.ShowCharset)
38 | shards := rule.DatabaseTables{}
39 | for _, table := range o.Rule.VTables() {
40 | shards = table.Topology().Enumerate()
41 | break
42 | }
43 |
44 | ret := dal.NewShowCharacterSetPlan(stmt, shards)
45 | ret.BindArgs(o.Args)
46 |
47 | return ret, nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_collation.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowCollation, optimizeShowCollation)
33 | }
34 |
35 | func optimizeShowCollation(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowCollation)
37 | ret := &dal.ShowCollationPlan{Stmt: stmt}
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_columns.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowColumns, optimizeShowColumns)
33 | }
34 |
35 | func optimizeShowColumns(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowColumns)
37 |
38 | vts := o.Rule.VTables()
39 | vtName := []string(stmt.TableName)[0]
40 | ret := &dal.ShowColumnsPlan{Stmt: stmt}
41 | ret.BindArgs(o.Args)
42 |
43 | if vTable, ok := vts[vtName]; ok {
44 | _, tblName, _ := vTable.Topology().Smallest()
45 | ret.Table = tblName
46 | }
47 |
48 | return ret, nil
49 | }
50 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_create_sequence.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowCreateSequence, optimizeShowCreateSequence)
33 | }
34 |
35 | func optimizeShowCreateSequence(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowCreateSequence)
37 | ret := dal.NewShowCreateSequencePlan(stmt)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_database_rule.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowDatabaseRules, optimizeShowDatabaseRules)
33 | }
34 |
35 | func optimizeShowDatabaseRules(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | rule := o.Rule
37 | stmt := o.Stmt.(*ast.ShowDatabaseRule)
38 | ret := dal.NewShowDatabaseRulesPlan(stmt)
39 | ret.BindArgs(o.Args)
40 | ret.SetRule(rule)
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_databases.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowDatabases, optimizeShowDatabases)
33 | }
34 |
35 | func optimizeShowDatabases(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | ret := &dal.ShowDatabasesPlan{Stmt: o.Stmt.(*ast.ShowDatabases)}
37 | ret.BindArgs(o.Args)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_master_status.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowMasterStatus, optimizeShowMasterStatus)
33 | }
34 |
35 | func optimizeShowMasterStatus(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowMasterStatus)
37 |
38 | ret := dal.NewShowMasterStatusPlan(stmt)
39 | ret.BindArgs(o.Args)
40 |
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_nodes.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowNodes, optimizeShowNodes)
33 | }
34 |
35 | func optimizeShowNodes(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowNodes)
37 | ret := dal.NewShowNodesPlan(stmt)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_replica_status.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowReplicaStatus, optimizeShowReplicaStatus)
33 | }
34 |
35 | func optimizeShowReplicaStatus(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowReplicaStatus)
37 |
38 | ret := dal.NewShowReplicaStatusPlan(stmt)
39 | ret.BindArgs(o.Args)
40 |
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_replicas.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowReplicas, optimizeShowReplicas)
33 | }
34 |
35 | func optimizeShowReplicas(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowReplicas)
37 |
38 | ret := dal.NewShowReplicasPlan(stmt)
39 | ret.BindArgs(o.Args)
40 |
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_sharding_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowShardingTable, optimizeShowShardingTable)
33 | }
34 |
35 | func optimizeShowShardingTable(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | rule := o.Rule
37 | stmt := o.Stmt.(*ast.ShowShardingTable)
38 | ret := dal.NewShowShardingTablePlan(stmt)
39 | ret.BindArgs(o.Args)
40 | ret.SetRule(rule)
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_status.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | *
17 | */
18 |
19 | package dal
20 |
21 | import (
22 | "context"
23 | )
24 |
25 | import (
26 | "github.com/arana-db/arana/pkg/proto"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeShowStatus, optimizeShowStatus)
34 | }
35 |
36 | func optimizeShowStatus(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | stmt := o.Stmt.(*ast.ShowStatus)
38 |
39 | ret := dal.NewShowStatusPlan(stmt)
40 | ret.BindArgs(o.Args)
41 |
42 | return ret, nil
43 | }
44 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_table_rule.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowTableRules, optimizeShowTableRules)
33 | }
34 |
35 | func optimizeShowTableRules(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | rule := o.Rule
37 | stmt := o.Stmt.(*ast.ShowTableRule)
38 | ret := dal.NewShowTableRulesPlan(stmt)
39 | ret.BindArgs(o.Args)
40 | ret.SetRule(rule)
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_table_status.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeShowTableStatus, optimizeShowTablesStatus)
34 | }
35 |
36 | func optimizeShowTablesStatus(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | for _, table := range o.Rule.VTables() {
39 | shards = table.Topology().Enumerate()
40 | break
41 | }
42 |
43 | stmt := o.Stmt.(*ast.ShowTableStatus)
44 |
45 | ret := &dal.ShowTableStatusPlan{Stmt: stmt, Database: stmt.Database, Shards: shards}
46 | ret.BindArgs(o.Args)
47 | return ret, nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_topology.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowTopology, optimizeShowTopology)
33 | }
34 |
35 | func optimizeShowTopology(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | rule := o.Rule
37 | stmt := o.Stmt.(*ast.ShowTopology)
38 | ret := dal.NewShowTopologyPlan(stmt)
39 | ret.BindArgs(o.Args)
40 | ret.SetRule(rule)
41 | return ret, nil
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_users.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowUsers, optimizeShowUsers)
33 | }
34 |
35 | func optimizeShowUsers(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.ShowUsers)
37 | ret := dal.NewShowUsersPlan(stmt)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_variables.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeShowVariables, optimizeShowVariables)
33 | }
34 |
35 | func optimizeShowVariables(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | ret := dal.NewShowVariablesPlan(o.Stmt.(*ast.ShowVariables))
37 | ret.BindArgs(o.Args)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dal/show_warnings.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/dal"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeShowWarnings, optimizeShowWarnings)
34 | }
35 |
36 | func optimizeShowWarnings(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | for _, table := range o.Rule.VTables() {
39 | shards = table.Topology().Enumerate()
40 | break
41 | }
42 |
43 | stmt := o.Stmt.(*ast.ShowWarnings)
44 |
45 | ret := dal.NewShowWarningsPlan(stmt, shards)
46 | ret.BindArgs(o.Args)
47 |
48 | return ret, nil
49 | }
50 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/check_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeCheckTable, optimizeCheckTable)
34 | }
35 |
36 | func optimizeCheckTable(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | shardsByName := make(map[string]rule.DatabaseTables)
39 |
40 | for _, table := range o.Rule.VTables() {
41 | shards = table.Topology().Enumerate()
42 | shardsByName[table.Name()] = shards
43 | break
44 | }
45 |
46 | stmt := o.Stmt.(*ast.CheckTableStmt)
47 | return ddl.NewCheckTablePlan(stmt, shards, shardsByName), nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/create_index.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeCreateIndex, optimizeCreateIndex)
33 | }
34 |
35 | func optimizeCreateIndex(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.CreateIndexStatement)
37 | ret := ddl.NewCreateIndexPlan(stmt)
38 | vt, ok := o.Rule.VTable(stmt.Table.Suffix())
39 |
40 | // table shard
41 | if !ok {
42 | return ret, nil
43 | }
44 |
45 | ret.SetShard(vt.Topology().Enumerate())
46 | return ret, nil
47 | }
48 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/drop_trigger.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeDropTrigger, optimizeTrigger)
34 | }
35 |
36 | func optimizeTrigger(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | for _, table := range o.Rule.VTables() {
39 | shards = table.Topology().Enumerate()
40 | break
41 | }
42 |
43 | ret := &ddl.DropTriggerPlan{Stmt: o.Stmt.(*ast.DropTriggerStatement), Shards: shards}
44 | ret.BindArgs(o.Args)
45 | return ret, nil
46 | }
47 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/rename_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeRenameTable, optimizeRenameTable)
34 | }
35 |
36 | func optimizeRenameTable(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | shardsByName := make(map[string]rule.DatabaseTables)
39 |
40 | for _, table := range o.Rule.VTables() {
41 | shards = table.Topology().Enumerate()
42 | shardsByName[table.Name()] = shards
43 | break
44 | }
45 |
46 | stmt := o.Stmt.(*ast.RenameTableStatement)
47 | return ddl.NewRenameTablePlan(stmt, shards, shardsByName), nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/repair_table.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeRepairTable, optimizeRepairTable)
34 | }
35 |
36 | func optimizeRepairTable(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | shards := rule.DatabaseTables{}
38 | shardsByName := make(map[string]rule.DatabaseTables)
39 |
40 | for _, table := range o.Rule.VTables() {
41 | shards = table.Topology().Enumerate()
42 | shardsByName[table.Name()] = shards
43 | break
44 | }
45 |
46 | stmt := o.Stmt.(*ast.RepairTableStmt)
47 | return ddl.NewRepairTablePlan(stmt, shards, shardsByName), nil
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/ddl/set_variable.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ddl
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/ddl"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeSetVariable, optimizeSetVariable)
33 | }
34 |
35 | func optimizeSetVariable(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | ret := &ddl.SetVariablePlan{Stmt: o.Stmt.(*ast.SetStatement)}
37 | ret.BindArgs(o.Args)
38 | return ret, nil
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dml/ext/ext.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ext
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/runtime/ast"
22 | )
23 |
24 | // SelectElementProvider provides previous upstream select element.
25 | type SelectElementProvider interface {
26 | // Prev returns the previous select element.
27 | Prev() ast.SelectElement
28 | }
29 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/dml/ext/weak_alias.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package ext
19 |
20 | import (
21 | "strings"
22 | )
23 |
24 | import (
25 | "github.com/pkg/errors"
26 | )
27 |
28 | import (
29 | "github.com/arana-db/arana/pkg/runtime/ast"
30 | )
31 |
32 | type WeakAliasSelectElement struct {
33 | ast.SelectElement
34 | WeakAlias string
35 | }
36 |
37 | func (re WeakAliasSelectElement) Prev() ast.SelectElement {
38 | if p, ok := re.SelectElement.(SelectElementProvider); ok {
39 | return p.Prev()
40 | }
41 | return re.SelectElement
42 | }
43 |
44 | func (re WeakAliasSelectElement) Restore(flag ast.RestoreFlag, sb *strings.Builder, args *[]int) error {
45 | if err := re.SelectElement.Restore(flag, sb, args); err != nil {
46 | return errors.WithStack(err)
47 | }
48 | sb.WriteString(" AS ")
49 | ast.WriteID(sb, re.WeakAlias)
50 | return nil
51 | }
52 |
53 | func (re WeakAliasSelectElement) Alias() string {
54 | return re.WeakAlias
55 | }
56 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/utility/describe.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package utility
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/runtime/ast"
27 | "github.com/arana-db/arana/pkg/runtime/optimize"
28 | "github.com/arana-db/arana/pkg/runtime/plan/utility"
29 | )
30 |
31 | func init() {
32 | optimize.Register(ast.SQLTypeDescribe, optimizeDescribeStatement)
33 | }
34 |
35 | func optimizeDescribeStatement(_ context.Context, o *optimize.Optimizer) (proto.Plan, error) {
36 | stmt := o.Stmt.(*ast.DescribeStatement)
37 | vts := o.Rule.VTables()
38 | vtName := []string(stmt.Table)[0]
39 | ret := utility.NewDescribePlan(stmt)
40 | ret.BindArgs(o.Args)
41 |
42 | if vTable, ok := vts[vtName]; ok {
43 | dbName, tblName, _ := vTable.Topology().Smallest()
44 | ret.Database = dbName
45 | ret.Table = tblName
46 | ret.Column = stmt.Column
47 | }
48 |
49 | return ret, nil
50 | }
51 |
--------------------------------------------------------------------------------
/pkg/runtime/optimize/utility/explain.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package utility
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/proto/rule"
27 | "github.com/arana-db/arana/pkg/runtime/ast"
28 | "github.com/arana-db/arana/pkg/runtime/optimize"
29 | "github.com/arana-db/arana/pkg/runtime/plan/utility"
30 | )
31 |
32 | func init() {
33 | optimize.Register(ast.SQLTypeExplain, optimzeExplainStatement)
34 | }
35 |
36 | func optimzeExplainStatement(ctx context.Context, o *optimize.Optimizer) (proto.Plan, error) {
37 | stmt := o.Stmt.(*ast.ExplainStatement)
38 |
39 | ret := utility.NewExplainPlan(stmt)
40 |
41 | var (
42 | shards rule.DatabaseTables
43 | err error
44 | )
45 |
46 | shards, err = o.ComputeShards(ctx, stmt.Table, nil, o.Args)
47 | if err != nil {
48 | return nil, err
49 | }
50 |
51 | ret.SetShards(shards)
52 |
53 | return ret, nil
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/runtime/plan/always.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package plan
19 |
20 | import (
21 | "context"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | "github.com/arana-db/arana/pkg/resultx"
27 | )
28 |
29 | var _ proto.Plan = (*AlwaysEmptyExecPlan)(nil)
30 |
31 | // AlwaysEmptyExecPlan represents an exec plan which affects nothing.
32 | type AlwaysEmptyExecPlan struct{}
33 |
34 | func (a AlwaysEmptyExecPlan) Type() proto.PlanType {
35 | return proto.PlanTypeExec
36 | }
37 |
38 | func (a AlwaysEmptyExecPlan) ExecIn(_ context.Context, _ proto.VConn) (proto.Result, error) {
39 | return resultx.New(), nil
40 | }
41 |
--------------------------------------------------------------------------------
/pkg/runtime/plan/dal/constant.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | const (
21 | headerPrefix = "Tables_in_"
22 | systemTablePrefix = "__arana_"
23 | )
24 |
--------------------------------------------------------------------------------
/pkg/runtime/plan/dal/show_database_rules_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/parser"
26 |
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | func TestShowDatabaseRulesSQL(t *testing.T) {
31 | sql := "SHOW DATABASE RULES FROM student"
32 |
33 | p := parser.New()
34 |
35 | stmtNodes, _, err := p.Parse(sql, "", "")
36 | assert.Nil(t, err)
37 | assert.NotNil(t, stmtNodes)
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/runtime/plan/dal/show_master_status_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package dal
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/parser"
26 |
27 | "github.com/stretchr/testify/assert"
28 | )
29 |
30 | func TestSQL(t *testing.T) {
31 | sql := "SHOW MASTER STATUS"
32 |
33 | p := parser.New()
34 |
35 | stmtNodes, _, err := p.Parse(sql, "", "")
36 | assert.Nil(t, err)
37 | assert.NotNil(t, stmtNodes)
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/runtime/plan/plan.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package plan
19 |
20 | import (
21 | "go.opentelemetry.io/otel"
22 | )
23 |
24 | import (
25 | "github.com/arana-db/arana/pkg/proto"
26 | )
27 |
28 | var Tracer = otel.Tracer("ExecPlan")
29 |
30 | type BasePlan struct {
31 | Args []proto.Value
32 | }
33 |
34 | func (bp *BasePlan) BindArgs(args []proto.Value) {
35 | bp.Args = args
36 | }
37 |
38 | func (bp *BasePlan) ToArgs(indexes []int) []proto.Value {
39 | if len(indexes) < 1 || len(bp.Args) < 1 {
40 | return nil
41 | }
42 | ret := make([]proto.Value, 0, len(indexes))
43 | for _, idx := range indexes {
44 | ret = append(ret, bp.Args[idx])
45 | }
46 | return ret
47 | }
48 |
--------------------------------------------------------------------------------
/pkg/selector/db_manager.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package selector
19 |
20 | type Selector interface {
21 | GetDataSourceNo() int
22 | }
23 |
--------------------------------------------------------------------------------
/pkg/selector/weight_random_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package selector
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | func TestGetDatasourceNo(t *testing.T) {
25 | weights := []int{
26 | 1, 2, 3,
27 | }
28 | selector := NewWeightRandomSelector(weights)
29 | no := selector.GetDataSourceNo()
30 | if no < 0 || no > 2 {
31 | t.Errorf("No.%d invalid", no)
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/pkg/server/server.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package server
19 |
20 | import (
21 | "github.com/arana-db/arana/pkg/proto"
22 | )
23 |
24 | type Server struct {
25 | listeners []proto.Listener
26 | }
27 |
28 | func NewServer() *Server {
29 | return &Server{
30 | listeners: make([]proto.Listener, 0),
31 | }
32 | }
33 |
34 | func (srv *Server) AddListener(listener proto.Listener) {
35 | srv.listeners = append(srv.listeners, listener)
36 | }
37 |
38 | func (srv *Server) Start() {
39 | for _, l := range srv.listeners {
40 | go l.Listen()
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/pkg/util/bufferpool/bufferpool.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package bufferpool
19 |
20 | import (
21 | "bytes"
22 | "sync"
23 | )
24 |
25 | var _bufferPool sync.Pool
26 |
27 | // Get borrows a Buffer from pool.
28 | func Get() *bytes.Buffer {
29 | if exist, ok := _bufferPool.Get().(*bytes.Buffer); ok {
30 | return exist
31 | }
32 | return new(bytes.Buffer)
33 | }
34 |
35 | // Put returns a Buffer to pool.
36 | func Put(b *bytes.Buffer) {
37 | if b == nil {
38 | return
39 | }
40 | const maxCap = 1024 * 1024
41 | // drop huge buff directly, if cap is over 1MB
42 | if b.Cap() > maxCap {
43 | return
44 | }
45 | b.Reset()
46 | _bufferPool.Put(b)
47 | }
48 |
--------------------------------------------------------------------------------
/pkg/util/bytesconv/bytesconv.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | // Copyright 2020 Gin Core Team. All rights reserved.
19 | // Use of this source code is governed by a MIT style
20 | // license that can be found in the LICENSE file.
21 |
22 | package bytesconv
23 |
24 | import (
25 | "unsafe"
26 | )
27 |
28 | // StringToBytes converts string to byte slice without a memory allocation.
29 | func StringToBytes(s string) []byte {
30 | return *(*[]byte)(unsafe.Pointer(
31 | &struct {
32 | string
33 | Cap int
34 | }{s, len(s)},
35 | ))
36 | }
37 |
38 | // BytesToString converts byte slice to string without a memory allocation.
39 | func BytesToString(b []byte) string {
40 | return *(*string)(unsafe.Pointer(&b))
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/util/config/config.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | // Copyright 2020 Gin Core Team. All rights reserved.
19 | // Use of this source code is governed by a MIT style
20 | // license that can be found in the LICENSE file.
21 |
22 | package config
23 |
24 | import (
25 | "sync"
26 | )
27 |
28 | var (
29 | _enableLocalMathCompu bool
30 | _enableLocalMathCompuSync sync.Once
31 | )
32 |
33 | // _enableLocalMathCompu returns true if config the local math computation
34 | func IsEnableLocalMathCompu(enable bool) bool {
35 | _enableLocalMathCompuSync.Do(func() {
36 | _enableLocalMathCompu = enable
37 | })
38 | return _enableLocalMathCompu
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/util/env/env.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | // Copyright 2020 Gin Core Team. All rights reserved.
19 | // Use of this source code is governed by a MIT style
20 | // license that can be found in the LICENSE file.
21 |
22 | package env
23 |
24 | import (
25 | "os"
26 | "strings"
27 | "sync"
28 | )
29 |
30 | import (
31 | "github.com/arana-db/arana/pkg/constants"
32 | )
33 |
34 | var (
35 | _isDevEnv bool
36 | _isDevEnvSync sync.Once
37 | )
38 |
39 | // IsDevelopEnvironment returns true in the develop environment
40 | func IsDevelopEnvironment() bool {
41 | _isDevEnvSync.Do(func() {
42 | switch strings.ToLower(os.Getenv(constants.EnvDevelopEnvironment)) {
43 | case "1", "yes", "on", "true":
44 | _isDevEnv = true
45 | }
46 | })
47 | return _isDevEnv
48 | }
49 |
--------------------------------------------------------------------------------
/pkg/util/file/file.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to Apache Software Foundation (ASF) under one or more contributor
3 | * license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright
5 | * ownership. Apache Software Foundation (ASF) licenses this file to you under
6 | * the Apache License, Version 2.0 (the "License"); you may
7 | * not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing,
13 | * software distributed under the License is distributed on an
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | * KIND, either express or implied. See the License for the
16 | * specific language governing permissions and limitations
17 | * under the License.
18 | *
19 | */
20 |
21 | package file
22 |
23 | import (
24 | "path/filepath"
25 | )
26 |
27 | func IsYaml(path string) bool {
28 | ext := filepath.Ext(path)
29 | if ext == ".yaml" || ext == ".yml" {
30 | return true
31 | }
32 | return false
33 | }
34 |
--------------------------------------------------------------------------------
/pkg/util/file/file_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package file
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestIsYaml(t *testing.T) {
29 | // Test with a valid YAML file
30 | path := "example.yaml"
31 | result := IsYaml(path)
32 | assert.True(t, result)
33 |
34 | // Test with a valid YML file
35 | path = "example.yml"
36 | result = IsYaml(path)
37 | assert.True(t, result)
38 |
39 | // Test with an invalid file extension
40 | path = "example.txt"
41 | result = IsYaml(path)
42 | assert.False(t, result)
43 | }
44 |
--------------------------------------------------------------------------------
/pkg/util/identity/identity.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package identity
19 |
20 | import (
21 | "os"
22 | )
23 |
24 | import (
25 | "github.com/google/uuid"
26 | )
27 |
28 | import (
29 | "github.com/arana-db/arana/pkg/util/net"
30 | )
31 |
32 | const (
33 | AranaNodeId = "ARANA_NODE_ID"
34 | PodName = "POD_NAME"
35 | )
36 |
37 | func GetNodeIdentity() string {
38 | nodeId := os.Getenv(AranaNodeId)
39 | if len(nodeId) != 0 {
40 | return nodeId
41 | }
42 |
43 | podName := os.Getenv(PodName)
44 | if len(podName) != 0 {
45 | return podName
46 | }
47 |
48 | ip, err := net.FindSelfIP()
49 | if err == nil {
50 | return ip
51 | }
52 |
53 | return uuid.NewString()
54 | }
55 |
--------------------------------------------------------------------------------
/pkg/util/math/abs.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package math
19 |
20 | import (
21 | "golang.org/x/exp/constraints"
22 | )
23 |
24 | // Abs computes abs.
25 | func Abs[T constraints.Signed](n T) T {
26 | if n < 0 {
27 | return -n
28 | }
29 | return n
30 | }
31 |
--------------------------------------------------------------------------------
/pkg/util/math/abs_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package math
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestAbs(t *testing.T) {
29 | // Test cases for positive numbers
30 | assert.Equal(t, 5, Abs(5))
31 | assert.Equal(t, int32(10), Abs(int32(10)))
32 |
33 | // Test cases for negative numbers
34 | assert.Equal(t, 5, Abs(-5))
35 | assert.Equal(t, int64(10), Abs(int64(-10)))
36 |
37 | // Test case for zero
38 | assert.Equal(t, 0, Abs(0))
39 | }
40 |
--------------------------------------------------------------------------------
/pkg/util/math/compare.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package math
19 |
20 | import (
21 | "golang.org/x/exp/constraints"
22 | )
23 |
24 | type Number interface {
25 | constraints.Integer | constraints.Float
26 | }
27 |
28 | func Max[T Number](a, b T) T {
29 | if a > b {
30 | return a
31 | }
32 | return b
33 | }
34 |
35 | func Min[T Number](a, b T) T {
36 | if a < b {
37 | return a
38 | }
39 | return b
40 | }
41 |
--------------------------------------------------------------------------------
/pkg/util/math/sal.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package math
19 |
20 | import (
21 | "fmt"
22 | )
23 |
24 | const DefaultBase = 16
25 |
26 | func EncodeProcessID(num1, base, num2 int64) (int64, error) {
27 | t := num1 << base
28 | if t < 0 {
29 | return 0, fmt.Errorf("integer operation result is out of range")
30 | }
31 | return t + num2, nil
32 | }
33 |
34 | func DecodeProcessID(num, base int64) (int64, int64) {
35 | t := int64(1<> base, num & t
38 | }
39 |
--------------------------------------------------------------------------------
/pkg/util/math/sal_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package math
19 |
20 | import (
21 | "testing"
22 | )
23 |
24 | import (
25 | "github.com/stretchr/testify/assert"
26 | )
27 |
28 | func TestCodeAndDecode(t *testing.T) {
29 | const (
30 | a = int64(692)
31 | b = int64(3)
32 | size = int64(16)
33 | )
34 |
35 | res, err := EncodeProcessID(a, size, b)
36 | assert.NoError(t, err)
37 |
38 | aa, bb := DecodeProcessID(res, size)
39 | assert.Equal(t, a, aa)
40 | assert.Equal(t, b, bb)
41 | }
42 |
--------------------------------------------------------------------------------
/pkg/util/runes/rune.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package runes
19 |
20 | import (
21 | "fmt"
22 | )
23 |
24 | func ConvertToRune(value interface{}) []rune {
25 | return []rune(fmt.Sprint(value))
26 | }
27 |
--------------------------------------------------------------------------------
/pkg/util/runes/rune_test.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package runes
19 |
20 | import (
21 | "reflect"
22 | "testing"
23 | )
24 |
25 | import (
26 | "github.com/stretchr/testify/assert"
27 | )
28 |
29 | func TestConvertToRune(t *testing.T) {
30 | tests := []struct {
31 | input interface{}
32 | output []rune
33 | }{
34 | {123, []rune("123")},
35 | {"hello", []rune("hello")},
36 | {true, []rune("true")},
37 | {nil, []rune("")},
38 | {struct{ name string }{name: "John"}, []rune("{John}")},
39 | }
40 |
41 | for _, test := range tests {
42 | result := ConvertToRune(test.input)
43 | assert.True(t, reflect.DeepEqual(result, test.output))
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/scripts/sequence.sql:
--------------------------------------------------------------------------------
1 | --
2 | -- Licensed to the Apache Software Foundation (ASF) under one or more
3 | -- contributor license agreements. See the NOTICE file distributed with
4 | -- this work for additional information regarding copyright ownership.
5 | -- The ASF licenses this file to You under the Apache License, Version 2.0
6 | -- (the "License"); you may not use this file except in compliance with
7 | -- the License. You may obtain a copy of the License at
8 | --
9 | -- http://www.apache.org/licenses/LICENSE-2.0
10 | --
11 | -- Unless required by applicable law or agreed to in writing, software
12 | -- distributed under the License is distributed on an "AS IS" BASIS,
13 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | -- See the License for the specific language governing permissions and
15 | -- limitations under the License.
16 | --
17 |
18 | CREATE DATABASE IF NOT EXISTS employees_0000 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
19 |
20 | CREATE TABLE IF NOT EXISTS `employees_0000`.`sequence`
21 | (
22 | `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
23 | `name` VARCHAR(64) NOT NULL,
24 | `value` BIGINT NOT NULL,
25 | `step` INT NOT NULL DEFAULT 10000,
26 | `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27 | `modified_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
28 | PRIMARY KEY (`id`),
29 | UNIQUE KEY `uk_name` (`name`)
30 | ) ENGINE = InnoDB
31 | DEFAULT CHARSET = utf8mb4;
32 |
--------------------------------------------------------------------------------
/staticcheck.conf:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | checks = ["all","-U1000","-ST1000","-ST1003","-ST1020","-ST1021","-S1016"]
19 |
--------------------------------------------------------------------------------
/testdata/fake_bootstrap.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
18 | listeners:
19 | - protocol_type: mysql
20 | server_version: 5.7.0
21 | socket_address:
22 | address: 0.0.0.0
23 | port: 13306
24 | config:
25 | name: file
26 | options:
27 |
28 | supervisor:
29 | username: root
30 | password: root
31 |
32 | logging:
33 | level: INFO
34 | path: ~/arana/logs
35 | max_size: 128m
36 | max_backups: 3
37 | max_age: 7
38 | compress: true
39 | console: true
40 |
--------------------------------------------------------------------------------
/testdata/fake_empty_config.yaml:
--------------------------------------------------------------------------------
1 | #
2 | # Licensed to the Apache Software Foundation (ASF) under one or more
3 | # contributor license agreements. See the NOTICE file distributed with
4 | # this work for additional information regarding copyright ownership.
5 | # The ASF licenses this file to You under the Apache License, Version 2.0
6 | # (the "License"); you may not use this file except in compliance with
7 | # the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing, software
12 | # distributed under the License is distributed on an "AS IS" BASIS,
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | # See the License for the specific language governing permissions and
15 | # limitations under the License.
16 | #
17 |
--------------------------------------------------------------------------------
/testdata/testdata.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | // Package testdata includes test resources.
19 | package testdata
20 |
21 | import (
22 | "path/filepath"
23 | "runtime"
24 | )
25 |
26 | var basepath string
27 |
28 | func init() {
29 | _, currentFile, _, _ := runtime.Caller(0)
30 | basepath = filepath.Dir(currentFile)
31 | }
32 |
33 | // Path gets the absolute path.
34 | func Path(rel string) string {
35 | if filepath.IsAbs(rel) {
36 | return rel
37 | }
38 | return filepath.Join(basepath, rel)
39 | }
40 |
--------------------------------------------------------------------------------
/third_party/base58/README.md:
--------------------------------------------------------------------------------
1 | base58
2 | ==========
3 |
4 | [](https://travis-ci.org/btcsuite/btcutil)
5 | [](http://copyfree.org)
6 | [](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58)
7 |
8 | Package base58 provides an API for encoding and decoding to and from the
9 | modified base58 encoding. It also provides an API to do Base58Check encoding,
10 | as described [here](https://en.bitcoin.it/wiki/Base58Check_encoding).
11 |
12 | A comprehensive suite of tests is provided to ensure proper functionality.
13 |
14 | ## Installation and Updating
15 |
16 | ```bash
17 | $ go get -u github.com/btcsuite/btcd/btcutil/base58
18 | ```
19 |
20 | ## Examples
21 |
22 | * [Decode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-Decode)
23 | Demonstrates how to decode modified base58 encoded data.
24 | * [Encode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-Encode)
25 | Demonstrates how to encode data using the modified base58 encoding scheme.
26 | * [CheckDecode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-CheckDecode)
27 | Demonstrates how to decode Base58Check encoded data.
28 | * [CheckEncode Example](http://godoc.org/github.com/btcsuite/btcd/btcutil/base58#example-CheckEncode)
29 | Demonstrates how to encode data using the Base58Check encoding scheme.
30 |
31 | ## License
32 |
33 | Package base58 is licensed under the [copyfree](http://copyfree.org) ISC
34 | License.
35 |
--------------------------------------------------------------------------------
/third_party/sync2/doc.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | //
19 | // Copyright 2019 The Vitess Authors.
20 | //
21 | // Licensed under the Apache License, Version 2.0 (the "License");
22 | // you may not use this file except in compliance with the License.
23 | // You may obtain a copy of the License at
24 | //
25 | // http://www.apache.org/licenses/LICENSE-2.0
26 | //
27 | // Unless required by applicable law or agreed to in writing, software
28 | // distributed under the License is distributed on an "AS IS" BASIS,
29 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 | // See the License for the specific language governing permissions and
31 | // limitations under the License.
32 | //
33 |
34 | // Package sync2 provides extra functionality along the same lines as sync.
35 | package sync2
36 |
--------------------------------------------------------------------------------