duckdb 1.5.0.dev44__cp314-cp314t-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 duckdb might be problematic. Click here for more details.

Files changed (47) hide show
  1. _duckdb.cpython-314t-darwin.so +0 -0
  2. duckdb/__init__.py +475 -0
  3. duckdb/__init__.pyi +713 -0
  4. duckdb/bytes_io_wrapper.py +66 -0
  5. duckdb/experimental/__init__.py +2 -0
  6. duckdb/experimental/spark/LICENSE +260 -0
  7. duckdb/experimental/spark/__init__.py +7 -0
  8. duckdb/experimental/spark/_globals.py +77 -0
  9. duckdb/experimental/spark/_typing.py +48 -0
  10. duckdb/experimental/spark/conf.py +45 -0
  11. duckdb/experimental/spark/context.py +164 -0
  12. duckdb/experimental/spark/errors/__init__.py +72 -0
  13. duckdb/experimental/spark/errors/error_classes.py +918 -0
  14. duckdb/experimental/spark/errors/exceptions/__init__.py +16 -0
  15. duckdb/experimental/spark/errors/exceptions/base.py +217 -0
  16. duckdb/experimental/spark/errors/utils.py +116 -0
  17. duckdb/experimental/spark/exception.py +15 -0
  18. duckdb/experimental/spark/sql/__init__.py +7 -0
  19. duckdb/experimental/spark/sql/_typing.py +93 -0
  20. duckdb/experimental/spark/sql/catalog.py +78 -0
  21. duckdb/experimental/spark/sql/column.py +368 -0
  22. duckdb/experimental/spark/sql/conf.py +23 -0
  23. duckdb/experimental/spark/sql/dataframe.py +1437 -0
  24. duckdb/experimental/spark/sql/functions.py +6221 -0
  25. duckdb/experimental/spark/sql/group.py +420 -0
  26. duckdb/experimental/spark/sql/readwriter.py +449 -0
  27. duckdb/experimental/spark/sql/session.py +292 -0
  28. duckdb/experimental/spark/sql/streaming.py +37 -0
  29. duckdb/experimental/spark/sql/type_utils.py +105 -0
  30. duckdb/experimental/spark/sql/types.py +1275 -0
  31. duckdb/experimental/spark/sql/udf.py +37 -0
  32. duckdb/filesystem.py +23 -0
  33. duckdb/functional/__init__.py +17 -0
  34. duckdb/functional/__init__.pyi +31 -0
  35. duckdb/polars_io.py +237 -0
  36. duckdb/query_graph/__main__.py +363 -0
  37. duckdb/typing/__init__.py +61 -0
  38. duckdb/typing/__init__.pyi +36 -0
  39. duckdb/udf.py +19 -0
  40. duckdb/value/__init__.py +0 -0
  41. duckdb/value/__init__.pyi +0 -0
  42. duckdb/value/constant/__init__.py +268 -0
  43. duckdb/value/constant/__init__.pyi +115 -0
  44. duckdb-1.5.0.dev44.dist-info/METADATA +80 -0
  45. duckdb-1.5.0.dev44.dist-info/RECORD +47 -0
  46. duckdb-1.5.0.dev44.dist-info/WHEEL +6 -0
  47. duckdb-1.5.0.dev44.dist-info/licenses/LICENSE +7 -0
