Transfer some cyan's sql queries into python.
Functions are not expected to work right now.