Skip to content

Commit

Permalink
Query: Adds Antlr dependency for local parsing optimization on non-Wi…
Browse files Browse the repository at this point in the history
…ndows x64 systems (#1743)

* Revert "Internal Query : Fix Remove Antlr dependency for now  (#1626)"

This reverts commit ed7faae.

* added test for antlr dependancy

Co-authored-by: j82w <j82w@users.noreply.github.com>
  • Loading branch information
bchong95 and j82w committed Aug 13, 2020
1 parent d8d56e6 commit 985b518
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 68 deletions.
1 change: 1 addition & 0 deletions Microsoft.Azure.Cosmos/src/Microsoft.Azure.Cosmos.csproj
Expand Up @@ -83,6 +83,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.8.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
Expand Down
34 changes: 16 additions & 18 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/CstToAstVisitor.cs
Expand Up @@ -4,7 +4,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using System;
using System.Collections.Generic;
using Antlr4.Runtime.Tree;
Expand Down Expand Up @@ -127,7 +126,7 @@ public override SqlObject VisitSql_query(sqlParser.Sql_queryContext context)
sqlOffsetLimitClause);
}

#region SELECT
#region SELECT
public override SqlObject VisitSelect_clause(sqlParser.Select_clauseContext context)
{
SqlSelectSpec sqlSelectSpec = (SqlSelectSpec)this.Visit(context.selection());
Expand Down Expand Up @@ -201,8 +200,8 @@ public override SqlObject VisitTop_spec(sqlParser.Top_specContext context)
Number64 topCount = CstToAstVisitor.GetNumber64ValueFromNode(context.NUMERIC_LITERAL());
return SqlTopSpec.Create(SqlNumberLiteral.Create(topCount));
}
#endregion
#region FROM
#endregion
#region FROM
public override SqlObject VisitFrom_clause(sqlParser.From_clauseContext context)
{
Contract.Requires(context != null);
Expand Down Expand Up @@ -311,16 +310,16 @@ public override SqlObject VisitStringPathExpression(sqlParser.StringPathExpressi

return SqlStringPathExpression.Create(pathExpression, stringIndex);
}
#endregion
#region WHERE
#endregion
#region WHERE
public override SqlObject VisitWhere_clause(sqlParser.Where_clauseContext context)
{
Contract.Requires(context != null);
SqlScalarExpression sqlScalarExpression = (SqlScalarExpression)this.Visit(context.scalar_expression());
return SqlWhereClause.Create(sqlScalarExpression);
}
#endregion
#region GROUP BY
#endregion
#region GROUP BY
public override SqlObject VisitGroup_by_clause(sqlParser.Group_by_clauseContext context)
{
Contract.Requires(context != null);
Expand All @@ -333,8 +332,8 @@ public override SqlObject VisitGroup_by_clause(sqlParser.Group_by_clauseContext

return SqlGroupByClause.Create(groupByColumns);
}
#endregion
#region ORDER BY
#endregion
#region ORDER BY
public override SqlObject VisitOrder_by_clause(sqlParser.Order_by_clauseContext context)
{
Contract.Requires(context != null);
Expand Down Expand Up @@ -366,8 +365,8 @@ public override SqlObject VisitOrder_by_clause(sqlParser.Order_by_clauseContext

return SqlOrderbyClause.Create(orderByItems);
}
#endregion
#region OFFSET LIMIT
#endregion
#region OFFSET LIMIT
public override SqlObject VisitOffset_limit_clause(sqlParser.Offset_limit_clauseContext context)
{
Contract.Requires(context != null);
Expand All @@ -384,8 +383,8 @@ public override SqlObject VisitOffset_limit_clause(sqlParser.Offset_limit_clause

return SqlOffsetLimitClause.Create(sqlOffsetSpec, sqlLimitSpec);
}
#endregion
#region ScalarExpressions
#endregion
#region ScalarExpressions
public override SqlObject VisitArrayCreateScalarExpression(sqlParser.ArrayCreateScalarExpressionContext context)
{
Contract.Requires(context != null);
Expand Down Expand Up @@ -638,9 +637,9 @@ public override SqlObject VisitUnaryScalarExpression(sqlParser.UnaryScalarExpres

return SqlUnaryScalarExpression.Create(unaryOperator, expression);
}
#endregion
#endregion

#region NOT IMPLEMENTED ON PURPOSE
#region NOT IMPLEMENTED ON PURPOSE
public override SqlObject VisitBinary_operator(sqlParser.Binary_operatorContext context)
{
throw new NotSupportedException();
Expand Down Expand Up @@ -690,7 +689,7 @@ public override SqlObject VisitScalar_expression_list(sqlParser.Scalar_expressio
{
throw new NotSupportedException();
}
#endregion
#endregion

private sealed class UnknownSqlObjectException : ArgumentOutOfRangeException
{
Expand Down Expand Up @@ -725,5 +724,4 @@ private static Number64 GetNumber64ValueFromNode(IParseTree parseTree)
return number64;
}
}
#endif
}
2 changes: 0 additions & 2 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/ErrorListener.cs
Expand Up @@ -6,7 +6,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -67,5 +66,4 @@ public ErrorListener(Parser parser, Lexer lexer, CommonTokenStream token_stream)
base.SyntaxError(output, recognizer, offendingSymbol, line, col, msg, e);
}
}
#endif
}
2 changes: 0 additions & 2 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlListener.cs
Expand Up @@ -21,7 +21,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using Antlr4.Runtime.Misc;
using IParseTreeListener = Antlr4.Runtime.Tree.IParseTreeListener;

Expand Down Expand Up @@ -583,5 +582,4 @@ internal interface IsqlListener : IParseTreeListener
/// <param name="context">The parse tree.</param>
void ExitLiteral([NotNull] sqlParser.LiteralContext context);
}
#endif
}
2 changes: 0 additions & 2 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/IsqlVisitor.cs
Expand Up @@ -21,7 +21,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using IToken = Antlr4.Runtime.IToken;
Expand Down Expand Up @@ -360,5 +359,4 @@ internal interface IsqlVisitor<Result> : IParseTreeVisitor<Result>
/// <return>The visitor result.</return>
Result VisitLiteral([NotNull] sqlParser.LiteralContext context);
}
#endif
}
3 changes: 0 additions & 3 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/LASets.cs
Expand Up @@ -6,7 +6,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -18,7 +17,6 @@ namespace Microsoft.Azure.Cosmos.Query.Core.Parser

