Skip to content

Commit

Permalink
Use a proper namespace for this project (#46)
Browse files Browse the repository at this point in the history
* Ensure license comment is present in all .cpp/.h source files

* Add flight_sql_fwd.h with a convenient namespace alias

* gitignore: Remove duckdb which is literally a folder under src

This confuses tools that parse .gitignore for other purposes and git
itself sometimes.

* src/duckdb: Use a proper namespace for this project

* src/sqlite: Use a proper namespace for this project

* src: Use a proper namespace for this project

Additionally:
 - Remove the fs namespace alias from headers
 - Wrap most global functions in the namespace
  • Loading branch information
felipecrv authored Jul 19, 2024
1 parent cc7f7fe commit ffe2690
Show file tree
Hide file tree
Showing 31 changed files with 1,315 additions and 1,168 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
arrow
duckdb

# build is out
build
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ set_target_properties(sqlfliteserver PROPERTIES PUBLIC_HEADER ${HEADER_FILES})
target_include_directories(sqlfliteserver PRIVATE
src/sqlite
src/duckdb
src/library/include
${SQLITE_INCLUDE_DIR}
${DUCKDB_INCLUDE_DIR}
${JWT_CPP_INCLUDE_DIR}
Expand Down
435 changes: 228 additions & 207 deletions src/duckdb/duckdb_server.cpp

Large diffs are not rendered by default.

168 changes: 91 additions & 77 deletions src/duckdb/duckdb_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@

#include <arrow/api.h>
#include <arrow/flight/sql/server.h>
#include "flight_sql_fwd.h"

namespace arrow {
namespace flight {
namespace sql {
namespace duckdbflight {
namespace sqlflite::ddb {

/// \brief Convert a column type to a ArrowType.
/// \param duckdb_type the duckdb type.
/// \return The equivalent ArrowType.
std::shared_ptr<DataType> GetArrowType(const char *duckdb_type);
std::shared_ptr<arrow::DataType> GetArrowType(const char *duckdb_type);

/// \brief Example implementation of FlightSqlServerBase backed by an in-memory DuckDB
/// database.
class DuckDBFlightSqlServer : public FlightSqlServerBase {
class DuckDBFlightSqlServer : public flight::sql::FlightSqlServerBase {
public:
~DuckDBFlightSqlServer() override;

Expand All @@ -46,102 +44,121 @@ class DuckDBFlightSqlServer : public FlightSqlServerBase {

/// \brief Auxiliary method used to execute an arbitrary SQL statement on the underlying
/// SQLite database.
Status ExecuteSql(const std::string &sql);
arrow::Status ExecuteSql(const std::string &sql);

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoStatement(
const ServerCallContext &context, const StatementQuery &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoStatement(
const flight::ServerCallContext &context,
const flight::sql::StatementQuery &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetStatement(
const ServerCallContext &context, const StatementQueryTicket &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetStatement(
const flight::ServerCallContext &context,
const flight::sql::StatementQueryTicket &command) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoCatalogs(
const ServerCallContext &context, const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoCatalogs(
const flight::ServerCallContext &context,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetCatalogs(
const ServerCallContext &context) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetCatalogs(
const flight::ServerCallContext &context) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoSchemas(
const ServerCallContext &context, const GetDbSchemas &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoSchemas(
const flight::ServerCallContext &context, const flight::sql::GetDbSchemas &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetDbSchemas(
const ServerCallContext &context, const GetDbSchemas &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetDbSchemas(
const flight::ServerCallContext &context,
const flight::sql::GetDbSchemas &command) override;

arrow::Result<int64_t> DoPutCommandStatementUpdate(
const ServerCallContext &context, const StatementUpdate &update) override;
const flight::ServerCallContext &context,
const flight::sql::StatementUpdate &update) override;

arrow::Result<ActionCreatePreparedStatementResult> CreatePreparedStatement(
const ServerCallContext &context,
const ActionCreatePreparedStatementRequest &request) override;
arrow::Result<flight::sql::ActionCreatePreparedStatementResult> CreatePreparedStatement(
const flight::ServerCallContext &context,
const flight::sql::ActionCreatePreparedStatementRequest &request) override;

Status ClosePreparedStatement(
const ServerCallContext &context,
const ActionClosePreparedStatementRequest &request) override;
arrow::Status ClosePreparedStatement(
const flight::ServerCallContext &context,
const flight::sql::ActionClosePreparedStatementRequest &request) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoPreparedStatement(
const ServerCallContext &context, const PreparedStatementQuery &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoPreparedStatement(
const flight::ServerCallContext &context,
const flight::sql::PreparedStatementQuery &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetPreparedStatement(
const ServerCallContext &context, const PreparedStatementQuery &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetPreparedStatement(
const flight::ServerCallContext &context,
const flight::sql::PreparedStatementQuery &command) override;

Status DoPutPreparedStatementQuery(const ServerCallContext &context,
const PreparedStatementQuery &command,
FlightMessageReader *reader,
FlightMetadataWriter *writer) override;
arrow::Status DoPutPreparedStatementQuery(
const flight::ServerCallContext &context,
const flight::sql::PreparedStatementQuery &command,
flight::FlightMessageReader *reader, flight::FlightMetadataWriter *writer) override;

arrow::Result<int64_t> DoPutPreparedStatementUpdate(
const ServerCallContext &context, const PreparedStatementUpdate &command,
FlightMessageReader *reader) override;
const flight::ServerCallContext &context,
const flight::sql::PreparedStatementUpdate &command,
flight::FlightMessageReader *reader) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoTables(
const ServerCallContext &context, const GetTables &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoTables(
const flight::ServerCallContext &context, const flight::sql::GetTables &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetTables(
const ServerCallContext &context, const GetTables &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetTables(
const flight::ServerCallContext &context,
const flight::sql::GetTables &command) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoTableTypes(
const ServerCallContext &context, const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoTableTypes(
const flight::ServerCallContext &context,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetTableTypes(
const ServerCallContext &context) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetTableTypes(
const flight::ServerCallContext &context) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoImportedKeys(
const ServerCallContext &context, const GetImportedKeys &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoImportedKeys(
const flight::ServerCallContext &context,
const flight::sql::GetImportedKeys &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetImportedKeys(
const ServerCallContext &context, const GetImportedKeys &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetImportedKeys(
const flight::ServerCallContext &context,
const flight::sql::GetImportedKeys &command) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoExportedKeys(
const ServerCallContext &context, const GetExportedKeys &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoExportedKeys(
const flight::ServerCallContext &context,
const flight::sql::GetExportedKeys &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetExportedKeys(
const ServerCallContext &context, const GetExportedKeys &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetExportedKeys(
const flight::ServerCallContext &context,
const flight::sql::GetExportedKeys &command) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoCrossReference(
const ServerCallContext &context, const GetCrossReference &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoCrossReference(
const flight::ServerCallContext &context,
const flight::sql::GetCrossReference &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetCrossReference(
const ServerCallContext &context, const GetCrossReference &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetCrossReference(
const flight::ServerCallContext &context,
const flight::sql::GetCrossReference &command) override;

arrow::Result<std::unique_ptr<FlightInfo>> GetFlightInfoPrimaryKeys(
const ServerCallContext &context, const GetPrimaryKeys &command,
const FlightDescriptor &descriptor) override;
arrow::Result<std::unique_ptr<flight::FlightInfo>> GetFlightInfoPrimaryKeys(
const flight::ServerCallContext &context,
const flight::sql::GetPrimaryKeys &command,
const flight::FlightDescriptor &descriptor) override;

arrow::Result<std::unique_ptr<FlightDataStream>> DoGetPrimaryKeys(
const ServerCallContext &context, const GetPrimaryKeys &command) override;
arrow::Result<std::unique_ptr<flight::FlightDataStream>> DoGetPrimaryKeys(
const flight::ServerCallContext &context,
const flight::sql::GetPrimaryKeys &command) override;

arrow::Result<ActionBeginTransactionResult> BeginTransaction(
const ServerCallContext &context,
const ActionBeginTransactionRequest &request) override;
arrow::Result<flight::sql::ActionBeginTransactionResult> BeginTransaction(
const flight::ServerCallContext &context,
const flight::sql::ActionBeginTransactionRequest &request) override;

Status EndTransaction(const ServerCallContext &context,
const ActionEndTransactionRequest &request) override;
arrow::Status EndTransaction(
const flight::ServerCallContext &context,
const flight::sql::ActionEndTransactionRequest &request) override;

private:
class Impl;
Expand All @@ -151,7 +168,4 @@ class DuckDBFlightSqlServer : public FlightSqlServerBase {
explicit DuckDBFlightSqlServer(std::shared_ptr<Impl> impl);
};

} // namespace duckdbflight
} // namespace sql
} // namespace flight
} // namespace arrow
} // namespace sqlflite::ddb
29 changes: 16 additions & 13 deletions src/duckdb/duckdb_sql_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
// specific language governing permissions and limitations
// under the License.

#include "duckdb_sql_info.h"
#include <cstdint>

#include <duckdb.hpp>
#include <arrow/flight/sql/types.h>
#include "arrow/util/config.h"

namespace arrow {
namespace flight {
namespace sql {
namespace duckdbflight {
#include "duckdb_sql_info.h"
#include "flight_sql_fwd.h"

namespace sql = flight::sql;

namespace sqlflite::ddb {

// clang-format off
/// \brief Gets the mapping from SQL info ids to SqlInfoResult instances.
/// \return the cache.
SqlInfoResultMap GetSqlInfoResultMap() {
sql::SqlInfoResultMap GetSqlInfoResultMap() {
using SqlInfo = sql::SqlInfoOptions::SqlInfo;
using SqlInfoOptions = sql::SqlInfoOptions;
using SqlInfoResult = sql::SqlInfoResult;

return {
{SqlInfoOptions::SqlInfo::FLIGHT_SQL_SERVER_NAME,
SqlInfoResult(std::string("db_name"))},
Expand All @@ -50,17 +56,17 @@ SqlInfoResultMap GetSqlInfoResultMap() {
SqlInfoResult(true)},
{SqlInfoOptions::SqlInfo::SQL_DDL_TABLE, SqlInfoResult(true)},
{SqlInfoOptions::SqlInfo::SQL_IDENTIFIER_CASE,
SqlInfoResult(int64_t(SqlInfoOptions::SqlSupportedCaseSensitivity::
SqlInfoResult(static_cast<int64_t>(SqlInfoOptions::SqlSupportedCaseSensitivity::
SQL_CASE_SENSITIVITY_CASE_INSENSITIVE))},
{SqlInfoOptions::SqlInfo::SQL_IDENTIFIER_QUOTE_CHAR,
SqlInfoResult(std::string("\""))},
{SqlInfoOptions::SqlInfo::SQL_QUOTED_IDENTIFIER_CASE,
SqlInfoResult(int64_t(SqlInfoOptions::SqlSupportedCaseSensitivity::
SqlInfoResult(static_cast<int64_t>(SqlInfoOptions::SqlSupportedCaseSensitivity::
SQL_CASE_SENSITIVITY_CASE_INSENSITIVE))},
{SqlInfoOptions::SqlInfo::SQL_ALL_TABLES_ARE_SELECTABLE, SqlInfoResult(true)},
{SqlInfoOptions::SqlInfo::SQL_NULL_ORDERING,
SqlInfoResult(
int64_t(SqlInfoOptions::SqlNullOrdering::SQL_NULLS_SORTED_AT_END))},
static_cast<int64_t>(SqlInfoOptions::SqlNullOrdering::SQL_NULLS_SORTED_AT_END))},
{SqlInfoOptions::SqlInfo::SQL_KEYWORDS,
SqlInfoResult(std::vector<std::string>({"ABORT_P"
"ABSOLUTE_P"
Expand Down Expand Up @@ -654,7 +660,4 @@ SqlInfoResultMap GetSqlInfoResultMap() {
}
// clang-format on

} // namespace duckdbflight
} // namespace sql
} // namespace flight
} // namespace arrow
} // namespace sqlflite::ddb
13 changes: 4 additions & 9 deletions src/duckdb/duckdb_sql_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
#pragma once

#include <arrow/flight/sql/types.h>
#include "flight_sql_fwd.h"

namespace arrow {
namespace flight {
namespace sql {
namespace duckdbflight {
namespace sqlflite::ddb {

/// \brief Gets the mapping from SQL info ids to SqlInfoResult instances.
/// \return the cache.
SqlInfoResultMap GetSqlInfoResultMap();
flight::sql::SqlInfoResultMap GetSqlInfoResultMap();

} // namespace duckdbflight
} // namespace sql
} // namespace flight
} // namespace arrow
} // namespace sqlflite::ddb
Loading

0 comments on commit ffe2690

Please sign in to comment.