mostlyai 5.4.2__tar.gz → 5.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. {mostlyai-5.4.2 → mostlyai-5.5.1}/PKG-INFO +3 -3
  2. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/__init__.py +1 -1
  3. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/auto_detect.py +4 -0
  4. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/base.py +51 -36
  5. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/bigquery.py +1 -1
  6. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/databricks.py +9 -9
  7. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/hive.py +4 -0
  8. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/redshift.py +3 -3
  9. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/non_context.py +242 -241
  10. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/jobs.py +7 -1
  11. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/plan.py +12 -2
  12. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_finalize_generation.py +78 -98
  13. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_finalize_training.py +49 -30
  14. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_pull_training_data.py +5 -1
  15. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/generators.py +11 -9
  16. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/api.py +6 -3
  17. {mostlyai-5.4.2 → mostlyai-5.5.1}/pyproject.toml +3 -3
  18. {mostlyai-5.4.2 → mostlyai-5.5.1}/.gitignore +0 -0
  19. {mostlyai-5.4.2 → mostlyai-5.5.1}/LICENSE +0 -0
  20. {mostlyai-5.4.2 → mostlyai-5.5.1}/README.md +0 -0
  21. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/__init__.py +0 -0
  22. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/base.py +0 -0
  23. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/context.py +0 -0
  24. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/conversions.py +0 -0
  25. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/__init__.py +0 -0
  26. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/mssql.py +0 -0
  27. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/mysql.py +0 -0
  28. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/oracle.py +0 -0
  29. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/postgresql.py +0 -0
  30. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/snowflake.py +0 -0
  31. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/sqlite.py +0 -0
  32. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/db/types_coercion.py +0 -0
  33. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/dtype.py +0 -0
  34. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/exceptions.py +0 -0
  35. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/__init__.py +0 -0
  36. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/base.py +0 -0
  37. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/__init__.py +0 -0
  38. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/aws.py +0 -0
  39. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/azure.py +0 -0
  40. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/bucket_based.py +0 -0
  41. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/gcs.py +0 -0
  42. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/container/minio.py +0 -0
  43. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/table/__init__.py +0 -0
  44. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/table/csv.py +0 -0
  45. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/table/feather.py +0 -0
  46. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/table/json.py +0 -0
  47. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/table/parquet.py +0 -0
  48. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/file/utils.py +0 -0
  49. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/language_model.py +0 -0
  50. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/metadata_objects.py +0 -0
  51. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/progress_callback.py +0 -0
  52. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/pull.py +0 -0
  53. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/pull_context.py +0 -0
  54. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/pull_utils.py +0 -0
  55. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/push.py +0 -0
  56. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/util/__init__.py +0 -0
  57. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/util/common.py +0 -0
  58. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_data/util/kerberos.py +0 -0
  59. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/__init__.py +0 -0
  60. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/cli.py +0 -0
  61. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/connectors.py +0 -0
  62. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/__init__.py +0 -0
  63. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/migration.py +0 -0
  64. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_analyze_training_data.py +0 -0
  65. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_create_data_report.py +0 -0
  66. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_create_model_report.py +0 -0
  67. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_deliver_data.py +0 -0
  68. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_encode_training_data.py +0 -0
  69. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_generate_data.py +0 -0
  70. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_generate_model_report_data.py +0 -0
  71. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/execution/step_train_model.py +0 -0
  72. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/progress.py +0 -0
  73. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/routes.py +0 -0
  74. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/server.py +0 -0
  75. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/storage.py +0 -0
  76. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/_local/synthetic_datasets.py +0 -0
  77. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/__init__.py +0 -0
  78. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/_base_utils.py +0 -0
  79. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/_naming_conventions.py +0 -0
  80. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/_utils.py +0 -0
  81. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/artifacts.py +0 -0
  82. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/base.py +0 -0
  83. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/connectors.py +0 -0
  84. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/datasets.py +0 -0
  85. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/exceptions.py +0 -0
  86. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/generators.py +0 -0
  87. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/client/synthetic_datasets.py +0 -0
  88. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/domain.py +0 -0
  89. {mostlyai-5.4.2 → mostlyai-5.5.1}/mostlyai/sdk/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mostlyai
