Skip to content

Commit

Permalink
Kusto-phase2: Add alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
kashwy authored and bkuschel committed Jul 7, 2022
1 parent 5ffc427 commit 652ff7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
18 changes: 0 additions & 18 deletions src/Parsers/Kusto/ParserKQLProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,7 @@ bool ParserKQLProject :: parseImpl(Pos & pos, ASTPtr & node, Expected & expected
if (op_pos.empty())
expr = "*";
else
{
for (auto it = op_pos.begin(); it != op_pos.end(); ++it)
{
pos = *it ;
while (!pos->isEnd() && pos->type != TokenType::PipeMark)
{
if (pos->type == TokenType::BareWord)
{
String tmp(pos->begin,pos->end);

if (it != op_pos.begin() && columns.find(tmp) == columns.end())
return false;
columns.insert(tmp);
}
++pos;
}
}
expr = getExprFromToken(op_pos.back());
}

Tokens tokens(expr.c_str(), expr.c_str()+expr.size());
IParser::Pos new_pos(tokens, pos.max_depth);
Expand Down
29 changes: 28 additions & 1 deletion src/Parsers/Kusto/ParserKQLQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ String ParserKQLBase :: getExprFromToken(Pos &pos)
String res;
std::vector<String> tokens;
std::unique_ptr<IParserKQLFunction> kql_function;
String alias;

while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
{
String token = String(pos->begin,pos->end);
String new_token;
if (!KQLOperators().convert(tokens,pos))
if (token == "=")
{
++pos;
if (String(pos->begin,pos->end) != "~" )
{
alias = tokens.back();
tokens.pop_back();
}
--pos;
}
else if (!KQLOperators().convert(tokens,pos))
{
if (pos->type == TokenType::BareWord )
{
Expand All @@ -40,8 +51,24 @@ String ParserKQLBase :: getExprFromToken(Pos &pos)
}
tokens.push_back(token);
}

if (pos->type == TokenType::Comma && !alias.empty())
{
tokens.pop_back();
tokens.push_back("AS");
tokens.push_back(alias);
tokens.push_back(",");
alias.clear();
}
++pos;
}

if (!alias.empty())
{
tokens.push_back("AS");
tokens.push_back(alias);
}

for (auto token:tokens)
res = res + token +" ";
return res;
Expand Down
4 changes: 0 additions & 4 deletions src/Parsers/tests/gtest_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery, ParserTest,
"Customers | project FirstName,LastName,Occupation | take 3 | project FirstName,LastName",
"SELECT\n FirstName,\n LastName\nFROM Customers\nLIMIT 3"
},
{
"Customers | project FirstName,LastName,Occupation | take 3 | project FirstName,LastName,Education",
"throws Syntax error"
},
{
"Customers | sort by FirstName desc",
"SELECT *\nFROM Customers\nORDER BY FirstName DESC"
Expand Down

0 comments on commit 652ff7c

Please sign in to comment.