pylegend 0.11.0__py3-none-any.whl → 0.12.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 (25) hide show
  1. pylegend/core/database/sql_to_string/db_extension.py +68 -6
  2. pylegend/core/language/legendql_api/legendql_api_custom_expressions.py +190 -5
  3. pylegend/core/language/pandas_api/pandas_api_series.py +3 -0
  4. pylegend/core/sql/metamodel.py +4 -1
  5. pylegend/core/tds/legendql_api/frames/functions/legendql_api_distinct_function.py +53 -7
  6. pylegend/core/tds/legendql_api/frames/legendql_api_base_tds_frame.py +146 -4
  7. pylegend/core/tds/legendql_api/frames/legendql_api_tds_frame.py +33 -2
  8. pylegend/core/tds/pandas_api/frames/functions/assign_function.py +65 -23
  9. pylegend/core/tds/pandas_api/frames/functions/drop.py +3 -3
  10. pylegend/core/tds/pandas_api/frames/functions/dropna.py +167 -0
  11. pylegend/core/tds/pandas_api/frames/functions/fillna.py +162 -0
  12. pylegend/core/tds/pandas_api/frames/functions/filter.py +10 -5
  13. pylegend/core/tds/pandas_api/frames/functions/truncate_function.py +151 -120
  14. pylegend/core/tds/pandas_api/frames/pandas_api_applied_function_tds_frame.py +7 -3
  15. pylegend/core/tds/pandas_api/frames/pandas_api_base_tds_frame.py +300 -34
  16. pylegend/core/tds/pandas_api/frames/pandas_api_tds_frame.py +78 -9
  17. pylegend/extensions/tds/pandas_api/frames/pandas_api_legend_function_input_frame.py +9 -4
  18. pylegend/extensions/tds/pandas_api/frames/pandas_api_legend_service_input_frame.py +12 -5
  19. pylegend/extensions/tds/pandas_api/frames/pandas_api_table_spec_input_frame.py +12 -4
  20. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/METADATA +1 -1
  21. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/RECORD +25 -23
  22. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/WHEEL +0 -0
  23. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/licenses/LICENSE +0 -0
  24. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/licenses/LICENSE.spdx +0 -0
  25. {pylegend-0.11.0.dist-info → pylegend-0.12.0.dist-info}/licenses/NOTICE +0 -0
