-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement chain group_by * Use 'sql_to_python' for GenericFunction type conversion
- Loading branch information
1 parent
6da688f
commit c6ca542
Showing
19 changed files
with
750 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .aggregate import any_value, avg, collect, concat, count, max, min, sum | ||
from .func import Func | ||
|
||
__all__ = [ | ||
"Func", | ||
"any_value", | ||
"avg", | ||
"collect", | ||
"concat", | ||
"count", | ||
"max", | ||
"min", | ||
"sum", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import func as sa_func | ||
|
||
from datachain.sql import functions as dc_func | ||
|
||
from .func import Func | ||
|
||
|
||
def count(col: Optional[str] = None) -> Func: | ||
return Func(inner=sa_func.count, col=col, result_type=int) | ||
|
||
|
||
def sum(col: str) -> Func: | ||
return Func(inner=sa_func.sum, col=col) | ||
|
||
|
||
def avg(col: str) -> Func: | ||
return Func(inner=dc_func.aggregate.avg, col=col) | ||
|
||
|
||
def min(col: str) -> Func: | ||
return Func(inner=sa_func.min, col=col) | ||
|
||
|
||
def max(col: str) -> Func: | ||
return Func(inner=sa_func.max, col=col) | ||
|
||
|
||
def any_value(col: str) -> Func: | ||
return Func(inner=dc_func.aggregate.any_value, col=col) | ||
|
||
|
||
def collect(col: str) -> Func: | ||
return Func(inner=dc_func.aggregate.collect, col=col, is_array=True) | ||
|
||
|
||
def concat(col: str, separator="") -> Func: | ||
def inner(arg): | ||
return dc_func.aggregate.group_concat(arg, separator) | ||
|
||
return Func(inner=inner, col=col, result_type=str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import TYPE_CHECKING, Callable, Optional | ||
|
||
from datachain.lib.convert.python_to_sql import python_to_sql | ||
from datachain.lib.utils import DataChainColumnError | ||
from datachain.query.schema import Column, ColumnMeta | ||
|
||
if TYPE_CHECKING: | ||
from datachain import DataType | ||
from datachain.lib.signal_schema import SignalSchema | ||
|
||
|
||
class Func: | ||
def __init__( | ||
self, | ||
inner: Callable, | ||
col: Optional[str] = None, | ||
result_type: Optional["DataType"] = None, | ||
is_array: bool = False, | ||
) -> None: | ||
self.inner = inner | ||
self.col = col | ||
self.result_type = result_type | ||
self.is_array = is_array | ||
|
||
@property | ||
def db_col(self) -> Optional[str]: | ||
return ColumnMeta.to_db_name(self.col) if self.col else None | ||
|
||
def db_col_type(self, signals_schema: "SignalSchema") -> Optional["DataType"]: | ||
if not self.db_col: | ||
return None | ||
col_type: type = signals_schema.get_column_type(self.db_col) | ||
return list[col_type] if self.is_array else col_type # type: ignore[valid-type] | ||
|
||
def get_result_type(self, signals_schema: "SignalSchema") -> "DataType": | ||
col_type = self.db_col_type(signals_schema) | ||
|
||
if self.result_type: | ||
return self.result_type | ||
|
||
if col_type: | ||
return col_type | ||
|
||
raise DataChainColumnError( | ||
str(self.inner), | ||
"Column name is required to infer result type", | ||
) | ||
|
||
def get_column( | ||
self, signals_schema: "SignalSchema", label: Optional[str] = None | ||
) -> Column: | ||
if self.col: | ||
if label == "collect": | ||
print(label) | ||
col_type = self.get_result_type(signals_schema) | ||
col = Column(self.db_col, python_to_sql(col_type)) | ||
func_col = self.inner(col) | ||
else: | ||
func_col = self.inner() | ||
|
||
if label: | ||
func_col = func_col.label(label) | ||
|
||
return func_col |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.