Fix review a little, try add simple test

This commit is contained in:
Arslan G 2021-06-02 03:53:30 +03:00
parent dc0a1001d8
commit 4fe9a24472
6 changed files with 59 additions and 29 deletions

View File

@ -6,6 +6,7 @@
#include <Columns/ColumnString.h>
#include <Columns/ColumnsNumber.h>
#include <Common/assert_cast.h>
#include <DataTypes/DataTypeNullable.h>
#include <ext/range.h>
#include <common/logger_useful.h>
@ -58,34 +59,15 @@ Block SQLiteBlockInputStream::readImpl()
for (const auto idx : ext::range(0, column_count))
{
const auto & sample = description.sample_block.getByPosition(idx);
int column_type = sqlite3_column_type(compiled_statement.get(), idx);
switch (column_type)
if (description.types[idx].second)
{
case SQLITE_INTEGER:
assert_cast<ColumnInt64 &>(*columns[idx]).insertValue(sqlite3_column_int64(compiled_statement.get(), idx));
break;
case SQLITE_FLOAT:
assert_cast<ColumnFloat64 &>(*columns[idx]).insertValue(sqlite3_column_double(compiled_statement.get(), idx));
break;
case SQLITE3_TEXT:
[[fallthrough]];
case SQLITE_BLOB:
[[fallthrough]];
case SQLITE_NULL:
{
const char * data = reinterpret_cast<const char *>(sqlite3_column_text(compiled_statement.get(), idx));
if (!data)
{
(*columns[idx]).insertFrom(*sample.column, 0);
break;
}
int len = sqlite3_column_bytes(compiled_statement.get(), idx);
assert_cast<ColumnString &>(*columns[idx]).insertData(data, len);
break;
}
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[idx]);
insertValue(column_nullable.getNestedColumn(), description.types[idx].first, idx);
column_nullable.getNullMapData().emplace_back(0);
}
else
{
insertValue(*columns[idx], description.types[idx].first, idx);
}
}
@ -98,8 +80,33 @@ Block SQLiteBlockInputStream::readImpl()
void SQLiteBlockInputStream::readSuffix()
{
if (compiled_statement)
{
compiled_statement.reset();
}
void SQLiteBlockInputStream::insertValue(
IColumn & column, const ExternalResultDescription::ValueType type, size_t idx)
{
switch (type)
{
case ValueType::vtUInt8: [[fallthrough]];
case ValueType::vtUInt16: [[fallthrough]];
case ValueType::vtUInt32: [[fallthrough]];
case ValueType::vtUInt64: [[fallthrough]];
case ValueType::vtInt8: [[fallthrough]];
case ValueType::vtInt16: [[fallthrough]];
case ValueType::vtInt32: [[fallthrough]];
case ValueType::vtInt64:
assert_cast<ColumnUInt64 &>(column).insertValue(sqlite3_column_int64(compiled_statement.get(), idx));
break;
case ValueType::vtFloat32: [[fallthrough]];
case ValueType::vtFloat64:
assert_cast<ColumnFloat64 &>(column).insertValue(sqlite3_column_double(compiled_statement.get(), idx));
break;
default:
const char * data = reinterpret_cast<const char *>(sqlite3_column_text(compiled_statement.get(), idx));
int len = sqlite3_column_bytes(compiled_statement.get(), idx);
assert_cast<ColumnString &>(column).insertData(data, len);
break;
}
}

View File

@ -29,6 +29,9 @@ private:
Block readImpl() override;
void readSuffix() override;
void insertValue(IColumn & column,
const ExternalResultDescription::ValueType type, size_t idx);
String query_str;
const UInt64 max_block_size;
ExternalResultDescription description;

View File

@ -229,7 +229,7 @@ DatabasePtr DatabaseFactory::getImpl(const ASTCreateQuery & create, const String
const ASTFunction * engine = engine_define->engine;
if (!engine->arguments || engine->arguments->children.size() != 1)
throw Exception("SQLite database requires 1 arguments: database path", ErrorCodes::BAD_ARGUMENTS);
throw Exception("SQLite database requires 1 argument: database path", ErrorCodes::BAD_ARGUMENTS);
const auto & arguments = engine->arguments->children;

View File

@ -0,0 +1,6 @@
1 hi
2 bye
1
2
hi
bye

View File

@ -0,0 +1,14 @@
DROP DATABASE IF EXISTS sqlite_database;
CREATE DATABASE sqlite_database
ENGINE = SQLite('tests/queries/0_stateless/test.sqlite3');
SELECT * FROM sqlite_database.`Some table`;
SELECT `some field` FROM sqlite_database.`Some table`;
SELECT `string field` FROM sqlite_database.`Some table`;
SELECT * FROM sqlite_database.`Empty table`;
SELECT field2 FROM sqlite_database.`Empty table`;

Binary file not shown.