MCPcopy Create free account
hub / github.com/ByConity/ByConity / write

Method write

src/Storages/StorageDistributed.cpp:718–760  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

716
717
718BlockOutputStreamPtr StorageDistributed::write(const ASTPtr &, const StorageMetadataPtr & metadata_snapshot, ContextPtr local_context)
719{
720 auto cluster = getCluster();
721 const auto & settings = local_context->getSettingsRef();
722
723 /// Ban an attempt to make async insert into the table belonging to DatabaseMemory
724 if (!storage_policy && !owned_cluster && !settings.insert_distributed_sync && !settings.insert_shard_id)
725 {
726 throw Exception("Storage " + getName() + " must have own data directory to enable asynchronous inserts",
727 ErrorCodes::BAD_ARGUMENTS);
728 }
729
730 auto shard_num = cluster->getLocalShardCount() + cluster->getRemoteShardCount();
731
732 /// If sharding key is not specified, then you can only write to a shard containing only one shard
733 if (!settings.insert_shard_id && !settings.insert_distributed_one_random_shard && !has_sharding_key && shard_num >= 2)
734 {
735 throw Exception(
736 "Method write is not supported by storage " + getName() + " with more than one shard and no sharding key provided",
737 ErrorCodes::STORAGE_REQUIRES_PARAMETER);
738 }
739
740 if (settings.insert_shard_id && settings.insert_shard_id > shard_num)
741 {
742 throw Exception("Shard id should be range from 1 to shard number", ErrorCodes::INVALID_SHARD_ID);
743 }
744
745 /// Force sync insertion if it is remote() table function
746 bool insert_sync = settings.insert_distributed_sync || settings.insert_shard_id || owned_cluster;
747 auto timeout = settings.insert_distributed_timeout;
748
749 Block sample_block;
750 if (!settings.insert_allow_materialized_columns)
751 sample_block = metadata_snapshot->getSampleBlockNonMaterialized();
752 else
753 sample_block = metadata_snapshot->getSampleBlock();
754
755 /// DistributedBlockOutputStream will not own cluster, but will own ConnectionPools of the cluster
756 return std::make_shared<DistributedBlockOutputStream>(
757 local_context, *this, metadata_snapshot,
758 createInsertToRemoteTableQuery(remote_database, remote_table, sample_block),
759 cluster, insert_sync, timeout, StorageID{remote_database, remote_table});
760}
761
762
763QueryPipelinePtr StorageDistributed::distributedWrite(const ASTInsertQuery & query, ContextPtr local_context)

Callers

nothing calls this directly

Calls 7

getLocalShardCountMethod · 0.80
getRemoteShardCountMethod · 0.80
ExceptionClass · 0.50
getNameFunction · 0.50
getSampleBlockMethod · 0.45

Tested by

no test coverage detected