matrixone-python-sdk 0.1.0__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.
Files changed (122) hide show
  1. matrixone/__init__.py +155 -0
  2. matrixone/account.py +723 -0
  3. matrixone/async_client.py +3913 -0
  4. matrixone/async_metadata_manager.py +311 -0
  5. matrixone/async_orm.py +123 -0
  6. matrixone/async_vector_index_manager.py +633 -0
  7. matrixone/base_client.py +208 -0
  8. matrixone/client.py +4672 -0
  9. matrixone/config.py +452 -0
  10. matrixone/connection_hooks.py +286 -0
  11. matrixone/exceptions.py +89 -0
  12. matrixone/logger.py +782 -0
  13. matrixone/metadata.py +820 -0
  14. matrixone/moctl.py +219 -0
  15. matrixone/orm.py +2277 -0
  16. matrixone/pitr.py +646 -0
  17. matrixone/pubsub.py +771 -0
  18. matrixone/restore.py +411 -0
  19. matrixone/search_vector_index.py +1176 -0
  20. matrixone/snapshot.py +550 -0
  21. matrixone/sql_builder.py +844 -0
  22. matrixone/sqlalchemy_ext/__init__.py +161 -0
  23. matrixone/sqlalchemy_ext/adapters.py +163 -0
  24. matrixone/sqlalchemy_ext/dialect.py +534 -0
  25. matrixone/sqlalchemy_ext/fulltext_index.py +895 -0
  26. matrixone/sqlalchemy_ext/fulltext_search.py +1686 -0
  27. matrixone/sqlalchemy_ext/hnsw_config.py +194 -0
  28. matrixone/sqlalchemy_ext/ivf_config.py +252 -0
  29. matrixone/sqlalchemy_ext/table_builder.py +351 -0
  30. matrixone/sqlalchemy_ext/vector_index.py +1721 -0
  31. matrixone/sqlalchemy_ext/vector_type.py +948 -0
  32. matrixone/version.py +580 -0
  33. matrixone_python_sdk-0.1.0.dist-info/METADATA +706 -0
  34. matrixone_python_sdk-0.1.0.dist-info/RECORD +122 -0
  35. matrixone_python_sdk-0.1.0.dist-info/WHEEL +5 -0
  36. matrixone_python_sdk-0.1.0.dist-info/entry_points.txt +5 -0
  37. matrixone_python_sdk-0.1.0.dist-info/licenses/LICENSE +200 -0
  38. matrixone_python_sdk-0.1.0.dist-info/top_level.txt +2 -0
  39. tests/__init__.py +19 -0
  40. tests/offline/__init__.py +20 -0
  41. tests/offline/conftest.py +77 -0
  42. tests/offline/test_account.py +703 -0
  43. tests/offline/test_async_client_query_comprehensive.py +1218 -0
  44. tests/offline/test_basic.py +54 -0
  45. tests/offline/test_case_sensitivity.py +227 -0
  46. tests/offline/test_connection_hooks_offline.py +287 -0
  47. tests/offline/test_dialect_schema_handling.py +609 -0
  48. tests/offline/test_explain_methods.py +346 -0
  49. tests/offline/test_filter_logical_in.py +237 -0
  50. tests/offline/test_fulltext_search_comprehensive.py +795 -0
  51. tests/offline/test_ivf_config.py +249 -0
  52. tests/offline/test_join_methods.py +281 -0
  53. tests/offline/test_join_sqlalchemy_compatibility.py +276 -0
  54. tests/offline/test_logical_in_method.py +237 -0
  55. tests/offline/test_matrixone_version_parsing.py +264 -0
  56. tests/offline/test_metadata_offline.py +557 -0
  57. tests/offline/test_moctl.py +300 -0
  58. tests/offline/test_moctl_simple.py +251 -0
  59. tests/offline/test_model_support_offline.py +359 -0
  60. tests/offline/test_model_support_simple.py +225 -0
  61. tests/offline/test_pinecone_filter_offline.py +377 -0
  62. tests/offline/test_pitr.py +585 -0
  63. tests/offline/test_pubsub.py +712 -0
  64. tests/offline/test_query_update.py +283 -0
  65. tests/offline/test_restore.py +445 -0
  66. tests/offline/test_snapshot_comprehensive.py +384 -0
  67. tests/offline/test_sql_escaping_edge_cases.py +551 -0
  68. tests/offline/test_sqlalchemy_integration.py +382 -0
  69. tests/offline/test_sqlalchemy_vector_integration.py +434 -0
  70. tests/offline/test_table_builder.py +198 -0
  71. tests/offline/test_unified_filter.py +398 -0
  72. tests/offline/test_unified_transaction.py +495 -0
  73. tests/offline/test_vector_index.py +238 -0
  74. tests/offline/test_vector_operations.py +688 -0
  75. tests/offline/test_vector_type.py +174 -0
  76. tests/offline/test_version_core.py +328 -0
  77. tests/offline/test_version_management.py +372 -0
  78. tests/offline/test_version_standalone.py +652 -0
  79. tests/online/__init__.py +20 -0
  80. tests/online/conftest.py +216 -0
  81. tests/online/test_account_management.py +194 -0
  82. tests/online/test_advanced_features.py +344 -0
  83. tests/online/test_async_client_interfaces.py +330 -0
  84. tests/online/test_async_client_online.py +285 -0
  85. tests/online/test_async_model_insert_online.py +293 -0
  86. tests/online/test_async_orm_online.py +300 -0
  87. tests/online/test_async_simple_query_online.py +802 -0
  88. tests/online/test_async_transaction_simple_query.py +300 -0
  89. tests/online/test_basic_connection.py +130 -0
  90. tests/online/test_client_online.py +238 -0
  91. tests/online/test_config.py +90 -0
  92. tests/online/test_config_validation.py +123 -0
  93. tests/online/test_connection_hooks_new_online.py +217 -0
  94. tests/online/test_dialect_schema_handling_online.py +331 -0
  95. tests/online/test_filter_logical_in_online.py +374 -0
  96. tests/online/test_fulltext_comprehensive.py +1773 -0
  97. tests/online/test_fulltext_label_online.py +433 -0
  98. tests/online/test_fulltext_search_online.py +842 -0
  99. tests/online/test_ivf_stats_online.py +506 -0
  100. tests/online/test_logger_integration.py +311 -0
  101. tests/online/test_matrixone_query_orm.py +540 -0
  102. tests/online/test_metadata_online.py +579 -0
  103. tests/online/test_model_insert_online.py +255 -0
  104. tests/online/test_mysql_driver_validation.py +213 -0
  105. tests/online/test_orm_advanced_features.py +2022 -0
  106. tests/online/test_orm_cte_integration.py +269 -0
  107. tests/online/test_orm_online.py +270 -0
  108. tests/online/test_pinecone_filter.py +708 -0
  109. tests/online/test_pubsub_operations.py +352 -0
  110. tests/online/test_query_methods.py +225 -0
  111. tests/online/test_query_update_online.py +433 -0
  112. tests/online/test_search_vector_index.py +557 -0
  113. tests/online/test_simple_fulltext_online.py +915 -0
  114. tests/online/test_snapshot_comprehensive.py +998 -0
  115. tests/online/test_sqlalchemy_engine_integration.py +336 -0
  116. tests/online/test_sqlalchemy_integration.py +425 -0
  117. tests/online/test_transaction_contexts.py +1219 -0
  118. tests/online/test_transaction_insert_methods.py +356 -0
  119. tests/online/test_transaction_query_methods.py +288 -0
  120. tests/online/test_unified_filter_online.py +529 -0
  121. tests/online/test_vector_comprehensive.py +706 -0
  122. tests/online/test_version_management.py +291 -0