3
- Version: 5.4.2
3
+ Version: 5.5.1
4
4
  Summary: Synthetic Data SDK
5
5
  Project-URL: homepage, https://app.mostly.ai/
6
6
  Project-URL: repository, https://github.com/mostly-ai/mostlyai
@@ -60,7 +60,7 @@ Requires-Dist: fastapi<0.116,>=0.115.6; extra == 'local'
60
60
  Requires-Dist: filelock>=3.16.1; extra == 'local'
61
61
  Requires-Dist: gcsfs>=2023.1.0; extra == 'local'
62
62
  Requires-Dist: joblib>=1.4.2; extra == 'local'
63
- Requires-Dist: mostlyai-engine==1.6.1; extra == 'local'
63
+ Requires-Dist: mostlyai-engine==1.7.0; extra == 'local'
64
64
  Requires-Dist: mostlyai-qa==1.9.8; extra == 'local'
65
65
  Requires-Dist: networkx<4,>=3.0; extra == 'local'
66
66
  Requires-Dist: openpyxl>=3.1.5; extra == 'local'
@@ -82,7 +82,7 @@ Requires-Dist: fastapi<0.116,>=0.115.6; extra == 'local-gpu'
82
82
  Requires-Dist: filelock>=3.16.1; extra == 'local-gpu'
83
83
  Requires-Dist: gcsfs>=2023.1.0; extra == 'local-gpu'
84
84
  Requires-Dist: joblib>=1.4.2; extra == 'local-gpu'
85
- Requires-Dist: mostlyai-engine[gpu]==1.6.1; extra == 'local-gpu'
85
+ Requires-Dist: mostlyai-engine[gpu]==1.7.0; extra == 'local-gpu'
86
86
  Requires-Dist: mostlyai-qa==1.9.8; extra == 'local-gpu'
87
87
  Requires-Dist: networkx<4,>=3.0; extra == 'local-gpu'
88
88
  Requires-Dist: openpyxl>=3.1.5; extra == 'local-gpu'
@@ -49,4 +49,4 @@ For more information, visit: https://mostly-ai.github.io/mostlyai/
49
49
  from mostlyai.sdk.client.api import MostlyAI
50
50
 
51
51
  __all__ = ["MostlyAI"]
52
- __version__ = "5.4.2" # Do not set this manually. Use poetry version [params].
52
+ __version__ = "5.5.1" # Do not set this manually. Use poetry version [params].
@@ -84,6 +84,10 @@ def auto_detect_encoding_type(x: pd.Series):
84
84
  x = x.astype(str)
85
85
  x = x[x.str.strip() != ""] # filter out empty and whitespace-only strings
86
86
 
87
+ # if all values are null or empty, default to categorical
88
+ if len(x) == 0:
89
+ return ModelEncodingType.tabular_categorical
90
+
87
91
  # if all non-null values can be converted to datetime -> datetime encoding
88
92
  if safe_convert_datetime(x).notna().all():
89
93
  return ModelEncodingType.tabular_datetime
@@ -877,6 +877,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
877
877
  def __init__(self, *args, **kwargs):
878
878
  self.is_view = kwargs.get("is_view", False)
879
879
  self.lazy_fetch_primary_key = kwargs.get("lazy_fetch_primary_key", True)
880
+ self.name_in_db = kwargs.get("name_in_db") # actual db table name, may differ from self.name
880
881
  super().__init__(*args, **kwargs)
881
882
 
882
883
  def __repr__(self):
@@ -896,11 +897,16 @@ class SqlAlchemyTable(DBTable, abc.ABC):
896
897
 
897
898
  #################### READ QUERY BUILDING & UTILS ####################
898
899
 
900
+ @property
901
+ def db_table_name(self) -> str:
902
+ # use name_in_db (actual db table name) if set, otherwise fall back to self.name
903
+ return self.name_in_db or self.name
904
+
899
905
  @property
900
906
  def _sa_table(self):
901
907
  with self.container.use_sa_engine() as sa_engine:
902
908
  return sa.Table(
903
- self.name,
909
+ self.db_table_name,
904
910
  self.container.sa_metadata,
905
911
  autoload_with=sa_engine,
906
912
  )
@@ -1125,38 +1131,47 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1125
1131
  fetch_chunk_size = fetch_chunk_size if fetch_chunk_size is not None else 100_000
1126
1132
  yield_chunk_size = yield_chunk_size if yield_chunk_size is not None else fetch_chunk_size
1127
1133
  stmt = self._sa_select(columns)
1128
- with self.container.use_sa_engine() as sa_engine:
1129
- chunk_idx = 0
1130
- total_time = 0
1131
- session = sessionmaker(bind=sa_engine)()
1132
- conn = session.connection()
1133
- results_generator = pd.read_sql_query(stmt, conn, dtype_backend="pyarrow", chunksize=fetch_chunk_size)
1134
- chunks_df = pd.DataFrame()
1135
- for chunk_df in results_generator:
1136
- t0 = time.time()
1137
- if where is not None:
1138
- where_column, where_values = next(iter(where.items()))
1139
- chunk_df = chunk_df[chunk_df[where_column].isin(where_values)]
1140
- if do_coerce_dtypes:
1141
- chunk_df = coerce_dtypes_by_encoding(chunk_df, self.encoding_types)
1142
- # accumulate chunks
1143
- chunks_df = pd.concat([chunks_df, chunk_df], ignore_index=True)
1144
- if len(chunks_df) >= yield_chunk_size:
1145
- # yield data once it reaches the yield_chunk_size
1134
+ chunk_idx = 0
1135
+ total_time = 0
1136
+ # use context managers to ensure proper cleanup of engine and session
1137
+ with (
1138
+ self.container.use_sa_engine() as sa_engine,
1139
+ sessionmaker(bind=sa_engine)() as session,
1140
+ ):
1141
+ results_generator = None
1142
+ try:
1143
+ conn = session.connection()
1144
+ results_generator = pd.read_sql_query(stmt, conn, dtype_backend="pyarrow", chunksize=fetch_chunk_size)
1145
+ chunks_df = pd.DataFrame()
1146
+ for chunk_df in results_generator:
1147
+ t0 = time.time()
1148
+ if where is not None:
1149
+ where_column, where_values = next(iter(where.items()))
1150
+ chunk_df = chunk_df[chunk_df[where_column].isin(where_values)]
1151
+ if do_coerce_dtypes:
1152
+ chunk_df = coerce_dtypes_by_encoding(chunk_df, self.encoding_types)
1153
+ # accumulate chunks
1154
+ chunks_df = pd.concat([chunks_df, chunk_df], ignore_index=True)
1155
+ if len(chunks_df) >= yield_chunk_size:
1156
+ # yield data once it reaches the yield_chunk_size
1157
+ yield chunks_df
1158
+ chunks_df = pd.DataFrame()
1159
+ chunk_idx += 1
1160
+ total_time += time.time() - t0
1161
+ if chunk_idx % 10 == 0:
1162
+ _LOG.info(f"processed {chunk_idx} chunks in {total_time:.2f}s")
1163
+ if len(chunks_df) > 0 or len(chunks_df.columns) > 0:
1164
+ # yield the remaining data
1146
1165
  yield chunks_df
