You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2022/12/20 01:06:14 UTC

[iotdb] 01/01: [IOTDB-5236] Fix DeleteData Python API cannot work (#8511)

This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch iotdb_5236
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 9ff209f97136c76dc6d30e46654d786f85eaac25
Author: Haonan <hh...@outlook.com>
AuthorDate: Tue Dec 20 09:01:23 2022 +0800

    [IOTDB-5236] Fix DeleteData Python API cannot work (#8511)
---
 client-py/iotdb/Session.py          |  26 ++++--
 client-py/tests/test_delete_data.py | 162 ++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+), 5 deletions(-)

diff --git a/client-py/iotdb/Session.py b/client-py/iotdb/Session.py
index a1254e227c..ebcc174dc9 100644
--- a/client-py/iotdb/Session.py
+++ b/client-py/iotdb/Session.py
@@ -99,7 +99,7 @@ class Session(object):
         self.__statement_id = None
         self.__zone_id = zone_id
 
-    def open(self, enable_rpc_compression):
+    def open(self, enable_rpc_compression=False):
         if not self.__is_close:
             return
         self.__transport = TTransport.TFramedTransport(
@@ -351,13 +351,29 @@ class Session(object):
         data_set.close_operation_handle()
         return result
 
-    def delete_data(self, paths_list, timestamp):
+    def delete_data(self, paths_list, end_time):
         """
-        delete all data <= time in multiple time series
+        delete all data <= end_time in multiple time series
         :param paths_list: time series list that the data in.
-        :param timestamp: data with time stamp less than or equal to time will be deleted.
+        :param end_time: data with time stamp less than or equal to time will be deleted.
         """
-        request = TSDeleteDataReq(self.__session_id, paths_list, timestamp)
+        request = TSDeleteDataReq(self.__session_id, paths_list, -9223372036854775808, end_time)
+        try:
+            status = self.__client.deleteData(request)
+            logger.debug(
+                "delete data from {}, message: {}".format(paths_list, status.message)
+            )
+        except TTransport.TException as e:
+            logger.exception("data deletion fails because: ", e)
+
+    def delete_data_in_range(self, paths_list, start_time, end_time):
+        """
+        delete data >= start_time and data <= end_time in multiple timeseries
+        :param paths_list: time series list that the data in.
+        :param start_time: delete range start time.
+        :param end_time: delete range end time.
+        """
+        request = TSDeleteDataReq(self.__session_id, paths_list, start_time, end_time)
         try:
             status = self.__client.deleteData(request)
             logger.debug(
diff --git a/client-py/tests/test_delete_data.py b/client-py/tests/test_delete_data.py
new file mode 100644
index 0000000000..a73aa73fc6
--- /dev/null
+++ b/client-py/tests/test_delete_data.py
@@ -0,0 +1,162 @@
+# 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.
+#
+
+# Uncomment the following line to use apache-iotdb module installed by pip3
+
+from iotdb.Session import Session
+from iotdb.IoTDBContainer import IoTDBContainer
+
+# whether the test has passed
+final_flag = True
+failed_count = 0
+
+
+def test_fail():
+    global failed_count
+    global final_flag
+    final_flag = False
+    failed_count += 1
+
+
+def print_message(message):
+    print("*********")
+    print(message)
+    print("*********")
+
+
+def test_delete_date():
+    with IoTDBContainer("iotdb:dev") as db:
+        db: IoTDBContainer
+        session = Session(db.get_container_host_ip(), db.get_exposed_port(6667))
+        session.open()
+        session.execute_non_query_statement("CREATE DATABASE root.str_test_01")
+
+        if not session.is_open():
+            print("can't open session")
+            exit(1)
+
+        # insert string records of one device
+        time_list = [1, 2, 3]
+        measurements_list = [
+            ["s_01", "s_02", "s_03"],
+            ["s_01", "s_02", "s_03"],
+            ["s_01", "s_02", "s_03"],
+        ]
+        values_list = [
+            ["False", "22", "33"],
+            ["True", "1", "23"],
+            ["False", "15", "26"],
+        ]
+
+        if (
+            session.insert_string_records_of_one_device(
+                "root.str_test_01.d_01",
+                time_list,
+                measurements_list,
+                values_list,
+            )
+            < 0
+        ):
+            test_fail()
+            print_message("insert string records of one device failed")
+
+        # insert aligned string record into the database.
+        time_list = [1, 2, 3]
+        measurements_list = [
+            ["s_01", "s_02", "s_03"],
+            ["s_01", "s_02", "s_03"],
+            ["s_01", "s_02", "s_03"],
+        ]
+        values_list = [
+            ["False", "22", "33"],
+            ["True", "1", "23"],
+            ["False", "15", "26"],
+        ]
+
+        if (
+            session.insert_aligned_string_records_of_one_device(
+                "root.str_test_01.d_02",
+                time_list,
+                measurements_list,
+                values_list,
+            )
+            < 0
+        ):
+            test_fail()
+            print_message("insert aligned record of one device failed")
+
+        # execute delete data
+        session.delete_data(["root.str_test_01.d_02.s_01", "root.str_test_01.d_02.s_02"], 1)
+
+        # execute raw data query sql statement
+        session_data_set = session.execute_raw_data_query(
+            ["root.str_test_01.d_02.s_01", "root.str_test_01.d_02.s_02"], 1, 4
+        )
+        session_data_set.set_fetch_size(1024)
+        expect_count = 2
+        actual_count = 0
+        while session_data_set.has_next():
+            print(session_data_set.next())
+            actual_count += 1
+        session_data_set.close_operation_handle()
+
+        if actual_count != expect_count:
+            test_fail()
+            print_message(
+                "query count mismatch: expect count: "
+                + str(expect_count)
+                + " actual count: "
+                + str(actual_count)
+            )
+        assert actual_count == expect_count
+
+        # execute delete data
+        session.delete_data_in_range(["root.str_test_01.d_02.s_01", "root.str_test_01.d_02.s_02"], 2, 3)
+
+        # execute raw data query sql statement
+        session_data_set = session.execute_raw_data_query(
+            ["root.str_test_01.d_02.s_01", "root.str_test_01.d_02.s_02"], 1, 4
+        )
+        session_data_set.set_fetch_size(1024)
+        expect_count = 0
+        actual_count = 0
+        while session_data_set.has_next():
+            print(session_data_set.next())
+            actual_count += 1
+        session_data_set.close_operation_handle()
+
+        if actual_count != expect_count:
+            test_fail()
+            print_message(
+                "query count mismatch: expect count: "
+                + str(expect_count)
+                + " actual count: "
+                + str(actual_count)
+            )
+        assert actual_count == expect_count
+
+        # close session connection.
+        session.close()
+
+
+if final_flag:
+    print("All executions done!!")
+else:
+    print("Some test failed, please have a check")
+    print("failed count: ", failed_count)
+    exit(1)