chdb 3.6.0__cp38-abi3-macosx_11_0_arm64.whl → 3.7.0__cp38-abi3-macosx_11_0_arm64.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.

Potentially problematic release.


This version of chdb might be problematic. Click here for more details.

chdb/session/state.py CHANGED
@@ -81,6 +81,24 @@ class Session:
81
81
  self.close()
82
82
 
83
83
  def close(self):
84
+ """Close the session and cleanup resources.
85
+
86
+ This method closes the underlying connection and resets the global session state.
87
+ After calling this method, the session becomes invalid and cannot be used for
88
+ further queries.
89
+
90
+ .. note::
91
+ This method is automatically called when the session is used as a context manager
92
+ or when the session object is destroyed.
93
+
94
+ .. warning::
95
+ Any attempt to use the session after calling close() will result in an error.
96
+
97
+ Examples:
98
+ >>> session = Session("test.db")
99
+ >>> session.query("SELECT 1")
100
+ >>> session.close() # Explicitly close the session
101
+ """
84
102
  if self._conn is not None:
85
103
  self._conn.close()
86
104
  self._conn = None
@@ -89,14 +107,102 @@ class Session:
89
107
  g_session_path = None
90
108
 
91
109
  def cleanup(self):
110
+ """Cleanup session resources with exception handling.
111
+
112
+ This method attempts to close the session while suppressing any exceptions
113
+ that might occur during the cleanup process. It's particularly useful in
114
+ error handling scenarios or when you need to ensure cleanup happens regardless
115
+ of the session state.
116
+
117
+ .. note::
118
+ This method will never raise an exception, making it safe to call in
119
+ finally blocks or destructors.
120
+
121
+ .. seealso::
122
+ :meth:`close` - For explicit session closing with error propagation
123
+
124
+ Examples:
125
+ >>> session = Session("test.db")
126
+ >>> try:
127
+ ... session.query("INVALID SQL")
128
+ ... finally:
129
+ ... session.cleanup() # Safe cleanup regardless of errors
130
+ """
92
131
  try:
93
132
  self.close()
94
133
  except: # noqa
95
134
  pass
96
135
 
97
136
  def query(self, sql, fmt="CSV", udf_path=""):
98
- """
99
- Execute a query.
137
+ """Execute a SQL query and return the results.
138
+
139
+ This method executes a SQL query against the session's database and returns
140
+ the results in the specified format. The method supports various output formats
141
+ and maintains session state between queries.
142
+
143
+ Args:
144
+ sql (str): SQL query string to execute
145
+ fmt (str, optional): Output format for results. Defaults to "CSV".
146
+ Available formats include:
147
+
148
+ - "CSV" - Comma-separated values
149
+ - "JSON" - JSON format
150
+ - "TabSeparated" - Tab-separated values
151
+ - "Pretty" - Pretty-printed table format
152
+ - "JSONCompact" - Compact JSON format
153
+ - "Arrow" - Apache Arrow format
154
+ - "Parquet" - Parquet format
155
+
156
+ udf_path (str, optional): Path to user-defined functions. Defaults to "".
157
+ If not specified, uses the UDF path from session initialization.
158
+
159
+ Returns:
160
+ Query results in the specified format. The exact return type depends on
161
+ the format parameter:
162
+
163
+ - String formats (CSV, JSON, etc.) return str
164
+ - Binary formats (Arrow, Parquet) return bytes
165
+
166
+ Raises:
167
+ RuntimeError: If the session is closed or invalid
168
+ ValueError: If the SQL query is malformed
169
+
170
+ .. note::
171
+ The "Debug" format is not supported and will be automatically converted
172
+ to "CSV" with a warning. For debugging, use connection string parameters
173
+ instead.
174
+
175
+ .. warning::
176
+ This method executes the query synchronously and loads all results into
177
+ memory. For large result sets, consider using :meth:`send_query` for
178
+ streaming results.
179
+
180
+ Examples:
181
+ >>> session = Session("test.db")
182
+ >>>
183
+ >>> # Basic query with default CSV format
184
+ >>> result = session.query("SELECT 1 as number")
185
+ >>> print(result)
186
+ number
187
+ 1
188
+
189
+ >>> # Query with JSON format
190
+ >>> result = session.query("SELECT 1 as number", fmt="JSON")
191
+ >>> print(result)
192
+ {"number": "1"}
193
+
194
+ >>> # Complex query with table creation
195
+ >>> session.query("CREATE TABLE test (id INT, name String)")
196
+ >>> session.query("INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob')")
197
+ >>> result = session.query("SELECT * FROM test ORDER BY id")
198
+ >>> print(result)
199
+ id,name
200
+ 1,Alice
201
+ 2,Bob
202
+
203
+ .. seealso::
204
+ :meth:`send_query` - For streaming query execution
205
+ :attr:`sql` - Alias for this method
100
206
  """
101
207
  if fmt == "Debug":
102
208
  warnings.warn(
@@ -111,8 +217,65 @@ Eg: conn = connect(f"db_path?verbose&log-level=test")"""
111
217
  sql = query
