mtsql 1.12.27__py3-none-any.whl → 1.12.29__py3-none-any.whl

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.
mt/sql/base.py CHANGED
@@ -29,6 +29,7 @@ __all__ = [
29
29
  "temp_table_find_new_id",
30
30
  "temp_table_drop",
31
31
  "to_temp_table",
32
+ "find_common_ids",
32
33
  ]
33
34
 
34
35
 
@@ -97,7 +98,7 @@ def run_func(
97
98
  raise
98
99
 
99
100
 
100
- def conn_ctx(engine):
101
+ def conn_ctx(engine: sa.engine.Engine):
101
102
  if isinstance(engine, sa.engine.Engine):
102
103
  return engine.begin()
103
104
  return ctx.nullcontext(engine)
@@ -456,10 +457,10 @@ def temp_table_drop(
456
457
 
457
458
  Parameters
458
459
  ----------
459
- id : int or str
460
- table id or table name. An id can be generated by invoking :func:`temp_table_find_new_id`.
461
460
  engine : sqlalchemy.engine.Engine
462
461
  connection engine to the server
462
+ id : int or str
463
+ table id or table name. An id can be generated by invoking :func:`temp_table_find_new_id`.
463
464
  logger : mt.logg.IndentedLoggerAdapter, optional
464
465
  logger for debugging
465
466
  """
@@ -494,3 +495,52 @@ def to_temp_table(df: pd.DataFrame, engine: sa.engine.Engine, **kwds):
494
495
  yield name
495
496
  finally:
496
497
  temp_table_drop(engine, tid)
498
+
499
+
500
+ def find_common_ids(
501
+ l_ids: tp.List[int],
502
+ frame_name: str,
503
+ engine: sa.engine.Engine,
504
+ schema: tp.Optional[str] = None,
505
+ id_col: str = "id",
506
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
507
+ ) -> tp.List[int]:
508
+ """Finds common ids between a list of ids and the ids in a given frame.
509
+
510
+ Parameters
511
+ ----------
512
+ l_ids : list of int
513
+ list of ids to be checked
514
+ frame_name : str
515
+ name of the frame to be checked against
516
+ engine : sqlalchemy.engine.Engine
517
+ connection engine to the server
518
+ schema : str, optional
519
+ schema of the frame. If None, the default schema is used.
520
+ id_col : str
521
+ name of the id column in the frame
522
+ logger : mt.logg.IndentedLoggerAdapter, optional
523
+ logger for debugging
524
+
525
+ Returns
526
+ -------
527
+ list of int
528
+ list of common ids
529
+ """
530
+
531
+ with conn_ctx(engine) as conn:
532
+ temp_table = create_temp_id_table(l_ids, conn, logger=logger)
533
+
534
+ full_frame_name = frame_sql(frame_name, schema=schema)
535
+
536
+ sql = f"""
537
+ SELECT t.id FROM {temp_table} AS t
538
+ INNER JOIN {full_frame_name} AS f
539
+ ON t.id = f.{id_col};
540
+ """
541
+
542
+ df_common = read_sql(sql, conn, index_col=None, logger=logger)
543
+
544
+ l_commonIds = df_common["id"].tolist()
545
+
546
+ return l_commonIds
mt/sql/psql.py CHANGED
@@ -36,6 +36,7 @@ __all__ = [
36
36
  "refresh_matview",
37
37
  "drop_matview",
38
38
  "frame_exists",
39
+ "count_estimate",
39
40
  "drop_frame",
40
41
  "list_columns_ext",
41
42
  "list_columns",
@@ -969,6 +970,39 @@ def frame_exists(
969
970
  )
970
971
 
971
972
 
973
+ def count_estimate(
974
+ frame_name,
975
+ engine,
976
+ schema: tp.Optional[str] = None,
977
+ nb_trials: int = 3,
978
+ logger: tp.Optional[logg.IndentedLoggerAdapter] = None,
979
+ ):
980
+ """Gives an estimate of the number of rows of a frame.
981
+
982
+ Parameters
983
+ ----------
984
+ frame_name: str
985
+ name of table or view
986
+ engine: sqlalchemy.engine.Engine
987
+ an sqlalchemy connection engine created by function `create_engine()`
988
+ schema: str or None
989
+ a valid schema name returned from `list_schemas()`
990
+ nb_trials: int
991
+ number of query trials
992
+ logger: mt.logg.IndentedLoggerAdapter, optional
993
+ logger for debugging
994
+
995
+ Returns
996
+ -------
997
+ retval: int
998
+ the estimated number of rows
999
+ """
1000
+ frame_sql_str = frame_sql(frame_name, schema=schema)
1001
+ query_str = f"SELECT reltuples::bigint FROM pg_class WHERE oid = '{frame_sql_str}'::regclass;"
1002
+ df = read_sql(query_str, engine, nb_trials=nb_trials, logger=logger)
1003
+ return df.iloc[0]["reltuples"]
1004
+
1005
+
972
1006
  def drop_frame(
973
1007
  frame_name,
974
1008
  engine,
mt/sql/version.py CHANGED
@@ -1,5 +1,5 @@
1
1
  MAJOR_VERSION = 1
2
2
  MINOR_VERSION = 12
3
- PATCH_VERSION = 27
3
+ PATCH_VERSION = 29
4
4
  version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
5
  __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mtsql
3
- Version: 1.12.27
3
+ Version: 1.12.29
4
4
  Summary: Extra Python modules to deal with the interaction between pandas dataframes and remote SQL servers, for Minh-Tri Pham
5
5
  Home-page: https://github.com/inteplus/mtsql
6
6
  Author: ['Minh-Tri Pham']
@@ -1,17 +1,17 @@
1
1
  mt/sql/__init__.py,sha256=b7zO50apZxt9Hg2eOkJhRLrXgACR8eS5b-Rphdn5qNQ,44
2
- mt/sql/base.py,sha256=S44nryyeguGNnzdf5ETmKXb9ewbAveiGYhEXVoS7lC8,13732
2
+ mt/sql/base.py,sha256=YM7BnNf1e3WizzH5Amg5xK-L5s0w9Cnl9-90htUqJlg,15096
3
3
  mt/sql/mysql.py,sha256=n2ENDctdUqZuSaDAcrqZYtPtawq3Wx4dOPCRsCB5Q4w,4894
4
- mt/sql/psql.py,sha256=8gWsF6ZobwkAxHsfecF95Em_cAlMitHHdQzinZLCi6s,67110
4
+ mt/sql/psql.py,sha256=U8XEyg4rQYr5gm8KohRWrpCNJKl5WC1yxJMBkkm1k_A,68125
5
5
  mt/sql/sqlite.py,sha256=T2ak_hhNi_zRfpg_gp8JhNHn7D2kl4i-Ey6-9ANMtz0,8678
6
- mt/sql/version.py,sha256=ptD5KFc_hmlAgpcycy390fxiNgeYGr3X1VE_tWMpADM,208
6
+ mt/sql/version.py,sha256=I7DJlgmw1b2vkxA_AWJYjd0FWS4W3BxvUIL3cHr587E,208
7
7
  mt/sql/redshift/__init__.py,sha256=S-eRxJWcrvncF7LZXuulCdPV-UERu9eiw6uyDb5aGsM,443
8
8
  mt/sql/redshift/commands.py,sha256=xhGUBf3bL66EYdZI5HCUtOx-XqPCnXT_P-LnhPgtzrY,40193
9
9
  mt/sql/redshift/ddl.py,sha256=eUcZj9oIajiE1wKKBAP-V64gYJ7cVnSAt8dLFfluOJA,9777
10
10
  mt/sql/redshift/dialect.py,sha256=-0JjJubZZHRw0abhl6H6rKWaUE9pKtGwAuX-62T0e_c,53399
11
11
  mt/sql/redshift/main.py,sha256=H8_5sjtJ7dzWoCMXzPNjYhrMQ18eLUQ9xg-aYl5QeTc,17104
12
12
  mt/sql/redshift/redshift-ca-bundle.crt,sha256=532qYkOpQOstFE0mdXE1GVtL3v00XDKgZNTr6gK5-KE,8621
13
- mtsql-1.12.27.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
- mtsql-1.12.27.dist-info/METADATA,sha256=wYa5NZjKZh5uYQvItCnEUqNQKXwfNpIK61C9BJ_2TEQ,740
15
- mtsql-1.12.27.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
- mtsql-1.12.27.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
- mtsql-1.12.27.dist-info/RECORD,,
13
+ mtsql-1.12.29.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
+ mtsql-1.12.29.dist-info/METADATA,sha256=dsOJv6kpa6gxLeky_Hsw939aE8bK3F8EMel1g4S1Xyo,740
15
+ mtsql-1.12.29.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ mtsql-1.12.29.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
+ mtsql-1.12.29.dist-info/RECORD,,