You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UPDATE with MERGE INTO and Temporary View - Execute Multiple Queries Statements #98
I need to join some tables to update data, and seems that the best approach to do it in Databricks is with MERGE INTO. However, it works fine in Databricks UI, but with Node JS connector it doesn't work because has multiples queries statements (divided by semicolons ";"). It's like this:
DROP VIEW IF EXISTS tempView;
CREATE TEMPORARY VIEW tempView AS
SELECT ... FROM table1 t1 JOIN... WHERE xyz="..." ;
MERGE INTO table2 t2 USING tempView ON t1.id = t2.id
WHEN MATCHED THEN
UPDATE SET
column1 = ...,
column2 = ...
How can I execute this query with NodeJS connector?
Hi @renatojmsantos! Just execute your queries sequentially within the same session:
constsession=awaitclient.openSession();awaitsession.executeStatement('DROP VIEW IF EXISTS tempView');awaitsession.executeStatement('CREATE TEMPORARY VIEW tempView ...');awaitsession.executeStatement('MERGE INTO table2 t2 USING tempView ...');
DBSQL UI internally does it in exactly the same way
And how can I handle with queryOperation to after fetchAll()?
I'm not sure I understand the question, but will try to answer anyway 😅
Each await session.executeStatement returns IOperation object which can be used to fetch data returned by query. If you just want to wait for query completion, you may wait for finished, like this:
constop=awaitsession.executeStatement('DROP VIEW IF EXISTS tempView');awaitop.finished();// once previous query has completed execution, you can safely run next query
Or if you want to get rows returned by a particular query, use fetchAll:
constop=awaitsession.executeStatement('MERGE INTO table2 t2 USING tempView ...');constresult=awaitop.fetchAll();
All relevant IOperation methods (fetchAll/fetchChunk/getSchema and, of course, finished) will wait for query completion.
So just do it as you'd do for executing a single query, the idea is to run all three queries within the same session, so they share the same context.
With Promise.all your queries will be executed in parallel, basically, almost simultaneously. So it's very likely that SELECT will be execute before CREATE TEMPORARY VIEW. You need to ensure that you execute queries sequentially:
asyncfunctionrunQuery(session,query){constop=awaitsession.executeStatement(query,{runAsync: true});result=awaitop.fetchAll();awaitop.close();returnresult;}awaitclient.connect({ ... }).then(async(client: any)=>{constsession=awaitclient.openSession();constqueries=queryStatement.replace(/\r?\n|\r?\s+/g," ").trim().split(";");for(constqueryofqueries){constresult=awaitrunQuery(session,query);// do something with result}awaitsession.close();});
Activity
kravets-levko commentedon Dec 15, 2022
Hi @renatojmsantos! Just execute your queries sequentially within the same session:
DBSQL UI internally does it in exactly the same way
renatojmsantos commentedon Dec 15, 2022
Hi @kravets-levko! Thank for your help. And how can I handle with queryOperation to after fetchAll()?
kravets-levko commentedon Dec 15, 2022
I'm not sure I understand the question, but will try to answer anyway 😅
Each
await session.executeStatementreturnsIOperationobject which can be used to fetch data returned by query. If you just want to wait for query completion, you may wait forfinished, like this:Or if you want to get rows returned by a particular query, use
fetchAll:All relevant
IOperationmethods (fetchAll/fetchChunk/getSchemaand, of course,finished) will wait for query completion.So just do it as you'd do for executing a single query, the idea is to run all three queries within the same session, so they share the same context.
renatojmsantos commentedon Dec 17, 2022
Thank you so much for your reply. I will try this way.
renatojmsantos commentedon Dec 20, 2022
Hi @kravets-levko! This works, but the issue that I have is related to have some relationships / dependencies between these multiple queries.
For example:
CREATE TEMPORARY VIEW tempView AS SELECT ... FROM table1 t1 JOIN... WHERE xyz="..." ;...select * from tempViewWhen it comes the second query, it doesn't know what is the
tempView...kravets-levko commentedon Dec 20, 2022
@renatojmsantos can you show your code please?
renatojmsantos commentedon Dec 20, 2022
Hi @kravets-levko, it's like this:
kravets-levko commentedon Dec 20, 2022
With
Promise.allyour queries will be executed in parallel, basically, almost simultaneously. So it's very likely thatSELECTwill be execute beforeCREATE TEMPORARY VIEW. You need to ensure that you execute queries sequentially:renatojmsantos commentedon Dec 21, 2022
Thank you so much! This worked and solved my issue. Have a great day!
kravets-levko commentedon Dec 21, 2022
Thank you @renatojmsantos! I'm glad it worked out!