Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add teradara qr #4881

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added client/app/assets/images/db-logos/teradata.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 126 additions & 0 deletions redash/query_runner/teradata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from redash.query_runner import *
from redash.utils import JSONEncoder, json_dumps, json_loads
import logging

logger = logging.getLogger(__name__)

try:
import teradatasql
enabled = True
except ImportError:
enabled = False

types_map = {
'str': TYPE_STRING,
'int': TYPE_INTEGER,
'Decimal': TYPE_FLOAT,
'date': TYPE_DATE,
'datetime': TYPE_DATETIME,
'float': TYPE_FLOAT,
}

class Teradata(BaseSQLQueryRunner):
noop_query = "help session"

@classmethod
def configuration_schema(cls):
return {
'type': 'object',
'properties': {
'host': {
'type': 'string'
},
'user': {
'type': 'string'
},
'password': {
'type': 'string'
},
'logmech': {
'type': 'string',
"default": "TD2"
},
'database': {
'type': 'string'
}
},
"order": ["host", "user", "password", "logmech", "database"],
'required': ["host",'user','password'],
'secret': ['password']
}


@classmethod
def name(cls):
return "Teradata"

@classmethod
def enabled(cls):
return enabled

def get_schema(self, get_stats=False):
logger.info('Teradata is trying to get schema')
query = """
SELECT col.DataBaseName AS TABLE_SCHEMA,
col.TableName as TABLE_NAME,
col.ColumnName AS COLUMN_NAME
FROM DBC.ColumnsVX col
WHERE (DataBaseName = '{database}' OR '{database}' = '')
""".format(database=self.configuration.get('database', ""))

results, error = self._run_query_internal(query)

if error is not None:
results = json_loads(results)
schema = {}
for row in results['rows']:
if row['DatabaseName'] != None:
table_name = '{}.{}'.format(row['DatabaseName'], row['TableName'])
else:
table_name = row['TableName']

if table_name not in schema:
schema[table_name] = {'name': table_name, 'columns': []}

schema[table_name]['columns'].append(row['ColumnName'])

return schema.values()

def _get_connection(self):
connection = teradatasql.connect(
host=self.configuration.get('host', ''),
user=self.configuration.get('user', ''),
password=self.configuration.get('password', ''),
logmech=self.configuration.get('logmech', ''),
database=self.configuration.get('database', '')
)

return connection

def run_query(self, query, user):
connection = None
try:
with self._get_connection() as session:
cursor = session.cursor()
cursor.execute(query)
desc = cursor.description
data_ = cursor.fetchall()
desc_new = [(col_name[0], type(val_type)) for col_name, val_type in zip(desc, data_[0])]
if desc is not None:
columns = self.fetch_columns([(i[0], types_map.get(i[1].__name__, None)) for i in desc_new])
rows = [dict(zip((c['name'] for c in columns), row)) for row in data_]
data = {'columns': columns, 'rows': rows}
json_data = json_dumps(data)
error = None
else:
json_data = None
error = "No data was returned."
except Exception as ex:
error = ex.args[0].split('\n')[0]
json_data = None
finally:
if connection:
connection.close()
return json_data, error

register(Teradata)
1 change: 1 addition & 0 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def email_server_is_configured():
"redash.query_runner.exasol",
"redash.query_runner.cloudwatch",
"redash.query_runner.cloudwatch_insights",
"redash.query_runner.teradata",
]

enabled_query_runners = array_from_string(
Expand Down
3 changes: 2 additions & 1 deletion requirements_all_ds.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ pydgraph==2.0.2
azure-kusto-data==0.0.35
pyexasol==0.12.0
python-rapidjson==0.8.0
pyodbc==4.0.28
pyodbc==4.0.28
teradatasql==16.20.0.59