Fixed SQLiteSource

This commit is contained in:
Maksim Kita 2021-08-13 00:58:24 +03:00
parent 124a87684f
commit d9a59370d3
6 changed files with 39 additions and 36 deletions

View File

@ -33,12 +33,7 @@ SQLiteSource::SQLiteSource(
, sqlite_db(std::move(sqlite_db_)) , sqlite_db(std::move(sqlite_db_))
{ {
description.init(sample_block); description.init(sample_block);
}
Chunk SQLiteSource::generate()
{
if (!compiled_statement)
{
sqlite3_stmt * compiled_stmt = nullptr; sqlite3_stmt * compiled_stmt = nullptr;
int status = sqlite3_prepare_v2(sqlite_db.get(), query_str.c_str(), query_str.size() + 1, &compiled_stmt, nullptr); int status = sqlite3_prepare_v2(sqlite_db.get(), query_str.c_str(), query_str.size() + 1, &compiled_stmt, nullptr);
@ -50,6 +45,11 @@ Chunk SQLiteSource::generate()
compiled_statement = std::unique_ptr<sqlite3_stmt, StatementDeleter>(compiled_stmt, StatementDeleter()); compiled_statement = std::unique_ptr<sqlite3_stmt, StatementDeleter>(compiled_stmt, StatementDeleter());
} }
Chunk SQLiteSource::generate()
{
if (!compiled_statement)
return {};
MutableColumns columns = description.sample_block.cloneEmptyColumns(); MutableColumns columns = description.sample_block.cloneEmptyColumns();
size_t num_rows = 0; size_t num_rows = 0;
@ -74,25 +74,25 @@ Chunk SQLiteSource::generate()
} }
int column_count = sqlite3_column_count(compiled_statement.get()); int column_count = sqlite3_column_count(compiled_statement.get());
for (const auto idx : collections::range(0, column_count))
{
const auto & sample = description.sample_block.getByPosition(idx);
if (sqlite3_column_type(compiled_statement.get(), idx) == SQLITE_NULL) for (int column_index = 0; column_index < column_count; ++column_index)
{ {
insertDefaultSQLiteValue(*columns[idx], *sample.column); if (sqlite3_column_type(compiled_statement.get(), column_index) == SQLITE_NULL)
{
columns[column_index]->insertDefault();
continue; continue;
} }
if (description.types[idx].second) auto & [type, is_nullable] = description.types[column_index];
if (is_nullable)
{ {
ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[idx]); ColumnNullable & column_nullable = assert_cast<ColumnNullable &>(*columns[column_index]);
insertValue(column_nullable.getNestedColumn(), description.types[idx].first, idx); insertValue(column_nullable.getNestedColumn(), type, column_index);
column_nullable.getNullMapData().emplace_back(0); column_nullable.getNullMapData().emplace_back(0);
} }
else else
{ {
insertValue(*columns[idx], description.types[idx].first, idx); insertValue(*columns[column_index], type, column_index);
} }
} }
@ -100,6 +100,12 @@ Chunk SQLiteSource::generate()
break; break;
} }
if (num_rows == 0)
{
compiled_statement.reset();
return {};
}
return Chunk(std::move(columns), num_rows); return Chunk(std::move(columns), num_rows);
} }

View File

@ -13,8 +13,10 @@
namespace DB namespace DB
{ {
class SQLiteSource : public SourceWithProgress class SQLiteSource : public SourceWithProgress
{ {
using SQLitePtr = std::shared_ptr<sqlite3>; using SQLitePtr = std::shared_ptr<sqlite3>;
public: public:
@ -26,10 +28,6 @@ public:
String getName() const override { return "SQLite"; } String getName() const override { return "SQLite"; }
private: private:
static void insertDefaultSQLiteValue(IColumn & column, const IColumn & sample_column)
{
column.insertFrom(sample_column, 0);
}
using ValueType = ExternalResultDescription::ValueType; using ValueType = ExternalResultDescription::ValueType;
@ -46,7 +44,6 @@ private:
UInt64 max_block_size; UInt64 max_block_size;
ExternalResultDescription description; ExternalResultDescription description;
SQLitePtr sqlite_db; SQLitePtr sqlite_db;
std::unique_ptr<sqlite3_stmt, StatementDeleter> compiled_statement; std::unique_ptr<sqlite3_stmt, StatementDeleter> compiled_statement;
}; };

View File

@ -31,7 +31,7 @@ void registerDictionarySourceRedis(DictionarySourceFactory & factory)
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include "RedisBlockInputStream.h" #include "RedisSource.h"
namespace DB namespace DB

View File

@ -1,4 +1,4 @@
#include "RedisBlockInputStream.h" #include "RedisSource.h"
#include <string> #include <string>
#include <vector> #include <vector>

View File

@ -26,9 +26,9 @@ SRCS(
CassandraHelpers.cpp CassandraHelpers.cpp
CassandraSource.cpp CassandraSource.cpp
ClickHouseDictionarySource.cpp ClickHouseDictionarySource.cpp
DictionaryBlockInputStream.cpp
DictionaryBlockInputStreamBase.cpp
DictionaryFactory.cpp DictionaryFactory.cpp
DictionarySource.cpp
DictionarySourceBase.cpp
DictionarySourceFactory.cpp DictionarySourceFactory.cpp
DictionarySourceHelpers.cpp DictionarySourceHelpers.cpp
DictionaryStructure.cpp DictionaryStructure.cpp
@ -57,8 +57,8 @@ SRCS(
PolygonDictionaryImplementations.cpp PolygonDictionaryImplementations.cpp
PolygonDictionaryUtils.cpp PolygonDictionaryUtils.cpp
RangeHashedDictionary.cpp RangeHashedDictionary.cpp
RedisBlockInputStream.cpp
RedisDictionarySource.cpp RedisDictionarySource.cpp
RedisSource.cpp
XDBCDictionarySource.cpp XDBCDictionarySource.cpp
getDictionaryConfigurationFromAST.cpp getDictionaryConfigurationFromAST.cpp
readInvalidateQuery.cpp readInvalidateQuery.cpp