Added test for JSONEachRow format. [#METR-22801]

This commit is contained in:
Vitaliy Lyudvichenko 2016-09-28 21:45:28 +03:00
parent 3c045e1545
commit f3d70eb87c
5 changed files with 25 additions and 9 deletions

View File

@ -14,6 +14,7 @@
#include <common/LocalDateTime.h>
#include <DB/Core/Types.h>
#include <DB/Core/StringRef.h>
#include <DB/Common/Exception.h>
#include <DB/Common/StringUtils.h>
@ -811,7 +812,7 @@ inline void skipWhitespaceIfAny(ReadBuffer & buf)
}
/// Skips json value. If the value contains objects (i.e. {...} sequence), an exception will be thrown.
void skipJSONFieldPlain(ReadBuffer & buf, const String & name_of_filed = "");
void skipJSONFieldPlain(ReadBuffer & buf, const StringRef & name_of_filed);
/** Прочитать сериализованный эксепшен.

View File

@ -106,7 +106,7 @@ bool JSONEachRowRowInputStream::read(Block & block)
throw Exception("Unknown field found while parsing JSONEachRow format: " + name_ref.toString(), ErrorCodes::INCORRECT_DATA);
skipColonDelimeter(istr);
skipJSONFieldPlain(istr, name_ref.toString());
skipJSONFieldPlain(istr, name_ref);
continue;
}

View File

@ -572,20 +572,20 @@ void readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf)
}
void skipJSONFieldPlain(ReadBuffer & buf, const String & name_of_filed)
void skipJSONFieldPlain(ReadBuffer & buf, const StringRef & name_of_filed)
{
if (buf.eof())
throw Exception("Unexpected EOF for key '" + name_of_filed + "'", ErrorCodes::INCORRECT_DATA);
throw Exception("Unexpected EOF for key '" + name_of_filed.toString() + "'", ErrorCodes::INCORRECT_DATA);
else if (*buf.position() == '"') /// skip double-quoted string
{
NullSink sink;
readJSONStringInto(sink, buf);
}
else if (isdigit(*buf.position())) /// skip number
else if (isNumericASCII(*buf.position())) /// skip number
{
double v;
if (!tryReadFloatText(v, buf))
throw Exception("Expected a number field for key '" + name_of_filed + "'", ErrorCodes::INCORRECT_DATA);
throw Exception("Expected a number field for key '" + name_of_filed.toString() + "'", ErrorCodes::INCORRECT_DATA);
}
else if (*buf.position() == 'n') /// skip null
{
@ -626,16 +626,16 @@ void skipJSONFieldPlain(ReadBuffer & buf, const String & name_of_filed)
break;
}
else
throw Exception("Unexpected symbol for key '" + name_of_filed + "'", ErrorCodes::INCORRECT_DATA);
throw Exception("Unexpected symbol for key '" + name_of_filed.toString() + "'", ErrorCodes::INCORRECT_DATA);
}
}
else if (*buf.position() == '{') /// fail on objects
{
throw Exception("Unexpected nested field for key '" + name_of_filed + "'", ErrorCodes::INCORRECT_DATA);
throw Exception("Unexpected nested field for key '" + name_of_filed.toString() + "'", ErrorCodes::INCORRECT_DATA);
}
else
{
throw Exception("Unexpected symbol for key '" + name_of_filed + "'", ErrorCodes::INCORRECT_DATA);
throw Exception("Unexpected symbol for key '" + name_of_filed.toString() + "'", ErrorCodes::INCORRECT_DATA);
}
}

View File

@ -0,0 +1,5 @@
1 ok
0
0
1 ok
1 ok

View File

@ -0,0 +1,10 @@
clickhouse-client -n --query "DROP TABLE IF EXISTS test.json_noisy; CREATE TABLE test.json_noisy (d1 UInt8, d2 String) ENGINE = Memory"
echo '{"d1" : 1, "d2" : "ok"}
{ }
{"t1" : 0, "t2":true,"t3":false, "t4":null,"t5":[],"t6":"trash" }
{"d2":"ok","t1":[[[]],true, null, false, "1","2",9.03,101], "t2":[["1","2"]], "d1":"1"}
{"d2":"ok","t1":[[[]],true, null, false, "1","2", 0.03, 1], "d1":"1", "t2":["1","2"]}' \
| clickhouse-client -n --query "SET input_format_skip_unknown_fields = 1; INSERT INTO test.json_noisy FORMAT JSONEachRow"
clickhouse-client -n --query "SELECT * FROM test.json_noisy; DROP TABLE IF EXISTS test.json_noisy;"