IGNITE-3757: ODBC: Added aggregate functions support. This closes #983.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/0e3a6e2d Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/0e3a6e2d Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/0e3a6e2d Branch: refs/heads/master Commit: 0e3a6e2df8b42f255a5a4688d5827dccaabfd3a4 Parents: 5a3b3e2 Author: isapego <[email protected]> Authored: Thu Aug 25 14:34:31 2016 +0300 Committer: vozerov-gridgain <[email protected]> Committed: Thu Aug 25 14:34:31 2016 +0300 ---------------------------------------------------------------------- modules/platforms/cpp/odbc-test/Makefile.am | 1 + .../cpp/odbc-test/project/vs/odbc-test.vcxproj | 1 + .../project/vs/odbc-test.vcxproj.filters | 3 + .../src/sql_aggregate_functions_test.cpp | 249 +++++++++++++++++++ .../cpp/odbc/src/config/connection_info.cpp | 5 +- 5 files changed, 256 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/0e3a6e2d/modules/platforms/cpp/odbc-test/Makefile.am ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc-test/Makefile.am b/modules/platforms/cpp/odbc-test/Makefile.am index 62bdec3..6288637 100644 --- a/modules/platforms/cpp/odbc-test/Makefile.am +++ b/modules/platforms/cpp/odbc-test/Makefile.am @@ -65,6 +65,7 @@ ignite_odbc_tests_SOURCES = \ src/sql_function_test_suite_fixture.cpp \ src/sql_string_functions_test.cpp \ src/sql_numeric_functions_test.cpp \ + src/sql_aggregate_functions_test.cpp \ ../odbc/src/cursor.cpp \ ../odbc/src/config/connection_info.cpp \ ../odbc/src/app/application_data_buffer.cpp \ http://git-wip-us.apache.org/repos/asf/ignite/blob/0e3a6e2d/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj b/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj index a83ed6b..e0b786a 100644 --- a/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj +++ b/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj @@ -168,6 +168,7 @@ <ClCompile Include="..\..\src\queries_test.cpp" /> <ClCompile Include="..\..\src\parser_test.cpp" /> <ClCompile Include="..\..\src\row_test.cpp" /> + <ClCompile Include="..\..\src\sql_aggregate_functions_test.cpp" /> <ClCompile Include="..\..\src\sql_function_test_suite_fixture.cpp" /> <ClCompile Include="..\..\src\sql_numeric_functions_test.cpp" /> <ClCompile Include="..\..\src\sql_string_functions_test.cpp" /> http://git-wip-us.apache.org/repos/asf/ignite/blob/0e3a6e2d/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj.filters ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj.filters b/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj.filters index f993ed9..763a364 100644 --- a/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj.filters +++ b/modules/platforms/cpp/odbc-test/project/vs/odbc-test.vcxproj.filters @@ -88,6 +88,9 @@ <ClCompile Include="..\..\src\sql_function_test_suite_fixture.cpp"> <Filter>Code</Filter> </ClCompile> + <ClCompile Include="..\..\src\sql_aggregate_functions_test.cpp"> + <Filter>Code</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\..\include\test_type.h"> http://git-wip-us.apache.org/repos/asf/ignite/blob/0e3a6e2d/modules/platforms/cpp/odbc-test/src/sql_aggregate_functions_test.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc-test/src/sql_aggregate_functions_test.cpp b/modules/platforms/cpp/odbc-test/src/sql_aggregate_functions_test.cpp new file mode 100644 index 0000000..6254913 --- /dev/null +++ b/modules/platforms/cpp/odbc-test/src/sql_aggregate_functions_test.cpp @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _USE_MATH_DEFINES + +#ifdef _WIN32 +# include <windows.h> +#endif + +#include <sql.h> +#include <sqlext.h> + +#include <cmath> + +#include <vector> +#include <string> + +#ifndef _MSC_VER +# define BOOST_TEST_DYN_LINK +#endif + +#include <boost/test/unit_test.hpp> + +#include "ignite/ignite.h" +#include "ignite/ignition.h" +#include "ignite/impl/binary/binary_utils.h" + +#include "test_type.h" +#include "test_utils.h" +#include "sql_function_test_suite_fixture.h" + +using namespace ignite; +using namespace ignite::cache; +using namespace ignite::cache::query; +using namespace ignite::common; + +using namespace boost::unit_test; + +using ignite::impl::binary::BinaryUtils; + +BOOST_FIXTURE_TEST_SUITE(SqlAggregateFunctionTestSuite, ignite::SqlFunctionTestSuiteFixture) + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionAvgInt) +{ + std::vector<TestType> in(3); + + in[0].i32Field = 43; + in[1].i32Field = 311; + in[2].i32Field = 7; + + int32_t avg = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + avg += in[i].i32Field; + } + + avg /= static_cast<int32_t>(in.size()); + + CheckSingleResult<int64_t>("SELECT {fn AVG(i32Field)} FROM TestType", avg); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionAvgIntDistinct) +{ + std::vector<TestType> in(3); + + in[0].i32Field = 43; + in[1].i32Field = 311; + in[2].i32Field = 7; + + int32_t avg = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + avg += in[i].i32Field; + } + + avg /= static_cast<int32_t>(in.size()); + + testCache.Put(in.size() + 10, in[0]); + + CheckSingleResult<int64_t>("SELECT {fn AVG(DISTINCT i32Field)} FROM TestType", avg); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionAvgFloat) +{ + std::vector<TestType> in(3); + + in[0].floatField = 43.0; + in[1].floatField = 311.0; + in[2].floatField = 7.0; + + float avg = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + avg += in[i].i32Field; + } + + avg /= in.size(); + + CheckSingleResult<float>("SELECT {fn AVG(floatField)} FROM TestType", avg); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionAvgFloatDistinct) +{ + std::vector<TestType> in(3); + + in[0].floatField = 43.0; + in[1].floatField = 311.0; + in[2].floatField = 7.0; + + float avg = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + avg += in[i].i32Field; + } + + avg /= in.size(); + + testCache.Put(in.size() + 10, in[0]); + + CheckSingleResult<float>("SELECT {fn AVG(DISTINCT floatField)} FROM TestType", avg); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionCount) +{ + std::vector<TestType> in(8); + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + testCache.Put(i, in[i]); + + CheckSingleResult<int64_t>("SELECT {fn COUNT(*)} FROM TestType", in.size()); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionCountDistinct) +{ + std::vector<TestType> in(8); + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + in[i].i32Field = i; + + testCache.Put(i, in[i]); + } + + testCache.Put(in.size() + 10, in[0]); + + CheckSingleResult<int64_t>("SELECT {fn COUNT(DISTINCT i32Field)} FROM TestType", in.size()); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionMax) +{ + std::vector<TestType> in(4); + + in[0].i32Field = 121; + in[1].i32Field = 17; + in[2].i32Field = 314041; + in[3].i32Field = 9410; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + testCache.Put(i, in[i]); + + CheckSingleResult<int64_t>("SELECT {fn MAX(i32Field)} FROM TestType", in[2].i32Field); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionMin) +{ + std::vector<TestType> in(4); + + in[0].i32Field = 121; + in[1].i32Field = 17; + in[2].i32Field = 314041; + in[3].i32Field = 9410; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + testCache.Put(i, in[i]); + + CheckSingleResult<int64_t>("SELECT {fn MIN(i32Field)} FROM TestType", in[1].i32Field); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionSum) +{ + std::vector<TestType> in(4); + + in[0].i32Field = 121; + in[1].i32Field = 17; + in[2].i32Field = 314041; + in[3].i32Field = 9410; + + int64_t sum = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + sum += in[i].i32Field; + } + + CheckSingleResult<int64_t>("SELECT {fn SUM(i32Field)} FROM TestType", sum); +} + +BOOST_AUTO_TEST_CASE(TestAggregateFunctionSumDistinct) +{ + std::vector<TestType> in(4); + + in[0].i32Field = 121; + in[1].i32Field = 17; + in[2].i32Field = 314041; + in[3].i32Field = 9410; + + int64_t sum = 0; + + for (int32_t i = 0; i < static_cast<int32_t>(in.size()); ++i) + { + testCache.Put(i, in[i]); + + sum += in[i].i32Field; + } + + testCache.Put(in.size() + 10, in[0]); + + CheckSingleResult<int64_t>("SELECT {fn SUM(DISTINCT i32Field)} FROM TestType", sum); +} + +BOOST_AUTO_TEST_SUITE_END() http://git-wip-us.apache.org/repos/asf/ignite/blob/0e3a6e2d/modules/platforms/cpp/odbc/src/config/connection_info.cpp ---------------------------------------------------------------------- diff --git a/modules/platforms/cpp/odbc/src/config/connection_info.cpp b/modules/platforms/cpp/odbc/src/config/connection_info.cpp index 1fed7c5..b2480c2 100644 --- a/modules/platforms/cpp/odbc/src/config/connection_info.cpp +++ b/modules/platforms/cpp/odbc/src/config/connection_info.cpp @@ -225,9 +225,8 @@ namespace ignite #ifdef SQL_AGGREGATE_FUNCTIONS // Bitmask enumerating support for aggregation functions. - intParams[SQL_AGGREGATE_FUNCTIONS] = SQL_AF_ALL | SQL_AF_AVG | - SQL_AF_COUNT | SQL_AF_DISTINCT | SQL_AF_MAX | SQL_AF_MIN | - SQL_AF_SUM; + intParams[SQL_AGGREGATE_FUNCTIONS] = SQL_AF_AVG | SQL_AF_COUNT | + SQL_AF_DISTINCT | SQL_AF_MAX | SQL_AF_MIN | SQL_AF_SUM; #endif // SQL_AGGREGATE_FUNCTIONS #ifdef SQL_NUMERIC_FUNCTIONS
