Fix incorrect getCreateTableQueryImpl with multidim arrays

This commit is contained in:
kssenii 2021-01-10 09:00:35 +00:00
parent 0088d66d59
commit fc9de76f7d
3 changed files with 50 additions and 10 deletions

View File

@ -3,6 +3,7 @@
#if USE_LIBPQXX
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeArray.h>
#include <Interpreters/Context.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTFunction.h>
@ -353,16 +354,7 @@ ASTPtr DatabasePostgreSQL::getCreateTableQueryImpl(const String & table_name, co
{
const auto & column_declaration = std::make_shared<ASTColumnDeclaration>();
column_declaration->name = column_type_and_name.name;
std::function<ASTPtr(const DataTypePtr &)> convert_datatype_to_query = [&](const DataTypePtr & data_type) -> ASTPtr
{
WhichDataType which(data_type);
if (!which.isNullable())
return std::make_shared<ASTIdentifier>(data_type->getName());
return makeASTFunction("Nullable", convert_datatype_to_query(typeid_cast<const DataTypeNullable *>(data_type.get())->getNestedType()));
};
column_declaration->type = convert_datatype_to_query(column_type_and_name.type);
column_declaration->type = getColumnDeclaration(column_type_and_name.type);
columns_expression_list->children.emplace_back(column_declaration);
}
@ -380,6 +372,20 @@ ASTPtr DatabasePostgreSQL::getCreateTableQueryImpl(const String & table_name, co
return create_table_query;
}
ASTPtr DatabasePostgreSQL::getColumnDeclaration(const DataTypePtr & data_type) const
{
WhichDataType which(data_type);
if (which.isNullable())
return makeASTFunction("Nullable", getColumnDeclaration(typeid_cast<const DataTypeNullable *>(data_type.get())->getNestedType()));
if (which.isArray())
return makeASTFunction("Array", getColumnDeclaration(typeid_cast<const DataTypeArray *>(data_type.get())->getNestedType()));
return std::make_shared<ASTIdentifier>(data_type->getName());
}
}
#endif

View File

@ -76,6 +76,7 @@ private:
std::unordered_set<std::string> fetchTablesList() const;
StoragePtr fetchTable(const String & table_name, const Context & context, const bool table_checked) const;
void removeOutdatedTables();
ASTPtr getColumnDeclaration(const DataTypePtr & data_type) const;
};
}

View File

@ -113,6 +113,39 @@ def test_postgresql_database_engine_queries(started_cluster):
assert 'test_database' not in node1.query('SHOW DATABASES')
def test_get_create_table_query_with_multidim_arrays(started_cluster):
conn = get_postgres_conn(True)
cursor = conn.cursor()
node1.query(
"CREATE DATABASE test_database ENGINE = PostgreSQL('postgres1:5432', 'test_database', 'postgres', 'mysecretpassword')")
cursor.execute("""
CREATE TABLE IF NOT EXISTS array_columns (
b Integer[][][] NOT NULL,
c Integer[][][]
)""")
node1.query("DETACH TABLE test_database.array_columns")
node1.query("ATTACH TABLE test_database.array_columns")
node1.query("INSERT INTO test_database.array_columns "
"VALUES ("
"[[[1, 1], [1, 1]], [[3, 3], [3, 3]], [[4, 4], [5, 5]]], "
"[[[1, NULL], [NULL, 1]], [[NULL, NULL], [NULL, NULL]], [[4, 4], [5, 5]]] "
")")
result = node1.query('''
SELECT * FROM test_database.array_columns''')
expected = (
"[[[1,1],[1,1]],[[3,3],[3,3]],[[4,4],[5,5]]]\t"
"[[[1,NULL],[NULL,1]],[[NULL,NULL],[NULL,NULL]],[[4,4],[5,5]]]\n"
)
assert(result == expected)
node1.query("DROP DATABASE test_database")
assert 'test_database' not in node1.query('SHOW DATABASES')
def test_postgresql_database_engine_table_cache(started_cluster):
conn = get_postgres_conn(True)
cursor = conn.cursor()