Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Client/ClientBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,7 @@ void ClientBase::sendData(Block & sample, const ColumnsDescription & columns_des
ConstraintsDescription{},
String{},
{},
std::nullopt /*table_allow_aggregate_function_states*/,
};
StoragePtr storage = std::make_shared<StorageFile>(StorageFile::FileSource::parse(in_file, client_context), args);
storage->startup();
Expand Down
15 changes: 15 additions & 0 deletions src/Core/FormatFactorySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,21 @@ Use geo column parser to convert Array(UInt8) into Point/MultiPoint/Linestring/P
)", 0) \
DECLARE(Bool, input_format_parquet_detect_variant_by_structure, true, R"(
Read a Parquet group that has no `VARIANT` logical type, but has the layout of an unshredded variant (a `metadata` and a `value` field of type `BYTE_ARRAY`), as a variant, i.e. as `Dynamic`, instead of `Tuple`. Spark 4.0 writes variant columns this way. Groups annotated with the `VARIANT` logical type are always read as a variant.
)", 0) \
DECLARE(Bool, allow_aggregate_function_states_in_open_formats, false, R"(
Allow `AggregateFunction` states in Parquet files and Iceberg tables. A state is stored as binary, with its ClickHouse
type recorded in `clickhouse.column_types` (Parquet file metadata) or `clickhouse.type.<field-id>.<schema-id>`
(Iceberg table properties). The recorded type chooses the deserializer for the stored bytes, so enable the setting
only for trusted sources.

When disabled, writing a state throws `SUPPORT_IS_DISABLED`. On read, a Parquet state column reads as `String`, and a
query on an Iceberg table with a state column throws `SUPPORT_IS_DISABLED`. `SimpleAggregateFunction` columns are not
affected.

A table can also set it in the `SETTINGS` clause of its engine (for example `S3` or `IcebergS3`). That value is stored
with the table and applies to every query on it, whatever the query sets.
More [in Parquet](/reference/formats/Parquet/Parquet#aggregate-function-states) and
[in Iceberg](/reference/engines/table-engines/integrations/iceberg#aggregate-function-states).
)", 0) \
DECLARE(Bool, output_format_parquet_geometadata, true, R"(
Allow to write information about geo columns in parquet metadata and encode columns in WKB format.
Expand Down
4 changes: 4 additions & 0 deletions src/Core/SettingsChangesHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const VersionToSettingsChangesMap & getSettingsChangesHistory()
{
{"azure_validate_etag_on_read", false, true, "New setting to detect concurrent in-place overwrites of Azure blobs during a read by pinning every `GET` to the listed `ETag` with `If-Match` and validating the `ETag` of the response, like `s3_validate_etag_on_read` does for S3. `compatibility` with versions before 26.10 restores the previous behavior (no validation)."},
});
addSettingsChanges(settings_changes_history, "26.8.39.20001.altinityantalya",
{
{"allow_aggregate_function_states_in_open_formats", false, false, "New setting allowing aggregate function states in Parquet files and Iceberg tables. With it disabled, writing a state throws `SUPPORT_IS_DISABLED`, a Parquet state column reads as `String`, and a query on an Iceberg table with a state column throws `SUPPORT_IS_DISABLED`."},
});
addSettingsChanges(settings_changes_history, "26.8.35.20001.altinityantalya",
{
{"delta_lake_clustering_predicate_fallback", false, true, "New setting to enable clustering-aware predicate fallback for Delta Lake tables with Liquid Clustering."},
Expand Down
71 changes: 69 additions & 2 deletions src/DataTypes/DataTypeAggregateFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/IAggregateFunction.h>
#include <Parsers/ASTDataType.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTIdentifier_fwd.h>
#include <Parsers/ASTLiteral.h>

Expand Down Expand Up @@ -534,12 +536,16 @@ combinator.
});
}

bool hasAggregateFunctionType(const DataTypePtr & type)
namespace
{

template <typename Predicate>
bool hasTypeMatching(const DataTypePtr & type, Predicate && predicate)
{
auto result = false;
auto check = [&](const IDataType & t)
{
result |= WhichDataType(t).isAggregateFunction();
result |= predicate(t);
};

check(*type);
Expand All @@ -548,3 +554,64 @@ bool hasAggregateFunctionType(const DataTypePtr & type)
}

}

bool hasAggregateFunctionType(const DataTypePtr & type)
{
return hasTypeMatching(type, [](const IDataType & t) { return WhichDataType(t).isAggregateFunction(); });
}

bool hasAggregateFunctionOrSimpleAggregateFunctionType(const DataTypePtr & type)
{
return hasTypeMatching(
type,
[](const IDataType & t)
{
return WhichDataType(t).isAggregateFunction()
|| typeid_cast<const DataTypeCustomSimpleAggregateFunction *>(t.getCustomName()) != nullptr;
});
}

String getClickHouseTypeAnnotation(const DataTypePtr & type)
{
return hasAggregateFunctionOrSimpleAggregateFunctionType(type) ? type->getName() : String{};
}

namespace
{

/// Checks a parsed type name without resolving aggregate functions through `DataTypeFactory`.
bool astHasAggregateFunctionType(const ASTPtr & ast)
{
std::string_view name;
if (const auto * data_type = ast->as<ASTDataType>())
name = data_type->name;
else if (const auto * identifier = ast->as<ASTIdentifier>())
name = identifier->name();

if (name == "AggregateFunction")
return true;

for (const auto & child : ast->children)
if (astHasAggregateFunctionType(child))
return true;

return false;
}

}

DataTypePtr resolveClickHouseTypeAnnotation(const String & type_name, bool allow_aggregate_function_states)
{
const auto ast = DataTypeFactory::parse(type_name);

/// Checked before `DataTypeFactory` resolves the aggregate function named by the annotation.
if (!allow_aggregate_function_states && astHasAggregateFunctionType(ast))
return nullptr;

auto type = DataTypeFactory::instance().get(ast);
/// As in stored table metadata, a state without an explicit version has version 0.
setVersionToAggregateFunctions(type, /*if_empty=*/ true);
return type;
}

}
12 changes: 12 additions & 0 deletions src/DataTypes/DataTypeAggregateFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,16 @@ void pinCurrentStateVersionToAggregateFunctions(DataTypePtr & type);
/// Checks type of any nested type is DataTypeAggregateFunction.
bool hasAggregateFunctionType(const DataTypePtr & type);

