<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Impala ODBC driver and python query with params in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394256#M248684</link>
    <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/117576"&gt;@disoardi&lt;/a&gt;&amp;nbsp;Parametric queries do only work if the "UseNativeQuery" option is set to 0, this is the default (but you might have it set to 1 in the DSN configuration).&lt;/P&gt;&lt;P&gt;Yo could try connecting with:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;crsr = pyodbc.connect('DSN=impala;UseNativeQuery=0', autocommit=True).cursor()&lt;/LI-CODE&gt;&lt;P&gt;See page 84 of the "&lt;SPAN&gt;Cloudera ODBC Connector for Apache Impala Installation and Configuration Guide" for the full description of this option.&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Sep 2024 23:51:30 GMT</pubDate>
    <dc:creator>zegab</dc:creator>
    <dc:date>2024-09-30T23:51:30Z</dc:date>
    <item>
      <title>Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394029#M248647</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to submit a query with params via ODBC to Impala. Thi is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pyodbc

# connect
crsr = pyodbc.connect('DSN=impala', autocommit=True).cursor()
param1 = 1000000000
param2 = '2023-01-01'

# long query
query = """
select *
from target_test.test d 
left outer join 
  (select cli, fsc, nom_rag_soc, dat_ini_val, dat_ccz_inl, dat_csz_efv, dta_cessata, sta, prg_var, cod_cau_csz_dl, rn 
  from 
    (select 
      c2.cod_cli as cli, 
      trim(c2.cod_fsc) as fsc,
      trim(c2.nom_rag_soc) as nom_rag_soc,
      c2.dat_ini_val,
      dat_ccz_inl,
      dat_csz_efv, 
      (case when c2.dat_csz_efv= '1900-01-01 00:00:00' then '2999-01-01 00:00:00' else c2.dat_csz_efv end) as dta_cessata,
      sta,
      prg_var,
      cod_cau_csz_dl,
      row_number() over (partition by cod_fsc order by (case when c2.dat_csz_efv='1900-01-01 00:00:00' then '2999-01-01 00:00:00' else c2.dat_csz_efv end) desc) as rn
    from target_test.test2 c2 
    where  
      c2.sta='c' 
      and c2.dat_ini_val = (select max(c1.dat_ini_val) from target_test.test3 c1 where c1.sta='c' and c2.cod_cli=c1.cod_cli and c2.cod_fsc=c1.cod_fsc) 
      and prg_var = (select max(prg_var) from target_test.v_padln_t_cliente xc where c2.cod_cli = xc.cod_cli and sta ='c' and xc.dat_ini_val = c2.dat_ini_val) 
    ) a 
    where rn=1 
  ) k 
on trim(d.cod_fsc)=k.fsc
  where 
    d.prg_rig &amp;lt; ?
    and d.dat_cre_flu &amp;gt;= ?
    and d.tip_flu = 'f24';
"""

try:
  res = crsr.execute(query, param1, param2).fetchall
  print(res)