112
218
 
113
219
  def send_query(self, sql, fmt="CSV") -> StreamingResult:
114
- """
115
- Execute a streaming query.
220
+ """Execute a SQL query and return a streaming result iterator.
221
+
222
+ This method executes a SQL query against the session's database and returns
223
+ a streaming result object that allows you to iterate over the results without
224
+ loading everything into memory at once. This is particularly useful for large
225
+ result sets.
226
+
227
+ Args:
228
+ sql (str): SQL query string to execute
229
+ fmt (str, optional): Output format for results. Defaults to "CSV".
230
+ Available formats include:
231
+
232
+ - "CSV" - Comma-separated values
233
+ - "JSON" - JSON format
234
+ - "TabSeparated" - Tab-separated values
235
+ - "JSONCompact" - Compact JSON format
236
+ - "Arrow" - Apache Arrow format
237
+ - "Parquet" - Parquet format
238
+
239
+ Returns:
240
+ StreamingResult: A streaming result iterator that yields query results
241
+ incrementally. The iterator can be used in for loops or converted to
242
+ other data structures.
243
+
244
+ Raises:
245
+ RuntimeError: If the session is closed or invalid
246
+ ValueError: If the SQL query is malformed
247
+
248
+ .. note::
249
+ The "Debug" format is not supported and will be automatically converted
250
+ to "CSV" with a warning. For debugging, use connection string parameters
251
+ instead.
252
+
253
+ .. warning::
254
+ The returned StreamingResult object should be consumed promptly or stored
255
+ appropriately, as it maintains a connection to the database.
256
+
257
+ Examples:
258
+ >>> session = Session("test.db")
259
+ >>> session.query("CREATE TABLE big_table (id INT, data String)")
260
+ >>>
261
+ >>> # Insert large dataset
262
+ >>> for i in range(1000):
263
+ ... session.query(f"INSERT INTO big_table VALUES ({i}, 'data_{i}')")
264
+ >>>
265
+ >>> # Stream results to avoid memory issues
266
+ >>> streaming_result = session.send_query("SELECT * FROM big_table ORDER BY id")
267
+ >>> for chunk in streaming_result:
268
+ ... print(f"Processing chunk: {len(chunk)} bytes")
269
+ ... # Process chunk without loading entire result set
270
+
271
+ >>> # Using with context manager
272
+ >>> with session.send_query("SELECT COUNT(*) FROM big_table") as stream:
273
+ ... for result in stream:
274
+ ... print(f"Count result: {result}")
275
+
276
+ .. seealso::
277
+ :meth:`query` - For non-streaming query execution
278
+ :class:`chdb.state.sqlitelike.StreamingResult` - Streaming result iterator
116
279
  """
117
280
  if fmt == "Debug":
118
281
  warnings.warn(