Created
October 9, 2017 22:29
-
-
Save onelittlenightmusic/ffa169295d27f3c001ea638fab924ab6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from sqlalchemy.sql import text, select, join | |
def wrap_array_for_columns(arr): | |
return map(lambda x: text(column_text(x)), arr) | |
def column_text(column_struct): | |
_name = column_struct.get("name") | |
_table = column_struct.get("table") | |
_alias = column_struct.get("alias", _name) | |
if _table is None: | |
return "%s as %s"%(_name, _alias) | |
return "%s.%s as %s"%(_table, _name, _alias) | |
a = 'restaurant_from_df' | |
b = 'restaurant_cross' | |
c = 'restaurant_from_json' | |
grade_json = 'json_array_elements(grades_json::json) grade' | |
s = select(wrap_array_for_columns( | |
[{"name": "grade->>'score'", "alias": "score"}, | |
{"name": "name", "table": a} | |
]), | |
from_obj=[join( | |
join(text(a),text(b),text('%s.restaurant_id=%s.restaurant_id'%(a,b))), | |
text(c), text('%s.restaurant_id=%s.restaurant_id'%(a,c)) | |
),text(grade_json)]) \ | |
.limit(text('10')) | |
select3 = str(s) | |
print(select3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment