-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Custom Query Type Format #429
Comments
I don't understand what you're trying to accomplish. As you pointed out, Do you have an example of what you think "set_query_format" would do? |
For example, I want to use datetime in the query, I can do db.query('SELECT %s', [datetime.datetime.now()]) it will be formatted as Therefore, I decided to use If custom types were supported via def format_whenever_instant(value: whenever.Instant, server_tz: tzinfo):
return format_time(value.to_tz(server_tz) # that formats to YYYY-MM-DD HH:MM:SS.nnnnnnnnn with proper timezone It will be easily supported just as db.query('SELECT %s', [whenever.Instant.now()]) On the implementation side: query_formatter_map = {}
def set_query_format(cls: type, formatter):
query_formatter_map[cls] = formatter
def format_bind_value(value: Any, server_tz: tzinfo = pytz.UTC, top_level: bool = True):
if type(value) in query_formatter_map:
formatter = query_formatter_map[type(value)]
return formatter(value, server_tz)
pass # the remaining code as usual
def format_query_value(value: Any, server_tz: tzinfo = pytz.UTC):
if type(value) in query_formatter_map:
formatter = query_formatter_map[type(value)]
return formatter(value, server_tz)
pass # the remaining code as usual I'd like to file a PR if you are happy with the change |
The same is technically true for all queries. But binding of some built-in types is already implemented. |
Okay, now that I understand that this particular issue is limited to query binding I think it's fine if you want to submit a PR. To add customized formatters like this for read/write formats is more complicated and definitely has performance implications, so I'm not in a hurry to add that kind of functionality. |
Is your feature request related to a problem? Please describe.
I'm trying to integrate 3rd party types into clickhouse-connect. For example,
whenever.Instant
I found them useful here:
https://clickhouse.com/docs/en/integrations/python#write-formats
https://clickhouse.com/docs/en/integrations/python#read-formats
The write-format could be related, but I found the query string formatting is its own function and rather limited.
Describe the solution you'd like
It would be best to add set_query_format, and refers to it when constructing query SQL.
Describe alternatives you've considered
I tried monkey-patching. I did patching format_bind_value and format_query_value. but the meanings of them are unclear.
The text was updated successfully, but these errors were encountered: