Repository: mindsdb/mindsdb

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

- [X] I have searched the existing integrations.

### Use Case

Vertica has very vast use cases and many features on its platform.
Vertica it self has ML In-Database Prediction ability but if anyone wants to use it with mindsdb then this integration  can help.
Also mindsdb has easy to use interface than Vertica..

### Motivation

I thought it would be helpful for those want to use vertica with mindsdb

### Implementation

If you want I can try to add this integration.

### Anything else?

_No response_


Discussion:
@ZoranPandovski  if you want to implement this , I can try !!


Sure

Resolving patch:
diff --git a/mindsdb/integrations/handlers/vertica_handler/__about__.py b/mindsdb/integrations/handlers/vertica_handler/__about__.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/vertica_handler/__about__.py
@@ -0,0 +1,9 @@
+__title__ = 'MindsDB Vertica handler'
+__package_name__ = 'mindsdb_vertica_handler'
+__version__ = '0.0.1'
+__description__ = "MindsDB handler for Vertica"
+__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/vertica_handler/__init__.py b/mindsdb/integrations/handlers/vertica_handler/__init__.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/vertica_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 .vertica_handler import (
+        VerticaHandler as Handler,
+        connection_args_example,
+        connection_args
+    )
+    import_error = None
+except Exception as e:
+    Handler = None
+    import_error = e
+
+title = 'Vertica'
+name = 'vertica'
+type = HANDLER_TYPE.DATA
+icon_path = 'icon.svg'
+
+__all__ = [
+    'Handler', 'version', 'name', 'type', 'title', 'description',
+    'connection_args', 'connection_args_example', 'import_error', 'icon_path'
+]
diff --git a/mindsdb/integrations/handlers/vertica_handler/vertica_handler.py b/mindsdb/integrations/handlers/vertica_handler/vertica_handler.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/vertica_handler/vertica_handler.py
@@ -0,0 +1,200 @@
+from collections import OrderedDict
+from typing import Optional
+import pandas as pd
+import vertica_python as vp
+
+
+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
+
+# from sqlalchemy_vertica.dialect_pyodbc  import VerticaDialect
+from sqla_vertica_python.vertica_python import VerticaDialect
+
+
+
+
+class VerticaHandler(DatabaseHandler):
+    """
+    This handler handles connection and execution of the Vertica statements.
+    """
+
+    name = 'vertica'
+
+    def __init__(self, name, connection_data: Optional[dict], **kwargs):
+        super().__init__(name)
+        
+        self.parser = parse_sql
+        self.dialect = 'vertica'
+        self.kwargs = kwargs
+        self.connection_data = connection_data
+        self.schema_name = connection_data['schema_name'] if 'schema_name' in connection_data else "public"
+
+        self.connection = None
+        self.is_connected = False
+
+
+    def connect(self):
+        if self.is_connected is True:
+            return self.connection
+
+        config = {
+            'host': self.connection_data['host'],
+            'port': self.connection_data['port'],
+            'user': self.connection_data['user'],
+            'password': self.connection_data['password'],
+            'database': self.connection_data['database']
+        }
+
+        connection = vp.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:
+       
+        result = StatusResponse(False)
+        need_to_close = self.is_connected is False
+
+        try:
+            connection = self.connect()
+            result.success = connection.opened()
+        except Exception as e:
+            log.error(f'Error connecting to Vertica {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 VERTICA
+        :return: returns the records from the current recordset
+        """
+
+        need_to_close = self.is_connected is False
+
+        connection = self.connect()
+        with connection.cursor() as cur:
+            try:
+                e=cur.execute(query)
+                result = e.fetchall()
+                if e.rowcount != -1:
+                    
+                    response = Response(
+                        RESPONSE_TYPE.TABLE,
+                        pd.DataFrame(
+                            result,
+                            columns=[x.name 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(VerticaDialect)
+        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 VERTICA
+        """
+        q = f'''SELECT 
+        TABLE_NAME,
+        TABLE_SCHEMA
+        from v_catalog.tables 
+        WHERE table_schema='{self.schema_name}' 
+        order by
+        table_name;'''
+
+         
+        return self.native_query(q)
+
+    def get_columns(self, table_name) -> Response:
+        """
+        Show details about the table
+        """
+        q = f'''SELECT 
+        column_name , 
+        data_type 
+        FROM v_catalog.columns 
+        WHERE table_name='{table_name}';'''
+
+        
+        return self.native_query(q)
+
+
+connection_args = OrderedDict(
+    user={
+        'type': ARG_TYPE.STR,
+        'description': 'The user name used to authenticate with the Vertica server.'
+    },
+    password={
+        'type': ARG_TYPE.STR,
+        'description': 'The password to authenticate the user with the VERTICA server.'
+    },
+    database={
+        'type': ARG_TYPE.STR,
+        'description': 'The database name to use when connecting with the VERTICA server.'
+    },
+    host={
+        'type': ARG_TYPE.STR,
+        'description': 'The host name or IP address of the VERTICA server. NOTE: use \'127.0.0.1\' instead of \'localhost\' to connect to local server.'
+    },
+    port={
+        'type': ARG_TYPE.INT,
+        'description': 'The TCP/IP port of the VERTICA server. Must be an integer.'
+    },
+    schema_name={
+        'type': ARG_TYPE.STR,
+        'description': 'Table are listed according to schema name (it is optional). Note: Default value is "public"'
+    }
+
+)
+
+connection_args_example = OrderedDict(
+    host='127.0.0.1',
+    port=5433,
+    user='root',
+    password='password',
+    database='database',
+    schema_name='xyz'
+)


Test patch:
diff --git a/mindsdb/integrations/handlers/vertica_handler/tests/__init__.py b/mindsdb/integrations/handlers/vertica_handler/tests/__init__.py
new file mode 100644
diff --git a/mindsdb/integrations/handlers/vertica_handler/tests/test_vertica_handler.py b/mindsdb/integrations/handlers/vertica_handler/tests/test_vertica_handler.py
new file mode 100644
--- /dev/null
+++ b/mindsdb/integrations/handlers/vertica_handler/tests/test_vertica_handler.py
@@ -0,0 +1,51 @@
+import unittest
+from mindsdb.integrations.handlers.vertica_handler.vertica_handler import VerticaHandler
+from mindsdb.api.mysql.mysql_proxy.libs.constants.response_type import RESPONSE_TYPE
+
+
+class VerticaHandlerTest(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        cls.kwargs = {
+            "host":'127.0.0.1',
+            "port":5433,
+            "user":'dbadmin',
+            "password":'',
+            "database":'VMart',
+            "schema_name":'public'
+    
+        }
+        cls.handler = VerticaHandler('test_vertica_handler', cls.kwargs)
+
+    def test_0_check_connection(self):
+        assert self.handler.check_connection()
+
+    def test_1_connect(self):
+        assert self.handler.connect()
+    
+    def test_2_create_table(self):
+        query = "CREATE Table TEST(id Number(1),Name Varchar(33))"
+        result = self.handler.query(query)
+        assert result.type is not RESPONSE_TYPE.ERROR 
+
+    def test_3_insert(self):
+        query = "INSERT INTO TEST (1,'lOVe yOU)"
+        result = self.handler.query(query)
+        assert result.type is not RESPONSE_TYPE.ERROR 
+
+    def test_4_native_query_select(self):
+        query = "SELECT * FROM TEST;"
+        result = self.handler.query(query)
+        assert result.type is RESPONSE_TYPE.TABLE 
+
+    def test_5_get_tables(self):
+        tables = self.handler.get_tables()
+        assert tables.type is not RESPONSE_TYPE.TABLE
+
+    def test_6_get_columns(self):
+        columns = self.handler.get_columns('TEMP')
+        assert columns.type is not RESPONSE_TYPE.ERROR
+
+
+if __name__ == '__main__':
+    unittest.main()