diff --git a/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp b/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp
index 825c5eb5f925..da8db2d4a386 100644
--- a/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp
@@ -1,32 +1,20 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
+#include "IParserKQLFunction.h"
+
+#include "KQLFunctionFactory.h"
+
 #include <Parsers/Kusto/ParserKQLOperators.h>
 
-namespace DB
-{
+#include <format>
+#include <numeric>
 
-namespace ErrorCodes
+namespace DB::ErrorCodes
 {
     extern const int SYNTAX_ERROR;
     extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
 }
 
-
+namespace DB
+{
 bool IParserKQLFunction::convert(String & out,IParser::Pos & pos)
 {
     return wrapConvertImpl(pos, IncreaseDepthTag{}, [&]
@@ -38,11 +26,11 @@ bool IParserKQLFunction::convert(String & out,IParser::Pos & pos)
     });
 }
 
-bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const String & ch_fn)
+bool IParserKQLFunction::directMapping(String & out, IParser::Pos & pos, const String & ch_fn)
 {
     std::vector<String> arguments;
 
-    String fn_name = getKQLFunctionName(pos); 
+    String fn_name = getKQLFunctionName(pos);
 
     if (fn_name.empty())
         return false;
@@ -52,17 +40,17 @@ bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const Str
     ++pos;
     while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
     {
-        String argument = getConvertedArgument(fn_name,pos);
+        String argument = getConvertedArgument(fn_name, pos);
         arguments.push_back(argument);
 
         if (pos->type == TokenType::ClosingRoundBracket)
         {
-            for (auto arg : arguments) 
+            for (auto arg : arguments)
             {
                 if (res.empty())
                     res = ch_fn + "(" + arg;
                 else
-                    res = res + ", "+ arg;
+                    res = res + ", " + arg;
             }
             res += ")";
 
@@ -76,6 +64,11 @@ bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const Str
     return false;
 }
 
+String IParserKQLFunction::getArgument(const String & function_name, IParser::Pos & pos)
+{
+    return getOptionalArgument(function_name, pos).value();
+}
+
 String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser::Pos & pos)
 {
     String converted_arg;
@@ -90,13 +83,13 @@ String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser:
 
     while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
     {
-        String token = String(pos->begin,pos->end);
+        String token = String(pos->begin, pos->end);
         String new_token;
-        if (!KQLOperators().convert(tokens,pos))
+        if (!KQLOperators().convert(tokens, pos))
         {
-            if (pos->type == TokenType::BareWord )
+            if (pos->type == TokenType::BareWord)
             {
-                tokens.push_back(IParserKQLFunction::getExpression(pos));
+                tokens.push_back(getExpression(pos));
             }
             else if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket)
             {
@@ -109,8 +102,8 @@ String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser:
         if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket)
             break;
     }
-    for (auto token : tokens) 
-        converted_arg = converted_arg + token +" ";
+    for (auto token : tokens)
+        converted_arg = converted_arg + token + " ";
 
     return converted_arg;
 }
@@ -124,19 +117,44 @@ String IParserKQLFunction::getKQLFunctionName(IParser::Pos & pos)
         --pos;
         return "";
     }
-    return  fn_name;
+    return fn_name;
+}
+
+std::optional<String> IParserKQLFunction::getOptionalArgument(const String & function_name, IParser::Pos & pos)
+{
+    std::optional<String> argument;
+    if (const auto & type = pos->type; type != TokenType::Comma && type != TokenType::OpeningRoundBracket)
+        return {};
+
+    ++pos;
+    return getConvertedArgument(function_name, pos);
+}
+
+String IParserKQLFunction::kqlCallToExpression(
+    const String & function_name, std::initializer_list<std::reference_wrapper<const String>> params, const uint32_t max_depth)
+{
+    const auto params_str = std::accumulate(
+        std::cbegin(params),
+        std::cend(params),
+        String(),
+        [](auto acc, const auto & param) { return (acc.empty() ? "" : ", ") + std::move(acc) + param.get(); });
+
+    const auto kql_call = std::format("{}({})", function_name, params_str);
+    Tokens call_tokens(kql_call.c_str(), kql_call.c_str() + kql_call.length());
+    IParser::Pos tokens_pos(call_tokens, max_depth);
+    return getExpression(tokens_pos);
 }
 
 void IParserKQLFunction::validateEndOfFunction(const String & fn_name, IParser::Pos & pos)
 {
-    if (pos->type != TokenType:: ClosingRoundBracket)
+    if (pos->type != TokenType::ClosingRoundBracket)
         throw Exception("Too many arguments in function: " + fn_name, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
 }
 
 String IParserKQLFunction::getExpression(IParser::Pos & pos)
 {
     String arg = String(pos->begin, pos->end);
-    if (pos->type == TokenType::BareWord )
+    if (pos->type == TokenType::BareWord)
     {
         String new_arg;
         auto fun = KQLFunctionFactory::get(arg);
@@ -148,5 +166,4 @@ String IParserKQLFunction::getExpression(IParser::Pos & pos)
     }
     return arg;
 }
-
 }