@@ -1,120 +1,151 @@
1
- # Copyright 2025 Goldman Sachs
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
- from datetime import date
17
- from pylegend._typing import (
18
- PyLegendList,
19
- PyLegendSequence,
20
- PyLegendUnion,
21
- )
22
- from pylegend.core.sql.metamodel import LongLiteral, QuerySpecification
23
- from pylegend.core.tds.pandas_api.frames.pandas_api_applied_function_tds_frame import PandasApiAppliedFunction
24
- from pylegend.core.tds.pandas_api.frames.pandas_api_base_tds_frame import PandasApiBaseTdsFrame
25
- from pylegend.core.tds.sql_query_helpers import create_sub_query, copy_query
26
- from pylegend.core.tds.tds_column import TdsColumn
27
- from pylegend.core.tds.tds_frame import FrameToPureConfig, FrameToSqlConfig
28
-
29
-
30
- class TruncateFunction(PandasApiAppliedFunction):
31
- __base_frame: PandasApiBaseTdsFrame
32
- __before: int
33
- __after: PyLegendUnion[int, None]
34
- __axis: PyLegendUnion[str, int]
35
- __copy: bool
36
-
37
- @classmethod
38
- def name(cls) -> str:
39
- return "truncate" # pragma: no cover
40
-
41
- def __init__(
42
- self,
43
- base_frame: PandasApiBaseTdsFrame,
44
- before: PyLegendUnion[date, str, int, None],
45
- after: PyLegendUnion[date, str, int, None],
46
- axis: PyLegendUnion[str, int],
47
- copy: bool,
48
- ) -> None:
49
- self.__base_frame = base_frame
50
- self.__before_input = before
51
- self.__after_input = after
52
- self.__axis = axis
53
- self.__copy = copy
54
-
55
- def to_sql(self, config: FrameToSqlConfig) -> QuerySpecification:
56
- base_query: QuerySpecification = self.__base_frame.to_sql_query_object(config)
57
- should_create_sub_query = (base_query.offset is not None) or (base_query.limit is not None)
58
- new_query = create_sub_query(base_query, config, "root") if should_create_sub_query else copy_query(base_query)
59
- new_query.offset = LongLiteral(self.__before)
60
- if self.__after is not None:
61
- new_query.limit = LongLiteral(self.__after - self.__before + 1)
62
- return new_query
63
-
64
- def to_pure(self, config: FrameToPureConfig) -> str:
65
- if self.__after is None:
66
- return f"{self.__base_frame.to_pure(config)}{config.separator(1)}" f"->drop({self.__before})"
67
-
68
- start_row = self.__before
69
- end_row = self.__after + 1
70
- return f"{self.__base_frame.to_pure(config)}{config.separator(1)}" f"->slice({start_row}, {end_row})"
71
-
72
- def base_frame(self) -> PandasApiBaseTdsFrame:
73
- return self.__base_frame
74
-
75
- def tds_frame_parameters(self) -> PyLegendList["PandasApiBaseTdsFrame"]:
76
- return []
77
-
78
- def calculate_columns(self) -> PyLegendSequence["TdsColumn"]:
79
- return [c.copy() for c in self.__base_frame.columns()]
80
-
81
- def validate(self) -> bool:
82
- if self.__axis not in [0, "index"]:
83
- raise NotImplementedError(
84
- f"The 'axis' parameter of the truncate function must be 0 or 'index', but got: {self.__axis}"
85
- )
86
-
87
- if self.__copy not in [True]:
88
- raise NotImplementedError(f"The 'copy' parameter of the truncate function must be True, but got: {self.__copy}")
89
-
90
- if self.__before_input is None:
91
- self.__before = 0
92
- else:
93
- self.__before = self.get_positive_integer_or_raise_exception(self.__before_input, variable_name="before")
94
-
95
- if self.__after_input is None:
96
- self.__after = None
97
- return True
98
- else:
99
- self.__after = self.get_positive_integer_or_raise_exception(self.__after_input, variable_name="after")
100
-
101
- if self.__before > self.__after:
102
- raise ValueError(
103
- f"The 'before' parameter of the truncate function must be less than or equal to the 'after' parameter, "
104
- f"but got: before={self.__before}, after={self.__after}"
105
- )
106
-
107
- return True
108
-
109
- def get_positive_integer_or_raise_exception(
110
- self, variable: PyLegendUnion[date, str, int, None], variable_name: str
111
- ) -> int:
112
- if type(variable) is not int:
113
- raise NotImplementedError(
114
- f"The '{variable_name}' parameter of the truncate function must be an integer, "
115
- f"but got: {variable} (type: {type(variable).__name__})"
116
- )
117
-
118
- if variable < 0:
119
- return 0
120
- return variable
1
+ # Copyright 2025 Goldman Sachs
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
+ from datetime import date
17
+ from pylegend._typing import (
18
+ PyLegendList,
19
+ PyLegendSequence,
20
+ PyLegendUnion,
21
+ PyLegendTuple,
22
+ PyLegendOptional,
23
+ )
24
+ from pylegend.core.sql.metamodel import LongLiteral, QuerySpecification
25
+ from pylegend.core.tds.pandas_api.frames.pandas_api_applied_function_tds_frame import PandasApiAppliedFunction
26
+ from pylegend.core.tds.pandas_api.frames.pandas_api_base_tds_frame import PandasApiBaseTdsFrame
27
+ from pylegend.core.tds.sql_query_helpers import create_sub_query, copy_query
28
+ from pylegend.core.tds.tds_column import TdsColumn
29
+ from pylegend.core.tds.tds_frame import FrameToPureConfig, FrameToSqlConfig
30
+
31
+
32
+ class TruncateFunction(PandasApiAppliedFunction):
33
+ __base_frame: PandasApiBaseTdsFrame
34
+ __before: int
35
+ __after: PyLegendUnion[int, None]
36
+ __axis: PyLegendUnion[str, int]
37
+ __copy: bool
38
+
39
+ @classmethod
40
+ def name(cls) -> str:
41
+ return "truncate" # pragma: no cover
42
+
43
+ def __init__(
44
+ self,
45
+ base_frame: PandasApiBaseTdsFrame,
46
+ before: PyLegendUnion[date, str, int, None],
47
+ after: PyLegendUnion[date, str, int, None],
48
+ axis: PyLegendUnion[str, int],
49
+ copy: bool,
50
+ ) -> None:
51
+ self.__base_frame = base_frame
52
+ self.__before_input = before
53
+ self.__after_input = after
54
+ self.__axis = axis
55
+ self.__copy = copy
56
+
57
+ def to_sql(self, config: FrameToSqlConfig) -> QuerySpecification:
58
+ base_query: QuerySpecification = self.__base_frame.to_sql_query_object(config)
59
+ should_create_sub_query = (base_query.offset is not None) or (base_query.limit is not None)
60
+ new_query = create_sub_query(base_query, config, "root") if should_create_sub_query else copy_query(base_query)
61
+ new_query.offset = LongLiteral(self.__before)
62
+
63
+ if self.__after is not None:
64
+ new_query.limit = LongLiteral(self.__after - self.__before + 1)
65
+ return new_query
66
+
67
+ def to_pure(self, config: FrameToPureConfig) -> str:
68
+ if self.__after is None:
69
+ return f"{self.__base_frame.to_pure(config)}{config.separator(1)}" f"->drop({self.__before})"
70
+
71
+ start_row = self.__before
72
+ end_row = self.__after + 1
73
+ return f"{self.__base_frame.to_pure(config)}{config.separator(1)}" f"->slice({start_row}, {end_row})"
74
+
75
+ def base_frame(self) -> PandasApiBaseTdsFrame:
76
+ return self.__base_frame
77
+
78
+ def tds_frame_parameters(self) -> PyLegendList["PandasApiBaseTdsFrame"]:
79
+ return []
80
+
81
+ def calculate_columns(self) -> PyLegendSequence["TdsColumn"]:
82
+ return [c.copy() for c in self.__base_frame.columns()]
83
+
84
+ def validate(self) -> bool:
85
+ if self.__axis not in [0, "index"]:
86
+ raise NotImplementedError(
87
+ f"The 'axis' parameter of the truncate function must be 0 or 'index', but got: {self.__axis}"
88
+ )
89
+
90
+ if self.__copy not in [True]:
91
+ raise NotImplementedError(f"The 'copy' parameter of the truncate function must be True, but got: {self.__copy}")
92
+
93
+ self.__before, self.__after = self.__normalize_before_and_after(self.__before_input, self.__after_input)
94
+ return True
95
+
96
+ @staticmethod
97
+ def __normalize_before_and_after(
98
+ before_input: PyLegendUnion[date, str, int, None],
99
+ after_input: PyLegendUnion[date, str, int, None]
100
+ ) -> PyLegendTuple[int, PyLegendOptional[int]]:
101
+
102
+ if isinstance(before_input, (date, str)):
103
+ raise NotImplementedError(
104
+ f"The 'before' parameter of the truncate function must be of type integer or None, "
105
+ f"but got: before={before_input} (type: {type(before_input).__name__})")
106
+
107
+ if isinstance(after_input, (date, str)):
108
+ raise NotImplementedError(
109
+ f"The 'after' parameter of the truncate function must be of type integer or None, "
110
+ f"but got: after={after_input} (type: {type(after_input).__name__})")
111
+
112
+ def __raise_error_if_before_gt_after(before_input: int, after_input: int) -> None:
113
+ if before_input > after_input:
114
+ raise ValueError(
115
+ f"The 'before' parameter of the truncate function must be less than or equal to the 'after' parameter, "
116
+ f"but got: before={before_input}, after={after_input}")
117
+
118
+ if before_input is None:
119
+ if after_input is None:
120
+ return 0, None
121
+
122
+ if isinstance(after_input, int) and after_input >= 0:
123
+ return 0, after_input
124
+
125
+ if isinstance(after_input, int) and after_input < 0:
126
+ return 0, -1
127
+
128
+ if isinstance(before_input, int) and before_input >= 0:
129
+ if after_input is None:
130
+ return before_input, None
131
+
132
+ if isinstance(after_input, int) and after_input >= 0:
133
+ __raise_error_if_before_gt_after(before_input, after_input)
134
+ return before_input, after_input
135
+
136
+ if isinstance(after_input, int) and after_input < 0:
137
+ __raise_error_if_before_gt_after(before_input, after_input)
138
+
139
+ if isinstance(before_input, int) and before_input < 0:
140
+ if after_input is None:
141
+ return 0, None
142
+
143
+ if isinstance(after_input, int) and after_input >= 0:
144
+ __raise_error_if_before_gt_after(before_input, after_input)
145
+ return 0, after_input
146
+
147
+ if isinstance(after_input, int) and after_input < 0:
148
+ __raise_error_if_before_gt_after(before_input, after_input)
149
+ return 0, -1
150
+
151
+ return 0, 0 # pragma: no cover
@@ -13,16 +13,17 @@
13
13
  # limitations under the License.