@@ -0,0 +1,420 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one or more
3
+ # contributor license agreements. See the NOTICE file distributed with
4
+ # this work for additional information regarding copyright ownership.
5
+ # The ASF licenses this file to You under the Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License. You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ from ..exception import ContributionsAcceptedError
19
+ from typing import Callable, TYPE_CHECKING, overload, Dict, Union, List
20
+
21
+ from .column import Column
22
+ from .session import SparkSession
23
+ from .dataframe import DataFrame
24
+ from .functions import _to_column_expr
25
+ from ._typing import ColumnOrName
26
+ from .types import NumericType
27
+
28
+ if TYPE_CHECKING:
29
+ from ._typing import LiteralType
30
+
31
+ __all__ = ["GroupedData", "Grouping"]
32
+
33
+ def _api_internal(self: "GroupedData", name: str, *cols: str) -> DataFrame:
34
+ expressions = ",".join(list(cols))
35
+ group_by = str(self._grouping) if self._grouping else ""
36
+ projections = self._grouping.get_columns()
37
+ jdf = getattr(self._df.relation, "apply")(
38
+ function_name=name, # aggregate function
39
+ function_aggr=expressions, # inputs to aggregate
40
+ group_expr=group_by, # groups
41
+ projected_columns=projections, # projections
42
+ )
43
+ return DataFrame(jdf, self.session)
44
+
45
+ def df_varargs_api(f: Callable[..., DataFrame]) -> Callable[..., DataFrame]:
46
+ def _api(self: "GroupedData", *cols: str) -> DataFrame:
47
+ name = f.__name__
48
+ return _api_internal(self, name, *cols)
49
+
50
+ _api.__name__ = f.__name__
51
+ _api.__doc__ = f.__doc__
52
+ return _api
53
+
54
+
55
+ class Grouping:
56
+ def __init__(self, *cols: "ColumnOrName", **kwargs):
57
+ self._type = ""
58
+ self._cols = [_to_column_expr(x) for x in cols]
59
+ if 'special' in kwargs:
60
+ special = kwargs['special']
61
+ accepted_special = ["cube", "rollup"]
62
+ assert special in accepted_special
63
+ self._type = special
64
+
65
+ def get_columns(self) -> str:
66
+ columns = ",".join([str(x) for x in self._cols])
67
+ return columns
68
+
69
+ def __str__(self):
70
+ columns = self.get_columns()
71
+ if self._type:
72
+ return self._type + '(' + columns + ')'
73
+ return columns
74
+
75
+
76
+ class GroupedData:
77
+ """
78
+ A set of methods for aggregations on a :class:`DataFrame`,
79
+ created by :func:`DataFrame.groupBy`.
80
+
81
+ """
82
+
83
+ def __init__(self, grouping: Grouping, df: DataFrame):
84
+ self._grouping = grouping
85
+ self._df = df
86
+ self.session: SparkSession = df.session
87
+
88
+ def __repr__(self) -> str:
89
+ return str(self._df)
90
+
91
+ def count(self) -> DataFrame:
92
+ """Counts the number of records for each group.
93
+
94
+ Examples
95
+ --------
96
+ >>> df = spark.createDataFrame(
97
+ ... [(2, "Alice"), (3, "Alice"), (5, "Bob"), (10, "Bob")], ["age", "name"])
98
+ >>> df.show()
99
+ +---+-----+
100
+ |age| name|
101
+ +---+-----+
102
+ | 2|Alice|
103
+ | 3|Alice|
104
+ | 5| Bob|
105
+ | 10| Bob|
106
+ +---+-----+
107
+
108
+ Group-by name, and count each group.
109
+
110
+ >>> df.groupBy(df.name).count().sort("name").show()
111
+ +-----+-----+
112
+ | name|count|
113
+ +-----+-----+
114
+ |Alice| 2|
115
+ | Bob| 2|
116
+ +-----+-----+
117
+ """
118
+ return _api_internal(self, "count").withColumnRenamed('count_star()', 'count')
119
+
120
+ @df_varargs_api
121
+ def mean(self, *cols: str) -> DataFrame:
122
+ """Computes average values for each numeric columns for each group.
123
+
124
+ :func:`mean` is an alias for :func:`avg`.
125
+
126
+ Parameters
127
+ ----------
128
+ cols : str
129
+ column names. Non-numeric columns are ignored.
130
+ """
131
+
132
+ def avg(self, *cols: str) -> DataFrame:
133
+ """Computes average values for each numeric columns for each group.
134
+
135
+ :func:`mean` is an alias for :func:`avg`.
136
+
137
+ Parameters
138
+ ----------
139
+ cols : str
140
+ column names. Non-numeric columns are ignored.
141
+
142
+ Examples
143
+ --------
144
+ >>> df = spark.createDataFrame([
145
+ ... (2, "Alice", 80), (3, "Alice", 100),
146
+ ... (5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])
147
+ >>> df.show()
148
+ +---+-----+------+
149
+ |age| name|height|
150
+ +---+-----+------+
151
+ | 2|Alice| 80|
152
+ | 3|Alice| 100|
153
+ | 5| Bob| 120|
154
+ | 10| Bob| 140|
155
+ +---+-----+------+
156
+
157
+ Group-by name, and calculate the mean of the age in each group.
158
+
159
+ >>> df.groupBy("name").avg('age').sort("name").show()
160
+ +-----+--------+
161
+ | name|avg(age)|
162
+ +-----+--------+
163
+ |Alice| 2.5|
164
+ | Bob| 7.5|
165
+ +-----+--------+
166
+
167
+ Calculate the mean of the age and height in all data.
168
+
169
+ >>> df.groupBy().avg('age', 'height').show()
170
+ +--------+-----------+
171
+ |avg(age)|avg(height)|
172
+ +--------+-----------+
173
+ | 5.0| 110.0|
174
+ +--------+-----------+
175
+ """
176
+ columns = list(cols)
177
+ if len(columns) == 0:
178
+ schema = self._df.schema
179
+ # Take only the numeric types of the relation
180
+ columns: List[str] = [x.name for x in schema.fields if isinstance(x.dataType, NumericType)]
181
+ return _api_internal(self, "avg", *columns)
182
+
183
+ @df_varargs_api
184
+ def max(self, *cols: str) -> DataFrame:
185
+ """Computes the max value for each numeric columns for each group.
186
+
187
+ Examples
188
+ --------
189
+ >>> df = spark.createDataFrame([
190
+ ... (2, "Alice", 80), (3, "Alice", 100),
191
+ ... (5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])
192
+ >>> df.show()
193
+ +---+-----+------+
194
+ |age| name|height|
195
+ +---+-----+------+
196
+ | 2|Alice| 80|
197
+ | 3|Alice| 100|
198
+ | 5| Bob| 120|
199
+ | 10| Bob| 140|
200
+ +---+-----+------+
201
+
202
+ Group-by name, and calculate the max of the age in each group.
203
+
204
+ >>> df.groupBy("name").max("age").sort("name").show()
205
+ +-----+--------+
206
+ | name|max(age)|
207
+ +-----+--------+
208
+ |Alice| 3|
209
+ | Bob| 10|
210
+ +-----+--------+
211
+
212
+ Calculate the max of the age and height in all data.
213
+
214
+ >>> df.groupBy().max("age", "height").show()
215
+ +--------+-----------+
216
+ |max(age)|max(height)|
217
+ +--------+-----------+
218
+ | 10| 140|
219
+ +--------+-----------+
220
+ """
221
+
222
+ @df_varargs_api
223
+ def min(self, *cols: str) -> DataFrame:
224
+ """Computes the min value for each numeric column for each group.
225
+
226
+ Parameters
227
+ ----------
228
+ cols : str
229
+ column names. Non-numeric columns are ignored.
230
+
231
+ Examples
232
+ --------
233
+ >>> df = spark.createDataFrame([
234
+ ... (2, "Alice", 80), (3, "Alice", 100),
235
+ ... (5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])
236
+ >>> df.show()
237
+ +---+-----+------+
238
+ |age| name|height|
239
+ +---+-----+------+
240
+ | 2|Alice| 80|
241
+ | 3|Alice| 100|
242
+ | 5| Bob| 120|
243
+ | 10| Bob| 140|
244
+ +---+-----+------+
245
+
246
+ Group-by name, and calculate the min of the age in each group.
247
+
248
+ >>> df.groupBy("name").min("age").sort("name").show()
249
+ +-----+--------+
250
+ | name|min(age)|
251
+ +-----+--------+
252
+ |Alice| 2|
253
+ | Bob| 5|
254
+ +-----+--------+
255
+
256
+ Calculate the min of the age and height in all data.
257
+
258
+ >>> df.groupBy().min("age", "height").show()
259
+ +--------+-----------+
260
+ |min(age)|min(height)|
261
+ +--------+-----------+
262
+ | 2| 80|
263
+ +--------+-----------+
264
+ """
265
+
266
+ @df_varargs_api
267
+ def sum(self, *cols: str) -> DataFrame:
268
+ """Computes the sum for each numeric columns for each group.
269
+
270
+ Parameters
271
+ ----------
272
+ cols : str
273
+ column names. Non-numeric columns are ignored.
274
+
275
+ Examples
276
+ --------
277
+ >>> df = spark.createDataFrame([
278
+ ... (2, "Alice", 80), (3, "Alice", 100),
279
+ ... (5, "Bob", 120), (10, "Bob", 140)], ["age", "name", "height"])
280
+ >>> df.show()
281
+ +---+-----+------+
282
+ |age| name|height|
283
+ +---+-----+------+
284
+ | 2|Alice| 80|
285
+ | 3|Alice| 100|
286
+ | 5| Bob| 120|
287
+ | 10| Bob| 140|
288
+ +---+-----+------+
289
+
290
+ Group-by name, and calculate the sum of the age in each group.
291
+
292
+ >>> df.groupBy("name").sum("age").sort("name").show()
293
+ +-----+--------+
294
+ | name|sum(age)|
295
+ +-----+--------+
296
+ |Alice| 5|
297
+ | Bob| 15|
298
+ +-----+--------+
299
+
300
+ Calculate the sum of the age and height in all data.
301
+
302
+ >>> df.groupBy().sum("age", "height").show()
303
+ +--------+-----------+
304
+ |sum(age)|sum(height)|
305
+ +--------+-----------+
306
+ | 20| 440|
307
+ +--------+-----------+
308
+ """
309
+
310
+ @overload
311
+ def agg(self, *exprs: Column) -> DataFrame:
312
+ ...
313
+
314
+ @overload
315
+ def agg(self, __exprs: Dict[str, str]) -> DataFrame:
316
+ ...
317
+
318
+ def agg(self, *exprs: Union[Column, Dict[str, str]]) -> DataFrame:
319
+ """Compute aggregates and returns the result as a :class:`DataFrame`.
320
+
321
+ The available aggregate functions can be:
322
+
323
+ 1. built-in aggregation functions, such as `avg`, `max`, `min`, `sum`, `count`
324
+
325
+ 2. group aggregate pandas UDFs, created with :func:`pyspark.sql.functions.pandas_udf`
326
+
327
+ .. note:: There is no partial aggregation with group aggregate UDFs, i.e.,
328
+ a full shuffle is required. Also, all the data of a group will be loaded into
329
+ memory, so the user should be aware of the potential OOM risk if data is skewed
330
+ and certain groups are too large to fit in memory.
331
+
332
+ .. seealso:: :func:`pyspark.sql.functions.pandas_udf`
333
+
334
+ If ``exprs`` is a single :class:`dict` mapping from string to string, then the key
335
+ is the column to perform aggregation on, and the value is the aggregate function.
336
+
337
+ Alternatively, ``exprs`` can also be a list of aggregate :class:`Column` expressions.
338
+
339
+ .. versionadded:: 1.3.0
340
+
341
+ .. versionchanged:: 3.4.0
342
+ Supports Spark Connect.
343
+
344
+ Parameters
345
+ ----------
346
+ exprs : dict
347
+ a dict mapping from column name (string) to aggregate functions (string),
348
+ or a list of :class:`Column`.
349
+
350
+ Notes
351
+ -----
352
+ Built-in aggregation functions and group aggregate pandas UDFs cannot be mixed
353
+ in a single call to this function.
354
+
355
+ Examples
356
+ --------
357
+ >>> from pyspark.sql import functions as F
358
+ >>> from pyspark.sql.functions import pandas_udf, PandasUDFType
359
+ >>> df = spark.createDataFrame(
360
+ ... [(2, "Alice"), (3, "Alice"), (5, "Bob"), (10, "Bob")], ["age", "name"])
361
+ >>> df.show()
362
+ +---+-----+
363
+ |age| name|
364
+ +---+-----+
365
+ | 2|Alice|
366
+ | 3|Alice|
367
+ | 5| Bob|
368
+ | 10| Bob|
369
+ +---+-----+
370
+
371
+ Group-by name, and count each group.
372
+
373
+ >>> df.groupBy(df.name)
374
+ GroupedData[grouping...: [name...], value: [age: bigint, name: string], type: GroupBy]
375
+
376
+ >>> df.groupBy(df.name).agg({"*": "count"}).sort("name").show()
377
+ +-----+--------+
378
+ | name|count(1)|
379
+ +-----+--------+
380
+ |Alice| 2|
381
+ | Bob| 2|
382
+ +-----+--------+
383
+
384
+ Group-by name, and calculate the minimum age.
385
+
386
+ >>> df.groupBy(df.name).agg(F.min(df.age)).sort("name").show()
387
+ +-----+--------+
388
+ | name|min(age)|
389
+ +-----+--------+
390
+ |Alice| 2|
391
+ | Bob| 5|
392
+ +-----+--------+
393
+
394
+ Same as above but uses pandas UDF.
395
+
396
+ >>> @pandas_udf('int', PandasUDFType.GROUPED_AGG) # doctest: +SKIP
397
+ ... def min_udf(v):
398
+ ... return v.min()
399
+ ...
400
+ >>> df.groupBy(df.name).agg(min_udf(df.age)).sort("name").show() # doctest: +SKIP
401
+ +-----+------------+
402
+ | name|min_udf(age)|
403
+ +-----+------------+
404
+ |Alice| 2|
405
+ | Bob| 5|
406
+ +-----+------------+
407
+ """
408
+ assert exprs, "exprs should not be empty"
409
+ if len(exprs) == 1 and isinstance(exprs[0], dict):
410
+ raise ContributionsAcceptedError
411
+ else:
412
+ # Columns
413
+ assert all(isinstance(c, Column) for c in exprs), "all exprs should be Column"
414
+ expressions = list(self._grouping._cols)
415
+ expressions.extend([x.expr for x in exprs])
416
+ group_by = str(self._grouping)
417
+ rel = self._df.relation.select(*expressions, groups=group_by)
418
+ return DataFrame(rel, self.session)
419
+
420
+ # TODO: add 'pivot'