From 355abf5990217adf3d9b862d50d9c386b682df6a Mon Sep 17 00:00:00 2001 From: Haonan Date: Fri, 29 Nov 2024 09:42:00 +0800 Subject: [PATCH] Refactor TableSession and TableSessionPool of Python client (#14232) * refector py table session * refector py table session * fix python 3.6 * fix IY * fix table session * Fix CI * fix IT * fix IT * fix IT * builder * fix example * tableSession python --- iotdb-client/client-py/iotdb/Session.py | 43 +++-- iotdb-client/client-py/iotdb/SessionPool.py | 20 +-- iotdb-client/client-py/iotdb/table_session.py | 147 ++++++++++++++++++ .../client-py/iotdb/table_session_pool.py | 116 ++++++++++++++ ... => session_aligned_timeseries_example.py} | 0 .../{SessionExample.py => session_example.py} | 0 ...PoolExample.py => session_pool_example.py} | 0 ...mple.py => table_model_session_example.py} | 48 +++--- ...py => table_model_session_pool_example.py} | 45 +++--- .../integration/dbapi}/__init__.py | 0 .../integration/dbapi}/test_connection.py | 2 +- .../integration/dbapi}/test_cursor.py | 2 +- .../integration/iotdb_container.py} | 0 .../integration/sqlalchemy}/__init__.py | 0 .../integration/sqlalchemy}/test_dialect.py | 2 +- .../integration/test_aligned_timeseries.py | 2 +- .../tests/integration/test_dataframe.py | 2 +- .../tests/integration/test_delete_data.py | 2 +- .../tests/integration/test_new_data_types.py | 2 +- .../tests/integration/test_one_device.py | 2 +- .../integration/test_relational_session.py | 37 ++--- .../tests/integration/test_session.py | 2 +- .../tests/integration/test_session_pool.py | 2 +- .../integration/test_tablemodel_insert.py | 37 +++-- .../tests/integration/test_tablet.py | 2 +- .../client-py/tests/integration/test_todf.py | 2 +- .../integration/test_treemodel_insert.py | 3 +- 27 files changed, 384 insertions(+), 136 deletions(-) create mode 100644 iotdb-client/client-py/iotdb/table_session.py create mode 100644 iotdb-client/client-py/iotdb/table_session_pool.py rename iotdb-client/client-py/{SessionAlignedTimeseriesExample.py => session_aligned_timeseries_example.py} (100%) rename iotdb-client/client-py/{SessionExample.py => session_example.py} (100%) rename iotdb-client/client-py/{SessionPoolExample.py => session_pool_example.py} (100%) rename iotdb-client/client-py/{TableModelSessionExample.py => table_model_session_example.py} (83%) rename iotdb-client/client-py/{TableModelSessionPoolExample.py => table_model_session_pool_example.py} (85%) rename iotdb-client/client-py/{iotdb/dbapi/tests => tests/integration/dbapi}/__init__.py (100%) rename iotdb-client/client-py/{iotdb/dbapi/tests => tests/integration/dbapi}/test_connection.py (96%) rename iotdb-client/client-py/{iotdb/dbapi/tests => tests/integration/dbapi}/test_cursor.py (98%) rename iotdb-client/client-py/{iotdb/IoTDBContainer.py => tests/integration/iotdb_container.py} (100%) rename iotdb-client/client-py/{iotdb/sqlalchemy/tests => tests/integration/sqlalchemy}/__init__.py (100%) rename iotdb-client/client-py/{iotdb/sqlalchemy/tests => tests/integration/sqlalchemy}/test_dialect.py (98%) diff --git a/iotdb-client/client-py/iotdb/Session.py b/iotdb-client/client-py/iotdb/Session.py index 12545e6f7a27..9926eb1273a7 100644 --- a/iotdb-client/client-py/iotdb/Session.py +++ b/iotdb-client/client-py/iotdb/Session.py @@ -69,7 +69,7 @@ class Session(object): SUCCESS_STATUS = 200 MULTIPLE_ERROR = 302 REDIRECTION_RECOMMEND = 400 - DEFAULT_FETCH_SIZE = 10000 + DEFAULT_FETCH_SIZE = 5000 DEFAULT_USER = "root" DEFAULT_PASSWORD = "root" DEFAULT_ZONE_ID = time.strftime("%z") @@ -85,8 +85,6 @@ def __init__( fetch_size=DEFAULT_FETCH_SIZE, zone_id=DEFAULT_ZONE_ID, enable_redirection=True, - sql_dialect=SQL_DIALECT, - database=None, ): self.__host = host self.__port = port @@ -107,8 +105,8 @@ def __init__( self.__enable_redirection = enable_redirection self.__device_id_to_endpoint = None self.__endpoint_to_connection = None - self.__sql_dialect = sql_dialect - self.__database = database + self.sql_dialect = self.SQL_DIALECT + self.database = None @classmethod def init_from_node_urls( @@ -119,8 +117,6 @@ def init_from_node_urls( fetch_size=DEFAULT_FETCH_SIZE, zone_id=DEFAULT_ZONE_ID, enable_redirection=True, - sql_dialect=SQL_DIALECT, - database=None, ): if node_urls is None: raise RuntimeError("node urls is empty") @@ -132,8 +128,6 @@ def init_from_node_urls( fetch_size, zone_id, enable_redirection, - sql_dialect=sql_dialect, - database=database, ) session.__hosts = [] session.__ports = [] @@ -196,9 +190,9 @@ def init_connection(self, endpoint): else: client = Client(TBinaryProtocol.TBinaryProtocolAccelerated(transport)) - configuration = {"version": "V_1_0", "sql_dialect": self.__sql_dialect} - if self.__database is not None: - configuration["db"] = self.__database + configuration = {"version": "V_1_0", "sql_dialect": self.sql_dialect} + if self.database is not None: + configuration["db"] = self.database open_req = TSOpenSessionReq( client_protocol=self.protocol_version, username=self.__user, @@ -1431,10 +1425,10 @@ def execute_non_query_statement(self, sql): else: raise IoTDBConnectionException(self.connection_error_msg()) from None - previous_db = self.__database + previous_db = self.database if resp.database is not None: - self.__database = resp.database - if previous_db != self.__database and self.__endpoint_to_connection is not None: + self.database = resp.database + if previous_db != self.database and self.__endpoint_to_connection is not None: iterator = iter(self.__endpoint_to_connection.items()) for entry in list(iterator): endpoint, connection = entry @@ -1604,7 +1598,7 @@ def verify_success(status: TSStatus): ): return 0 - raise RuntimeError(str(status.code) + ": " + status.message) + raise RuntimeError(f"{status.code}: {status.message}") @staticmethod def verify_success_by_list(status_list: list): @@ -1612,14 +1606,15 @@ def verify_success_by_list(status_list: list): verify success of operation :param status_list: execution result status """ - message = str(Session.MULTIPLE_ERROR) + ": " - for status in status_list: - if ( - status.code != Session.SUCCESS_STATUS - and status.code != Session.REDIRECTION_RECOMMEND - ): - message += status.message + "; " - raise RuntimeError(message) + error_messages = [ + status.message + for status in status_list + if status.code + not in {Session.SUCCESS_STATUS, Session.REDIRECTION_RECOMMEND} + ] + if error_messages: + message = f"{Session.MULTIPLE_ERROR}: {'; '.join(error_messages)}" + raise RuntimeError(message) @staticmethod def verify_success_with_redirection(status: TSStatus): diff --git a/iotdb-client/client-py/iotdb/SessionPool.py b/iotdb-client/client-py/iotdb/SessionPool.py index 9585bfe3e104..9fd6e5d05422 100644 --- a/iotdb-client/client-py/iotdb/SessionPool.py +++ b/iotdb-client/client-py/iotdb/SessionPool.py @@ -43,8 +43,7 @@ def __init__( time_zone: str = DEFAULT_TIME_ZONE, max_retry: int = DEFAULT_MAX_RETRY, enable_compression: bool = False, - sql_dialect: str = SQL_DIALECT, - database: str = None, + enable_redirection: bool = True, ): self.host = host self.port = port @@ -61,8 +60,7 @@ def __init__( self.time_zone = time_zone self.max_retry = max_retry self.enable_compression = enable_compression - self.sql_dialect = sql_dialect - self.database = database + self.enable_redirection = enable_redirection class SessionPool(object): @@ -76,6 +74,8 @@ def __init__( self.__queue = Queue(max_pool_size) self.__lock = Lock() self.__closed = False + self.sql_dialect = SQL_DIALECT + self.database = None def __construct_session(self) -> Session: if len(self.__pool_config.node_urls) > 0: @@ -85,10 +85,10 @@ def __construct_session(self) -> Session: self.__pool_config.password, self.__pool_config.fetch_size, self.__pool_config.time_zone, - enable_redirection=True, - sql_dialect=self.__pool_config.sql_dialect, - database=self.__pool_config.database, + enable_redirection=self.__pool_config.enable_redirection, ) + session.sql_dialect = self.sql_dialect + session.database = self.database else: session = Session( @@ -98,10 +98,10 @@ def __construct_session(self) -> Session: self.__pool_config.password, self.__pool_config.fetch_size, self.__pool_config.time_zone, - enable_redirection=True, - sql_dialect=self.__pool_config.sql_dialect, - database=self.__pool_config.database, + enable_redirection=self.__pool_config.enable_redirection, ) + session.sql_dialect = self.sql_dialect + session.database = self.database session.open(self.__pool_config.enable_compression) return session diff --git a/iotdb-client/client-py/iotdb/table_session.py b/iotdb-client/client-py/iotdb/table_session.py new file mode 100644 index 000000000000..5008f342e560 --- /dev/null +++ b/iotdb-client/client-py/iotdb/table_session.py @@ -0,0 +1,147 @@ +# 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. +# +from typing import Union + +from iotdb.Session import Session +from iotdb.utils.NumpyTablet import NumpyTablet +from iotdb.utils.SessionDataSet import SessionDataSet +from iotdb.utils.Tablet import Tablet + + +class TableSessionConfig(object): + + def __init__( + self, + node_urls: list = None, + username: str = Session.DEFAULT_USER, + password: str = Session.DEFAULT_PASSWORD, + database: str = None, + fetch_size: int = 5000, + time_zone: str = Session.DEFAULT_ZONE_ID, + enable_redirection: bool = True, + enable_compression: bool = False, + ): + """ + Initialize a TableSessionConfig object with the provided parameters. + + Parameters: + node_urls (list, optional): A list of node URLs for the database connection. + Defaults to ["localhost:6667"]. + username (str, optional): The username for the database connection. + Defaults to "root". + password (str, optional): The password for the database connection. + Defaults to "root". + database (str, optional): The target database to connect to. Defaults to None. + fetch_size (int, optional): The number of rows to fetch per query. Defaults to 5000. + time_zone (str, optional): The default time zone for the session. + Defaults to Session.DEFAULT_ZONE_ID. + enable_redirection (bool, optional): Whether to enable redirection. + Defaults to False. + enable_compression (bool, optional): Whether to enable data compression. + Defaults to False. + + """ + if node_urls is None: + node_urls = ["localhost:6667"] + self.node_urls = node_urls + self.username = username + self.password = password + self.database = database + self.fetch_size = fetch_size + self.time_zone = time_zone + self.enable_redirection = enable_redirection + self.enable_compression = enable_compression + + +class TableSession(object): + + def __init__( + self, table_session_config: TableSessionConfig = None, session_pool=None + ): + self.__session_pool = session_pool + if self.__session_pool is None: + self.__session = Session.init_from_node_urls( + table_session_config.node_urls, + table_session_config.username, + table_session_config.password, + table_session_config.fetch_size, + table_session_config.time_zone, + table_session_config.enable_redirection, + ) + self.__session.sql_dialect = "table" + self.__session.database = table_session_config.database + self.__session.open(table_session_config.enable_compression) + else: + self.__session = self.__session_pool.get_session() + + def insert(self, tablet: Union[Tablet, NumpyTablet]): + """ + Insert data into the database. + + Parameters: + tablet (Tablet | NumpyTablet): The tablet containing the data to be inserted. + Accepts either a `Tablet` or `NumpyTablet`. + + Raises: + IoTDBConnectionException: If there is an issue with the database connection. + """ + self.__session.insert_relational_tablet(tablet) + + def execute_non_query_statement(self, sql: str): + """ + Execute a non-query SQL statement. + + Parameters: + sql (str): The SQL statement to execute. Typically used for commands + such as INSERT, DELETE, or UPDATE. + + Raises: + IoTDBConnectionException: If there is an issue with the database connection. + """ + self.__session.execute_non_query_statement(sql) + + def execute_query_statement( + self, sql: str, timeout_in_ms: int = 0 + ) -> SessionDataSet: + """ + Execute a query SQL statement and return the result set. + + Parameters: + sql (str): The SQL query to execute. + timeout_in_ms (int, optional): Timeout for the query in milliseconds. Defaults to 0, + which means no timeout. + + Returns: + SessionDataSet: The result set of the query. + + Raises: + IoTDBConnectionException: If there is an issue with the database connection. + """ + return self.__session.execute_query_statement(sql, timeout_in_ms) + + def close(self): + """ + Close the session and release resources. + + Raises: + IoTDBConnectionException: If there is an issue closing the connection. + """ + if self.__session_pool is None: + self.__session.close() + else: + self.__session_pool.put_back(self.__session) diff --git a/iotdb-client/client-py/iotdb/table_session_pool.py b/iotdb-client/client-py/iotdb/table_session_pool.py new file mode 100644 index 000000000000..8e3275a86e53 --- /dev/null +++ b/iotdb-client/client-py/iotdb/table_session_pool.py @@ -0,0 +1,116 @@ +# 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. +# +from iotdb.Session import Session +from iotdb.SessionPool import SessionPool, PoolConfig +from iotdb.table_session import TableSession + + +class TableSessionPoolConfig(object): + def __init__( + self, + node_urls: list = None, + max_pool_size: int = 5, + username: str = Session.DEFAULT_USER, + password: str = Session.DEFAULT_PASSWORD, + database: str = None, + fetch_size: int = 5000, + time_zone: str = Session.DEFAULT_ZONE_ID, + enable_redirection: bool = False, + enable_compression: bool = False, + wait_timeout_in_ms: int = 10000, + max_retry: int = 3, + ): + """ + Initialize a TableSessionPoolConfig object with the provided parameters. + + Parameters: + node_urls (list, optional): A list of node URLs for the database connection. + Defaults to None. + max_pool_size (int, optional): The maximum number of sessions in the pool. + Defaults to 5. + username (str, optional): The username for the database connection. + Defaults to Session.DEFAULT_USER. + password (str, optional): The password for the database connection. + Defaults to Session.DEFAULT_PASSWORD. + database (str, optional): The target database to connect to. Defaults to None. + fetch_size (int, optional): The number of rows to fetch per query. Defaults to 5000. + time_zone (str, optional): The default time zone for the session pool. + Defaults to Session.DEFAULT_ZONE_ID. + enable_redirection (bool, optional): Whether to enable redirection. + Defaults to False. + enable_compression (bool, optional): Whether to enable data compression. + Defaults to False. + wait_timeout_in_ms (int, optional): The maximum time (in milliseconds) to wait for a session + to become available. Defaults to 10000. + max_retry (int, optional): The maximum number of retry attempts for operations. Defaults to 3. + + """ + if node_urls is None: + node_urls = ["localhost:6667"] + self.pool_config = PoolConfig( + node_urls=node_urls, + user_name=username, + password=password, + fetch_size=fetch_size, + time_zone=time_zone, + max_retry=max_retry, + enable_redirection=enable_redirection, + enable_compression=enable_compression, + ) + self.max_pool_size = max_pool_size + self.wait_timeout_in_ms = wait_timeout_in_ms + self.database = database + + +class TableSessionPool(object): + + def __init__(self, table_session_pool_config: TableSessionPoolConfig): + pool_config = table_session_pool_config.pool_config + max_pool_size = table_session_pool_config.max_pool_size + wait_timeout_in_ms = table_session_pool_config.wait_timeout_in_ms + self.__session_pool = SessionPool( + pool_config, max_pool_size, wait_timeout_in_ms + ) + self.__session_pool.sql_dialect = "table" + self.__session_pool.database = table_session_pool_config.database + + def get_session(self) -> TableSession: + """ + Retrieve a new TableSession instance. + + Returns: + TableSession: A new session object configured with the session pool. + + Notes: + The session is initialized with the underlying session pool for managing + connections. Ensure proper usage of the session's lifecycle. + """ + return TableSession(None, session_pool=self.__session_pool) + + def close(self): + """ + Close the session pool and release all resources. + + This method closes the underlying session pool, ensuring that all + resources associated with it are properly released. + + Notes: + After calling this method, the session pool cannot be used to retrieve + new sessions, and any attempt to do so may raise an exception. + """ + self.__session_pool.close() diff --git a/iotdb-client/client-py/SessionAlignedTimeseriesExample.py b/iotdb-client/client-py/session_aligned_timeseries_example.py similarity index 100% rename from iotdb-client/client-py/SessionAlignedTimeseriesExample.py rename to iotdb-client/client-py/session_aligned_timeseries_example.py diff --git a/iotdb-client/client-py/SessionExample.py b/iotdb-client/client-py/session_example.py similarity index 100% rename from iotdb-client/client-py/SessionExample.py rename to iotdb-client/client-py/session_example.py diff --git a/iotdb-client/client-py/SessionPoolExample.py b/iotdb-client/client-py/session_pool_example.py similarity index 100% rename from iotdb-client/client-py/SessionPoolExample.py rename to iotdb-client/client-py/session_pool_example.py diff --git a/iotdb-client/client-py/TableModelSessionExample.py b/iotdb-client/client-py/table_model_session_example.py similarity index 83% rename from iotdb-client/client-py/TableModelSessionExample.py rename to iotdb-client/client-py/table_model_session_example.py index 3a31a4637927..108373b55a37 100644 --- a/iotdb-client/client-py/TableModelSessionExample.py +++ b/iotdb-client/client-py/table_model_session_example.py @@ -17,20 +17,20 @@ # import numpy as np -from iotdb.Session import Session +from iotdb.table_session import TableSession, TableSessionConfig from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.NumpyTablet import NumpyTablet from iotdb.utils.Tablet import ColumnType, Tablet # creating session connection. -ip = "127.0.0.1" -port_ = "6667" -username_ = "root" -password_ = "root" - # don't specify database in constructor -session = Session(ip, port_, username_, password_, sql_dialect="table", database="db1") -session.open(False) +config = TableSessionConfig( + node_urls=["localhost:6667"], + username="root", + password="root", + time_zone="UTC+8", +) +session = TableSession(config) session.execute_non_query_statement("CREATE DATABASE test1") session.execute_non_query_statement("CREATE DATABASE test2") @@ -64,10 +64,14 @@ session.close() # specify database in constructor -session = Session( - ip, port_, username_, password_, sql_dialect="table", database="test1" +config = TableSessionConfig( + node_urls=["localhost:6667"], + username="root", + password="root", + database="test1", + time_zone="UTC+8", ) -session.open(False) +session = TableSession(config) # show tables from current database with session.execute_query_statement("SHOW TABLES") as session_data_set: @@ -87,9 +91,13 @@ session.close() -# insert tablet by insert_relational_tablet -session = Session(ip, port_, username_, password_, sql_dialect="table") -session.open(False) +# insert data by tablet +config = TableSessionConfig( + node_urls=["localhost:6667"], + username="root", + password="root", +) +session = TableSession(config) session.execute_non_query_statement("CREATE DATABASE IF NOT EXISTS db1") session.execute_non_query_statement('USE "db1"') session.execute_non_query_statement( @@ -115,7 +123,7 @@ timestamps.append(row) values.append(["id:" + str(row), "attr:" + str(row), row * 1.0]) tablet = Tablet("table5", column_names, data_types, values, timestamps, column_types) -session.insert_relational_tablet(tablet) +session.insert(tablet) session.execute_non_query_statement("FLush") @@ -134,16 +142,16 @@ np_timestamps, column_types=column_types, ) -session.insert_relational_tablet(np_tablet) +session.insert(np_tablet) with session.execute_query_statement("select * from table5 order by time") as dataset: print(dataset.get_column_names()) while dataset.has_next(): row_record = dataset.next() - # print(row_record.get_fields()[0].get_long_value()) - # print(row_record.get_fields()[1].get_string_value()) - # print(row_record.get_fields()[2].get_string_value()) - # print(row_record.get_fields()[3].get_double_value()) + print(row_record.get_fields()[0].get_long_value()) + print(row_record.get_fields()[1].get_string_value()) + print(row_record.get_fields()[2].get_string_value()) + print(row_record.get_fields()[3].get_double_value()) print(row_record) with session.execute_query_statement("select * from table5 order by time") as dataset: diff --git a/iotdb-client/client-py/TableModelSessionPoolExample.py b/iotdb-client/client-py/table_model_session_pool_example.py similarity index 85% rename from iotdb-client/client-py/TableModelSessionPoolExample.py rename to iotdb-client/client-py/table_model_session_pool_example.py index 44d6c353499e..64aeec917c0c 100644 --- a/iotdb-client/client-py/TableModelSessionPoolExample.py +++ b/iotdb-client/client-py/table_model_session_pool_example.py @@ -19,7 +19,7 @@ import numpy as np -from iotdb.SessionPool import PoolConfig, SessionPool +from iotdb.table_session_pool import TableSessionPool, TableSessionPoolConfig from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.NumpyTablet import NumpyTablet from iotdb.utils.Tablet import ColumnType, Tablet @@ -48,7 +48,7 @@ def prepare_data(): while res.has_next(): print(res.next()) - session_pool.put_back(session) + session.close() def insert_data(num: int): @@ -74,7 +74,7 @@ def insert_data(num: int): tablet = Tablet( "table" + str(num), column_names, data_types, values, timestamps, column_types ) - session.insert_relational_tablet(tablet) + session.insert(tablet) session.execute_non_query_statement("FLush") np_timestamps = np.arange(15, 30, dtype=np.dtype(">i8")) @@ -92,8 +92,8 @@ def insert_data(num: int): np_timestamps, column_types=column_types, ) - session.insert_relational_tablet(np_tablet) - session_pool.put_back(session) + session.insert(np_tablet) + session.close() def query_data(): @@ -110,38 +110,37 @@ def query_data(): while res.has_next(): print(res.next()) - session_pool.put_back(session) + session.close() def delete_data(): session = session_pool.get_session() session.execute_non_query_statement("drop database db1") print("data has been deleted. now the databases are:") - res = session.execute_statement("show databases") + res = session.execute_query_statement("show databases") while res.has_next(): print(res.next()) - session_pool.put_back(session) + session.close() -ip = "127.0.0.1" -port = "6667" +# Create a session pool username = "root" password = "root" -pool_config = PoolConfig( - node_urls=["127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"], - user_name=username, - password=password, - fetch_size=1024, - time_zone="UTC+8", - max_retry=3, - sql_dialect="table", - database="db1", -) +node_urls = ["127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"] +fetch_size = 1024 +database = "db1" max_pool_size = 5 wait_timeout_in_ms = 3000 - -# Create a session pool -session_pool = SessionPool(pool_config, max_pool_size, wait_timeout_in_ms) +config = TableSessionPoolConfig( + node_urls=node_urls, + username=username, + password=password, + database=database, + max_pool_size=max_pool_size, + fetch_size=fetch_size, + wait_timeout_in_ms=wait_timeout_in_ms, +) +session_pool = TableSessionPool(config) prepare_data() diff --git a/iotdb-client/client-py/iotdb/dbapi/tests/__init__.py b/iotdb-client/client-py/tests/integration/dbapi/__init__.py similarity index 100% rename from iotdb-client/client-py/iotdb/dbapi/tests/__init__.py rename to iotdb-client/client-py/tests/integration/dbapi/__init__.py diff --git a/iotdb-client/client-py/iotdb/dbapi/tests/test_connection.py b/iotdb-client/client-py/tests/integration/dbapi/test_connection.py similarity index 96% rename from iotdb-client/client-py/iotdb/dbapi/tests/test_connection.py rename to iotdb-client/client-py/tests/integration/dbapi/test_connection.py index d0d878ce2be0..590fe5f7da5b 100644 --- a/iotdb-client/client-py/iotdb/dbapi/tests/test_connection.py +++ b/iotdb-client/client-py/tests/integration/dbapi/test_connection.py @@ -16,7 +16,7 @@ # under the License. # -from iotdb.IoTDBContainer import IoTDBContainer +from tests.integration.iotdb_container import IoTDBContainer from iotdb.dbapi import connect final_flag = True diff --git a/iotdb-client/client-py/iotdb/dbapi/tests/test_cursor.py b/iotdb-client/client-py/tests/integration/dbapi/test_cursor.py similarity index 98% rename from iotdb-client/client-py/iotdb/dbapi/tests/test_cursor.py rename to iotdb-client/client-py/tests/integration/dbapi/test_cursor.py index 4737aa95eafb..eb6f70b838d0 100644 --- a/iotdb-client/client-py/iotdb/dbapi/tests/test_cursor.py +++ b/iotdb-client/client-py/tests/integration/dbapi/test_cursor.py @@ -16,7 +16,7 @@ # under the License. # -from iotdb.IoTDBContainer import IoTDBContainer +from tests.integration.iotdb_container import IoTDBContainer from iotdb.dbapi import connect from iotdb.dbapi.Cursor import Cursor diff --git a/iotdb-client/client-py/iotdb/IoTDBContainer.py b/iotdb-client/client-py/tests/integration/iotdb_container.py similarity index 100% rename from iotdb-client/client-py/iotdb/IoTDBContainer.py rename to iotdb-client/client-py/tests/integration/iotdb_container.py diff --git a/iotdb-client/client-py/iotdb/sqlalchemy/tests/__init__.py b/iotdb-client/client-py/tests/integration/sqlalchemy/__init__.py similarity index 100% rename from iotdb-client/client-py/iotdb/sqlalchemy/tests/__init__.py rename to iotdb-client/client-py/tests/integration/sqlalchemy/__init__.py diff --git a/iotdb-client/client-py/iotdb/sqlalchemy/tests/test_dialect.py b/iotdb-client/client-py/tests/integration/sqlalchemy/test_dialect.py similarity index 98% rename from iotdb-client/client-py/iotdb/sqlalchemy/tests/test_dialect.py rename to iotdb-client/client-py/tests/integration/sqlalchemy/test_dialect.py index cd6fae15bb1c..f6100f0a11f2 100644 --- a/iotdb-client/client-py/iotdb/sqlalchemy/tests/test_dialect.py +++ b/iotdb-client/client-py/tests/integration/sqlalchemy/test_dialect.py @@ -21,7 +21,7 @@ from sqlalchemy import create_engine, inspect from sqlalchemy.dialects import registry -from iotdb.IoTDBContainer import IoTDBContainer +from tests.integration.iotdb_container import IoTDBContainer final_flag = True failed_count = 0 diff --git a/iotdb-client/client-py/tests/integration/test_aligned_timeseries.py b/iotdb-client/client-py/tests/integration/test_aligned_timeseries.py index f2a038905388..2c36f9484276 100644 --- a/iotdb-client/client-py/tests/integration/test_aligned_timeseries.py +++ b/iotdb-client/client-py/tests/integration/test_aligned_timeseries.py @@ -20,7 +20,7 @@ from iotdb.Session import Session from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor from iotdb.utils.Tablet import Tablet -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer # whether the test has passed final_flag = True diff --git a/iotdb-client/client-py/tests/integration/test_dataframe.py b/iotdb-client/client-py/tests/integration/test_dataframe.py index a93efdbea3e5..f314fbac1843 100644 --- a/iotdb-client/client-py/tests/integration/test_dataframe.py +++ b/iotdb-client/client-py/tests/integration/test_dataframe.py @@ -17,7 +17,7 @@ # from iotdb.Session import Session -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer from numpy.testing import assert_array_equal diff --git a/iotdb-client/client-py/tests/integration/test_delete_data.py b/iotdb-client/client-py/tests/integration/test_delete_data.py index 8f4457472649..cc4103947f7d 100644 --- a/iotdb-client/client-py/tests/integration/test_delete_data.py +++ b/iotdb-client/client-py/tests/integration/test_delete_data.py @@ -19,7 +19,7 @@ # Uncomment the following line to use apache-iotdb module installed by pip3 from iotdb.Session import Session -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer # whether the test has passed final_flag = True diff --git a/iotdb-client/client-py/tests/integration/test_new_data_types.py b/iotdb-client/client-py/tests/integration/test_new_data_types.py index ce89b5b83165..997831f9692b 100644 --- a/iotdb-client/client-py/tests/integration/test_new_data_types.py +++ b/iotdb-client/client-py/tests/integration/test_new_data_types.py @@ -24,7 +24,7 @@ from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.NumpyTablet import NumpyTablet from iotdb.utils.Tablet import Tablet -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer def test_session(): diff --git a/iotdb-client/client-py/tests/integration/test_one_device.py b/iotdb-client/client-py/tests/integration/test_one_device.py index ce4c02b0123e..337445eb3b87 100644 --- a/iotdb-client/client-py/tests/integration/test_one_device.py +++ b/iotdb-client/client-py/tests/integration/test_one_device.py @@ -19,7 +19,7 @@ # Uncomment the following line to use apache-iotdb module installed by pip3 from iotdb.Session import Session -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer # whether the test has passed final_flag = True diff --git a/iotdb-client/client-py/tests/integration/test_relational_session.py b/iotdb-client/client-py/tests/integration/test_relational_session.py index cb5382ddb490..bcbb85c5b4f9 100644 --- a/iotdb-client/client-py/tests/integration/test_relational_session.py +++ b/iotdb-client/client-py/tests/integration/test_relational_session.py @@ -18,12 +18,12 @@ import numpy as np -from iotdb.Session import Session -from iotdb.SessionPool import PoolConfig, create_session_pool +from iotdb.table_session import TableSession, TableSessionConfig +from iotdb.table_session_pool import TableSessionPool, TableSessionPoolConfig from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.NumpyTablet import NumpyTablet from iotdb.utils.Tablet import Tablet, ColumnType -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer def test_session(): @@ -37,32 +37,17 @@ def test_session_pool(): def session_test(use_session_pool=False): with IoTDBContainer("iotdb:dev") as db: db: IoTDBContainer - if use_session_pool: - pool_config = PoolConfig( - db.get_container_host_ip(), - db.get_exposed_port(6667), - "root", - "root", - None, - 1024, - "Asia/Shanghai", - 3, - sql_dialect="table", + config = TableSessionPoolConfig( + node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"] ) - session_pool = create_session_pool(pool_config, 1, 3000) + session_pool = TableSessionPool(config) session = session_pool.get_session() else: - session = Session( - db.get_container_host_ip(), - db.get_exposed_port(6667), - sql_dialect="table", + config = TableSessionConfig( + node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"] ) - session.open(False) - - if not session.is_open(): - print("can't open session") - exit(1) + session = TableSession(config) session.execute_non_query_statement("CREATE DATABASE IF NOT EXISTS db1") session.execute_non_query_statement('USE "db1"') @@ -91,7 +76,7 @@ def session_test(use_session_pool=False): tablet = Tablet( "table5", column_names, data_types, values, timestamps, column_types ) - session.insert_relational_tablet(tablet) + session.insert(tablet) session.execute_non_query_statement("FLush") @@ -110,7 +95,7 @@ def session_test(use_session_pool=False): np_timestamps, column_types=column_types, ) - session.insert_relational_tablet(np_tablet) + session.insert(np_tablet) with session.execute_query_statement( "select * from table5 order by time" diff --git a/iotdb-client/client-py/tests/integration/test_session.py b/iotdb-client/client-py/tests/integration/test_session.py index 197b9b9de522..7695f4133c26 100644 --- a/iotdb-client/client-py/tests/integration/test_session.py +++ b/iotdb-client/client-py/tests/integration/test_session.py @@ -25,7 +25,7 @@ from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor from iotdb.utils.NumpyTablet import NumpyTablet from iotdb.utils.Tablet import Tablet -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer # whether the test has passed final_flag = True diff --git a/iotdb-client/client-py/tests/integration/test_session_pool.py b/iotdb-client/client-py/tests/integration/test_session_pool.py index 43351be25169..f85ffa80eb47 100644 --- a/iotdb-client/client-py/tests/integration/test_session_pool.py +++ b/iotdb-client/client-py/tests/integration/test_session_pool.py @@ -17,7 +17,7 @@ # from threading import Thread -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer from iotdb.SessionPool import create_session_pool, PoolConfig CONTAINER_NAME = "iotdb:dev" diff --git a/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py b/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py index 01f0a33027db..037a876a73eb 100644 --- a/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py +++ b/iotdb-client/client-py/tests/integration/test_tablemodel_insert.py @@ -17,24 +17,23 @@ # import numpy as np -from datetime import date -from iotdb.Session import Session +from iotdb.table_session import TableSession, TableSessionConfig from iotdb.utils.BitMap import BitMap from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.Tablet import Tablet, ColumnType from iotdb.utils.NumpyTablet import NumpyTablet from datetime import date -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer # Test inserting tablet data -def test_insert_relational_tablet_use_tablet(): +def test_insert_use_tablet(): with IoTDBContainer("iotdb:dev") as db: db: IoTDBContainer - session = Session( - db.get_container_host_ip(), db.get_exposed_port(6667), sql_dialect="table" + config = TableSessionConfig( + node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"] ) - session.open() + session = TableSession(config) # Preparation before testing session.execute_non_query_statement( @@ -322,7 +321,7 @@ def test_insert_relational_tablet_use_tablet(): tablet = Tablet( table_name, column_names, data_types, values, timestamps, column_types ) - session.insert_relational_tablet(tablet) + session.insert(tablet) # Calculate the number of rows in the actual time series actual = 0 with session.execute_query_statement( @@ -601,7 +600,7 @@ def test_insert_relational_tablet_use_tablet(): tablet = Tablet( table_name, column_names, data_types, values, timestamps, column_types ) - session.insert_relational_tablet(tablet) + session.insert(tablet) # Calculate the number of rows in the actual time series actual = 0 with session.execute_query_statement( @@ -621,10 +620,10 @@ def test_insert_relational_tablet_use_tablet(): def test_insert_relational_tablet_use_numpy_tablet(): with IoTDBContainer("iotdb:dev") as db: db: IoTDBContainer - session = Session( - db.get_container_host_ip(), db.get_exposed_port(6667), sql_dialect="table" + config = TableSessionConfig( + node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"] ) - session.open() + session = TableSession(config) # Preparation before testing session.execute_non_query_statement( @@ -852,7 +851,7 @@ def test_insert_relational_tablet_use_numpy_tablet(): np_timestamps, column_types=column_types, ) - session.insert_relational_tablet(np_tablet) + session.insert(np_tablet) # Calculate the number of rows in the actual time series actual = 0 with session.execute_query_statement( @@ -1083,7 +1082,7 @@ def test_insert_relational_tablet_use_numpy_tablet(): bitmaps=np_bitmaps_, column_types=column_types, ) - session.insert_relational_tablet(np_tablet) + session.insert(np_tablet) # Calculate the number of rows in the actual time series actual = 0 with session.execute_query_statement( @@ -1103,10 +1102,10 @@ def test_insert_relational_tablet_use_numpy_tablet(): def test_insert_relational_tablet_auto_create(): with IoTDBContainer("iotdb:dev") as db: db: IoTDBContainer - session = Session( - db.get_container_host_ip(), db.get_exposed_port(6667), sql_dialect="table" + config = TableSessionConfig( + node_urls=[f"{db.get_container_host_ip()}:{db.get_exposed_port(6667)}"] ) - session.open() + session = TableSession(config) # Preparation before testing session.execute_non_query_statement( @@ -1382,7 +1381,7 @@ def test_insert_relational_tablet_auto_create(): tablet = Tablet( table_name, column_names, data_types, values, timestamps, column_types ) - session.insert_relational_tablet(tablet) + session.insert(tablet) # 2、Test inserting NumpyTablet data(Insert 10 times) for i in range(1, 10): @@ -1588,6 +1587,6 @@ def test_insert_relational_tablet_auto_create(): np_timestamps, column_types=column_types, ) - session.insert_relational_tablet(np_tablet) + session.insert(np_tablet) session.close() diff --git a/iotdb-client/client-py/tests/integration/test_tablet.py b/iotdb-client/client-py/tests/integration/test_tablet.py index 8a035318d9f3..f91c1bc6dd64 100644 --- a/iotdb-client/client-py/tests/integration/test_tablet.py +++ b/iotdb-client/client-py/tests/integration/test_tablet.py @@ -19,7 +19,7 @@ import pandas as pd from pandas.testing import assert_frame_equal -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer from iotdb.Session import Session from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.Tablet import Tablet diff --git a/iotdb-client/client-py/tests/integration/test_todf.py b/iotdb-client/client-py/tests/integration/test_todf.py index 03e97974776a..9cd8ad26e6ba 100644 --- a/iotdb-client/client-py/tests/integration/test_todf.py +++ b/iotdb-client/client-py/tests/integration/test_todf.py @@ -22,7 +22,7 @@ import pandas as pd from pandas.testing import assert_frame_equal -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer from iotdb.Session import Session from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor from iotdb.utils.Tablet import Tablet diff --git a/iotdb-client/client-py/tests/integration/test_treemodel_insert.py b/iotdb-client/client-py/tests/integration/test_treemodel_insert.py index 5d65543af29d..9feda17133f9 100644 --- a/iotdb-client/client-py/tests/integration/test_treemodel_insert.py +++ b/iotdb-client/client-py/tests/integration/test_treemodel_insert.py @@ -17,9 +17,8 @@ # import numpy as np -from iotdb.IoTDBContainer import IoTDBContainer +from .iotdb_container import IoTDBContainer from iotdb.Session import Session -from iotdb.utils.IoTDBConstants import TSDataType from iotdb.utils.Tablet import Tablet from datetime import date from iotdb.utils.BitMap import BitMap