Skip to content

Commit dcfa218

Browse files
committed
Draft enable ODBC global setup/teardown
1 parent 6145eda commit dcfa218

File tree

5 files changed

+134
-6
lines changed

5 files changed

+134
-6
lines changed

cpp/src/arrow/compute/registry.cc

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,31 @@
3030
#include "arrow/util/config.h" // For ARROW_COMPUTE
3131
#include "arrow/util/logging.h"
3232

33+
#include <iostream> // -AL- TEMP remove later
34+
3335
namespace arrow {
3436
namespace compute {
3537

3638
class FunctionRegistry::FunctionRegistryImpl {
3739
public:
3840
explicit FunctionRegistryImpl(FunctionRegistryImpl* parent = NULLPTR)
39-
: parent_(parent) {}
40-
~FunctionRegistryImpl() {}
41+
: parent_(parent) {// -AL- TEMP remove later
42+
std::cout << "-AL- FunctionRegistryImpl constructor called\n";
43+
}
44+
~FunctionRegistryImpl() { // -AL- TEMP remove later
45+
size_t name_size = name_to_function_.size();
46+
std::cout << "-AL- ~FunctionRegistryImpl name_to_function_ size:" << name_size <<
47+
"\n";
48+
49+
//auto it = name_to_function_.find(std::string("split_pattern"));
50+
//auto func = it->second;
51+
//for (auto& kv : name_to_function_) {
52+
// kv.second.reset();
53+
//}
54+
//name_to_function_.clear();
55+
56+
std::cout << "-AL- ~FunctionRegistryImpl destructor called\n";
57+
}
4158

4259
Status CanAddFunction(std::shared_ptr<Function> function, bool allow_overwrite) {
4360
if (parent_ != NULLPTR) {
@@ -127,6 +144,9 @@ class FunctionRegistry::FunctionRegistryImpl {
127144

128145
const Function* cast_function() { return cast_function_; }
129146

147+
void ClearFunctioRegistry() { name_to_function_.clear();
148+
}
149+
130150
private:
131151
// must not acquire mutex
132152
Status CanAddFunctionName(const std::string& name, bool allow_overwrite) {
@@ -215,6 +235,7 @@ class FunctionRegistry::FunctionRegistryImpl {
215235
};
216236

217237
std::unique_ptr<FunctionRegistry> FunctionRegistry::Make() {
238+
std::cout << "-AL- Make is called once TEMP \n";
218239
return std::unique_ptr<FunctionRegistry>(new FunctionRegistry());
219240
}
220241

@@ -277,9 +298,14 @@ int FunctionRegistry::num_functions() const { return impl_->num_functions(); }
277298

278299
const Function* FunctionRegistry::cast_function() const { return impl_->cast_function(); }
279300

301+
void FunctionRegistry::ClearFunctioRegistry() {
302+
impl_->ClearFunctioRegistry();
303+
}
304+
280305
namespace internal {
281306

282307
static std::unique_ptr<FunctionRegistry> CreateBuiltInRegistry() {
308+
std::cout << "-AL- CreateBuiltInRegistry is called once TEMP \n";
283309
auto registry = FunctionRegistry::Make();
284310

285311
// Register core kernels

cpp/src/arrow/compute/registry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class ARROW_EXPORT FunctionRegistry {
111111
///
112112
/// Helpful for get cast function as needed.
113113
const Function* cast_function() const;
114+
115+
// -AL- todo docs?
116+
void ClearFunctioRegistry();
114117

115118
private:
116119
FunctionRegistry();

cpp/src/arrow/flight/sql/odbc/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_arrow_test(flight_sql_odbc_test
3434
SOURCES
3535
odbc_test_suite.cc
3636
odbc_test_suite.h
37+
odbc_test_util.h
3738
connection_test.cc
3839
# Enable Protobuf cleanup after test execution
3940
# GH-46889: move protobuf_test_util to a more common location

cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h"
2121

2222
#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h"
23+
#include "arrow/flight/sql/odbc/tests/odbc_test_util.h"
2324

2425
// For DSN registration
2526
#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h"
@@ -28,6 +29,13 @@
2829

2930
namespace arrow::flight::sql::odbc {
3031

32+
// -AL- todo rename later
33+
auto alinatest_env = ::testing::AddGlobalTestEnvironment(new AlinaTestEnvironment);
34+
35+
AlinaTestEnvironment* GetAlinaTestEnv() {
36+
return ::arrow::internal::checked_cast<AlinaTestEnvironment*>(alinatest_env);
37+
}
38+
3139
void ODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) {
3240
// Allocate an environment handle
3341
ASSERT_EQ(SQL_SUCCESS, SQLAllocEnv(&env));
@@ -54,12 +62,16 @@ void ODBCRemoteTestBase::ConnectWithString(std::string connect_str) {
5462
SQLWCHAR out_str[kOdbcBufferSize];
5563
SQLSMALLINT out_str_len;
5664

65+
// -AL- once local conn handles are removed,can rename back to conn
66+
SQLHDBC global_conn = GetAlinaTestEnv()->getConnHandle();
67+
//SQLHDBC global_conn = conn; //-AL- TEMP
68+
5769
// Connecting to ODBC server.
5870
ASSERT_EQ(SQL_SUCCESS,
59-
SQLDriverConnect(conn, NULL, &connect_str0[0],
71+
SQLDriverConnect(global_conn, NULL, &connect_str0[0],
6072
static_cast<SQLSMALLINT>(connect_str0.size()), out_str,
6173
kOdbcBufferSize, &out_str_len, SQL_DRIVER_NOPROMPT))
62-
<< GetOdbcErrorMessage(SQL_HANDLE_DBC, conn);
74+
<< GetOdbcErrorMessage(SQL_HANDLE_DBC, global_conn);
6375

6476
// GH-47710: TODO Allocate a statement using alloc handle
6577
// ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt));
@@ -69,9 +81,12 @@ void ODBCRemoteTestBase::Disconnect() {
6981
// GH-47710: TODO Close statement
7082
// EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_STMT, stmt));
7183

84+
// -AL- once local conn handles are removed,can rename back to conn
85+
SQLHDBC global_conn = GetAlinaTestEnv()->getConnHandle();
86+
7287
// Disconnect from ODBC
73-
EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(conn))
74-
<< GetOdbcErrorMessage(SQL_HANDLE_DBC, conn);
88+
EXPECT_EQ(SQL_SUCCESS, SQLDisconnect(global_conn))
89+
<< GetOdbcErrorMessage(SQL_HANDLE_DBC, global_conn);
7590

7691
FreeEnvConnHandles();
7792
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include <gtest/gtest.h>
19+
20+
#include "arrow/flight/sql/odbc/odbc_impl/platform.h"
21+
22+
#include "arrow/compute/api.h" // -AL- attempt to keep registry alive
23+
24+
#include <sql.h>
25+
#include <sqlucode.h>
26+
27+
#include <iostream>
28+
29+
namespace arrow::flight::sql::odbc {
30+
31+
// -AL- todo update to actual ODBC allocation
32+
class AlinaTestEnvironment : public ::testing::Environment {
33+
public:
34+
void SetUp() override {
35+
int x = 1;
36+
// -AL- todo add env_v2 for v2 after global setup/teardown works.
37+
38+
// Allocate an environment handle
39+
ASSERT_EQ(SQL_SUCCESS, SQLAllocEnv(&env));
40+
41+
ASSERT_EQ(SQL_SUCCESS,
42+
SQLSetEnvAttr(
43+
env, SQL_ATTR_ODBC_VERSION,
44+
reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(SQL_OV_ODBC3)), 0));
45+
46+
// Allocate a connection using alloc handle
47+
ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn));
48+
std::cout << "-AL- AlinaTestEnvironment::SetUp\n";
49+
}
50+
51+
void TearDown() override {
52+
53+
54+
// -AL- this doesn't work
55+
//// Remove function registry before test exits
56+
auto reg = arrow::compute::GetFunctionRegistry();
57+
reg->ClearFunctioRegistry();
58+
// delete reg;
59+
60+
61+
int y = 1;
62+
// Free connection handle
63+
EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_DBC, conn));
64+
65+
// Free environment handle
66+
EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_ENV, env));
67+
std::cout << "-AL- AlinaTestEnvironment::TearDown\n";
68+
69+
}
70+
71+
SQLHENV getEnvHandle() { return env; }
72+
73+
SQLHDBC getConnHandle() { return conn; }
74+
75+
private:
76+
/** ODBC Environment. */
77+
SQLHENV env = 0;
78+
79+
/** ODBC Connect. */
80+
SQLHDBC conn = 0;
81+
};
82+
83+
} // namespace arrow::flight::sql::odbc

0 commit comments

Comments
 (0)