azure-kusto-python: preserve data types in dynamic values

Viewed 39

I'm using Kusto SDK for Python to query data from Azure Data Explorer. When some query returns single values having Kusto datetime type, they are conveniently converted to Python own datetime type.

from azure.kusto.data import KustoClient, KustoConnectionStringBuilder

cluster = "https://help.kusto.windows.net"
# use another auth method if necessary
kcsb = KustoConnectionStringBuilder.with_az_cli_authentication(cluster)
client = KustoClient(kcsb)
db_name = "Samples"

query_1 = "print x=datetime(2022-09-22)"
response_1 = client.execute_query(db_name, query_1)
result_1 = response_1.primary_results[0].to_dict()["data"]
print(result_1)
print(type(result_1[0]["x"]))

Result:

[{'x': datetime.datetime(2022, 9, 22, 0, 0, tzinfo=tzutc())}]
<class 'datetime.datetime'>

However, using the make_list function to aggregate values from multiple rows, datetimes are implicitly converted to strings. From what I understand, this is due to make_list returning a dynamic value, which gets serialized as JSON and thus causes non-JSON values to be converted to strings. This is a minimum example:

query_2 = "print x=dynamic(datetime(2022-09-22))"
response_2 = client.execute_query(db_name, query_2)
result_2 = response_2.primary_results[0].to_dict()["data"]
print(result_2)
print(type(result_2[0]["x"]))

Result:

[{'x': '2022-09-22T00:00:00.0000000Z'}]
<class 'str'>

Is there any setting or workaround to prevent datetimes inside dynamic values to be converted to strings?

0 Answers
Related