Repository: mindsdb/mindsdb

Issue:
[New Integration]: Integration with Matrixone
### Is there an existing integration?

- [X] I have searched the existing integrations.

### Use Case

**MatrixOne is a future-oriented hyper-converged cloud and edge native DBMS that supports transactional, analytical, and streaming workloads with a simplified and distributed database engine, across multiple data centers, clouds, edges and other heterogeneous infrastructures.**




### Motivation

- Below all are the amazing application of matrixone.
- For more [info ](https://github.com/matrixorigin/matrixone)

![Matrixone](https://raw.githubusercontent.com/matrixorigin/artwork/main/docs/overview/all-in-one.png)



### Implementation

_No response_

### Anything else?

_No response_


Discussion:

Resolving patch:
diff --git a/mindsdb/integrations/handlers/matrixone_handler/__about__.py b/mindsdb/integrations/handlers/matrixone_handler/__about__.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/matrixone_handler/__about__.py
@@ -0,0 +1,9 @@
+__title__ = 'MindsDB MatrixOne handler'
+__package_name__ = 'mindsdb_matrixone_handler'
+__version__ = '0.0.1'
+__description__ = "MindsDB handler for Matrixone"
+__author__ = 'Parthiv Makwana'
+__github__ = 'https://github.com/mindsdb/mindsdb'
+__pypi__ = 'https://pypi.org/project/mindsdb/'
+__license__ = 'GPL-3.0'
+__copyright__ = 'Copyright 2022- mindsdb'
diff --git a/mindsdb/integrations/handlers/matrixone_handler/__init__.py b/mindsdb/integrations/handlers/matrixone_handler/__init__.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/matrixone_handler/__init__.py
@@ -0,0 +1,23 @@
+from mindsdb.integrations.libs.const import HANDLER_TYPE
+
+from .__about__ import __version__ as version, __description__ as description
+try:
+    from .matrixone_handler import (
+        MatrixOneHandler as Handler,
+        connection_args_example,
+        connection_args
+    )
+    import_error = None
+except Exception as e:
+    Handler = None
+    import_error = e
+
+title = 'MatrixOne'
+name = 'matrixone'
+type = HANDLER_TYPE.DATA
+icon_path = 'icon.png'
+
+__all__ = [
+    'Handler', 'version', 'name', 'type', 'title', 'description',
+    'connection_args', 'connection_args_example', 'import_error', 'icon_path'
+]
diff --git a/mindsdb/integrations/handlers/matrixone_handler/matrixone_handler.py b/mindsdb/integrations/handlers/matrixone_handler/matrixone_handler.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/matrixone_handler/matrixone_handler.py
@@ -0,0 +1,215 @@
+from collections import OrderedDict
+from typing import Optional
+import pandas as pd
+import pymysql as matone
+from pymysql.cursors import DictCursor as dict
+from mindsdb_sql import parse_sql
+from mindsdb_sql.render.sqlalchemy_render import SqlalchemyRender
+from mindsdb_sql.parser.ast.base import ASTNode
+
+from mindsdb.utilities.log import log
+from mindsdb.integrations.libs.base_handler import DatabaseHandler
+from mindsdb.integrations.libs.response import (
+    HandlerStatusResponse as StatusResponse,
+    HandlerResponse as Response,
+    RESPONSE_TYPE
+)
+from mindsdb.integrations.libs.const import HANDLER_CONNECTION_ARG_TYPE as ARG_TYPE
+
+
+class MatrixOneHandler(DatabaseHandler):
+    """
+    This handler handles connection and execution of the MatrixOne statements.
+    """
+
+    name = 'matrixone'
+
+    def __init__(self, name,connection_data: Optional[dict], **kwargs):
+        super().__init__(name)
+        self.mysql_url = None
+        self.parser = parse_sql
+        self.dialect = 'mysql'
+        self.connection_data = connection_data
+        self.database = self.connection_data.get('database')
+
+        self.connection = None
+        self.is_connected = False
+
+    def connect(self):
+        if self.is_connected is True:
+            return self.connection
+
+        config = {
+            'host': self.connection_data.get('host'),
+            'port': self.connection_data.get('port'),
+            'user': self.connection_data.get('user'),
+            'password': self.connection_data.get('password'),
+            'database': self.connection_data.get('database')
+        }
+
+        ssl = self.connection_data.get('ssl')
+        if ssl is True:
+            ssl_ca = self.connection_data.get('ssl_ca')
+            ssl_cert = self.connection_data.get('ssl_cert')
+            ssl_key = self.connection_data.get('ssl_key')
+            config['client_flags'] = [matone.constants.ClientFlag.SSL]
+            if ssl_ca is not None:
+                config["ssl_ca"] = ssl_ca
+            if ssl_cert is not None:
+                config["ssl_cert"] = ssl_cert
+            if ssl_key is not None:
+                config["ssl_key"] = ssl_key
+
+        connection = matone.connect(**config)
+        self.is_connected = True
+        self.connection = connection
+        return self.connection
+
+    def disconnect(self):
+        if self.is_connected is False:
+            return
+        self.connection.close()
+        self.is_connected = False
+        return
+
+    def check_connection(self) -> StatusResponse:
+        """
+        Check the connection of the MatrixOne database
+        :return: success status and error message if error occurs
+        """
+
+        result = StatusResponse(False)
+        need_to_close = self.is_connected is False
+
+        try:
+            connection = self.connect()
+            result.success = connection.open
+        except Exception as e:
+            log.error(f'Error connecting to MatrixOne {self.connection_data["database"]}, {e}!')
+            result.error_message = str(e)
+
+        if result.success is True and need_to_close:
+            self.disconnect()
+        if result.success is False and self.is_connected is True:
+            self.is_connected = False
+
+        return result
+
+    def native_query(self, query: str) -> Response:
+        """
+        Receive SQL query and runs it
+        :param query: The SQL query to run in MatrixOne
+        :return: returns the records from the current recordset
+        """
+
+        need_to_close = self.is_connected is False
+
+        connection = self.connect()
+        with connection.cursor(cursor=dict) as cur:
+            try:
+                cur.execute(query)
+                if cur._rows:
+                    result = cur.fetchall()
+                    response = Response(
+                        RESPONSE_TYPE.TABLE,
+                        pd.DataFrame(
+                            result,
+                            # columns=[x[0] for x in cur.description]
+                        )
+                    )
+                else:
+                    response = Response(RESPONSE_TYPE.OK)
+                connection.commit()
+            except Exception as e:
+                log.error(f'Error running query: {query} on {self.connection_data["database"]}!')
+                response = Response(
+                    RESPONSE_TYPE.ERROR,
+                    error_message=str(e)
+                )
+                connection.rollback()
+
+        if need_to_close is True:
+            self.disconnect()
+
+        return response
+
+    def query(self, query: ASTNode) -> Response:
+        """
+        Retrieve the data from the SQL statement.
+        """
+        renderer = SqlalchemyRender('mysql')
+        query_str = renderer.get_string(query, with_failback=True)
+        return self.native_query(query_str)
+
+    def get_tables(self) -> Response:
+        """
+        Get a list with all of the tabels in MatrixOne
+        """
+        q = "SHOW TABLES;"
+        result = self.native_query(q)
+        df = result.data_frame
+        result.data_frame = df.rename(columns={df.columns[0]: 'table_name'})
+        return result
+
+    def get_columns(self, table_name) -> Response:
+        """
+        Show details about the table
+        """
+        q = f"SHOW COLUMNS FROM {table_name};"
+        result = self.native_query(q)
+        df=result.data_frame
+        result.data_frame=df.rename(columns={
+            df.columns[0]: 'COLUMN_NAME',
+            df.columns[1]: 'DATA TYPE'
+        })
+
+
+        return result
+
+
+connection_args = OrderedDict(
+    user={
+        'type': ARG_TYPE.STR,
+        'description': 'The user name used to authenticate with the MatrixOne server.'
+    },
+    password={
+        'type': ARG_TYPE.STR,
+        'description': 'The password to authenticate the user with the MatrixOne server.'
+    },
+    database={
+        'type': ARG_TYPE.STR,
+        'description': 'The database name to use when connecting with the MatrixOne server.'
+    },
+    host={
+        'type': ARG_TYPE.STR,
+        'description': 'The host name or IP address of the MatrixOne server. '
+    },
+    port={
+        'type': ARG_TYPE.INT,
+        'description': 'The TCP/IP port of the MatrixOne server. Must be an integer.'
+    },
+    ssl={
+        'type': ARG_TYPE.BOOL,
+        'description': 'Set it to False to disable ssl.'
+    },
+    ssl_ca={
+        'type': ARG_TYPE.PATH,
+        'description': 'Path or URL of the Certificate Authority (CA) certificate file'
+    },
+    ssl_cert={
+        'type': ARG_TYPE.PATH,
+        'description': 'Path name or URL of the server public key certificate file'
+    },
+    ssl_key={
+        'type': ARG_TYPE.PATH,
+        'description': 'The path name or URL of the server private key file'
+    }
+)
+
+connection_args_example = OrderedDict(
+    host='127.0.0.1',
+    port=6001,
+    user='dump',
+    password='111',
+    database='mo_catalog'
+)


Test patch:
diff --git a/mindsdb/integrations/handlers/matrixone_handler/tests/__init__.py b/mindsdb/integrations/handlers/matrixone_handler/tests/__init__.py
new file mode 100644
diff --git a/mindsdb/integrations/handlers/matrixone_handler/tests/test_matrixone_handler.py b/mindsdb/integrations/handlers/matrixone_handler/tests/test_matrixone_handler.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/matrixone_handler/tests/test_matrixone_handler.py
@@ -0,0 +1,59 @@
+import unittest
+
+from mindsdb.integrations.handlers.matrixone_handler.matrixone_handler import MatrixOneHandler
+from mindsdb.api.mysql.mysql_proxy.libs.constants.response_type import RESPONSE_TYPE
+
+
+class MatrixOneHandlerTest(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        cls.kwargs = {
+            "host": "localhost",
+            "port": 6001,
+            "user": "dump",
+            "password": "111",
+            "database": "mo_catalog",
+            "ssl": False
+        }
+        cls.handler = MatrixOneHandler('test_mysql_handler', cls.kwargs)
+
+
+    def test_0_connect(self):
+         self.handler.connect()
+
+    
+
+    def test_1_drop_table(self):
+        res = self.handler.query("DROP TABLE IF EXISTS PREM;")
+        assert res.type is  not RESPONSE_TYPE.ERROR 
+
+
+    def test_2_create_table(self):
+        res = self.handler.query("CREATE TABLE IF NOT EXISTS PREM (Premi varchar(50));")
+        assert res.type is not  RESPONSE_TYPE.ERROR 
+
+    def test_3_insert_table(self):
+        res = self.handler.query("INSERT INTO PREM VALUES('Radha <3 Krishna');")
+        assert res.type is not  RESPONSE_TYPE.ERROR 
+    
+
+    def test_4_get_tables(self):
+        tables = self.handler.get_tables()
+        assert tables.type is not  RESPONSE_TYPE.ERROR
+
+ 
+    def test_5_select_query(self):
+        query = "SELECT * FROM PREM;"
+        result = self.handler.native_query(query)
+        assert result.type is not  RESPONSE_TYPE.ERROR
+    
+    def test_6_get_columns(self):
+        
+        result = self.handler.get_columns('PREM')
+        assert result.type is not  RESPONSE_TYPE.ERROR
+
+    def test_7_check_connection(self):
+         self.handler.check_connection()
+
+if __name__ == '__main__':
+    unittest.main()
\ No newline at end of file