@@ -0,0 +1,194 @@
1
+ # Copyright 2021 - 2022 Matrix Origin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ HNSW index configuration management for MatrixOne.
17
+
18
+ This module provides utilities for managing HNSW index configuration
19
+ in MatrixOne, including enabling/disabling HNSW indexing and setting
20
+ search parameters.
21
+ """
22
+
23
+ from typing import Optional
24
+
25
+ from sqlalchemy import text
26
+ from sqlalchemy.engine import Connection, Engine
27
+
28
+
29
+ def _exec_sql_safe(connection, sql: str):
30
+ """Execute SQL safely, bypassing bind parameter parsing when possible."""
31
+ if hasattr(connection, 'exec_driver_sql'):
32
+ # Escape % to %% for pymysql's format string handling
33
+ escaped_sql = sql.replace('%', '%%')
34
+ return connection.exec_driver_sql(escaped_sql)
35
+ else:
36
+ return connection.execute(text(sql))
37
+
38
+
39
+ class HNSWConfig:
40
+ """
41
+ Configuration manager for HNSW indexing in MatrixOne.
42
+
43
+ Provides methods to enable/disable HNSW indexing and manage
44
+ HNSW-specific parameters.
45
+ """
46
+
47
+ def __init__(self, engine: Engine):
48
+ """
49
+ Initialize HNSWConfig.
50
+
51
+ Args:
52
+
53
+ engine: SQLAlchemy engine instance
54
+ """
55
+ self.engine = engine
56
+
57
+ def enable_hnsw_indexing(self, connection: Optional[Connection] = None) -> bool:
58
+ """
59
+ Enable HNSW indexing in MatrixOne.
60
+
61
+ Args:
62
+
63
+ connection: Optional existing database connection
64
+
65
+ Returns:
66
+
67
+ bool: True if successful, False otherwise
68
+ """
69
+ try:
70
+ if connection:
71
+ _exec_sql_safe(connection, "SET experimental_hnsw_index = 1")
72
+ else:
73
+ with self.engine.begin() as conn:
74
+ _exec_sql_safe(conn, "SET experimental_hnsw_index = 1")
75
+ return True
76
+ except Exception as e:
77
+ print(f"Failed to enable HNSW indexing: {e}")
78
+ return False
79
+
80
+ def disable_hnsw_indexing(self, connection: Optional[Connection] = None) -> bool:
81
+ """
82
+ Disable HNSW indexing in MatrixOne.
83
+
84
+ Args:
85
+
86
+ connection: Optional existing database connection
87
+
88
+ Returns:
89
+
90
+ bool: True if successful, False otherwise
91
+ """
92
+ try:
93
+ if connection:
94
+ _exec_sql_safe(connection, "SET experimental_hnsw_index = 0")
95
+ else:
96
+ with self.engine.begin() as conn:
97
+ _exec_sql_safe(conn, "SET experimental_hnsw_index = 0")
98
+ return True
99
+ except Exception as e:
100
+ print(f"Failed to disable HNSW indexing: {e}")
101
+ return False
102
+
103
+ def get_hnsw_status(self, connection: Optional[Connection] = None) -> Optional[bool]:
104
+ """
105
+ Get the current HNSW indexing status.
106
+
107
+ Args:
108
+
109
+ connection: Optional existing database connection
110
+
111
+ Returns:
112
+
113
+ bool: True if HNSW indexing is enabled, False if disabled, None if error
114
+ """
115
+ try:
116
+ if connection:
117
+ result = _exec_sql_safe(connection, "SHOW VARIABLES LIKE 'experimental_hnsw_index'")
118
+ else:
119
+ with self.engine.begin() as conn:
120
+ result = _exec_sql_safe(conn, "SHOW VARIABLES LIKE 'experimental_hnsw_index'")
121
+
122
+ row = result.fetchone()
123
+ if row:
124
+ return row[1] in ("1", "on", "ON")
125
+ return None
126
+ except Exception as e:
127
+ print(f"Failed to get HNSW status: {e}")
128
+ return None
129
+
130
+
131
+ def create_hnsw_config(engine: Engine) -> HNSWConfig:
132
+ """
133
+ Create an HNSWConfig instance.
134
+
135
+ Args:
136
+
137
+ engine: SQLAlchemy engine instance
138
+
139
+ Returns:
140
+
141
+ HNSWConfig instance
142
+ """
143
+ return HNSWConfig(engine)
144
+
145
+
146
+ def enable_hnsw_indexing(engine: Engine, connection: Optional[Connection] = None) -> bool:
147
+ """
148
+ Enable HNSW indexing in MatrixOne.
149
+
150
+ Args:
151
+
152
+ engine: SQLAlchemy engine instance
153
+ connection: Optional existing database connection
154
+
155
+ Returns:
156
+
157
+ bool: True if successful, False otherwise
158
+ """
159
+ config = create_hnsw_config(engine)
160
+ return config.enable_hnsw_indexing(connection)
161
+
162
+
163
+ def disable_hnsw_indexing(engine: Engine, connection: Optional[Connection] = None) -> bool:
164
+ """
165
+ Disable HNSW indexing in MatrixOne.
166
+
167
+ Args:
168
+
169
+ engine: SQLAlchemy engine instance
170
+ connection: Optional existing database connection
171
+
172
+ Returns:
173
+
174
+ bool: True if successful, False otherwise
175
+ """
176
+ config = create_hnsw_config(engine)
177
+ return config.disable_hnsw_indexing(connection)
178
+
179
+
180
+ def get_hnsw_status(engine: Engine, connection: Optional[Connection] = None) -> Optional[bool]:
181
+ """
182
+ Get the current HNSW indexing status.
183
+
184
+ Args:
185
+
186
+ engine: SQLAlchemy engine instance
187
+ connection: Optional existing database connection
188
+
189
+ Returns:
190
+
191
+ bool: True if HNSW indexing is enabled, False if disabled, None if error
192
+ """
193
+ config = create_hnsw_config(engine)
194
+ return config.get_hnsw_status(connection)
@@ -0,0 +1,252 @@
1
+ # Copyright 2021 - 2022 Matrix Origin
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ IVF (Inverted File) index configuration utilities for MatrixOne.
17
+ """
18
+
19
+ from typing import Any, Dict, Optional
20
+
21
+ from sqlalchemy import text
22
+ from sqlalchemy.engine import Engine
23
+
24
+
25
+ def _exec_sql_safe(connection, sql: str):
26
+ """Execute SQL safely, bypassing bind parameter parsing when possible."""
27
+ if hasattr(connection, 'exec_driver_sql'):
28
+ # Escape % to %% for pymysql's format string handling
29
+ escaped_sql = sql.replace('%', '%%')
30
+ return connection.exec_driver_sql(escaped_sql)
31
+ else:
32
+ return connection.execute(text(sql))
33
+
34
+
35
+ class IVFConfig:
36
+ """
37
+ Configuration manager for IVF indexing in MatrixOne.
38
+
39
+ This class provides utilities to enable/disable IVF indexing
40
+ and configure search parameters.
41
+ """
42
+
43
+ def __init__(self, engine: Engine):
44
+ """
45
+ Initialize IVF configuration manager.
46
+
47
+ Args:
48
+
49
+ engine: SQLAlchemy engine connected to MatrixOne
50
+ """
51
+ self.engine = engine
52
+
53
+ def enable_ivf_indexing(self) -> bool:
54
+ """
55
+ Enable IVF indexing in MatrixOne.
56
+
57
+ Returns:
58
+
59
+ True if successful, False otherwise
60
+ """
61
+ try:
62
+ with self.engine.begin() as conn:
63
+ _exec_sql_safe(conn, "SET experimental_ivf_index = 1")
64
+ return True
65
+ except Exception:
66
+ return False
67
+
68
+ def disable_ivf_indexing(self) -> bool:
69
+ """
70
+ Disable IVF indexing in MatrixOne.
71
+
72
+ Returns:
73
+
74
+ True if successful, False otherwise
75
+ """
76
+ try:
77
+ with self.engine.begin() as conn:
78
+ _exec_sql_safe(conn, "SET experimental_ivf_index = 0")
79
+ return True
80
+ except Exception:
81
+ return False
82
+
83
+ def set_probe_limit(self, limit: int) -> bool:
84
+ """
85
+ Set the probe limit for IVF index search.
86
+
87
+ Args:
88
+
89
+ limit: Number of probes to use during search
90
+
91
+ Returns:
92
+
93
+ True if successful, False otherwise
94
+ """
95
+ try:
96
+ with self.engine.begin() as conn:
97
+ _exec_sql_safe(conn, f"SET probe_limit = {limit}")
98
+ return True
99
+ except Exception:
100
+ return False
101
+
102
+ def get_ivf_status(self) -> Dict[str, Any]:
103
+ """
104
+ Get current IVF configuration status.
105
+
106
+ Returns:
107
+
108
+ Dictionary with current IVF settings
109
+ """
110
+ status = {"ivf_enabled": None, "probe_limit": None, "error": None}
111
+
112
+ try:
113
+ with self.engine.begin() as conn:
114
+ # Get IVF index setting
115
+ result = _exec_sql_safe(conn, "SHOW VARIABLES LIKE 'experimental_ivf_index'")
116
+ ivf_setting = result.fetchone()
117
+ if ivf_setting:
118
+ status["ivf_enabled"] = ivf_setting[1] == "1"
119
+
120
+ # Get probe limit setting
121
+ result = _exec_sql_safe(conn, "SHOW VARIABLES LIKE 'probe_limit'")
122
+ probe_setting = result.fetchone()
123
+ if probe_setting:
124
+ status["probe_limit"] = int(probe_setting[1])
125
+
126
+ except Exception as e:
127
+ status["error"] = str(e)
128
+
129
+ return status
130
+
131
+ def configure_ivf(self, enabled: bool = True, probe_limit: Optional[int] = None) -> bool:
132
+ """
133
+ Configure IVF indexing with multiple parameters.
134
+
135
+ Args:
136
+
137
+ enabled: Whether to enable IVF indexing
138
+ probe_limit: Probe limit for search (optional)
139
+
140
+ Returns:
141
+
142
+ True if all operations successful, False otherwise
143
+ """
144
+ success = True
145
+
146
+ # Enable/disable IVF
147
+ if enabled:
148
+ success &= self.enable_ivf_indexing()
149
+ else:
150
+ success &= self.disable_ivf_indexing()
151
+
152
+ # Set probe limit if specified
153
+ if probe_limit is not None:
154
+ success &= self.set_probe_limit(probe_limit)
155
+
156
+ return success
157
+
158
+ def is_ivf_supported(self) -> bool:
159
+ """
160
+ Check if IVF indexing is supported in the current MatrixOne instance.
161
+
162
+ Returns:
163
+
164
+ True if IVF is supported, False otherwise
165
+ """
166
+ try:
167
+ with self.engine.begin() as conn:
168
+ _exec_sql_safe(conn, "SHOW VARIABLES LIKE 'experimental_ivf_index'")
169
+ return True
170
+ except Exception:
171
+ return False
172
+
173
+
174
+ def create_ivf_config(engine: Engine) -> IVFConfig:
175
+ """
176
+ Create an IVF configuration manager.
177
+
178
+ Args:
179
+
180
+ engine: SQLAlchemy engine connected to MatrixOne
181
+
182
+ Returns:
183
+
184
+ IVFConfig instance
185
+ """
186
+ return IVFConfig(engine)
187
+
188
+
189
+ # Convenience functions for direct use
190
+ def enable_ivf_indexing(engine: Engine) -> bool:
191
+ """
192
+ Enable IVF indexing.
193
+
194
+ Args:
195
+
196
+ engine: SQLAlchemy engine
197
+
198
+ Returns:
199
+
200
+ True if successful, False otherwise
201
+ """
202
+ config = IVFConfig(engine)
203
+ return config.enable_ivf_indexing()
204
+
205
+
206
+ def disable_ivf_indexing(engine: Engine) -> bool:
207
+ """
208
+ Disable IVF indexing.
209
+
210
+ Args:
211
+
212
+ engine: SQLAlchemy engine
213
+
214
+ Returns:
215
+
216
+ True if successful, False otherwise
217
+ """
218
+ config = IVFConfig(engine)
219
+ return config.disable_ivf_indexing()
220
+
221
+
222
+ def set_probe_limit(engine: Engine, limit: int) -> bool:
223
+ """
224
+ Set probe limit for IVF search.
225
+
226
+ Args:
227
+
228
+ engine: SQLAlchemy engine
229
+ limit: Probe limit value
230
+
231
+ Returns:
232
+
233
+ True if successful, False otherwise
234
+ """
235
+ config = IVFConfig(engine)
236
+ return config.set_probe_limit(limit)
237
+
238
+
239
+ def get_ivf_status(engine: Engine) -> Dict[str, Any]:
240
+ """
241
+ Get current IVF configuration status.
242
+
243
+ Args:
244
+
245
+ engine: SQLAlchemy engine
246
+
247
+ Returns:
248
+
249
+ Dictionary with current IVF settings
250
+ """
251
+ config = IVFConfig(engine)
252
+ return config.get_ivf_status()