diff --git a/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.h b/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.h
index 7ed3841583b9..784a1ea3c6b2 100644
--- a/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.h
+++ b/src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include <Parsers/IParser.h>
+
 namespace DB
 {
 class IParserKQLFunction
@@ -16,7 +16,11 @@ class IParserKQLFunction
             pos = begin;
         return res;
     }
-    struct IncreaseDepthTag {};
+
+    struct IncreaseDepthTag
+    {
+    };
+
     template <typename F>
     ALWAYS_INLINE static bool wrapConvertImpl(IParser::Pos & pos, IncreaseDepthTag, const F & func)
     {
@@ -28,17 +32,23 @@ class IParserKQLFunction
             pos = begin;
         return res;
     }
-    bool convert(String & out,IParser::Pos & pos);
+
+    bool convert(String & out, IParser::Pos & pos);
     virtual const char * getName() const = 0;
     virtual ~IParserKQLFunction() = default;
+
     static String getExpression(IParser::Pos & pos);
+
 protected:
-    virtual bool convertImpl(String & out,IParser::Pos & pos) = 0;
-    static bool directMapping(String  &out,IParser::Pos & pos,const String & ch_fn);
+    virtual bool convertImpl(String & out, IParser::Pos & pos) = 0;
+
+    static bool directMapping(String & out, IParser::Pos & pos, const String & ch_fn);
+    static String getArgument(const String & function_name, IParser::Pos & pos);
     static String getConvertedArgument(const String & fn_name, IParser::Pos & pos);
+    static std::optional<String> getOptionalArgument(const String & function_name, IParser::Pos & pos);
+    static String kqlCallToExpression(
+        const String & function_name, std::initializer_list<std::reference_wrapper<const String>> params, uint32_t max_depth);
     static void validateEndOfFunction(const String & fn_name, IParser::Pos & pos);
     static String getKQLFunctionName(IParser::Pos & pos);
 };
-
 }
