Skip to content

Commit 9b57970

Browse files
committed
Session management created by OML in UDF
1 parent 9d95bbe commit 9b57970

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

dbt/include/oracle/macros/materializations/python_model/python.sql

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,23 @@
5656
{% macro py_script_postfix(model) %}
5757
def main(action, client_identifier, clientinfo, module):
5858
import oml
59-
try:
60-
connection = oml.core.methods._get_conn()
61-
except Exception:
62-
print("Exception getting connection object from OML client")
63-
else:
64-
session_info = {"action": action,
65-
"client_identifier": client_identifier,
66-
"clientinfo": clientinfo,
67-
"module": module}
68-
for k, v in session_info.items():
69-
try:
70-
setattr(connection, k, v)
71-
except AttributeError:
72-
print(f"Python driver does not support setting {k}")
59+
def set_connection_attributes():
60+
try:
61+
connection = oml.core.methods._get_conn()
62+
except Exception:
63+
raise
64+
else:
65+
session_info = {"action": action,
66+
"client_identifier": client_identifier,
67+
"clientinfo": clientinfo,
68+
"module": module}
69+
for k, v in session_info.items():
70+
try:
71+
setattr(connection, k, v)
72+
except AttributeError:
73+
print(f"Python driver does not support setting {k}")
74+
75+
set_connection_attributes()
7376

7477
import pandas as pd
7578
{{ build_ref_function(model ) }}
@@ -101,6 +104,12 @@ def main(action, client_identifier, clientinfo, module):
101104
self.this = this()
102105
self.is_incremental = {{ is_incremental() }}
103106

107+
def materialize(df, table, session):
108+
if isinstance(df, pd.core.frame.DataFrame):
109+
oml.create(df, table=table)
110+
elif isinstance(df, oml.core.frame.DataFrame):
111+
df.materialize(table=table)
112+
104113
{{ model.raw_code | indent(width=4, first=False, blank=True)}}
105114

106115

dbt/include/oracle/macros/materializations/table/table.sql

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,26 @@
100100

101101
{% macro py_write_table(compiled_code, target_relation, temporary=False) %}
102102
{{ compiled_code.replace(model.raw_code, "", 1) }}
103-
def materialize(df, table, session):
104-
if isinstance(df, pd.core.frame.DataFrame):
105-
oml.create(df, table=table)
106-
elif isinstance(df, oml.core.frame.DataFrame):
107-
df.materialize(table=table)
108-
109-
dbt = dbtObj(load_df_function=oml.sync)
110-
final_df = model(dbt, session=oml)
111-
112-
{{ log("Python model materialization is " ~ model.config.materialized, info=True) }}
113-
{% if model.config.materialized.lower() == 'table' %}
114-
table_name = f"{dbt.this.identifier}__dbt_tmp"
115-
{% else %}
116-
# incremental materialization
117-
{% if temporary %}
118-
table_name = "{{target_relation.identifier}}"
119-
{% else %}
120-
table_name = dbt.this.identifier
121-
{% endif %}
122-
{% endif %}
123-
materialize(final_df, table=table_name.upper(), session=oml)
124-
return pd.DataFrame.from_dict({"result": [1]})
103+
try:
104+
dbt = dbtObj(load_df_function=oml.sync)
105+
set_connection_attributes()
106+
final_df = model(dbt, session=oml)
107+
{{ log("Python model materialization is " ~ model.config.materialized, info=True) }}
108+
{% if model.config.materialized.lower() == 'table' %}
109+
table_name = f"{dbt.this.identifier}__dbt_tmp"
110+
{% else %}
111+
# incremental materialization
112+
{% if temporary %}
113+
table_name = "{{target_relation.identifier}}"
114+
{% else %}
115+
table_name = dbt.this.identifier
116+
{% endif %}
117+
{% endif %}
118+
materialize(final_df, table=table_name.upper(), session=oml)
119+
return pd.DataFrame.from_dict({"result": [1]})
120+
except Exception:
121+
raise
122+
finally:
123+
connection = oml.core.methods._get_conn()
124+
connection.close()
125125
{% endmacro %}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def model(dbt, session):
2+
dbt.config(materialized="table")
3+
dbt.config(async_flag=True)
4+
dbt.config(timeout=1800)
5+
# oml.core.DataFrame referencing a dbt-sql model
6+
sales = session.sync(query="SELECT * FROM SH.SALES")
7+
return sales

0 commit comments

Comments
 (0)