2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Interpreters/convertFieldToType.h>
|
2017-07-12 02:40:28 +00:00
|
|
|
#include <Parsers/TokenIterator.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <DataStreams/ValuesRowInputStream.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Block.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_PARSE_INPUT_ASSERTION_FAILED;
|
|
|
|
extern const int CANNOT_PARSE_QUOTED_STRING;
|
2018-01-14 00:12:23 +00:00
|
|
|
extern const int CANNOT_PARSE_NUMBER;
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_PARSE_DATE;
|
|
|
|
extern const int CANNOT_PARSE_DATETIME;
|
|
|
|
extern const int CANNOT_READ_ARRAY_FROM_TEXT;
|
|
|
|
extern const int CANNOT_PARSE_DATE;
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
extern const int VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE;
|
2016-02-13 06:37:19 +00:00
|
|
|
}
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2016-02-13 06:37:19 +00:00
|
|
|
|
2018-06-08 01:51:55 +00:00
|
|
|
ValuesRowInputStream::ValuesRowInputStream(ReadBuffer & istr_, const Block & header_, const Context & context_, const FormatSettings & format_settings)
|
|
|
|
: istr(istr_), header(header_), context(context_), format_settings(format_settings)
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// In this format, BOM at beginning of stream cannot be confused with value, so it is safe to skip it.
|
|
|
|
skipBOMIfExists(istr);
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-14 20:58:18 +00:00
|
|
|
bool ValuesRowInputStream::read(MutableColumns & columns)
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
2017-12-14 20:58:18 +00:00
|
|
|
size_t num_columns = columns.size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
skipWhitespaceIfAny(istr);
|
|
|
|
|
|
|
|
if (istr.eof() || *istr.position() == ';')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/** Typically, this is the usual format for streaming parsing.
|
|
|
|
* But as an exception, it also supports processing arbitrary expressions instead of values.
|
|
|
|
* This is very inefficient. But if there are no expressions, then there is no overhead.
|
|
|
|
*/
|
2017-07-12 02:41:41 +00:00
|
|
|
ParserExpression parser;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
assertChar('(', istr);
|
|
|
|
|
2017-12-14 20:58:18 +00:00
|
|
|
for (size_t i = 0; i < num_columns; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
skipWhitespaceIfAny(istr);
|
|
|
|
|
|
|
|
char * prev_istr_position = istr.position();
|
|
|
|
size_t prev_istr_bytes = istr.count() - istr.offset();
|
|
|
|
|
|
|
|
bool rollback_on_exception = false;
|
|
|
|
try
|
|
|
|
{
|
2018-06-08 01:51:55 +00:00
|
|
|
header.getByPosition(i).type->deserializeTextQuoted(*columns[i], istr, format_settings);
|
2017-04-01 07:20:54 +00:00
|
|
|
rollback_on_exception = true;
|
|
|
|
skipWhitespaceIfAny(istr);
|
|
|
|
|
2017-12-14 20:58:18 +00:00
|
|
|
if (i != num_columns - 1)
|
2017-04-01 07:20:54 +00:00
|
|
|
assertChar(',', istr);
|
|
|
|
else
|
|
|
|
assertChar(')', istr);
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
2018-06-08 01:51:55 +00:00
|
|
|
if (!format_settings.values.interpret_expressions)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw;
|
|
|
|
|
|
|
|
/** The normal streaming parser could not parse the value.
|
|
|
|
* Let's try to parse it with a SQL parser as a constant expression.
|
|
|
|
* This is an exceptional case.
|
|
|
|
*/
|
|
|
|
if (e.code() == ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED
|
|
|
|
|| e.code() == ErrorCodes::CANNOT_PARSE_QUOTED_STRING
|
2018-01-14 00:12:23 +00:00
|
|
|
|| e.code() == ErrorCodes::CANNOT_PARSE_NUMBER
|
2017-04-01 07:20:54 +00:00
|
|
|
|| e.code() == ErrorCodes::CANNOT_PARSE_DATE
|
|
|
|
|| e.code() == ErrorCodes::CANNOT_PARSE_DATETIME
|
|
|
|
|| e.code() == ErrorCodes::CANNOT_READ_ARRAY_FROM_TEXT)
|
|
|
|
{
|
2017-07-12 02:40:28 +00:00
|
|
|
/// TODO Case when the expression does not fit entirely in the buffer.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// If the beginning of the value is no longer in the buffer.
|
|
|
|
if (istr.count() - istr.offset() != prev_istr_bytes)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
if (rollback_on_exception)
|
2017-12-14 20:58:18 +00:00
|
|
|
columns[i]->popBack(1);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-18 01:11:48 +00:00
|
|
|
const IDataType & type = *header.getByPosition(i).type;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-13 04:20:56 +00:00
|
|
|
Expected expected;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
Tokens tokens(prev_istr_position, istr.buffer().end());
|
|
|
|
TokenIterator token_iterator(tokens);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ASTPtr ast;
|
2017-07-12 02:40:28 +00:00
|
|
|
if (!parser.parse(token_iterator, ast, expected))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Cannot parse expression of type " + type.getName() + " here: "
|
|
|
|
+ String(prev_istr_position, std::min(SHOW_CHARS_ON_SYNTAX_ERROR, istr.buffer().end() - prev_istr_position)),
|
|
|
|
ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
2017-07-12 02:40:28 +00:00
|
|
|
istr.position() = const_cast<char *>(token_iterator->begin);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::pair<Field, DataTypePtr> value_raw = evaluateConstantExpression(ast, context);
|
|
|
|
Field value = convertFieldToType(value_raw.first, type, value_raw.second.get());
|
|
|
|
|
|
|
|
if (value.isNull())
|
|
|
|
{
|
|
|
|
/// Check that we are indeed allowed to insert a NULL.
|
2017-07-16 03:05:40 +00:00
|
|
|
if (!type.isNullable())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception{"Expression returns value " + applyVisitor(FieldVisitorToString(), value)
|
|
|
|
+ ", that is out of range of type " + type.getName()
|
|
|
|
+ ", at: " + String(prev_istr_position, std::min(SHOW_CHARS_ON_SYNTAX_ERROR, istr.buffer().end() - prev_istr_position)),
|
|
|
|
ErrorCodes::VALUE_IS_OUT_OF_RANGE_OF_DATA_TYPE};
|
|
|
|
}
|
|
|
|
|
2017-12-14 20:58:18 +00:00
|
|
|
columns[i]->insert(value);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
skipWhitespaceIfAny(istr);
|
|
|
|
|
2017-12-14 20:58:18 +00:00
|
|
|
if (i != num_columns - 1)
|
2017-04-01 07:20:54 +00:00
|
|
|
assertChar(',', istr);
|
|
|
|
else
|
|
|
|
assertChar(')', istr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
skipWhitespaceIfAny(istr);
|
|
|
|
if (!istr.eof() && *istr.position() == ',')
|
|
|
|
++istr.position();
|
|
|
|
|
|
|
|
return true;
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|