except pyodbc.Error as e:
  e.args = (e.args[1],)
  raise&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this is the output:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="disoardi_0-1727356242556.png" style="width: 400px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/41889i77F9A49EB0966B70/image-size/medium?v=v2&amp;amp;px=400" role="button" title="disoardi_0-1727356242556.png" alt="disoardi_0-1727356242556.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I installed this driver: ClouderaImpalaODBC-2.7.1.1009-1.x86_64.rpm&lt;/P&gt;&lt;P&gt;If I try whit simple query, without subqueries, it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 13:25:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394029#M248647</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-09-26T13:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394035#M248650</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/117576"&gt;@disoardi&lt;/a&gt;,&amp;nbsp;Welcome to our community! To help you get the best possible answer, I have tagged our Impala experts&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/75213"&gt;@ChethanYM&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/91929"&gt;@SVB&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/12885"&gt;@mszurap&lt;/a&gt;&amp;nbsp;&amp;nbsp;who may be able to assist you further.&lt;BR /&gt;&lt;BR /&gt;Please feel free to provide any additional information or details about your query, and we hope that you will find a satisfactory solution to your question.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 14:10:37 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394035#M248650</guid>
      <dc:creator>VidyaSargur</dc:creator>
      <dc:date>2024-09-26T14:10:37Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394192#M248677</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/32119"&gt;@VidyaSargur&lt;/a&gt; and thanks for your suggestion.&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/75213"&gt;@ChethanYM&lt;/a&gt;, &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/91929"&gt;@SVB&lt;/a&gt; or &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/12885"&gt;@mszurap&lt;/a&gt; any ideas about this issue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2024 13:09:25 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394192#M248677</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-09-30T13:09:25Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394256#M248684</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/117576"&gt;@disoardi&lt;/a&gt;&amp;nbsp;Parametric queries do only work if the "UseNativeQuery" option is set to 0, this is the default (but you might have it set to 1 in the DSN configuration).&lt;/P&gt;&lt;P&gt;Yo could try connecting with:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;crsr = pyodbc.connect('DSN=impala;UseNativeQuery=0', autocommit=True).cursor()&lt;/LI-CODE&gt;&lt;P&gt;See page 84 of the "&lt;SPAN&gt;Cloudera ODBC Connector for Apache Impala Installation and Configuration Guide" for the full description of this option.&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2024 23:51:30 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394256#M248684</guid>
      <dc:creator>zegab</dc:creator>
      <dc:date>2024-09-30T23:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394285#M248687</link>
      <description>&lt;P&gt;I tried but the problem remains the same.&lt;/P&gt;&lt;P&gt;Thanks for the tip&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 08:49:01 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394285#M248687</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-10-01T08:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394301#M248690</link>
      <description>&lt;P&gt;The error message shows that Impala gets the query with question marks in it, which is not good, as Impala itself doesn't supports prepared statements or query parameters. All of this functionality should be done in the ODBC driver.&lt;/P&gt;&lt;P&gt;You've written that a simple query without subquery works. Does the simple query works with or without parameter substitution?&lt;/P&gt;&lt;P&gt;Since the whole prepared statement/query substitution is don by the ODBC driver, and not by Impala, you would get no performance gains from using it. So I believe this only useful if you are porting some existing code/queries to use Impala.&lt;BR /&gt;You can just use a python f-string or the .format() function to do the parameter substitution by yourself in your code, it won't hurt performance.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Oct 2024 10:29:40 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394301#M248690</guid>
      <dc:creator>zegab</dc:creator>
      <dc:date>2024-10-01T10:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394356#M248705</link>
      <description>&lt;P&gt;The simple query, without subqueries, &lt;STRONG&gt;with&lt;/STRONG&gt; params works.&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;SELECT * FROM target_test.test2 where id = ?;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;About the support of params in the ODBC driver for impala I found this: &lt;A href="https://community.cloudera.com/t5/Support-Questions/Unable-to-pass-parameter-to-Impala-from-SSRS/m-p/65620/highlight/false#M4261" target="_blank"&gt;https://community.cloudera.com/t5/Support-Questions/Unable-to-pass-parameter-to-Impala-from-SSRS/m-p/65620/highlight/false#M4261&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Quickly the ODBC driver for impala supports the standard SQLBindParameter() API to subsitute ? in the query text with parameters.&lt;/P&gt;&lt;P&gt;but this seems not to work for complex queries.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 07:33:37 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394356#M248705</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-10-02T07:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394372#M248709</link>
      <description>&lt;P&gt;I would try if replacing the sub-queries with 'WITH' statements would help. Maybe the query is just too complex for this query-rewrite/parameter substitution engine n the ODBC driver.&lt;/P&gt;&lt;P&gt;If that not helps, there are some logging options for the driver, I would use those to see if they give any useful information what is happening inside the driver.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Oct 2024 09:43:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394372#M248709</guid>
      <dc:creator>zegab</dc:creator>
      <dc:date>2024-10-02T09:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394467#M248724</link>
      <description>&lt;P&gt;I tried but not works. The complet test below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pyodbc

# connect
crsr = pyodbc.connect('DSN=impala1;', autocommit=True).cursor()

# unnamed parameters
params = ('C', 1000000000, '2023-01-01')

# short query
query = """SELECT *
        FROM test1.test1 d 
        INNER JOIN 
            (SELECT * 
            FROM test1.test2 c1
            WHERE dat_ini_val = (SELECT max(c2.dat_ini_val) 
                                FROM test1.test2 c2
                                WHERE c2.sta = ? 
                                AND c2.cod_cli=c1.cod_cli 
                                AND c1.cod_fsc=c2.cod_fsc)) c 
        ON d.cod_cli = c.cod_cli
        WHERE cast(D.cod_fsc as string) &amp;lt;&amp;gt; ''  
        AND d.prg_rig &amp;lt; ? AND d.dat_cre_flu &amp;gt;= ? AND d.TIP_FLU = 'F24';"""

try:
    res = crsr.execute(query, params).fetchall() #&amp;lt;-- this works
    print('Query simple DONE!!!') 
except pyodbc.Error as e:
    e.args = (e.args[1],)
    print(e)