/// Checks type or any nested type is `AggregateFunction` or `SimpleAggregateFunction`.
bool hasAggregateFunctionOrSimpleAggregateFunctionType(const DataTypePtr & type);

/// The ClickHouse type name recorded for `type` in the metadata of a Parquet file or an Iceberg table,
/// empty if the file or table schema represents the type itself.
String getClickHouseTypeAnnotation(const DataTypePtr & type);

/// Resolves a ClickHouse type name recorded in the metadata of a Parquet file or an Iceberg table.
/// Returns nullptr, without resolving any aggregate function, if the name has an `AggregateFunction`
/// and `allow_aggregate_function_states` is false. A state without an explicit version has version 0.
DataTypePtr resolveClickHouseTypeAnnotation(const String & type_name, bool allow_aggregate_function_states);

}
28 changes: 19 additions & 9 deletions src/DataTypes/DataTypeFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,34 @@ DataTypePtr DataTypeFactory::tryGet(const String & full_name) const
return getImpl<true>(full_name);
}

template <bool nullptr_on_error>
DataTypePtr DataTypeFactory::getImpl(const String & full_name) const
namespace
{
/// Data type parser can be invoked from coroutines with small stack.
/// Value 315 is known to cause stack overflow in some test configurations (debug build, sanitizers)
/// let's make the threshold significantly lower.
/// It is impractical for user to have complex data types with this depth.

/// Data type parser can be invoked from coroutines with small stack.
/// Value 315 is known to cause stack overflow in some test configurations (debug build, sanitizers)
/// let's make the threshold significantly lower.
/// It is impractical for user to have complex data types with this depth.
#if defined(SANITIZER) || !defined(NDEBUG)
static constexpr size_t data_type_max_parse_depth = 150;
constexpr size_t data_type_max_parse_depth = 150;
#else
static constexpr size_t data_type_max_parse_depth = 300;
constexpr size_t data_type_max_parse_depth = 300;
#endif

}

ASTPtr DataTypeFactory::parse(const String & full_name)
{
ParserDataType parser;
return parseQuery(parser, full_name.data(), full_name.data() + full_name.size(), "data type", false, data_type_max_parse_depth, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS);
}

