Added support for SQL-style quoting [#CLICKHOUSE-20].

This commit is contained in:
Alexey Milovidov 2017-06-25 06:43:37 +03:00
parent 275a1d4651
commit dbc61a6a43
11 changed files with 71 additions and 37 deletions

View File

@ -21,33 +21,33 @@ void NamesAndTypesList::readText(ReadBuffer & buf)
{
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
DB::assertString("columns format version: 1\n", buf);
assertString("columns format version: 1\n", buf);
size_t count;
DB::readText(count, buf);
DB::assertString(" columns:\n", buf);
assertString(" columns:\n", buf);
resize(count);
for (NameAndTypePair & it : *this)
{
DB::readBackQuotedString(it.name, buf);
DB::assertChar(' ', buf);
readBackQuotedStringWithSQLStyle(it.name, buf);
assertChar(' ', buf);
String type_name;
DB::readString(type_name, buf);
readString(type_name, buf);
it.type = data_type_factory.get(type_name);
DB::assertChar('\n', buf);
assertChar('\n', buf);
}
}
void NamesAndTypesList::writeText(WriteBuffer & buf) const
{
DB::writeString("columns format version: 1\n", buf);
writeString("columns format version: 1\n", buf);
DB::writeText(size(), buf);
DB::writeString(" columns:\n", buf);
writeString(" columns:\n", buf);
for (const auto & it : *this)
{
DB::writeBackQuotedString(it.name, buf);
DB::writeChar(' ', buf);
DB::writeString(it.type->getName(), buf);
DB::writeChar('\n', buf);
writeBackQuotedString(it.name, buf);
writeChar(' ', buf);
writeString(it.type->getName(), buf);
writeChar('\n', buf);
}
}

View File

@ -190,7 +190,7 @@ void DataTypeAggregateFunction::serializeTextQuoted(const IColumn & column, size
void DataTypeAggregateFunction::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const
{
String s;
readQuotedString(s, istr);
readQuotedStringWithSQLStyle(s, istr);
deserializeFromString(function, column, s);
}

View File

@ -166,7 +166,7 @@ template <typename Type>
void DataTypeEnum<Type>::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const
{
std::string name;
readQuotedString(name, istr);
readQuotedStringWithSQLStyle(name, istr);
static_cast<ColumnType &>(column).getData().push_back(getValue(StringRef(name)));
}

View File

@ -155,7 +155,7 @@ void DataTypeFixedString::serializeTextQuoted(const IColumn & column, size_t row
void DataTypeFixedString::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const
{
read(*this, column, [&istr](ColumnFixedString::Chars_t & data) { readQuotedStringInto(data, istr); });
read(*this, column, [&istr](ColumnFixedString::Chars_t & data) { readQuotedStringInto<true>(data, istr); });
}

View File

@ -262,7 +262,7 @@ void DataTypeString::serializeTextQuoted(const IColumn & column, size_t row_num,
void DataTypeString::deserializeTextQuoted(IColumn & column, ReadBuffer & istr) const
{
read(column, istr, [&](ColumnString::Chars_t & data) { readQuotedStringInto(data, istr); });
read(column, istr, [&](ColumnString::Chars_t & data) { readQuotedStringInto<true>(data, istr); });
}

View File

@ -357,7 +357,13 @@ template void readEscapedStringInto<PaddedPODArray<UInt8>>(PaddedPODArray<UInt8>
template void readEscapedStringInto<NullSink>(NullSink & s, ReadBuffer & buf);
template <char quote, typename Vector>
/** If enable_sql_style_quoting == true,
* strings like 'abc''def' will be parsed as abc'def.
* Please note, that even with SQL style quoting enabled,
* backslash escape sequences are also parsed,
* that could be slightly confusing.
*/
template <char quote, bool enable_sql_style_quoting, typename Vector>
static void readAnyQuotedStringInto(Vector & s, ReadBuffer & buf)
{
if (buf.eof() || *buf.position() != quote)
@ -378,6 +384,14 @@ static void readAnyQuotedStringInto(Vector & s, ReadBuffer & buf)
if (*buf.position() == quote)
{
++buf.position();
if (enable_sql_style_quoting && !buf.eof() && *buf.position() == quote)
{
s.push_back(quote);
++buf.position();
continue;
}
return;
}
@ -389,49 +403,64 @@ static void readAnyQuotedStringInto(Vector & s, ReadBuffer & buf)
ErrorCodes::CANNOT_PARSE_QUOTED_STRING);
}
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readQuotedStringInto(Vector & s, ReadBuffer & buf)
{
readAnyQuotedStringInto<'\''>(s, buf);
readAnyQuotedStringInto<'\'', enable_sql_style_quoting>(s, buf);
}
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readDoubleQuotedStringInto(Vector & s, ReadBuffer & buf)
{
readAnyQuotedStringInto<'"'>(s, buf);
readAnyQuotedStringInto<'"', enable_sql_style_quoting>(s, buf);
}
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readBackQuotedStringInto(Vector & s, ReadBuffer & buf)
{
readAnyQuotedStringInto<'`'>(s, buf);
readAnyQuotedStringInto<'`', enable_sql_style_quoting>(s, buf);
}
void readQuotedString(String & s, ReadBuffer & buf)
{
s.clear();
readQuotedStringInto(s, buf);
readQuotedStringInto<false>(s, buf);
}
template void readQuotedStringInto<PaddedPODArray<UInt8>>(PaddedPODArray<UInt8> & s, ReadBuffer & buf);
template void readDoubleQuotedStringInto(NullSink & s, ReadBuffer & buf);
void readQuotedStringWithSQLStyle(String & s, ReadBuffer & buf)
{
s.clear();
readQuotedStringInto<true>(s, buf);
}
template void readQuotedStringInto<true>(PaddedPODArray<UInt8> & s, ReadBuffer & buf);
template void readDoubleQuotedStringInto<false>(NullSink & s, ReadBuffer & buf);
void readDoubleQuotedString(String & s, ReadBuffer & buf)
{
s.clear();
readDoubleQuotedStringInto(s, buf);
readDoubleQuotedStringInto<false>(s, buf);
}
template void readDoubleQuotedStringInto<PaddedPODArray<UInt8>>(PaddedPODArray<UInt8> & s, ReadBuffer & buf);
void readDoubleQuotedStringWithSQLStyle(String & s, ReadBuffer & buf)
{
s.clear();
readDoubleQuotedStringInto<true>(s, buf);
}
void readBackQuotedString(String & s, ReadBuffer & buf)
{
s.clear();
readBackQuotedStringInto(s, buf);
readBackQuotedStringInto<false>(s, buf);
}
template void readBackQuotedStringInto<PaddedPODArray<UInt8>>(PaddedPODArray<UInt8> & s, ReadBuffer & buf);
void readBackQuotedStringWithSQLStyle(String & s, ReadBuffer & buf)
{
s.clear();
readBackQuotedStringInto<true>(s, buf);
}
template <typename Vector>

View File

@ -495,12 +495,15 @@ void readString(String & s, ReadBuffer & buf);
void readEscapedString(String & s, ReadBuffer & buf);
void readQuotedString(String & s, ReadBuffer & buf);
void readQuotedStringWithSQLStyle(String & s, ReadBuffer & buf);
void readDoubleQuotedString(String & s, ReadBuffer & buf);
void readDoubleQuotedStringWithSQLStyle(String & s, ReadBuffer & buf);
void readJSONString(String & s, ReadBuffer & buf);
void readBackQuotedString(String & s, ReadBuffer & buf);
void readBackQuotedStringWithSQLStyle(String & s, ReadBuffer & buf);
void readStringUntilEOF(String & s, ReadBuffer & buf);
@ -526,13 +529,13 @@ void readStringInto(Vector & s, ReadBuffer & buf);
template <typename Vector>
void readEscapedStringInto(Vector & s, ReadBuffer & buf);
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readQuotedStringInto(Vector & s, ReadBuffer & buf);
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readDoubleQuotedStringInto(Vector & s, ReadBuffer & buf);
template <typename Vector>
template <bool enable_sql_style_quoting, typename Vector>
void readBackQuotedStringInto(Vector & s, ReadBuffer & buf);
template <typename Vector>

View File

@ -144,9 +144,9 @@ bool ParserIdentifier::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_pa
String s;
if (*pos == '`')
readBackQuotedString(s, buf);
readBackQuotedStringWithSQLStyle(s, buf);
else
readDoubleQuotedString(s, buf);
readDoubleQuotedStringWithSQLStyle(s, buf);
if (s.empty()) /// Identifiers "empty string" are not allowed.
return false;
@ -532,7 +532,7 @@ bool ParserStringLiteral::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max
try
{
readQuotedString(s, in);
readQuotedStringWithSQLStyle(s, in);
}
catch (const Exception & e)
{

View File

@ -78,7 +78,7 @@ ColumnsDescription<true> ColumnsDescription<true>::parse(const String & str)
for (size_t i = 0; i < count; ++i)
{
String column_name;
readBackQuotedString(column_name, buf);
readBackQuotedStringWithSQLStyle(column_name, buf);
assertChar(' ', buf);
String type_name;

View File

@ -0,0 +1 @@
0 hello\'world

View File

@ -0,0 +1 @@
SELECT "ta`ble".dummy, 'hello''world' AS "hel""lo" FROM system.one AS `ta``ble`;