# long query
query = """SELECT *
FROM test1.test1 d 
LEFT OUTER JOIN 
    (SELECT cli, fsc, nom_rag_soc, dat_ini_val, dat_ccz_inl, Dat_csz_efv, sta, prg_var, COD_CAU_CSZ_DL
     FROM 
        (SELECT c2.cod_cli AS cli, 
                TRIM(CAST(c2.cod_fsc AS STRING)) AS fsc,
                TRIM(CAST(c2.nom_rag_soc AS STRING)) AS nom_rag_soc, 
                c2.dat_ini_val, 
                c2.dat_ccz_inl, 
                c2.Dat_csz_efv,
                c2.sta, 
                c2.prg_var, 
                c2.COD_CAU_CSZ_DL
                ,(CASE WHEN c2.Dat_csz_efv = '1900-01-01 00:00:00' THEN '2999-01-01' ELSE c2.Dat_csz_efv END) AS dta_cessata
                ,ROW_NUMBER () OVER (PARTITION BY cod_fsc ORDER BY (case when c2.Dat_csz_efv='1900-01-01 00:00:00' then '2999-01-01' ELSE c2.Dat_csz_efv END) desc) rn
         FROM test1.test2 c2 
         WHERE c2.sta = ?
        ) A
    ) k 
ON TRIM(CAST(d.COD_fsc AS STRING)) = k.fsc
WHERE d.prg_rig &amp;lt; ? 
  AND d.dat_cre_flu &amp;gt;= ? 
  AND d.TIP_FLU = 'F24';"""


try:
    res = crsr.execute(query, params).fetchall() #&amp;lt;-- this does not work
    print('Query complex DONE!!!')
except pyodbc.Error as e:
    e.args = (e.args[1],)
    print(e)


query = """
WITH prova as (SELECT c2.cod_cli AS cli, 
                TRIM(CAST(c2.cod_fsc AS STRING)) AS fsc,
                TRIM(CAST(c2.nom_rag_soc AS STRING)) AS nom_rag_soc, 
                c2.dat_ini_val, 
                c2.dat_ccz_inl, 
                c2.Dat_csz_efv,
                c2.sta, 
                c2.prg_var, 
                c2.COD_CAU_CSZ_DL
                ,(CASE WHEN c2.Dat_csz_efv = '1900-01-01 00:00:00' THEN '2999-01-01' ELSE c2.Dat_csz_efv END) AS dta_cessata
                ,ROW_NUMBER () OVER (PARTITION BY cod_fsc ORDER BY (case when c2.Dat_csz_efv='1900-01-01 00:00:00' then '2999-01-01' ELSE c2.Dat_csz_efv END) desc) rn
         FROM test1.test2 c2 
         WHERE c2.sta = 'C'
)

SELECT *
FROM test1.test1 d 
LEFT OUTER JOIN 
    (SELECT cli, fsc, nom_rag_soc, dat_ini_val, dat_ccz_inl, Dat_csz_efv, sta, prg_var, COD_CAU_CSZ_DL
     FROM prova
    ) k 
ON TRIM(CAST(d.COD_fsc AS STRING)) = k.fsc
WHERE d.prg_rig &amp;lt; ? 
  AND d.dat_cre_flu &amp;gt;= ? 
  AND d.TIP_FLU = 'F24';
"""

params = (1000000000, '2023-01-01')

try:
    res = crsr.execute(query, params).fetchall() #&amp;lt;-- this does not work
    print('Query with DONE!!!')
except pyodbc.Error as e:
    e.args = (e.args[1],)
    print(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the simple query, with parameters, works, others do not&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 08:43:40 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394467#M248724</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-10-03T08:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394615#M248760</link>
      <description>&lt;P&gt;Hi-&lt;BR /&gt;&lt;BR /&gt;If you are using the latest ODBC and seeing this issue, then this could be hitting an ODBC driver 'corner case' bug. Please open a ticket with Cloudera Support, and we will take it further to the Driver Team.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Oct 2024 19:35:28 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394615#M248760</guid>
      <dc:creator>Boris G</dc:creator>
      <dc:date>2024-10-06T19:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Impala ODBC driver and python query with params</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394684#M248784</link>
      <description>&lt;P&gt;Sure, it’s the latest version available. We have opened a case to cloudera, but being Impala and python widely used together I am surprised not finding lots bug report about this.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2024 14:52:22 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Impala-ODBC-driver-and-python-query-with-params/m-p/394684#M248784</guid>
      <dc:creator>disoardi</dc:creator>
      <dc:date>2024-10-07T14:52:22Z</dc:date>
    </item>
  </channel>
</rss>

