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>
|
|
|
|
#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
|
|
|
extern const int BAD_ARGUMENTS;
|
2021-04-23 12:53:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
, default_serialization(getObjectSerialization(schema_format))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataTypeObject::equals(const IDataType & rhs) const
|
|
|
|
{
|
|
|
|
if (const auto * object = typeid_cast<const DataTypeObject *>(&rhs))
|
|
|
|
return schema_format == object->schema_format;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SerializationPtr DataTypeObject::doGetDefaultSerialization() const
|
|
|
|
{
|
|
|
|
return default_serialization;
|
|
|
|
}
|
|
|
|
|
2021-07-23 23:15:44 +00:00
|
|
|
static constexpr auto NAME_DEFAULT = "Default";
|
|
|
|
static constexpr auto NAME_NULL = "Null";
|
|
|
|
|
2021-04-23 12:53:38 +00:00
|
|
|
String DataTypeObject::doGetName() const
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString out;
|
2021-07-23 23:15:44 +00:00
|
|
|
out << "Object(" << quote << schema_format;
|
|
|
|
if (is_nullable)
|
|
|
|
out << ", " << quote << NAME_NULL;
|
|
|
|
out << ")";
|
2021-04-23 12:53:38 +00:00
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static DataTypePtr create(const ASTPtr & arguments)
|
|
|
|
{
|
2021-07-23 23:15:44 +00:00
|
|
|
if (!arguments || arguments->children.size() < 1 || arguments->children.size() > 2)
|
|
|
|
throw Exception("Object data type family must have one or two arguments -"
|
|
|
|
" name of schema format and type of default value",
|
2021-04-23 12:53:38 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2021-07-23 23:15:44 +00:00
|
|
|
const auto * literal = arguments->children[0]->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 first argument");
|
|
|
|
|
|
|
|
bool is_nullable = false;
|
|
|
|
if (arguments->children.size() == 2)
|
|
|
|
{
|
|
|
|
const auto * default_literal = arguments->children[1]->as<ASTLiteral>();
|
|
|
|
if (!default_literal || default_literal->value.getType() != Field::Types::String)
|
|
|
|
throw Exception(ErrorCodes::UNEXPECTED_AST_STRUCTURE,
|
|
|
|
"Object data type family must have a const string as its second argument");
|
|
|
|
|
|
|
|
const auto & default_kind = default_literal->value.get<const String &>();
|
|
|
|
if (default_kind == NAME_NULL)
|
|
|
|
is_nullable = true;
|
|
|
|
else if (default_kind != NAME_DEFAULT)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Unexpected type of default value '{}'. Should be {} or {}",
|
|
|
|
default_kind, NAME_DEFAULT, NAME_NULL);
|
|
|
|
}
|
2021-04-23 12:53:38 +00:00
|
|
|
|
2021-07-23 23:15:44 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|