Skip to content

Commit 5cd91a7

Browse files
committed
rebuild and recheck
1 parent bcb0c25 commit 5cd91a7

File tree

16 files changed

+409
-229
lines changed

16 files changed

+409
-229
lines changed

README.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
"name": "stdout",
267267
"output_type": "stream",
268268
"text": [
269-
"BigQuery_DBHandle(db_model=BigQueryModel, conn=<google.cloud.bigquery.client.Client object at 0x7fb578a72e80>)\n"
269+
"BigQuery_DBHandle(db_model=BigQueryModel, conn=<google.cloud.bigquery.client.Client object at 0x7fa6a0a4da30>)\n"
270270
]
271271
}
272272
],
@@ -485,7 +485,7 @@
485485
" \"irrelevantCol2\",\n",
486486
" ],\n",
487487
" )\n",
488-
" .extend({\"probability\": \"((assessmentTotal * 0.237)).exp()\"})\n",
488+
" .extend({\"probability\": \"(assessmentTotal * 0.237).exp()\"})\n",
489489
" .extend({\"total\": \"probability.sum()\"}, partition_by=[\"subjectID\"])\n",
490490
" .extend({\"probability\": \"probability / total\"})\n",
491491
" .extend(\n",
@@ -566,7 +566,7 @@
566566
" `data-algebra-test.test_1.d`\n",
567567
" ),\n",
568568
" `extend_1` AS (\n",
569-
" SELECT -- .extend({ 'probability': '((assessmentTotal * 0.237)).exp()'})\n",
569+
" SELECT -- .extend({ 'probability': '(assessmentTotal * 0.237).exp()'})\n",
570570
" `surveyCategory` ,\n",
571571
" `subjectID` ,\n",
572572
" EXP(`assessmentTotal` * 0.237) AS `probability`\n",
@@ -681,24 +681,24 @@
681681
" <tbody>\n",
682682
" <tr>\n",
683683
" <th>0</th>\n",
684-
" <td>withdrawal behavior</td>\n",
685-
" <td>0.670622</td>\n",
686-
" <td>1</td>\n",
687-
" </tr>\n",
688-
" <tr>\n",
689-
" <th>1</th>\n",
690684
" <td>positive re-framing</td>\n",
691685
" <td>0.558974</td>\n",
692686
" <td>2</td>\n",
693687
" </tr>\n",
688+
" <tr>\n",
689+
" <th>1</th>\n",
690+
" <td>withdrawal behavior</td>\n",
691+
" <td>0.670622</td>\n",
692+
" <td>1</td>\n",
693+
" </tr>\n",
694694
" </tbody>\n",
695695
"</table>\n",
696696
"</div>"
697697
],
698698
"text/plain": [
699699
" diagnosis probability subjectID\n",
700-
"0 withdrawal behavior 0.670622 1\n",
701-
"1 positive re-framing 0.558974 2"
700+
"0 positive re-framing 0.558974 2\n",
701+
"1 withdrawal behavior 0.670622 1"
702702
]
703703
},
704704
"execution_count": 8,

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ db_handle = data_algebra.BigQuery.example_handle()
173173
print(db_handle)
174174
```
175175

176-
BigQuery_DBHandle(db_model=BigQueryModel, conn=<google.cloud.bigquery.client.Client object at 0x7fb578a72e80>)
176+
BigQuery_DBHandle(db_model=BigQueryModel, conn=<google.cloud.bigquery.client.Client object at 0x7fa6a0a4da30>)
177177

178178

179179

@@ -301,7 +301,7 @@ print(py_source)
301301
"irrelevantCol2",
302302
],
303303
)
304-
.extend({"probability": "((assessmentTotal * 0.237)).exp()"})
304+
.extend({"probability": "(assessmentTotal * 0.237).exp()"})
305305
.extend({"total": "probability.sum()"}, partition_by=["subjectID"])
306306
.extend({"probability": "probability / total"})
307307
.extend(
@@ -357,7 +357,7 @@ print(sql)
357357
`data-algebra-test.test_1.d`
358358
),
359359
`extend_1` AS (
360-
SELECT -- .extend({ 'probability': '((assessmentTotal * 0.237)).exp()'})
360+
SELECT -- .extend({ 'probability': '(assessmentTotal * 0.237).exp()'})
361361
`surveyCategory` ,
362362
`subjectID` ,
363363
EXP(`assessmentTotal` * 0.237) AS `probability`
@@ -437,16 +437,16 @@ db_handle.read_query(sql)
437437
<tbody>
438438
<tr>
439439
<th>0</th>
440-
<td>withdrawal behavior</td>
441-
<td>0.670622</td>
442-
<td>1</td>
443-
</tr>
444-
<tr>
445-
<th>1</th>
446440
<td>positive re-framing</td>
447441
<td>0.558974</td>
448442
<td>2</td>
449443
</tr>
444+
<tr>
445+
<th>1</th>
446+
<td>withdrawal behavior</td>
447+
<td>0.670622</td>
448+
<td>1</td>
449+
</tr>
450450
</tbody>
451451
</table>
452452
</div>

build/lib/data_algebra/MySQL.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _MySQL_concat_expr(dbmodel, expression):
4343

4444
# map from op-name to special SQL formatting code
4545
MySQL_formatters = {
46-
"___": lambda dbmodel, expression: expression.to_python(),
46+
"___": lambda dbmodel, expression: str(expression.to_python()),
4747
"is_bad": _MySQL_is_bad_expr,
4848
"concat": _MySQL_concat_expr,
4949
}

build/lib/data_algebra/PostgreSQL.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# map from op-name to special SQL formatting code
1616
PostgreSQL_formatters = {
17-
"___": lambda dbmodel, expression: expression.to_python(),
17+
"___": lambda dbmodel, expression: str(expression.to_python()),
1818
}
1919

2020

build/lib/data_algebra/SparkSQL.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def coalesce_step(x):
5555

5656
# map from op-name to special SQL formatting code
5757
SparkSQL_formatters = {
58-
"___": lambda dbmodel, expression: expression.to_python(),
58+
"___": lambda dbmodel, expression: str(expression.to_python()),
5959
"is_bad": _sparksql_is_bad_expr,
6060
"coalesce": _sparksql_coalesce_expr,
6161
}

build/lib/data_algebra/db_model.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,9 @@ def extend_to_sql(
879879
oi.get_column_names(cols_used_in_term)
880880
cols_used_in_term.update(window_vars)
881881
declared_term_dependencies[ci] = cols_used_in_term
882-
annotation = extend_node.to_python_implementation(
882+
annotation = str(extend_node.to_python_implementation(
883883
print_sources=False, indent=-1
884-
)
884+
))
885885
# TODO: see if we can merge with subsql instead of building a new one
886886
if (
887887
self.allow_extend_merges
@@ -978,9 +978,9 @@ def project_to_sql(
978978
quoted_query_name=self.quote_identifier(view_name),
979979
sub_sql=subsql.to_bound_near_sql(columns=subusing),
980980
suffix=suffix,
981-
annotation=project_node.to_python_implementation(
981+
annotation=str(project_node.to_python_implementation(
982982
print_sources=False, indent=-1
983-
),
983+
)),
984984
)
985985
return near_sql
986986

@@ -1018,9 +1018,9 @@ def select_rows_to_sql(
10181018
quoted_query_name=self.quote_identifier(view_name),
10191019
sub_sql=subsql.to_bound_near_sql(columns=subusing),
10201020
suffix=suffix,
1021-
annotation=select_rows_node.to_python_implementation(
1021+
annotation=str(select_rows_node.to_python_implementation(
10221022
print_sources=False, indent=-1
1023-
),
1023+
)),
10241024
)
10251025
return near_sql
10261026

@@ -1127,9 +1127,9 @@ def order_to_sql(
11271127
quoted_query_name=self.quote_identifier(view_name),
11281128
sub_sql=subsql.to_bound_near_sql(columns=subusing),
11291129
suffix=suffix,
1130-
annotation=order_node.to_python_implementation(
1130+
annotation=str(order_node.to_python_implementation(
11311131
print_sources=False, indent=-1
1132-
),
1132+
)),
11331133
)
11341134
return near_sql
11351135

@@ -1162,9 +1162,9 @@ def rename_to_sql(
11621162
terms=terms,
11631163
quoted_query_name=self.quote_identifier(view_name),
11641164
sub_sql=subsql.to_bound_near_sql(columns=subusing),
1165-
annotation=rename_node.to_python_implementation(
1165+
annotation=str(rename_node.to_python_implementation(
11661166
print_sources=False, indent=-1
1167-
),
1167+
)),
11681168
)
11691169
return near_sql
11701170

@@ -1259,9 +1259,9 @@ def natural_join_to_sql(
12591259
joiner=jointype + " JOIN",
12601260
sub_sql2=sql_right.to_bound_near_sql(columns=using_right, force_sql=False),
12611261
suffix=on_terms,
1262-
annotation=join_node.to_python_implementation(
1262+
annotation=str(join_node.to_python_implementation(
12631263
print_sources=False, indent=-1
1264-
),
1264+
)),
12651265
)
12661266
return near_sql
12671267

@@ -1315,9 +1315,9 @@ def concat_rows_to_sql(
13151315
sub_sql2=sql_right.to_bound_near_sql(
13161316
columns=using_left, force_sql=True, constants=constants_right,
13171317
),
1318-
annotation=concat_node.to_python_implementation(
1318+
annotation=str(concat_node.to_python_implementation(
13191319
print_sources=False, indent=-1
1320-
),
1320+
)),
13211321
)
13221322
return near_sql
13231323

build/lib/data_algebra/expr_rep.py

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@
1414
# http://tomerfiliba.com/blog/Infix-Operators/
1515

1616

17+
class PythonText:
18+
"""
19+
Class for holding text representation of Python, with possible additional annotations.
20+
str() method returns only the text for interoperability.
21+
"""
22+
def __init__(self, s: str, *, is_in_parens: bool = False):
23+
assert isinstance(s, str)
24+
assert isinstance(is_in_parens, bool)
25+
self.s = s
26+
self.is_in_parens = is_in_parens
27+
28+
def __str__(self):
29+
return self.s
30+
31+
def __repr__(self):
32+
return self.s.__repr__()
33+
34+
1735
# list of window/aggregation functions that must be windowed/aggregated
1836
# (note some other functions work in more than one mode)
1937
# noinspection SpellCheckingInspection
@@ -130,7 +148,12 @@ def evaluate(self, data_frame):
130148

131149
# emitters
132150

133-
def to_python(self, *, want_inline_parens=False):
151+
def to_python(self, *, want_inline_parens : bool = False) -> PythonText:
152+
"""
153+
Convert parsed expression into a string
154+
155+
:param want_inline_parens: bool if True put parens around complex expressions that don't already have a grouper.
156+
"""
134157
raise NotImplementedError(
135158
"base class method called"
136159
) # https://docs.python.org/3/library/exceptions.html
@@ -144,10 +167,10 @@ def to_source(self, *, want_inline_parens=False, dialect="Python"):
144167
# printing
145168

146169
def __repr__(self):
147-
return self.to_python(want_inline_parens=False)
170+
return str(self.to_python(want_inline_parens=False))
148171

149172
def __str__(self):
150-
return self.to_python(want_inline_parens=False)
173+
return str(self.to_python(want_inline_parens=False))
151174

152175

153176
def _is_none_value(x):
@@ -696,8 +719,8 @@ def replace_view(self, view):
696719
def evaluate(self, data_frame):
697720
return self.value
698721

699-
def to_python(self, *, want_inline_parens=False):
700-
return self.value.__repr__()
722+
def to_python(self, *, want_inline_parens : bool = False) -> PythonText:
723+
return PythonText(self.value.__repr__(), is_in_parens=False)
701724

702725
# don't collect -5 as a complex expression
703726
def __neg__(self):
@@ -752,14 +775,14 @@ def evaluate(self, data_frame):
752775
res[i] = vi
753776
return res
754777

755-
def to_python(self, *, want_inline_parens=False):
778+
def to_python(self, *, want_inline_parens : bool = False) -> PythonText:
756779
def li_to_python(value):
757780
try:
758-
return value.to_python(want_inline_parens=want_inline_parens)
781+
return str(value.to_python(want_inline_parens=False))
759782
except AttributeError:
760783
return str(value) # TODO: check if this should be repr?
761784

762-
return "[" + ", ".join([li_to_python(ai) for ai in self.value]) + "]"
785+
return PythonText("[" + ", ".join([li_to_python(ai) for ai in self.value]) + "]", is_in_parens=False)
763786

764787
def get_column_names(self, columns_seen):
765788
for ti in self.value:
@@ -814,8 +837,8 @@ def get_views(self):
814837
def replace_view(self, view):
815838
return ColumnReference(view=view, column_name=self.column_name)
816839

817-
def to_python(self, want_inline_parens=False):
818-
return self.column_name
840+
def to_python(self, *, want_inline_parens : bool = False) -> PythonText:
841+
return PythonText(self.column_name, is_in_parens=False)
819842

820843
def get_column_names(self, columns_seen):
821844
columns_seen.add(self.column_name)
@@ -964,32 +987,36 @@ def evaluate(self, data_frame):
964987
pass
965988
raise KeyError(f"function {self.op} not found")
966989

967-
def to_python(self, *, want_inline_parens=False):
968-
subs = [ai.to_python(want_inline_parens=True) for ai in self.args]
969-
if len(subs) <= 0:
970-
return self.op + "()"
971-
if len(subs) == 1:
990+
def to_python(self, *, want_inline_parens : bool = False) -> PythonText:
991+
n_args = len(self.args)
992+
if n_args <= 0:
993+
return PythonText(self.op + "()", is_in_parens=False)
994+
if n_args == 1:
995+
sub_0 = self.args[0].to_python(want_inline_parens=False)
972996
if self.inline:
973-
return self.op + self.args[0].to_python(want_inline_parens=True)
997+
if sub_0.is_in_parens:
998+
return PythonText(self.op + str(sub_0), is_in_parens=False)
999+
return PythonText(self.op + '(' + str(sub_0) + ')', is_in_parens=False)
9741000
if self.method:
975-
if isinstance(self.args[0], ColumnReference):
976-
return subs[0] + "." + self.op + "()"
977-
else:
978-
return "(" + subs[0] + ")." + self.op + "()"
1001+
if sub_0.is_in_parens or isinstance(self.args[0], ColumnReference):
1002+
return PythonText(str(sub_0) + "." + self.op + "()", is_in_parens=False)
1003+
return PythonText("(" + str(sub_0) + ")." + self.op + "()", is_in_parens=False)
9791004
if self.inline:
980-
result = ""
981-
if want_inline_parens:
982-
result = result + "("
983-
result = result + (" " + self.op + " ").join(subs)
1005+
subs = [str(ai.to_python(want_inline_parens=True)) for ai in self.args]
1006+
result = (" " + self.op + " ").join(subs)
9841007
if want_inline_parens:
985-
result = result + ")"
986-
return result
1008+
return PythonText('(' + result + ')', is_in_parens=True)
1009+
return PythonText(result, is_in_parens=False)
1010+
subs = [ai.to_python(want_inline_parens=False) for ai in self.args]
1011+
subs_0 = subs[0]
1012+
subs = [str(si) for si in subs]
9871013
if self.method:
988-
if isinstance(self.args[0], ColumnReference):
989-
return subs[0] + "." + self.op + "(" + ", ".join(subs[1:]) + ")"
1014+
if subs_0.is_in_parens or isinstance(self.args[0], ColumnReference):
1015+
return PythonText(subs[0] + "." + self.op + "(" + ", ".join(subs[1:]) + ")", is_in_parens=False)
9901016
else:
991-
return "(" + subs[0] + ")." + self.op + "(" + ", ".join(subs[1:]) + ")"
992-
return self.op + "(" + ", ".join(subs) + ")"
1017+
return PythonText("(" + subs[0] + ")." + self.op + "(" + ", ".join(subs[1:]) + ")", is_in_parens=False)
1018+
# treat as fn call
1019+
return PythonText(self.op + '(' + ", ".join(subs) + ')', is_in_parens=False)
9931020

9941021

9951022
# define with def so function has usable __name__

0 commit comments

Comments
 (0)