From 95bf3b3e59a0a55858b0915437db2e19b8fe399d Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Sun, 27 Oct 2024 21:01:06 +0800 Subject: [PATCH 01/10] feat(client-cpp): add sqlDialect, database, insertRelationalTablet(not tested). --- .../client-cpp-example/src/SessionExample.cpp | 2 + iotdb-client/client-cpp/src/main/Session.cpp | 38 ++++++++++++++++++- iotdb-client/client-cpp/src/main/Session.h | 22 +++++++++++ .../client-cpp/src/test/cpp/sessionIT.cpp | 6 +++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/example/client-cpp-example/src/SessionExample.cpp b/example/client-cpp-example/src/SessionExample.cpp index c3f5602cb08a..b7662743fef6 100644 --- a/example/client-cpp-example/src/SessionExample.cpp +++ b/example/client-cpp-example/src/SessionExample.cpp @@ -377,6 +377,8 @@ int main() { LOG_LEVEL = LEVEL_DEBUG; session = new Session("127.0.0.1", 6667, "root", "root"); + // session.setSqlDialect("tree"); + // session.setDatabase(""); session->open(false); cout << "setStorageGroup: root.sg1\n" << endl; diff --git a/iotdb-client/client-cpp/src/main/Session.cpp b/iotdb-client/client-cpp/src/main/Session.cpp index 3a11da9ac7ec..11e29bba757b 100644 --- a/iotdb-client/client-cpp/src/main/Session.cpp +++ b/iotdb-client/client-cpp/src/main/Session.cpp @@ -876,6 +876,10 @@ void Session::open(bool enableRPCCompression, int connectionTimeoutInMs) { std::map configuration; configuration["version"] = getVersionString(version); + configuration["sql_dialect"] = sqlDialect; + if (database != "") { + configuration["db"] = database; + } TSOpenSessionReq openReq; openReq.__set_username(username); @@ -1386,6 +1390,35 @@ void Session::insertTablet(Tablet &tablet, bool sorted) { insertTablet(request); } +void Session::insertRelationalTablet(Tablet &tablet, bool sorted) { + TSInsertTabletReq request; + buildInsertTabletReq(request, sessionId, tablet, sorted); + request.__set_writeToTable(true); + std::vector columnCategories; + for (auto &schema: tablet.schemas) { + columnCategories.push_back(static_cast(schema.second)); + } + request.__set_columnCategories(columnCategories); + try { + TSStatus respStatus; + client->insertTablet(respStatus, request); + RpcUtils::verifySuccess(respStatus); + } catch (const TTransportException &e) { + log_debug(e.what()); + throw IoTDBConnectionException(e.what()); + } catch (const IoTDBException &e) { + log_debug(e.what()); + throw; + } catch (const exception &e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } +} + +void Session::insertRelationalTablet(Tablet &tablet) { + insertRelationalTablet(tablet, false); +} + void Session::insertAlignedTablet(Tablet &tablet) { insertAlignedTablet(tablet, false); } @@ -1917,7 +1950,10 @@ void Session::executeNonQueryStatement(const string &sql) { req.__set_timeout(0); //0 means no timeout. This value keep consistent to JAVA SDK. TSExecuteStatementResp resp; try { - client->executeUpdateStatement(resp, req); + client->executeUpdateStatementV2(resp, req); + if (resp.database != "") { + database = resp.database; + } RpcUtils::verifySuccess(resp.status); } catch (const TTransportException &e) { log_debug(e.what()); diff --git a/iotdb-client/client-cpp/src/main/Session.h b/iotdb-client/client-cpp/src/main/Session.h index d6c3bfc9a236..ecc8641e7c04 100644 --- a/iotdb-client/client-cpp/src/main/Session.h +++ b/iotdb-client/client-cpp/src/main/Session.h @@ -973,6 +973,8 @@ class Session { const static int DEFAULT_FETCH_SIZE = 10000; const static int DEFAULT_TIMEOUT_MS = 0; Version::Version version; + std::string sqlDialect = "tree"; // default sql dialect + std::string database; private: static bool checkSorted(const Tablet &tablet); @@ -1046,6 +1048,22 @@ class Session { ~Session(); + void setSqlDialect(const std::string &dialect){ + this->sqlDialect = dialect; + } + + void setDatabase(const std::string &database) { + this->database = database; + } + + string getDatabase() { + return database; + } + + void changeDatabase(string database) { + this->database = database; + } + int64_t getSessionId(); void open(); @@ -1124,6 +1142,10 @@ class Session { void insertTablet(Tablet &tablet, bool sorted); + void insertRelationalTablet(Tablet &tablet); + + void insertRelationalTablet(Tablet &tablet, bool sorted); + static void buildInsertTabletReq(TSInsertTabletReq &request, int64_t sessionId, Tablet &tablet, bool sorted); void insertTablet(const TSInsertTabletReq &request); diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp index 8016a4e37b32..9d605dbcf2b1 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp @@ -300,6 +300,12 @@ TEST_CASE("Test insertTablet ", "[testInsertTablet]") { REQUIRE(count == 100); } +// TEST_CASE("Test insertRelationalTablet", "testInsertRelationalTablet") { +// CaseReporter cr("testInsertRelationalTablet"); +// session->executeNonQueryStatement("USE \"db1\""); + +// } + TEST_CASE("Test Last query ", "[testLastQuery]") { CaseReporter cr("testLastQuery"); prepareTimeseries(); From d5a3dbab599ae1908cc0495f89ae539567ea483c Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Wed, 13 Nov 2024 15:46:09 +0800 Subject: [PATCH 02/10] temporary --- iotdb-client/client-cpp/src/main/Session.h | 14 ++++++++++++++ iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/iotdb-client/client-cpp/src/main/Session.h b/iotdb-client/client-cpp/src/main/Session.h index ecc8641e7c04..7444a53bf58e 100644 --- a/iotdb-client/client-cpp/src/main/Session.h +++ b/iotdb-client/client-cpp/src/main/Session.h @@ -536,6 +536,20 @@ class Field { Field() = default; }; +enum ColumnCategory { + ID, + MEASUREMENT, + ATTRIBUTE; +} + +std::vector ColumCategory_nCopy(ColumnCategory type, int n) { + std::vector res; + for (int i = 0; i < n; i++) { + res.push_back(type); + } + return res; +} + /* * A tablet data of one device, the tablet contains multiple measurements of this device that share * the same time column. diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp index 9d605dbcf2b1..006e31a71169 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp @@ -300,11 +300,11 @@ TEST_CASE("Test insertTablet ", "[testInsertTablet]") { REQUIRE(count == 100); } -// TEST_CASE("Test insertRelationalTablet", "testInsertRelationalTablet") { -// CaseReporter cr("testInsertRelationalTablet"); -// session->executeNonQueryStatement("USE \"db1\""); +TEST_CASE("Test insertRelationalTablet", "testInsertRelationalTablet") { + CaseReporter cr("testInsertRelationalTablet"); + session->executeNonQueryStatement("USE \"db1\""); -// } +} TEST_CASE("Test Last query ", "[testLastQuery]") { CaseReporter cr("testLastQuery"); From 5489ea5db43843197a7379b743e9e6f31e9f08bc Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 15:22:08 +0800 Subject: [PATCH 03/10] feat: add test for client-cpp insertRelationalTablet --- iotdb-client/client-cpp/src/main/Session.cpp | 4 +- iotdb-client/client-cpp/src/main/Session.h | 28 +++-- .../client-cpp/src/test/CMakeLists.txt | 17 ++- .../client-cpp/src/test/cpp/sessionIT.cpp | 6 - .../src/test/cpp/sessionRelationalIT.cpp | 118 ++++++++++++++++++ .../client-cpp/src/test/main_Relational.cpp | 43 +++++++ 6 files changed, 192 insertions(+), 24 deletions(-) create mode 100644 iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp create mode 100644 iotdb-client/client-cpp/src/test/main_Relational.cpp diff --git a/iotdb-client/client-cpp/src/main/Session.cpp b/iotdb-client/client-cpp/src/main/Session.cpp index 11e29bba757b..c945dab76677 100644 --- a/iotdb-client/client-cpp/src/main/Session.cpp +++ b/iotdb-client/client-cpp/src/main/Session.cpp @@ -1395,8 +1395,8 @@ void Session::insertRelationalTablet(Tablet &tablet, bool sorted) { buildInsertTabletReq(request, sessionId, tablet, sorted); request.__set_writeToTable(true); std::vector columnCategories; - for (auto &schema: tablet.schemas) { - columnCategories.push_back(static_cast(schema.second)); + for (auto &category: tablet.columnTypes) { + columnCategories.push_back(static_cast(category)); } request.__set_columnCategories(columnCategories); try { diff --git a/iotdb-client/client-cpp/src/main/Session.h b/iotdb-client/client-cpp/src/main/Session.h index 7444a53bf58e..9fbb18d81a65 100644 --- a/iotdb-client/client-cpp/src/main/Session.h +++ b/iotdb-client/client-cpp/src/main/Session.h @@ -536,19 +536,11 @@ class Field { Field() = default; }; -enum ColumnCategory { +enum class ColumnCategory { ID, MEASUREMENT, - ATTRIBUTE; -} - -std::vector ColumCategory_nCopy(ColumnCategory type, int n) { - std::vector res; - for (int i = 0; i < n; i++) { - res.push_back(type); - } - return res; -} + ATTRIBUTE +}; /* * A tablet data of one device, the tablet contains multiple measurements of this device that share @@ -574,6 +566,7 @@ class Tablet { public: std::string deviceId; // deviceId of this tablet std::vector> schemas; // the list of measurement schemas for creating the tablet + std::vector columnTypes; // the list of column types (used in table model) std::vector timestamps; // timestamps in this tablet std::vector values; // each object is a primitive type array, which represents values of one measurement std::vector bitMaps; // each bitmap represents the existence of each value in the current column @@ -594,6 +587,11 @@ class Tablet { const std::vector> ×eries) : Tablet(deviceId, timeseries, DEFAULT_ROW_SIZE) {} + Tablet(const std::string &deviceId, + const std::vector> ×eries, + const std::vector &columnTypes) + : Tablet(deviceId, timeseries, columnTypes, DEFAULT_ROW_SIZE) {} + /** * Return a tablet with the specified number of rows (maxBatchSize). Only * call this constructor directly for testing purposes. Tablet should normally @@ -602,10 +600,16 @@ class Tablet { * @param deviceId the name of the device specified to be written in * @param schemas the list of measurement schemas for creating the row * batch + * @param columnTypes the list of column types (used in table model) * @param maxRowNumber the maximum number of rows for this tablet */ + Tablet(const std::string &deviceId, + const std::vector> &schemas, + int maxRowNumber) + : Tablet(deviceId, schemas, std::vector(schemas.size(), ColumnCategory::MEASUREMENT), maxRowNumber) {} Tablet(const std::string &deviceId, const std::vector> &schemas, - size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas), + const std::vector columnTypes, + size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas), columnTypes(columnTypes), maxRowNumber(maxRowNumber), isAligned(_isAligned) { // create timestamp column timestamps.resize(maxRowNumber); diff --git a/iotdb-client/client-cpp/src/test/CMakeLists.txt b/iotdb-client/client-cpp/src/test/CMakeLists.txt index d38c975de201..b07d93bf2734 100644 --- a/iotdb-client/client-cpp/src/test/CMakeLists.txt +++ b/iotdb-client/client-cpp/src/test/CMakeLists.txt @@ -21,6 +21,7 @@ INCLUDE( CTest ) SET(CMAKE_CXX_STANDARD 11) SET(CMAKE_CXX_STANDARD_REQUIRED ON) SET(TARGET_NAME session_tests) +SET(TARGET_NAME_RELATIONAL session_relational_tests) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g -O2") ENABLE_TESTING() @@ -46,19 +47,27 @@ ELSE() SET(THRIFT_STATIC_LIB "${CMAKE_SOURCE_DIR}/../../thrift/lib/libthrift.a") ENDIF() -ADD_EXECUTABLE(${TARGET_NAME} main.cpp cpp/sessionIT.cpp) +# ADD_EXECUTABLE(${TARGET_NAME} main.cpp cpp/sessionIT.cpp) +ADD_EXECUTABLE(${TARGET_NAME_RELATIONAL} main_Relational.cpp cpp/sessionRelationalIT.cpp) # Link with shared library iotdb_session and pthread IF(MSVC) - TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB}) + # TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB}) + TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session ${THRIFT_STATIC_LIB}) ELSE() TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread) + # TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session pthread) ENDIF() -TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} PUBLIC ./catch2/) + +# Add Catch2 include directory +# TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} PUBLIC ./catch2/) +TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME_RELATIONAL} PUBLIC ./catch2/) # Add 'sessionIT' to the project to be run by ctest IF(MSVC) - ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME}) + # ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME}) + ADD_TEST(NAME sessionRelationalIT CONFIGURATIONS Release COMMAND ${TARGET_NAME_RELATIONAL}) ELSE() ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME}) + # ADD_TEST(NAME sessionRelationalIT COMMAND ${TARGET_NAME_RELATIONAL}) ENDIF() diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp index 006e31a71169..8016a4e37b32 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionIT.cpp @@ -300,12 +300,6 @@ TEST_CASE("Test insertTablet ", "[testInsertTablet]") { REQUIRE(count == 100); } -TEST_CASE("Test insertRelationalTablet", "testInsertRelationalTablet") { - CaseReporter cr("testInsertRelationalTablet"); - session->executeNonQueryStatement("USE \"db1\""); - -} - TEST_CASE("Test Last query ", "[testLastQuery]") { CaseReporter cr("testLastQuery"); prepareTimeseries(); diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp new file mode 100644 index 000000000000..2099b68726aa --- /dev/null +++ b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp @@ -0,0 +1,118 @@ +/** + * 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. + */ + +#include "catch.hpp" +#include "Session.h" +#include + +using namespace std; + +extern Session *session; + +static int global_test_id = 0; +class CaseReporter +{ +public: + CaseReporter(const char *caseNameArg) : caseName(caseNameArg) + { + test_id = global_test_id++; + std::cout << "Test " << test_id << ": " << caseName << std::endl; + } + ~CaseReporter() + { + std::cout << "Test " << test_id << ": " << caseName << " Done"<< std::endl << std::endl; + } +private: + const char *caseName; + int test_id; +}; + +TEST_CASE("Create table success", "[createTable]") { + // REQUIRE(true); + CaseReporter cr("createTable"); + session->executeNonQueryStatement("CREATE DATABASE db1"); + session->executeNonQueryStatement("CREATE DATABASE db2"); + session->executeNonQueryStatement("USE \"db1\""); + REQUIRE(session->getDatabase() == "db1"); + session->executeNonQueryStatement(" + CREATE TABLE table0 ( + id1 string id, + attr1 string attribute, + m1 double measurement)"); + unique_ptr *sessionDataSet = session->executeQueryStatement("SHOW TABLES"); + sessionDataSet->setFetchSize(1024); + bool tableExist = false; + while (sessionDataSet->hasNext()) { + if (sessionDataSet->next()->fields[0].stringV == "table0") { + tableExist = true; + break; + } + } + REQUIRE(tableExist == true); +} + +TEST_CASE("Test insertRelationalTablet", "[testInsertRelationalTablet]") { + CaseReporter cr("testInsertRelationalTablet"); + session->executeNonQueryStatement("CREATE DATABASE IF NOT EXIST db1"); + session->executeNonQueryStatement("USE db1"); + session->executeNonQueryStatement("CREATE TABLE table1 ( + id1 string id, + attr1 string attribute, + m1 double measurement)"); + vector> schemaList; + schemaList.push_back(make_pair("id1", TSDataType::TEXT)); + schemaList.push_back(make_pair("attr1", TSDataType::TEXT)); + schemaList.push_back(make_pair("m1", TSDataType::DOUBLE)); + vector columnTypes = {ColumnCategory::ID, ColumnCategory::ATTRIBUTE, ColumnCategory::MEASUREMENT}; + + int64_t timestamp = 0; + Tablet tablet("table1", schemaList, columnTypes, 15); + + for (int row = 0; row < 15; row++) { + int rowIndex = tablet.rowSize++; + tablet.timestamps[rowIndex] = timestamp + row; + string data = "id:"; data += to_string(row); + tablet.addValue(0, rowIndex, &data); + data = "attr:"; data += to_string(row); + tablet.addValue(1, rowIndex, &data); + double value = row * 1.1; + tablet.addValue(2, rowIndex, &value); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertRelationalTablet(tablet, true); + tablet.reset(); + } + } + + if (tablet.rowSize != 0) { + session->insertRelationalTablet(tablet, true); + tablet.reset(); + } + session->executeNonQueryStatement("FLUSH"); + + int cnt = 0; + unique_ptr sessionDataSet = session->executeQueryStatement("SELECT * FROM table1 order by time"); + while (sessionDataSet->hasNext()) { + RowRecord *rowRecord = sessionDataSet->next(); + REQUIRE(rowRecord->fields[1].stringV == string("id:") + to_string(cnt)); + REQUIRE(rowRecord->fields[2].stringV == string("attr:") + to_string(cnt)); + REQUIRE(fabs(rowRecord->fields[3].doubleV - cnt * 1.1) < 0.0001); + cnt++; + } + REQUIRE(cnt == 15); +} diff --git a/iotdb-client/client-cpp/src/test/main_Relational.cpp b/iotdb-client/client-cpp/src/test/main_Relational.cpp new file mode 100644 index 000000000000..1f40facd9629 --- /dev/null +++ b/iotdb-client/client-cpp/src/test/main_Relational.cpp @@ -0,0 +1,43 @@ +/** + * 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 CATCH_CONFIG_MAIN + +#include +#include "Session.h" + +Session *session = new Session("127.0.0.1", 6667, "root", "root"); +session->setSqlDialect("table"); + +struct SessionListener : Catch::TestEventListenerBase { + + using TestEventListenerBase::TestEventListenerBase; + + void testCaseStarting(Catch::TestCaseInfo const &testInfo) override { + // Perform some setup before a test case is run + session->open(false); + } + + void testCaseEnded(Catch::TestCaseStats const &testCaseStats) override { + // Tear-down after a test case is run + session->close(); + } +}; + +CATCH_REGISTER_LISTENER( SessionListener ) \ No newline at end of file From f5729652275c87f9663a665f55dfe94d2bcb038a Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 15:40:48 +0800 Subject: [PATCH 04/10] fix cmakes in linux --- iotdb-client/client-cpp/src/test/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/iotdb-client/client-cpp/src/test/CMakeLists.txt b/iotdb-client/client-cpp/src/test/CMakeLists.txt index b07d93bf2734..e4c1a984e51d 100644 --- a/iotdb-client/client-cpp/src/test/CMakeLists.txt +++ b/iotdb-client/client-cpp/src/test/CMakeLists.txt @@ -55,8 +55,8 @@ IF(MSVC) # TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB}) TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session ${THRIFT_STATIC_LIB}) ELSE() - TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread) - # TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session pthread) + # TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread) + TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session pthread) ENDIF() # Add Catch2 include directory @@ -68,6 +68,6 @@ IF(MSVC) # ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME}) ADD_TEST(NAME sessionRelationalIT CONFIGURATIONS Release COMMAND ${TARGET_NAME_RELATIONAL}) ELSE() - ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME}) - # ADD_TEST(NAME sessionRelationalIT COMMAND ${TARGET_NAME_RELATIONAL}) + # ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME}) + ADD_TEST(NAME sessionRelationalIT COMMAND ${TARGET_NAME_RELATIONAL}) ENDIF() From 0f96f70b464b92a2a1e0acfc2f36318c2f05ff9c Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 16:02:11 +0800 Subject: [PATCH 05/10] fix: test files --- iotdb-client/client-cpp/src/test/main_Relational.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iotdb-client/client-cpp/src/test/main_Relational.cpp b/iotdb-client/client-cpp/src/test/main_Relational.cpp index 1f40facd9629..649ad8a3b0ec 100644 --- a/iotdb-client/client-cpp/src/test/main_Relational.cpp +++ b/iotdb-client/client-cpp/src/test/main_Relational.cpp @@ -23,7 +23,6 @@ #include "Session.h" Session *session = new Session("127.0.0.1", 6667, "root", "root"); -session->setSqlDialect("table"); struct SessionListener : Catch::TestEventListenerBase { @@ -31,6 +30,7 @@ struct SessionListener : Catch::TestEventListenerBase { void testCaseStarting(Catch::TestCaseInfo const &testInfo) override { // Perform some setup before a test case is run + session->setSqlDialect("table"); session->open(false); } From 857f2cae441810138c0f7b3777f617ae67da2218 Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 16:08:02 +0800 Subject: [PATCH 06/10] fix: multiline strings --- .../src/test/cpp/sessionRelationalIT.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp index 2099b68726aa..b3507892f059 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp @@ -50,11 +50,10 @@ TEST_CASE("Create table success", "[createTable]") { session->executeNonQueryStatement("CREATE DATABASE db2"); session->executeNonQueryStatement("USE \"db1\""); REQUIRE(session->getDatabase() == "db1"); - session->executeNonQueryStatement(" - CREATE TABLE table0 ( - id1 string id, - attr1 string attribute, - m1 double measurement)"); + session->executeNonQueryStatement("CREATE TABLE table0 (" + "id1 string id," + "attr1 string attribute," + "m1 double measurement)"); unique_ptr *sessionDataSet = session->executeQueryStatement("SHOW TABLES"); sessionDataSet->setFetchSize(1024); bool tableExist = false; @@ -71,10 +70,10 @@ TEST_CASE("Test insertRelationalTablet", "[testInsertRelationalTablet]") { CaseReporter cr("testInsertRelationalTablet"); session->executeNonQueryStatement("CREATE DATABASE IF NOT EXIST db1"); session->executeNonQueryStatement("USE db1"); - session->executeNonQueryStatement("CREATE TABLE table1 ( - id1 string id, - attr1 string attribute, - m1 double measurement)"); + session->executeNonQueryStatement("CREATE TABLE table1 (" + "id1 string id," + "attr1 string attribute," + "m1 double measurement)"); vector> schemaList; schemaList.push_back(make_pair("id1", TSDataType::TEXT)); schemaList.push_back(make_pair("attr1", TSDataType::TEXT)); From 5cc92088da0da5b792d2aacb6847daced4e89f25 Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 16:11:01 +0800 Subject: [PATCH 07/10] fix: unique_ptr --- iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp index b3507892f059..1616c77b8664 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp @@ -54,7 +54,7 @@ TEST_CASE("Create table success", "[createTable]") { "id1 string id," "attr1 string attribute," "m1 double measurement)"); - unique_ptr *sessionDataSet = session->executeQueryStatement("SHOW TABLES"); + unique_ptr sessionDataSet = session->executeQueryStatement("SHOW TABLES"); sessionDataSet->setFetchSize(1024); bool tableExist = false; while (sessionDataSet->hasNext()) { From 58e409d95418f5eb8efd85ffcb0d424093980eed Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 20:36:16 +0800 Subject: [PATCH 08/10] fix: test success. --- iotdb-client/client-cpp/src/test/CMakeLists.txt | 12 ++++++------ .../client-cpp/src/test/cpp/sessionRelationalIT.cpp | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/iotdb-client/client-cpp/src/test/CMakeLists.txt b/iotdb-client/client-cpp/src/test/CMakeLists.txt index e4c1a984e51d..186c08b634d4 100644 --- a/iotdb-client/client-cpp/src/test/CMakeLists.txt +++ b/iotdb-client/client-cpp/src/test/CMakeLists.txt @@ -47,27 +47,27 @@ ELSE() SET(THRIFT_STATIC_LIB "${CMAKE_SOURCE_DIR}/../../thrift/lib/libthrift.a") ENDIF() -# ADD_EXECUTABLE(${TARGET_NAME} main.cpp cpp/sessionIT.cpp) +ADD_EXECUTABLE(${TARGET_NAME} main.cpp cpp/sessionIT.cpp) ADD_EXECUTABLE(${TARGET_NAME_RELATIONAL} main_Relational.cpp cpp/sessionRelationalIT.cpp) # Link with shared library iotdb_session and pthread IF(MSVC) - # TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB}) + TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB}) TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session ${THRIFT_STATIC_LIB}) ELSE() - # TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread) + TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread) TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session pthread) ENDIF() # Add Catch2 include directory -# TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} PUBLIC ./catch2/) +TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} PUBLIC ./catch2/) TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME_RELATIONAL} PUBLIC ./catch2/) # Add 'sessionIT' to the project to be run by ctest IF(MSVC) - # ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME}) + ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME}) ADD_TEST(NAME sessionRelationalIT CONFIGURATIONS Release COMMAND ${TARGET_NAME_RELATIONAL}) ELSE() - # ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME}) + ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME}) ADD_TEST(NAME sessionRelationalIT COMMAND ${TARGET_NAME_RELATIONAL}) ENDIF() diff --git a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp index 1616c77b8664..edb310087ce4 100644 --- a/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp +++ b/iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp @@ -68,7 +68,7 @@ TEST_CASE("Create table success", "[createTable]") { TEST_CASE("Test insertRelationalTablet", "[testInsertRelationalTablet]") { CaseReporter cr("testInsertRelationalTablet"); - session->executeNonQueryStatement("CREATE DATABASE IF NOT EXIST db1"); + session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1"); session->executeNonQueryStatement("USE db1"); session->executeNonQueryStatement("CREATE TABLE table1 (" "id1 string id," From af55e9d8977dd3cf37ba8523e076bb7f7b731be4 Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 14 Nov 2024 21:38:02 +0800 Subject: [PATCH 09/10] chore: add SessionTableModelExample.cpp --- .../client-cpp-example/src/SessionExample.cpp | 2 - .../src/SessionTableModelExample.cpp | 100 ++++++++++++++++++ iotdb-client/client-cpp/src/main/Session.cpp | 12 +++ iotdb-client/client-cpp/src/main/Session.h | 6 ++ 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 example/client-cpp-example/src/SessionTableModelExample.cpp diff --git a/example/client-cpp-example/src/SessionExample.cpp b/example/client-cpp-example/src/SessionExample.cpp index b7662743fef6..c3f5602cb08a 100644 --- a/example/client-cpp-example/src/SessionExample.cpp +++ b/example/client-cpp-example/src/SessionExample.cpp @@ -377,8 +377,6 @@ int main() { LOG_LEVEL = LEVEL_DEBUG; session = new Session("127.0.0.1", 6667, "root", "root"); - // session.setSqlDialect("tree"); - // session.setDatabase(""); session->open(false); cout << "setStorageGroup: root.sg1\n" << endl; diff --git a/example/client-cpp-example/src/SessionTableModelExample.cpp b/example/client-cpp-example/src/SessionTableModelExample.cpp new file mode 100644 index 000000000000..1e932dbbc250 --- /dev/null +++ b/example/client-cpp-example/src/SessionTableModelExample.cpp @@ -0,0 +1,100 @@ +/* + * 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. + */ + +#include "Session.h" + +using namespace std; + +Session *session; + +void insertRelationalTablet() { + vector> schemaList; + schemaList.push_back(make_pair("id1", TSDataType::TEXT)); + schemaList.push_back(make_pair("attr1", TSDataType::TEXT)); + schemaList.push_back(make_pair("m1", TSDataType::DOUBLE)); + vector columnTypes = {ColumnCategory::ID, ColumnCategory::ATTRIBUTE, ColumnCategory::MEASUREMENT}; + + int64_t timestamp = 0; + Tablet tablet("table1", schemaList, columnTypes, 15); + + for (int row = 0; row < 15; row++) { + int rowIndex = tablet.rowSize++; + tablet.timestamps[rowIndex] = timestamp + row; + string data = "id:"; data += to_string(row); + tablet.addValue(0, rowIndex, &data); + data = "attr:"; data += to_string(row); + tablet.addValue(1, rowIndex, &data); + double value = row * 1.1; + tablet.addValue(2, rowIndex, &value); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertRelationalTablet(tablet, true); + tablet.reset(); + } + } + + if (tablet.rowSize != 0) { + session->insertRelationalTablet(tablet, true); + tablet.reset(); + } +} + +int main() { + + session = new Session("127.0.0.1", 6667, "root", "root"); + session->setSqlDialect("table"); + session->open(false); + + cout << "Create Database db1" << endl; + try { + session->createDatabase("db1"); + } catch (IoTDBException &e) { + cout << e.what() << endl; + } + + cout << "Use db1 as database" << endl; + session->executeNonQueryStatement("USE db1"); + + cout << "Create Table table1" << endl; + try { + session->executeNonQueryStatement("CREATE TABLE table1 (" + "id1 string id," + "attr1 string attribute," + "m1 double measurement)"); + } catch (IoTDBException &e) { + cout << e.what() << endl; + } + + cout << "InsertRelationalTablet" << endl; + insertRelationalTablet(); + + cout << "Drop Database db1" << endl; + try { + session->deleteDatabase("db1"); + } catch (IoTDBException &e) { + cout << e.what() << endl; + } + + cout << "session close\n" << endl; + session->close(); + + delete session; + + cout << "finished!\n" << endl; + return 0; +} \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/Session.cpp b/iotdb-client/client-cpp/src/main/Session.cpp index c945dab76677..95fba95d1da3 100644 --- a/iotdb-client/client-cpp/src/main/Session.cpp +++ b/iotdb-client/client-cpp/src/main/Session.cpp @@ -1686,6 +1686,18 @@ void Session::deleteStorageGroups(const vector &storageGroups) { } } +void Session::createDatabase(const string &database) { + this->setStorageGroup(database); +} + +void Session::deleteDatabase(const string &database) { + this->deleteStorageGroups(vector{database}); +} + +void Session::deleteDatabases(const vector &databases) { + this->deleteStorageGroups(databases); +} + void Session::createTimeseries(const string &path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, diff --git a/iotdb-client/client-cpp/src/main/Session.h b/iotdb-client/client-cpp/src/main/Session.h index 9fbb18d81a65..eaaea85ae04a 100644 --- a/iotdb-client/client-cpp/src/main/Session.h +++ b/iotdb-client/client-cpp/src/main/Session.h @@ -1205,6 +1205,12 @@ class Session { void deleteStorageGroups(const std::vector &storageGroups); + void createDatabase(const std::string &database); + + void deleteDatabase(const std::string &database); + + void deleteDatabases(const std::vector &databases); + void createTimeseries(const std::string &path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor); From d7142deed5d5898031a4bcde03cf3de1fabc0561 Mon Sep 17 00:00:00 2001 From: xiangmy21 <2248278431@qq.com> Date: Thu, 21 Nov 2024 19:34:20 +0800 Subject: [PATCH 10/10] fix(client-cpp): add dataType "TIMESTAMP" parsing --- iotdb-client/client-cpp/src/main/Session.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iotdb-client/client-cpp/src/main/Session.cpp b/iotdb-client/client-cpp/src/main/Session.cpp index 95fba95d1da3..317ed8e07f86 100644 --- a/iotdb-client/client-cpp/src/main/Session.cpp +++ b/iotdb-client/client-cpp/src/main/Session.cpp @@ -34,13 +34,14 @@ static const int64_t QUERY_TIMEOUT_MS = -1; LogLevelType LOG_LEVEL = LEVEL_DEBUG; TSDataType::TSDataType getTSDataTypeFromString(const string &str) { - // BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, NULLTYPE + // BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, NULLTYPE if (str == "BOOLEAN") return TSDataType::BOOLEAN; else if (str == "INT32") return TSDataType::INT32; else if (str == "INT64") return TSDataType::INT64; else if (str == "FLOAT") return TSDataType::FLOAT; else if (str == "DOUBLE") return TSDataType::DOUBLE; else if (str == "TEXT") return TSDataType::TEXT; + else if (str == "TIMESTAMP") return TSDataType::INT64; else if (str == "NULLTYPE") return TSDataType::NULLTYPE; return TSDataType::TEXT; }