| 684 | } |
| 685 | |
| 686 | std::shared_ptr<::fedb::client::TabletClient> SQLClusterRouter::GetTabletClient( |
| 687 | const std::string& db, const std::string& sql, const std::shared_ptr<SQLRequestRow>& row) { |
| 688 | std::shared_ptr<::fedb::catalog::TabletAccessor> tablet; |
| 689 | auto cache = GetCache(db, sql); |
| 690 | if (!cache) { |
| 691 | ::hybridse::vm::ExplainOutput explain; |
| 692 | ::hybridse::base::Status vm_status; |
| 693 | if (cluster_sdk_->GetEngine()->Explain(sql, db, ::hybridse::vm::kBatchMode, &explain, &vm_status)) { |
| 694 | std::shared_ptr<::hybridse::sdk::SchemaImpl> schema; |
| 695 | if (explain.input_schema.size() > 0) { |
| 696 | schema = std::make_shared<::hybridse::sdk::SchemaImpl>(explain.input_schema); |
| 697 | } else { |
| 698 | auto table_info = cluster_sdk_->GetTableInfo(db, explain.router.GetMainTable()); |
| 699 | ::hybridse::vm::Schema raw_schema; |
| 700 | if (table_info && |
| 701 | ::fedb::catalog::SchemaAdapter::ConvertSchema(table_info->column_desc(), &raw_schema)) { |
| 702 | schema = std::make_shared<::hybridse::sdk::SchemaImpl>(raw_schema); |
| 703 | } |
| 704 | } |
| 705 | if (schema) { |
| 706 | cache = std::make_shared<SQLCache>(schema, explain.router); |
| 707 | SetCache(db, sql, cache); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | if (cache) { |
| 712 | const std::string& col = cache->router.GetRouterCol(); |
| 713 | const std::string& main_table = cache->router.GetMainTable(); |
| 714 | if (!main_table.empty()) { |
| 715 | DLOG(INFO) << "get main table" << main_table; |
| 716 | std::string val; |
| 717 | if (!col.empty() && row && row->GetRecordVal(col, &val)) { |
| 718 | tablet = cluster_sdk_->GetTablet(db, main_table, val); |
| 719 | } |
| 720 | if (!tablet) { |
| 721 | tablet = cluster_sdk_->GetTablet(db, main_table); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | if (!tablet) { |
| 726 | tablet = cluster_sdk_->GetTablet(); |
| 727 | } |
| 728 | if (!tablet) { |
| 729 | LOG(WARNING) << "fail to get tablet"; |
| 730 | return std::shared_ptr<::fedb::client::TabletClient>(); |
| 731 | } |
| 732 | return tablet->GetClient(); |
| 733 | } |
| 734 | |
| 735 | std::shared_ptr<TableReader> SQLClusterRouter::GetTableReader() { |
| 736 | std::shared_ptr<TableReaderImpl> reader(new TableReaderImpl(cluster_sdk_)); |