Merge pull request #33676 from ClickHouse/fix-8340

Correctly determine current database if CREATE TEMPORARY TABLE AS SELECT is queried inside a named HTTP session.
This commit is contained in:
tavplubix 2022-01-17 16:52:09 +03:00 committed by GitHub
commit 34b3894422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 19 deletions

View File

@ -725,9 +725,8 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const
if (create.storage || create.is_dictionary || create.isView())
{
if (create.temporary && create.storage && create.storage->engine && create.storage->engine->name != "Memory")
throw Exception(
"Temporary tables can only be created with ENGINE = Memory, not " + create.storage->engine->name,
ErrorCodes::INCORRECT_QUERY);
throw Exception(ErrorCodes::INCORRECT_QUERY,
"Temporary tables can only be created with ENGINE = Memory, not {}", create.storage->engine->name);
return;
}
@ -1254,17 +1253,14 @@ BlockIO InterpreterCreateQuery::fillTableIfNeeded(const ASTCreateQuery & create)
{
/// If the query is a CREATE SELECT, insert the data into the table.
if (create.select && !create.attach
&& !create.is_ordinary_view && !create.is_live_view && !create.is_window_view && (!create.is_materialized_view || create.is_populate))
&& !create.is_ordinary_view && !create.is_live_view && !create.is_window_view
&& (!create.is_materialized_view || create.is_populate))
{
auto insert = std::make_shared<ASTInsertQuery>();
insert->table_id = {create.getDatabase(), create.getTable(), create.uuid};
insert->select = create.select->clone();
if (create.temporary && !getContext()->getSessionContext()->hasQueryContext())
getContext()->getSessionContext()->makeQueryContext();
return InterpreterInsertQuery(insert,
create.temporary ? getContext()->getSessionContext() : getContext(),
return InterpreterInsertQuery(insert, getContext(),
getContext()->getSettingsRef().insert_allow_materialized_columns).execute();
}

View File

@ -36,9 +36,9 @@ public:
~Session();
Session(const Session &&) = delete;
Session& operator=(const Session &&) = delete;
Session & operator=(const Session &&) = delete;
Session(const Session &) = delete;
Session& operator=(const Session &) = delete;
Session & operator=(const Session &) = delete;
/// Provides information about the authentication type of a specified user.
AuthenticationType getAuthenticationType(const String & user_name) const;
@ -97,4 +97,3 @@ private:
};
}

View File

@ -10,10 +10,7 @@
#include <IO/CascadeWriteBuffer.h>
#include <IO/ConcatReadBuffer.h>
#include <IO/MemoryReadWriteBuffer.h>
#include <IO/ReadBufferFromIStream.h>
#include <IO/ReadBufferFromString.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteBufferFromTemporaryFile.h>
#include <IO/WriteHelpers.h>
#include <IO/copyData.h>
@ -27,7 +24,6 @@
#include <base/logger_useful.h>
#include <Common/SettingsChanges.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/escapeForFileName.h>
#include <Common/setThreadName.h>
#include <Common/typeid_cast.h>
@ -41,13 +37,11 @@
#include <Poco/Base64Encoder.h>
#include <Poco/Net/HTTPBasicCredentials.h>
#include <Poco/Net/HTTPStream.h>
#include <Poco/Net/NetException.h>
#include <Poco/MemoryStream.h>
#include <Poco/StreamCopier.h>
#include <Poco/String.h>
#include <chrono>
#include <iomanip>
#include <sstream>
@ -56,7 +50,6 @@ namespace DB
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int CANNOT_PARSE_TEXT;
extern const int CANNOT_PARSE_ESCAPE_SEQUENCE;

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
# Tags: no-parallel
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
DATABASE='test_02177'
SESSION_ID="$RANDOM$RANDOM$RANDOM"
${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}" -d "DROP DATABASE IF EXISTS ${DATABASE}"
${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}" -d "CREATE DATABASE ${DATABASE}"
CLICKHOUSE_URL_PARAMS="database=${DATABASE}"
[ -v CLICKHOUSE_LOG_COMMENT ] && CLICKHOUSE_URL_PARAMS="${CLICKHOUSE_URL_PARAMS}&log_comment=${CLICKHOUSE_LOG_COMMENT}"
CLICKHOUSE_URL="${CLICKHOUSE_PORT_HTTP_PROTO}://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT_HTTP}/?${CLICKHOUSE_URL_PARAMS}"
${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&session_id=${SESSION_ID}" -d 'CREATE TEMPORARY TABLE t AS SELECT currentDatabase()'
${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}&session_id=${SESSION_ID}" -d 'SELECT * FROM t'
${CLICKHOUSE_CURL} -sS "${CLICKHOUSE_URL}" -d "DROP DATABASE ${DATABASE}"