|
| 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 | +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" |
| 18 | + |
| 19 | +#include "arrow/flight/sql/odbc/odbc_impl/platform.h" |
| 20 | + |
| 21 | +#include <sql.h> |
| 22 | +#include <sqltypes.h> |
| 23 | +#include <sqlucode.h> |
| 24 | + |
| 25 | +#include <limits> |
| 26 | + |
| 27 | +#include <gmock/gmock.h> |
| 28 | +#include <gtest/gtest.h> |
| 29 | + |
| 30 | +namespace arrow::flight::sql::odbc { |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +class StatementTest : public T {}; |
| 34 | + |
| 35 | +class StatementMockTest : public FlightSQLODBCMockTestBase {}; |
| 36 | +class StatementRemoteTest : public FlightSQLODBCRemoteTestBase {}; |
| 37 | +using TestTypes = ::testing::Types<StatementMockTest, StatementRemoteTest>; |
| 38 | +TYPED_TEST_SUITE(StatementTest, TestTypes); |
| 39 | + |
| 40 | +TYPED_TEST(StatementTest, TestSQLExtendedFetchRowFetching) { |
| 41 | + // Set SQL_ROWSET_SIZE to fetch 3 rows at once |
| 42 | + |
| 43 | + constexpr SQLULEN rows = 3; |
| 44 | + SQLINTEGER val[rows]; |
| 45 | + SQLLEN buf_len = sizeof(val); |
| 46 | + SQLLEN ind[rows]; |
| 47 | + |
| 48 | + // Same variable will be used for column 1, the value of `val` |
| 49 | + // should be updated after every SQLFetch call. |
| 50 | + ASSERT_EQ(SQL_SUCCESS, SQLBindCol(this->stmt, 1, SQL_C_LONG, val, buf_len, ind)); |
| 51 | + |
| 52 | + ASSERT_EQ(SQL_SUCCESS, SQLSetStmtAttr(this->stmt, SQL_ROWSET_SIZE, |
| 53 | + reinterpret_cast<SQLPOINTER>(rows), 0)); |
| 54 | + |
| 55 | + std::wstring wsql = |
| 56 | + LR"( |
| 57 | + SELECT 1 AS small_table |
| 58 | + UNION ALL |
| 59 | + SELECT 2 |
| 60 | + UNION ALL |
| 61 | + SELECT 3; |
| 62 | + )"; |
| 63 | + std::vector<SQLWCHAR> sql0(wsql.begin(), wsql.end()); |
| 64 | + |
| 65 | + ASSERT_EQ(SQL_SUCCESS, |
| 66 | + SQLExecDirect(this->stmt, &sql0[0], static_cast<SQLINTEGER>(sql0.size()))); |
| 67 | + |
| 68 | + // Fetch row 1-3. |
| 69 | + SQLULEN row_count; |
| 70 | + SQLUSMALLINT row_status[rows]; |
| 71 | + |
| 72 | + ASSERT_EQ(SQL_SUCCESS, |
| 73 | + SQLExtendedFetch(this->stmt, SQL_FETCH_NEXT, 0, &row_count, row_status)); |
| 74 | + EXPECT_EQ(3, row_count); |
| 75 | + |
| 76 | + for (int i = 0; i < rows; i++) { |
| 77 | + EXPECT_EQ(SQL_SUCCESS, row_status[i]); |
| 78 | + } |
| 79 | + |
| 80 | + // Verify 1 is returned for row 1 |
| 81 | + EXPECT_EQ(1, val[0]); |
| 82 | + // Verify 2 is returned for row 2 |
| 83 | + EXPECT_EQ(2, val[1]); |
| 84 | + // Verify 3 is returned for row 3 |
| 85 | + EXPECT_EQ(3, val[2]); |
| 86 | + |
| 87 | + // Verify result set has no more data beyond row 3 |
| 88 | + SQLULEN row_count2; |
| 89 | + SQLUSMALLINT row_status2[rows]; |
| 90 | + EXPECT_EQ(SQL_NO_DATA, |
| 91 | + SQLExtendedFetch(this->stmt, SQL_FETCH_NEXT, 0, &row_count2, row_status2)); |
| 92 | +} |
| 93 | + |
| 94 | +TEST_F(StatementRemoteTest, DISABLED_TestSQLExtendedFetchQueryNullIndicator) { |
| 95 | + // GH-47110: SQLExtendedFetch should return SQL_SUCCESS_WITH_INFO for 22002 |
| 96 | + // Limitation on mock test server prevents null from working properly, so use remote |
| 97 | + // server instead. Mock server has type `DENSE_UNION` for null column data. |
| 98 | + SQLINTEGER val; |
| 99 | + |
| 100 | + ASSERT_EQ(SQL_SUCCESS, SQLBindCol(this->stmt, 1, SQL_C_LONG, &val, 0, 0)); |
| 101 | + |
| 102 | + std::wstring wsql = L"SELECT null as null_col;"; |
| 103 | + std::vector<SQLWCHAR> sql0(wsql.begin(), wsql.end()); |
| 104 | + |
| 105 | + ASSERT_EQ(SQL_SUCCESS, |
| 106 | + SQLExecDirect(this->stmt, &sql0[0], static_cast<SQLINTEGER>(sql0.size()))); |
| 107 | + |
| 108 | + SQLULEN row_count1; |
| 109 | + SQLUSMALLINT row_status1[1]; |
| 110 | + |
| 111 | + // SQLExtendedFetch should return SQL_SUCCESS_WITH_INFO for 22002 state |
| 112 | + ASSERT_EQ(SQL_SUCCESS_WITH_INFO, |
| 113 | + SQLExtendedFetch(this->stmt, SQL_FETCH_NEXT, 0, &row_count1, row_status1)); |
| 114 | + VerifyOdbcErrorState(SQL_HANDLE_STMT, this->stmt, kErrorState22002); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace arrow::flight::sql::odbc |
0 commit comments