14
14
 
15
15
  from abc import ABCMeta, abstractmethod
16
+
16
17
  from pylegend._typing import (
17
18
  PyLegendSequence,
18
19
  PyLegendList,
20
+ PyLegendType
19
21
  )
20
22
  from pylegend.core.sql.metamodel import QuerySpecification
23
+ from pylegend.core.tds.pandas_api.frames.pandas_api_base_tds_frame import PandasApiBaseTdsFrame
21
24
  from pylegend.core.tds.tds_column import TdsColumn
22
- from pylegend.core.tds.tds_frame import FrameToSqlConfig
23
25
  from pylegend.core.tds.tds_frame import FrameToPureConfig
24
- from pylegend.core.tds.pandas_api.frames.pandas_api_base_tds_frame import PandasApiBaseTdsFrame
25
-
26
+ from pylegend.core.tds.tds_frame import FrameToSqlConfig, PyLegendTdsFrame
26
27
 
27
28
  __all__: PyLegendSequence[str] = [
28
29
  "PandasApiAppliedFunctionTdsFrame",
@@ -69,6 +70,9 @@ class PandasApiAppliedFunctionTdsFrame(PandasApiBaseTdsFrame):
69
70
  super().__init__(columns=applied_function.calculate_columns())
70
71
  self.__applied_function = applied_function
71
72
 
73
+ def get_super_type(self) -> PyLegendType[PyLegendTdsFrame]:
74
+ return type(self) # pragma: no cover
75
+
72
76
  def to_sql_query_object(self, config: FrameToSqlConfig) -> QuerySpecification:
73
77
  return self.__applied_function.to_sql(config)
74
78