1147
- chunks_df = pd.DataFrame()
1148
- chunk_idx += 1
1149
- total_time += time.time() - t0
1150
- if chunk_idx % 10 == 0:
1151
- _LOG.info(f"processed {chunk_idx} chunks in {total_time:.2f}s")
1152
- if len(chunks_df) > 0 or len(chunks_df.columns) > 0:
1153
- # yield the remaining data
1154
- yield chunks_df
1155
- _LOG.info(f"finished reading {chunk_idx} chunks in {time.time() - t00:.2f}s (strategy=scan)")
1166
+ _LOG.info(f"finished reading {chunk_idx} chunks in {time.time() - t00:.2f}s (strategy=scan)")
1167
+ finally:
1168
+ # explicitly close the results generator to prevent connection leaks
1169
+ if results_generator is not None:
1170
+ results_generator.close()
1156
1171
 
1157
1172
  def is_column_indexed(self, column: str) -> bool:
1158
1173
  with self.container.use_sa_engine() as sa_engine:
1159
- indexes = sa.inspect(sa_engine).get_indexes(table_name=self.name, schema=self.container.dbschema)
1174
+ indexes = sa.inspect(sa_engine).get_indexes(table_name=self.db_table_name, schema=self.container.dbschema)
1160
1175
  for index in indexes:
1161
1176
  if column in index.get("column_names", []):
1162
1177
  return True
@@ -1214,7 +1229,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1214
1229
  do_coerce_dtypes: bool | None = False,
1215
1230
  ) -> pd.DataFrame:
1216
1231
  t0 = time.time()
1217
- _LOG.info(f"read from table `{self.name}` started")
1232
+ _LOG.info(f"read from table `{self.db_table_name}` started")
1218
1233
  columns = columns if columns is not None else self.columns
1219
1234
  stmt = self._sa_select(columns)
1220
1235
  stmts = self._sa_where(stmt, where)
@@ -1232,7 +1247,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1232
1247
  df = self._df_limit(df, limit)
1233
1248
  if do_coerce_dtypes:
1234
1249
  df = coerce_dtypes_by_encoding(df, self.encoding_types)
1235
- _LOG.info(f"read DB data `{self.name}` {df.shape} in {time.time() - t0:.2f}s")
1250
+ _LOG.info(f"read DB data `{self.db_table_name}` {df.shape} in {time.time() - t0:.2f}s")
1236
1251
  return df
1237
1252
 
1238
1253
  def create_table(self, df: pd.DataFrame | None = None, **kwargs) -> None:
@@ -1245,7 +1260,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1245
1260
  with self.container.use_sa_engine(mode="write_data") as sa_engine:
1246
1261
  try:
1247
1262
  df.head(0).to_sql(
1248
- name=self.name,
1263
+ name=self.db_table_name,
1249
1264
  con=sa_engine,
1250
1265
  schema=self.container.dbschema,
1251
1266
  index=False,
@@ -1257,7 +1272,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1257
1272
  raise
1258
1273
 
1259
1274
  dtypes_msg = f"with dtypes=`{kwargs['dtype']}`" if "dtype" in kwargs else ""
1260
- _LOG.info(f"Successfully created table `{self.name}` schema {dtypes_msg}")
1275
+ _LOG.info(f"Successfully created table `{self.db_table_name}` schema {dtypes_msg}")
1261
1276
 
1262
1277
  def write_chunks(self, chunks: Iterable[pd.DataFrame], dtypes: dict[str, Any], **kwargs) -> None:
1263
1278
  _LOG.info(f"write data in {len(chunks)} chunks (n_jobs={self.WRITE_CHUNKS_N_JOBS})")
@@ -1269,7 +1284,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1269
1284
  sa_create_engine_kwargs=self.container.sa_create_engine_kwargs,
1270
1285
  sa_create_engine_connect_kwargs=self.container.sa_engine_connection_kwargs,
1271
1286
  sa_multiple_inserts=self.SA_MULTIPLE_INSERTS,
1272
- table_name=self.name,
1287
+ table_name=self.db_table_name,
1273
1288
  table_schema=self.container.dbschema,
1274
1289
  table_dtypes=dtypes,
1275
1290
  chunk_init=self.INIT_WRITE_CHUNK,
@@ -1289,7 +1304,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1289
1304
  ) -> None:
