Skip to content

Commit fc4088a

Browse files
tcyameterstick-copybara
authored andcommitted
For alias that has special characters, instead of sanitize it, use `` to escape it.
PiperOrigin-RevId: 350897245
1 parent c378520 commit fc4088a

File tree

5 files changed

+113
-138
lines changed

5 files changed

+113
-138
lines changed

meterstick_demo.ipynb

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@
7878
},
7979
{
8080
"cell_type": "code",
81-
"execution_count": null,
81+
"execution_count": 1,
8282
"metadata": {
83+
"executionInfo": {
84+
"elapsed": 12094,
85+
"status": "ok",
86+
"timestamp": 1608033023655,
87+
"user": {
88+
"displayName": "",
89+
"photoUrl": "",
90+
"userId": ""
91+
},
92+
"user_tz": 480
93+
},
8394
"id": "f8zpdK4-y-CQ"
8495
},
8596
"outputs": [],
@@ -93,15 +104,15 @@
93104
},
94105
{
95106
"cell_type": "code",
96-
"execution_count": null,
107+
"execution_count": 2,
97108
"metadata": {
98109
"colab": {
99110
"height": 204
100111
},
101112
"executionInfo": {
102-
"elapsed": 490,
113+
"elapsed": 608,
103114
"status": "ok",
104-
"timestamp": 1607761536163,
115+
"timestamp": 1608033024296,
105116
"user": {
106117
"displayName": "",
107118
"photoUrl": "",
@@ -110,7 +121,7 @@
110121
"user_tz": 480
111122
},
112123
"id": "hyDE-bxMBxQY",
113-
"outputId": "bea4054f-8d56-40d7-9763-d3826c20dcf3"
124+
"outputId": "68ec8f8c-ec98-4e56-cb99-315878c9928c"
114125
},
115126
"outputs": [
116127
{
@@ -201,7 +212,7 @@
201212
"4 16 1.470001 Desktop ctrl non-US 2"
202213
]
203214
},
204-
"execution_count": 3,
215+
"execution_count": 2,
205216
"metadata": {
206217
"tags": []
207218
},
@@ -5796,38 +5807,35 @@
57965807
},
57975808
{
57985809
"cell_type": "code",
5799-
"execution_count": null,
5810+
"execution_count": 3,
58005811
"metadata": {
5801-
"colab": {
5802-
"height": 119
5803-
},
58045812
"executionInfo": {
5805-
"elapsed": 450,
5813+
"elapsed": 380,
58065814
"status": "ok",
5807-
"timestamp": 1599954217684,
5815+
"timestamp": 1608033025528,
58085816
"user": {
58095817
"displayName": "",
58105818
"photoUrl": "",
58115819
"userId": ""
58125820
},
5813-
"user_tz": 420
5821+
"user_tz": 480
58145822
},
58155823
"id": "i9wHC71rred8",
5816-
"outputId": "cdef4ce9-3bb6-43db-8001-dd55c67a458f"
5824+
"outputId": "cea68e98-192e-44f2-bb5f-fd20886f5f35"
58175825
},
58185826
"outputs": [
58195827
{
58205828
"data": {
58215829
"text/plain": [
58225830
"SELECT\n",
58235831
" grp,\n",
5824-
" SUM(IF(Y \u003e 0, X, NULL)) AS sum_X,\n",
5825-
" SUM(X) AS sum_X_1\n",
5832+
" SUM(IF(Y \u003e 0, X, NULL)) AS `sum(X)`,\n",
5833+
" SUM(X) AS `sum(X)_1`\n",
58265834
"FROM T\n",
58275835
"GROUP BY grp"
58285836
]
58295837
},
5830-
"execution_count": 10,
5838+
"execution_count": 3,
58315839
"metadata": {
58325840
"tags": []
58335841
},

metrics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,16 @@ def compute_on_sql(
422422
res = execute(query)
423423
else:
424424
raise ValueError('Unrecognized SQL engine!')
425+
# We replace '$' with 'macro_' in the generated SQL. To recover the names,
426+
# we cannot just replace 'macro_' with '$' because 'macro_' might be in the
427+
# orignal names. So we set index using the replaced names then reset index
428+
# names, which recovers all indexes. Unfortunately it's not as easy to
429+
# recover metric/column names, so for the remaining columns we just replace
430+
# 'macro_' with '$'.
425431
if indexes:
426-
res.set_index(list(map(utils.sql_name_sanitize, indexes)), inplace=True)
432+
res.set_index([k.replace('$', 'macro_') for k in indexes], inplace=True)
427433
res.index.names = indexes
434+
res.columns = [c.replace('macro_', '$') for c in res.columns]
428435
if split_by: # Use a stable sort.
429436
res.sort_values(split_by, kind='mergesort', inplace=True)
430437
return utils.melt(res) if melted else res

operations.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,6 @@ def display(res,
488488
AbsoluteChange('', '').name_tmpl.format(''),
489489
PercentChange('', '').name_tmpl.format('')
490490
]
491-
comparison_suffix += [
492-
utils.sql_name_sanitize(c.lower()) for c in comparison_suffix
493-
]
494491
comparison_suffix = '(%s)$' % '|'.join(comparison_suffix)
495492
# Don't use inplace=True. It will change the index of 'raw' too.
496493
res.index = res.index.set_levels(
@@ -747,10 +744,7 @@ def compute_on_sql(
747744
raw = res.iloc[:, -n_metrics:]
748745
res = res.iloc[:, :3 * n_metrics]
749746
change = self.children[0]
750-
raw.columns = [
751-
utils.sql_name_sanitize(change.name_tmpl.lower().format(c))
752-
for c in raw.columns
753-
]
747+
raw.columns = [change.name_tmpl.format(c) for c in raw.columns]
754748
raw = utils.melt(raw)
755749
raw.columns = ['_base_value']
756750
indexes = [i for i in indexes if i not in change.extra_index]

0 commit comments

Comments
 (0)