internal class LASets
{

private readonly Dictionary<Pair<ATNState, int>, bool> visited = new Dictionary<Pair<ATNState, int>, bool>();
private readonly bool logParse = false;
private readonly bool logClosure = false;
Expand Down Expand Up @@ -797,5 +795,4 @@ private string PrintResult(List<List<Edge>> all_parses)
return sb.ToString();
}
}
#endif
}
Expand Up @@ -21,7 +21,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;

Expand Down Expand Up @@ -563,5 +562,4 @@ internal partial class sqlBaseVisitor<Result> : AbstractParseTreeVisitor<Result>
/// <return>The visitor result.</return>
public virtual Result VisitLiteral([NotNull] sqlParser.LiteralContext context) { return VisitChildren(context); }
}
#endif
}
2 changes: 0 additions & 2 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/sqlLexer.cs
Expand Up @@ -21,7 +21,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using System;
using System.IO;
using Antlr4.Runtime;
Expand Down Expand Up @@ -563,5 +562,4 @@ static sqlLexer()
public static readonly ATN _ATN =
new ATNDeserializer().Deserialize(_serializedATN);
}
#endif
}
4 changes: 1 addition & 3 deletions Microsoft.Azure.Cosmos/src/Query/Core/Parser/sqlParser.cs
Expand Up @@ -21,7 +21,6 @@

namespace Microsoft.Azure.Cosmos.Query.Core.Parser
{
#if false
using System;
using System.IO;
using Antlr4.Runtime;
Expand Down Expand Up @@ -3326,5 +3325,4 @@ private bool scalar_expression_sempred(Scalar_expressionContext _localctx, int p
public static readonly ATN _ATN =
new ATNDeserializer().Deserialize(_serializedATN);
}
#endif
}
}
9 changes: 0 additions & 9 deletions Microsoft.Azure.Cosmos/src/SqlObjects/SqlQuery.cs
Expand Up @@ -5,10 +5,8 @@ namespace Microsoft.Azure.Cosmos.SqlObjects
{
using System;
using System.Runtime.ExceptionServices;
#if false
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
#endif
using Microsoft.Azure.Cosmos.Query.Core.Parser;
using Microsoft.Azure.Cosmos.SqlObjects.Visitors;

Expand Down Expand Up @@ -76,7 +74,6 @@ public static bool TryParse(string text, out SqlQuery sqlQuery)
throw new ArgumentNullException(nameof(text));
}

#if false
AntlrInputStream str = new AntlrInputStream(text);
sqlLexer lexer = new sqlLexer(str);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Expand Down Expand Up @@ -106,13 +103,8 @@ public static bool TryParse(string text, out SqlQuery sqlQuery)

sqlQuery = (SqlQuery)CstToAstVisitor.Singleton.Visit(programContext);
return true;
#else
sqlQuery = default;
return false;
#endif
}

#if false
private sealed class ThrowExceptionOnErrors : IAntlrErrorStrategy
{
public static readonly ThrowExceptionOnErrors Singleton = new ThrowExceptionOnErrors();
Expand Down Expand Up @@ -153,6 +145,5 @@ public void Sync(Parser recognizer)
// Do nothing
}
}
#endif
}
}
Expand Up @@ -675,12 +675,10 @@ async Task<List<CosmosElement>> AssertPassthroughAsync(string query, Cosmos.Part
Assert.IsTrue(feedOptions.TestSettings.Stats.PipelineType.HasValue);
Assert.AreEqual(TestInjections.PipelineType.Passthrough, feedOptions.TestSettings.Stats.PipelineType.Value);

#if false
if (pk.HasValue)
{
Assert.AreEqual(0, cosmosQueryClientCore.QueryPlanCalls);
}
#endif

return queryResults;
}
Expand Down

0 comments on commit 985b518

Please sign in to comment.