1290
1305
  t0 = time.time()
1291
1306
  assert self.is_output
1292
- _LOG.info(f"write data {df.shape} to table `{self.name}` started")
1307
+ _LOG.info(f"write data {df.shape} to table `{self.db_table_name}` started")
1293
1308
 
1294
1309
  df = _normalize_for_sql(df)
1295
1310
 
@@ -1306,7 +1321,7 @@ class SqlAlchemyTable(DBTable, abc.ABC):
1306
1321
  chunks = [df[i : i + write_chunk_size] for i in range(0, len(df), write_chunk_size)]
1307
1322
  self.write_chunks(chunks, dtypes)
1308
1323
 
1309
- _LOG.info(f"write to table `{self.name}` finished in {time.time() - t0:.2f}s")
1324
+ _LOG.info(f"write to table `{self.db_table_name}` finished in {time.time() - t0:.2f}s")
1310
1325
 
1311
1326
  def has_child(self) -> bool:
1312
1327
  return len(self.container.schema.get_relations_from_table(str(self.name))) > 0
@@ -211,7 +211,7 @@ class BigQueryTable(SqlAlchemyTable):
211
211
  _write_chunk,
212
212
  credentials_info=self.container.sa_create_engine_kwargs["credentials_info"],
213
213
  dataframe=chunk,
214
- table_id=f"{self.container.dbschema}.{self.name}",
214
+ table_id=f"{self.container.dbschema}.{self.db_table_name}",
215
215
  if_exists=True,
216
216
  )
217
217
 
@@ -282,7 +282,7 @@ class DatabricksTable(SqlAlchemyTable):
282
282
  if_exists = kwargs.get("if_exists", "append")
283
283
  with self.container.init_sa_connection() as connection:
284
284
  cursor = connection.connection.cursor()
285
- cursor.columns(schema_name=self.container.dbschema, table_name=self.name)
285
+ cursor.columns(schema_name=self.container.dbschema, table_name=self.db_table_name)
286
286
  result = cursor.fetchall()
287
287
  if result: # the table already exists
288
288
  if if_exists == "replace":
@@ -291,9 +291,9 @@ class DatabricksTable(SqlAlchemyTable):
291
291
  elif if_exists == "fail":
292
292
  raise MostlyDataException("Destination location already exists.")
293
293
  # create an empty table without schema
294
- query = f"CREATE TABLE IF NOT EXISTS {self.container.dbname}.{self.container.dbschema}.{self.name}"
294
+ query = f"CREATE TABLE IF NOT EXISTS {self.container.dbname}.{self.container.dbschema}.{self.db_table_name}"
295
295
  cursor.execute(query)
296
- _LOG.info(f"created table `{self.name}` under schema `{self.container.dbschema}`")
296
+ _LOG.info(f"created table `{self.db_table_name}` under schema `{self.container.dbschema}`")
297
297
 
298
298
  def create_volume(self, volume_name) -> None:
299
299
  self.drop_volume_if_exists(volume_name)
@@ -307,20 +307,20 @@ class DatabricksTable(SqlAlchemyTable):
307
307
  _LOG.info(f"dropped temporary volume `{volume_name}` if existed")
308
308
 
309
309
  def drop_table_if_exists(self) -> None:
310
- query = f"DROP TABLE IF EXISTS {self.container.dbname}.{self.container.dbschema}.{self.name};"
310
+ query = f"DROP TABLE IF EXISTS {self.container.dbname}.{self.container.dbschema}.{self.db_table_name};"
311
311
  self._execute(query)
