2021-04-23 12:53:38 +00:00
|
|
|
#include <DataTypes/DataTypeObject.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <DataTypes/Serializations/SerializationObject.h>
|
|
|
|
|
|
|
|
#include <Parsers/IAST.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
2022-02-25 15:07:30 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2021-04-23 12:53:38 +00:00
|
|
|
#include <IO/Operators.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int UNEXPECTED_AST_STRUCTURE;
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:15:44 +00:00
|
|
|
DataTypeObject::DataTypeObject(const String & schema_format_, bool is_nullable_)
|
2021-04-23 12:53:38 +00:00
|
|
|
: schema_format(Poco::toLower(schema_format_))
|
2021-07-23 23:15:44 +00:00
|
|
|
, is_nullable(is_nullable_)
|
2021-04-23 12:53:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataTypeObject::equals(const IDataType & rhs) const
|
|
|
|
{
|
|
|
|
if (const auto * object = typeid_cast<const DataTypeObject *>(&rhs))
|
2022-02-17 19:00:25 +00:00
|
|
|
return schema_format == object->schema_format && is_nullable == object->is_nullable;
|
2021-04-23 12:53:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializationPtr DataTypeObject::doGetDefaultSerialization() const
|
|
|
|
{
|
2022-03-18 14:52:07 +00:00
|
|
|
return getObjectSerialization(schema_format);
|
2021-04-23 12:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String DataTypeObject::doGetName() const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString out;
|
2021-07-23 23:15:44 +00:00
|
|
|
if (is_nullable)
|
2022-02-25 15:07:30 +00:00
|
|
|
out << "Object(Nullable(" << quote << schema_format << "))";
|
|
|
|
else
|
|
|
|
out << "Object(" << quote << schema_format << ")";
|
2021-04-23 12:53:38 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static DataTypePtr create(const ASTPtr & arguments)
|
|
|
|
{
|
2022-02-25 15:07:30 +00:00
|
|
|
if (!arguments || arguments->children.size() != 1)
|
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Object data type family must have one argument - name of schema format");
|
2021-07-23 23:15:44 +00:00
|
|
|
|
2022-02-25 15:07:30 +00:00
|
|
|
ASTPtr schema_argument = arguments->children[0];
|
2021-07-23 23:15:44 +00:00
|
|
|
bool is_nullable = false;
|
2022-02-25 15:07:30 +00:00
|
|
|
|
|
|
|
if (const auto * func = schema_argument->as<ASTFunction>())
|
2021-07-23 23:15:44 +00:00
|
|
|
{
|
2022-02-25 15:07:30 +00:00
|
|
|
if (func->name != "Nullable" || func->arguments->children.size() != 1)
|
2021-07-23 23:15:44 +00:00
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE,
|
2022-02-25 15:07:30 +00:00
|
|
|
"Expected 'Nullable(<schema_name>)' as parameter for type Object", func->name);
|
2021-07-23 23:15:44 +00:00
|
|
|
|
2022-02-25 15:07:30 +00:00
|
|
|
schema_argument = func->arguments->children[0];
|
|
|
|
is_nullable = true;
|
2021-07-23 23:15:44 +00:00
|
|
|
}
|
2021-04-23 12:53:38 +00:00
|
|
|
|
2022-02-25 15:07:30 +00:00
|
|
|
const auto * literal = schema_argument->as<ASTLiteral>();
|
|
|
|
if (!literal || literal->value.getType() != Field::Types::String)
|
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE,
|
|
|
|
"Object data type family must have a const string as its schema name parameter");
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeObject>(literal->value.get<const String &>(), is_nullable);
|
2021-04-23 12:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerDataTypeObject(DataTypeFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerDataType("Object", create);
|
2021-05-27 16:54:27 +00:00
|
|
|
factory.registerSimpleDataType("JSON",
|
2021-07-23 23:15:44 +00:00
|
|
|
[] { return std::make_shared<DataTypeObject>("JSON", false); },
|
2021-05-27 16:54:27 +00:00
|
|
|
DataTypeFactory::CaseInsensitive);
|
2021-04-23 12:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|