import IDBSQLSession from '@databricks/sql/dist/contracts/IDBSQLSession'; import IOperation from '@databricks/sql/dist/contracts/IOperation'; import { getDataBricksConnection, databricksDisconnect } from './databricks-connector'; const containsClientIdOrClient = (whereClause: string): boolean => { let query = whereClause.trim(); //wiping off whitespaces between words in the query query = query.replace(/\s+/g, ''); return (query.indexOf('CLIENT_ID=') !== -1) || (query.indexOf('CLIENT=') !== -1); } /** * Verifying if the database is client_catalog, then clint or client_id should be in the where clause * @param query */ const validateQuery = (dbxQuery: string) : void => { let query = dbxQuery.toUpperCase(); if (query.includes('CLIENT_CATALOG')) { if(query.includes('INSERT')){ if(!query.includes('CLIENT_ID')){ throw new Error('Insert queries in client_catalog database must contain CLIENT_ID.'); } } else { const whereIndex = query.indexOf('WHERE'); if (whereIndex !== -1) { const whereClause = query.substring(whereIndex + 5); if (!containsClientIdOrClient(whereClause)) { throw new Error('Query must contain client_id or client in the WHERE clause when using client_catalog.'); } } else { throw new Error('Query must contain a WHERE clause when using client_catalog.'); } } } return; } /** * It will exectue the query on the given connection * @stable * @async * @param connection * @param query * @param runAsync */ export const databricksExecute = async (connection: any, query: string, runAsync: boolean = true) => { let session: IDBSQLSession | undefined; let queryOperation: IOperation | undefined; try { session = await connection.openSession(); if(session){ queryOperation = await session.executeStatement( query, { runAsync: runAsync, } ); const result = await queryOperation.fetchAll(); console.log("Fetched result for the given query"); return result; } } catch(error) { console.error("Error: " + error) console.error("Executed Query : " + query); throw new Error("Error in databricksExecute: " + error); } finally { try { if (session) { if(queryOperation){ await queryOperation.close(); } await session.close(); } } catch (closeError) { console.error("Error closing session: " + closeError); throw new Error("Error closing session: " + closeError); } } } /** * It will create connection and disconnect after exectuing the query * @stable * @async * @param client * @param query * @param runAsync * @param warehouseId */ export const createDatabricksConnectionAndExecute = async (client: string, query: string, runAsync: boolean = true, warehouseId: string | null = null) => { let connection: any | undefined; try { connection = await getDataBricksConnection(warehouseId); let result = await databricksExecute(connection, query, runAsync); return { client, query, result }; } catch (err) { console.log("Error: " + err) return { client, query } } finally { if(connection){ databricksDisconnect(connection); } } } /** * Will accept list of client array and execute for all the clients by creating a seprate connection in parallel, callback will be called after each query execution for each client * This callback calls with (client, query, result) so that you process the data as in when it is getting exectued. * @stable * @async * @param clientsArray * @param query * @param runAsync * @param warehouseId */ export const databricksExecuteForMultiClients = async (clientsArray: Array, query: string, runAsync: boolean = true, warehouseId: string | null = null) => { let promiseArray: Array> = []; clientsArray.map(async (client: string) => { promiseArray.push(createDatabricksConnectionAndExecute(client, query, runAsync, warehouseId)) }); return promiseArray; } /** * This will run queries on the same connection in parallel and return array of promises * @stable * @async * @param connection * @param queries * @param runAsync */ export const databricksParallelQueriesRunner = (connection: any, queries: Array, runAsync: boolean = true) => { let promiseArray: Array> = []; for (let i = 0; i < queries.length; i++) { promiseArray.push(databricksExecute(connection, queries[i], runAsync)); } return Promise.all(promiseArray).catch((err)=>{ console.log({err}); console.log({queries}); }); }