template <bool nullptr_on_error>
DataTypePtr DataTypeFactory::getImpl(const String & full_name) const
{
ASTPtr ast;
if constexpr (nullptr_on_error)
{
ParserDataType parser;
String out_err;
const char * start = full_name.data();
ast = tryParseQuery(parser, start, start + full_name.size(), out_err, false, "data type", false,
Expand All @@ -143,7 +153,7 @@ DataTypePtr DataTypeFactory::getImpl(const String & full_name) const
}
else
{
ast = parseQuery(parser, full_name.data(), full_name.data() + full_name.size(), "data type", false, data_type_max_parse_depth, DBMS_DEFAULT_MAX_PARSER_BACKTRACKS);
ast = parse(full_name);
}

return getImpl<nullptr_on_error>(ast);
Expand Down
2 changes: 2 additions & 0 deletions src/DataTypes/DataTypeFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class DataTypeFactory final : private boost::noncopyable, public IFactoryWithAli
DataTypePtr get(const String & full_name) const;
DataTypePtr get(const String & family_name, const ASTPtr & parameters) const;
DataTypePtr get(const ASTPtr & ast) const;
/// Parses a type name with the nesting depth limit that `get` applies, without resolving it.
static ASTPtr parse(const String & full_name);
DataTypePtr getCustom(DataTypeCustomDescPtr customization) const;
DataTypePtr getCustom(const String & base_name, DataTypeCustomDescPtr customization) const;

Expand Down
29 changes: 29 additions & 0 deletions src/DataTypes/tests/gtest_resolve_clickhouse_type_annotation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <gtest/gtest.h>

#include <Common/tests/gtest_global_register.h>
#include <DataTypes/DataTypeAggregateFunction.h>

using namespace DB;

/// With states disabled, a name with an `AggregateFunction` is not resolved, so its aggregate function is never looked up.
TEST(ResolveClickHouseTypeAnnotation, SkipsOnlyAggregateFunctionStatesWhenDisabled)
{
tryRegisterAggregateFunctions();

for (const auto * name : {
"AggregateFunction(uniq, UInt64)",
"AggregateFunction(0, sumMap, Array(UInt8), Array(UInt32))",
"Array(AggregateFunction(no_such_aggregate_function, UInt64))",
"Map(String, AggregateFunction(uniq, UInt64))",
"Tuple(a UInt64, b Array(AggregateFunction(uniq, UInt64)))"})
EXPECT_EQ(resolveClickHouseTypeAnnotation(name, /*allow_aggregate_function_states=*/ false), nullptr) << name;

for (const auto * name : {
"UInt64",
"SimpleAggregateFunction(sum, UInt64)",
"Array(SimpleAggregateFunction(max, String))",
"Tuple(a UInt64, b Array(Nullable(String)))",
"Enum8('AggregateFunction' = 1)",
"Tuple(`AggregateFunction` String)"})
EXPECT_NE(resolveClickHouseTypeAnnotation(name, /*allow_aggregate_function_states=*/ false), nullptr) << name;
}
1 change: 1 addition & 0 deletions src/Databases/DataLake/GlueCatalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,7 @@ bool GlueCatalog::updateSchema(
Int32 /*previous_schema_id*/,
Int32 /*new_last_column_id*/,
Poco::JSON::Object::Ptr /*metadata*/,
Poco::JSON::Object::Ptr /*previous_properties*/,
const DB::ForwardedAuthTokenPtr & auth_token) const
{
std::vector<Aws::Glue::Model::Column> columns;
Expand Down
1 change: 1 addition & 0 deletions src/Databases/DataLake/GlueCatalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class GlueCatalog final : public ICatalog, private DB::WithContext
Int32 previous_schema_id,
Int32 new_last_column_id,
Poco::JSON::Object::Ptr metadata,
Poco::JSON::Object::Ptr previous_properties,
const DB::ForwardedAuthTokenPtr & auth_token) const override;

void dropTable(
Expand Down
1 change: 1 addition & 0 deletions src/Databases/DataLake/ICatalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ bool ICatalog::updateSchema(
Int32 /*previous_schema_id*/,
Int32 /*new_last_column_id*/,
Poco::JSON::Object::Ptr /*metadata*/,
Poco::JSON::Object::Ptr /*previous_properties*/,
const DB::ForwardedAuthTokenPtr & /*auth_token*/) const
{
throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "updateSchema is not implemented");
Expand Down
2 changes: 2 additions & 0 deletions src/Databases/DataLake/ICatalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class ICatalog
/// `new_schema` is the full new schema object and `previous_schema_id` is the schema id that the
/// catalog is expected to currently have - used to build the optimistic-concurrency requirement
/// (`assert-current-schema-id`) on transactional catalogs (e.g. Iceberg REST).
/// `previous_properties` are the table properties before the change.
/// Returns `false` on a recoverable conflict so the caller can retry, throws otherwise.
virtual bool updateSchema(
const String & namespace_name,
Expand All @@ -374,6 +375,7 @@ class ICatalog
Int32 previous_schema_id,
Int32 new_last_column_id,
Poco::JSON::Object::Ptr metadata,
Poco::JSON::Object::Ptr previous_properties,
const DB::ForwardedAuthTokenPtr & auth_token) const;

/// Drop table from catalog.
Expand Down
Loading
Loading