312
- _LOG.info(f"dropped table `{self.name}` if existed")
312
+ _LOG.info(f"dropped table `{self.db_table_name}` if existed")
313
313
 
314
314
  def copy_data_from_volume_to_table(self, volume_name: str) -> None:
315
315
  query = (
316
- f"COPY INTO {self.container.dbname}.{self.container.dbschema}.{self.name} "
316
+ f"COPY INTO {self.container.dbname}.{self.container.dbschema}.{self.db_table_name} "
317
317
  f"FROM '/Volumes/{self.container.dbname}/{self.container.dbschema}/{volume_name}/' "
318
318
  "FILEFORMAT = PARQUET "
319
319
  "FORMAT_OPTIONS ('inferSchema' = 'true') "
320
320
  "COPY_OPTIONS ('mergeSchema' = 'true');"
321
321
  )
322
322
  self._execute(query)
323
- _LOG.info(f"copied data from volume `{volume_name}` to table `{self.name}`")
323
+ _LOG.info(f"copied data from volume `{volume_name}` to table `{self.db_table_name}`")
324
324
 
325
325
  def write_data(
326
326
  self,
@@ -330,7 +330,7 @@ class DatabricksTable(SqlAlchemyTable):
330
330
  ) -> None:
331
331
  t0 = time.time()
332
332
  assert self.is_output
333
- _LOG.info(f"write data {df.shape} to table `{self.name}` started")
333
+ _LOG.info(f"write data {df.shape} to table `{self.db_table_name}` started")
334
334
  # include a unique timestamp in the volume name to avoid conflicts
335
335
  volume_name = f"{self.name}_temp_{time.time_ns()}"
336
336
  try:
@@ -340,7 +340,7 @@ class DatabricksTable(SqlAlchemyTable):
340
340
  self.write_data_to_volume(df=df, volume_name=volume_name)
341
341
  self.copy_data_from_volume_to_table(volume_name)
342
342
 
343
- _LOG.info(f"write to table `{self.name}` finished in {time.time() - t0:.2f}s")
343
+ _LOG.info(f"write to table `{self.db_table_name}` finished in {time.time() - t0:.2f}s")
344
344
  except Exception as e:
345
345
  if if_exists != "append":
346
346
  self.drop_table_if_exists()
@@ -172,6 +172,10 @@ class HiveContainer(SqlAlchemyContainer):
172
172
  def table_class(cls):
173
173
  return HiveTable
174
174
 
175
+ def use_sa_engine(self, mode="read_data", dispose=False):
176
+ # always keep the engine alive to reuse the cached connection
177
+ return super().use_sa_engine(mode=mode, dispose=dispose)
178
+
175
179
  def does_database_exist(self) -> bool:
176
180
  with self.init_sa_connection("db_exist_check") as connection:
177
181
  result = connection.execute(sa.text("SHOW DATABASES"))
@@ -379,9 +379,9 @@ class RedshiftTable(PostgresqlTable):
379
379
  available_tables = inspector.get_table_names(schema=schema)
380
380
 
381
381
  # redshift dialect's has_table is case-insensitive, so match accordingly
382
- actual_table_name = self.name
382
+ actual_table_name = self.db_table_name
383
383
  for table in available_tables:
384
- if table.lower() == self.name.lower():
384
+ if table.lower() == self.db_table_name.lower():
385
385
  actual_table_name = table
386
386
  break
387
387
 
@@ -407,7 +407,7 @@ class RedshiftTable(PostgresqlTable):
407
407
 
408
408
  # find table case-insensitively and drop using sqlalchemy
409
409
  for t in existing_tables:
410
- if t.lower() == self.name.lower():
410
+ if t.lower() == self.db_table_name.lower():
411
411
  # use sqlalchemy Table object for safe drop operation
412
412
  table_to_drop = sa.Table(t, sa.MetaData(), schema=schema)
413
413
  table_to_drop.drop(sa_engine, checkfirst=False)