-
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.cpp
index 1bfb094518f7..aaa136943e68 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLAggregationFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h
index 6e7130420f4c..95789bae370f 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class ArgMax : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.cpp
index 2a06c4e715be..269859a3050f 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLBinaryFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h
index 94ca3a5abbfe..591c0fd236e6 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class BinaryAnd : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp
index 9129d82aa780..8a93f298fad8 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp
@@ -1,7 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
+#include "KQLCastingFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h
index fa6a20e60687..4b244c6364e5 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class ToBool : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp
index 0f098cbebda3..7b4e8f81dbf0 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLDateTimeFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h
index 7627465ab5bc..8528f9634e44 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.cpp
index a6ff0a374ebc..117674678076 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLDynamicFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h
index e36fd60eaeaf..5d34b6d9ef00 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class ArrayConcat : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.cpp b/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.cpp
index 25e0c2af2f91..25f69bde652e 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.cpp
@@ -1,20 +1,15 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
+#include "KQLFunctionFactory.h"
+
+#include "IParserKQLFunction.h"
+#include "KQLDateTimeFunctions.h"
+#include "KQLStringFunctions.h"
+#include "KQLDynamicFunctions.h"
+#include "KQLCastingFunctions.h"
+#include "KQLAggregationFunctions.h"
+#include "KQLTimeSeriesFunctions.h"
+#include "KQLIPFunctions.h"
+#include "KQLBinaryFunctions.h"
+#include "KQLGeneralFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h b/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h
index 7c5f0d547345..fee18128ea5d 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h
@@ -1,8 +1,9 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 #include <unordered_map>
+
 namespace DB
 {
     enum class KQLFunctionValue : uint16_t
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.cpp
index 253292a7d9df..b19686d6423b 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLGeneralFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h
index 802fd152333f..1d0b65616ef0 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class Bin : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.cpp
index 0383292669d8..2f82c7e1d0cc 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.cpp
@@ -1,30 +1,9 @@
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/ParserSetQuery.h>
+#include "KQLIPFunctions.h"
 
 #include <format>
 
-namespace DB::ErrorCodes
-{
-extern const int SYNTAX_ERROR;
-}
-
 namespace DB
 {
-
 bool Ipv4Compare::convertImpl(String & out, IParser::Pos & pos)
 {
     String res = String(pos->begin, pos->end);
@@ -38,13 +17,15 @@ bool Ipv4IsInRange::convertImpl(String & out, IParser::Pos & pos)
     if (function_name.empty())
         return false;
 
-    ++pos;
-
-    const auto ip_address = getConvertedArgument(function_name, pos);
-    ++pos;
-
-    const auto ip_range = getConvertedArgument(function_name, pos);
-    out = std::format("isIPAddressInRange({0}, concat({1}, if(position({1}, '/') > 0, '', '/32')))", ip_address, ip_range);
+    const auto ip_address = getArgument(function_name, pos);
+    const auto ip_range = getArgument(function_name, pos);
+    out = std::format(
+        "if(isNull(IPv4StringToNumOrNull({0}) as ip) or isNull({2} as calculated_mask) or "
+        "isNull(toIPv4OrNull(tokens[1]) as range_prefix_ip), null, isIPAddressInRange(IPv4NumToString(assumeNotNull(ip)), "
+        "concat(IPv4NumToString(assumeNotNull(range_prefix_ip)), '/', toString(assumeNotNull(calculated_mask)))))",
+        ip_address,
+        ip_range,
+        kqlCallToExpression("ipv4_netmask_suffix", {ip_range}, pos.max_depth));
     return true;
 }
 
@@ -57,26 +38,24 @@ bool Ipv4IsMatch::convertImpl(String & out, IParser::Pos & pos)
 
 bool Ipv4IsPrivate::convertImpl(String & out, IParser::Pos & pos)
 {
-    static const std::array<String, 3> PRIVATE_SUBNETS{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"};
+    static const std::array<String, 3> s_private_subnets{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"};
 
     const auto function_name = getKQLFunctionName(pos);
     if (function_name.empty())
         return false;
 
-    ++pos;
-
-    const auto ip_address = getConvertedArgument(function_name, pos);
+    const auto ip_address = getArgument(function_name, pos);
 
     out += std::format(
         "multiIf(length(splitByChar('/', {0}) as tokens) > 2 or isNull(toIPv4OrNull(tokens[1]) as nullable_ip), null, "
-        "length(tokens) = 2 and isNull(toUInt8OrNull(tokens[-1]) as suffix), null, "
+        "length(tokens) = 2 and isNull(toUInt8OrNull(tokens[-1]) as mask), null, "
         "ignore(assumeNotNull(nullable_ip) as ip, "
-        "IPv4CIDRToRange(ip, assumeNotNull(suffix)) as range, IPv4NumToString(tupleElement(range, 1)) as begin, "
+        "IPv4CIDRToRange(ip, assumeNotNull(mask)) as range, IPv4NumToString(tupleElement(range, 1)) as begin, "
         "IPv4NumToString(tupleElement(range, 2)) as end), null, ",
         ip_address);
-    for (int i = 0; i < std::ssize(PRIVATE_SUBNETS); ++i)
+    for (int i = 0; i < std::ssize(s_private_subnets); ++i)
     {
-        const auto & subnet = PRIVATE_SUBNETS[i];
+        const auto & subnet = s_private_subnets[i];
         out += std::format(
             "length(tokens) = 1 and isIPAddressInRange(IPv4NumToString(ip), '{0}') or "
             "isIPAddressInRange(begin, '{0}') and isIPAddressInRange(end, '{0}'), true, ",
@@ -93,19 +72,27 @@ bool Ipv4NetmaskSuffix::convertImpl(String & out, IParser::Pos & pos)
     if (function_name.empty())
         return false;
 
-    ++pos;
-
-    const auto ip_range = getConvertedArgument(function_name, pos);
+    const auto ip_range = getArgument(function_name, pos);
     out = std::format(
         "multiIf(length(splitByChar('/', {0}) as tokens) > 2 or not isIPv4String(tokens[1]), null, "
-        "length(tokens) = 1, 32, isNull(toUInt8OrNull(tokens[-1]) as suffix), null, toUInt8(min2(suffix, 32)))",
+        "length(tokens) = 1, 32, isNull(toUInt8OrNull(tokens[-1]) as mask), null, toUInt8(min2(mask, 32)))",
         ip_range);
     return true;
 }
 
 bool ParseIpv4::convertImpl(String & out, IParser::Pos & pos)
 {
-    return directMapping(out, pos, "toIPv4OrNull");
+    const auto function_name = getKQLFunctionName(pos);
+    if (function_name.empty())
+        return false;
+
+    const auto ip_address = getArgument(function_name, pos);
+    out = std::format(
+        "multiIf(length(splitByChar('/', {0}) as tokens) = 1, IPv4StringToNumOrNull(tokens[1]) as ip, "
+        "length(tokens) = 2 and isNotNull(ip) and isNotNull(toUInt8OrNull(tokens[-1]) as mask), "
+        "tupleElement(IPv4CIDRToRange(assumeNotNull(ip), assumeNotNull(mask)), 1), null)",
+        ip_address);
+    return true;
 }
 
 bool ParseIpv4Mask::convertImpl(String & out, IParser::Pos & pos)
@@ -131,7 +118,17 @@ bool Ipv6IsMatch::convertImpl(String & out, IParser::Pos & pos)
 
 bool ParseIpv6::convertImpl(String & out, IParser::Pos & pos)
 {
-    return directMapping(out, pos, "toIPv6OrNull");
+    const auto function_name = getKQLFunctionName(pos);
+    if (function_name.empty())
+        return false;
+
+    const auto ip_address = getArgument(function_name, pos);
+    out = std::format(
+        "if(isNull(ifNull(if(isNull({1} as ipv4), null, IPv4ToIPv6(ipv4)), IPv6StringToNumOrNull({0})) as ipv6), null, "
+        "arrayStringConcat(flatten(extractAllGroups(lower(hex(assumeNotNull(ipv6))), '([\\da-f]{{4}})')), ':'))",
+        ip_address,
+        kqlCallToExpression("parse_ipv4", {ip_address}, pos.max_depth));
+    return true;
 }
 
 bool ParseIpv6Mask::convertImpl(String & out, IParser::Pos & pos)
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.h
index 3ee5dda4c839..9a28b997669a 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLIPFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class Ipv4Compare : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp
index c2d1bd251da8..3fc322b4cf2b 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp
@@ -1,11 +1,11 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
-#include <format>
-#include <cstdlib>
+#include "KQLStringFunctions.h"
+
 #include <Parsers/CommonParsers.h>
 
+#include <cstdlib>
+#include <format>
+#include <unordered_map>
+
 namespace DB::ErrorCodes
 {
 extern const int SYNTAX_ERROR;
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.h
index 43840c1253f1..209377e6a587 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class Base64EncodeToString : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.cpp
index 74b7811f29ef..d5be8e262a84 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.cpp
+++ b/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.cpp
@@ -1,19 +1,4 @@
-#include <Parsers/IParserBase.h>
-#include <Parsers/ParserSetQuery.h>
-#include <Parsers/ASTExpressionList.h>
-#include <Parsers/ASTSelectWithUnionQuery.h>
-#include <Parsers/Kusto/ParserKQLQuery.h>
-#include <Parsers/Kusto/ParserKQLStatement.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
-#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
+#include "KQLTimeSeriesFunctions.h"
 
 namespace DB
 {
diff --git a/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h b/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h
index fa97dec151c7..999a27f6b391 100644
--- a/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h
+++ b/src/Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h
@@ -1,7 +1,7 @@
 #pragma once
 
-#include <Parsers/IParserBase.h>
-#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
+#include "IParserKQLFunction.h"
+
 namespace DB
 {
 class SeriesFir : public IParserKQLFunction
diff --git a/src/Parsers/Kusto/ParserKQLQuery.cpp b/src/Parsers/Kusto/ParserKQLQuery.cpp
index 7f00a76fa726..9c5e3bb6a28b 100644
--- a/src/Parsers/Kusto/ParserKQLQuery.cpp
+++ b/src/Parsers/Kusto/ParserKQLQuery.cpp
@@ -12,6 +12,7 @@
 #include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
 #include <Parsers/Kusto/ParserKQLOperators.h>
 #include <Parsers/Kusto/ParserKQLPrint.h>
+
 namespace DB
 {
 
