Skip to content

Support for running T-SQL query #67

@meavk

Description

@meavk

RTI kusto supports T-SQL along with KQL. However, the tool kusto_query currently supports only KQL.

Azure Data Explorer identifies that it's a T-SQL query when there is an empty comment -- in the beginning. This syntax can be used to identify that It's T-SQL query. Following syntax can be used to set the query_language to sql.

        # Set language to SQL if query starts with "--"
        # https://learn.microsoft.com/en-us/azure/data-explorer/t-sql#query-with-t-sql
        lines = query.lstrip().splitlines()
        if lines and lines[0].strip() == "--":
            # https://learn.microsoft.com/en-us/kusto/api/rest/t-sql?view=microsoft-fabric#request-structure
            crp.set_option("query_language", "sql")

Also, some modification will be needed in the doc string.

def kusto_query(query: str, cluster_uri: str, database: Optional[str] = None) -> List[Dict[str, Any]]:
    """
    Executes a KQL or T-SQL query on the specified database. 
    The type of query is auto-detected based on the first line of the query. In a T-SQL query, the first line must be '--'.
    If no database is provided, it will use the default database.

    :param query: The KQL/T-SQL query to execute. 
    :param cluster_uri: The URI of the Kusto cluster.
    :param database: Optional database name. If not provided, uses the default database.
    :return: The result of the query execution as a list of dictionaries (json).
    """
    return _execute(query, cluster_uri, database=database)

Note: I have tested this locally, can raise a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions