diff options
author | Bjørn Christian Seime <bjorn.christian@seime.no> | 2022-12-02 14:52:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-02 14:52:26 +0100 |
commit | 81b884ac555806ae2f0a75773accfd8fe27ecbe1 (patch) | |
tree | 0f92b9ef73a90854cacc96796562d3f21ff1cff6 /container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java | |
parent | c956ac4cb73b329243072aabe35f0da508c02d0f (diff) |
Revert "Let list handling catch up with Java 17"
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java')
-rw-r--r-- | container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java b/container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java index 2c5c1ad83fe..32880f9b1a8 100644 --- a/container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java +++ b/container-search/src/main/java/com/yahoo/search/yql/ProgramParser.java @@ -2,7 +2,13 @@ package com.yahoo.search.yql; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.yahoo.search.yql.yqlplusParser.Annotate_expressionContext; import com.yahoo.search.yql.yqlplusParser.AnnotationContext; import com.yahoo.search.yql.yqlplusParser.ArgumentContext; @@ -60,20 +66,21 @@ import org.antlr.v4.runtime.tree.RuleNode; import org.antlr.v4.runtime.tree.TerminalNode; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; +import java.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Stream; /** * Translate the ANTLR grammar into the logical representation. */ final class ProgramParser { + public yqlplusParser prepareParser(String programName, InputStream input) throws IOException { + return prepareParser(programName, new CaseInsensitiveCharStream(CharStreams.fromStream(input))); + } + public yqlplusParser prepareParser(String programName, String input) throws IOException { return prepareParser(programName, new CaseInsensitiveCharStream(CharStreams.fromString(input))); } @@ -135,9 +142,13 @@ final class ProgramParser { } private List<String> readName(Namespaced_nameContext node) { - return node.children.stream() - .filter(elt -> !(getParseTreeIndex(elt) == yqlplusParser.DOT)) - .map(ParseTree::getText).toList(); + List<String> path = Lists.newArrayList(); + for (ParseTree elt:node.children) { + if (!(getParseTreeIndex(elt) == yqlplusParser.DOT)) { + path.add(elt.getText()); + } + } + return path; } static class Binding { @@ -153,7 +164,7 @@ final class ProgramParser { } public List<String> toPathWith(List<String> rest) { - return Stream.concat(toPath().stream(), rest.stream()).toList(); + return ImmutableList.copyOf(Iterables.concat(toPath(), rest)); } } @@ -162,9 +173,9 @@ final class ProgramParser { final Scope root; final Scope parent; - Set<String> cursors = Set.of(); - Set<String> variables = Set.of(); - Map<String, Binding> bindings = new HashMap<>(); + Set<String> cursors = ImmutableSet.of(); + Set<String> variables = ImmutableSet.of(); + Map<String, Binding> bindings = Maps.newHashMap(); final yqlplusParser parser; final String programName; @@ -220,7 +231,7 @@ final class ProgramParser { throw new ProgramCompileException(loc, "Alias '%s' is already used.", name); } if (cursors.isEmpty()) { - cursors = new HashSet<>(); + cursors = Sets.newHashSet(); } cursors.add(name); } @@ -230,7 +241,7 @@ final class ProgramParser { throw new ProgramCompileException(loc, "Variable/argument '%s' is already used.", name); } if (variables.isEmpty()) { - variables = new HashSet<>(); + variables = Sets.newHashSet(); } variables.add(name); @@ -300,7 +311,7 @@ final class ProgramParser { // OrderbyContext orderby() List<Orderby_fieldContext> orderFieds = ((OrderbyContext) child) .orderby_fields().orderby_field(); - orderby = new ArrayList<>(orderFieds.size()); + orderby = Lists.newArrayListWithExpectedSize(orderFieds.size()); for (var field: orderFieds) { orderby.add(convertSortKey(field, scope)); } @@ -364,7 +375,7 @@ final class ProgramParser { } private OperatorNode<SequenceOperator> readMultiSource(Scope scope, Source_listContext multiSource) { - List<List<String>> sourceNameList = new ArrayList<>(); + List<List<String>> sourceNameList = Lists.newArrayList(); List<Namespaced_nameContext> nameSpaces = multiSource.namespaced_name(); for(Namespaced_nameContext node : nameSpaces) { List<String> name = readName(node); @@ -378,16 +389,16 @@ final class ProgramParser { for (Pipeline_stepContext step:nodes) { if (getParseTreeIndex(step.getChild(0)) == yqlplusParser.RULE_vespa_grouping) { result = OperatorNode.create(SequenceOperator.PIPE, result, List.of(), - List.of(convertExpr(step.getChild(0), scope))); + ImmutableList.of(convertExpr(step.getChild(0), scope))); } else { List<String> name = readName(step.namespaced_name()); - List<OperatorNode<ExpressionOperator>> args = List.of(); + List<OperatorNode<ExpressionOperator>> args = ImmutableList.of(); // LPAREN (argument[$in_select] (COMMA argument[$in_select])*) RPAREN if (step.getChildCount() > 1) { ArgumentsContext arguments = step.arguments(); if (arguments.getChildCount() > 2) { List<ArgumentContext> argumentContextList = arguments.argument(); - args = new ArrayList<>(argumentContextList.size()); + args = Lists.newArrayListWithExpectedSize(argumentContextList.size()); for (ArgumentContext argumentContext: argumentContextList) { args.add(convertExpr(argumentContext.expression(), scope.getRoot())); @@ -460,11 +471,11 @@ final class ProgramParser { case yqlplusParser.RULE_call_source -> { List<String> names = readName(dataSourceNode.getChild(Namespaced_nameContext.class, 0)); alias = assignAlias(names.get(names.size() - 1), aliasContext, scope); - List<OperatorNode<ExpressionOperator>> arguments = List.of(); + List<OperatorNode<ExpressionOperator>> arguments = ImmutableList.of(); ArgumentsContext argumentsContext = dataSourceNode.getRuleContext(ArgumentsContext.class, 0); if (argumentsContext != null) { List<ArgumentContext> argumentContexts = argumentsContext.argument(); - arguments = new ArrayList<>(argumentContexts.size()); + arguments = Lists.newArrayListWithExpectedSize(argumentContexts.size()); for (ArgumentContext argumentContext : argumentContexts) { arguments.add(convertExpr(argumentContext, scope)); } @@ -503,7 +514,7 @@ final class ProgramParser { yqlplusParser parser, String programName) { Scope scope = new Scope(parser, programName); - List<OperatorNode<StatementOperator>> stmts = new ArrayList<>(); + List<OperatorNode<StatementOperator>> stmts = Lists.newArrayList(); int output = 0; for (ParseTree node : program.children) { if (!(node instanceof ParserRuleContext ruleContext)) continue; @@ -613,8 +624,8 @@ final class ProgramParser { } case yqlplusParser.RULE_map_expression: { List<Property_name_and_valueContext> propertyList = ((Map_expressionContext)parseTree).property_name_and_value(); - List<String> names = new ArrayList<>(propertyList.size()); - List<OperatorNode<ExpressionOperator>> exprs = new ArrayList<>(propertyList.size()); + List<String> names = Lists.newArrayListWithExpectedSize(propertyList.size()); + List<OperatorNode<ExpressionOperator>> exprs = Lists.newArrayListWithCapacity(propertyList.size()); for (Property_name_and_valueContext child : propertyList) { // : propertyName ':' expression[$expression::namespace] -> // ^(PROPERTY propertyName expression) @@ -625,7 +636,7 @@ final class ProgramParser { } case yqlplusParser.RULE_array_literal: { List<Constant_expressionContext> expressionList = ((Array_literalContext) parseTree).constant_expression(); - List<OperatorNode<ExpressionOperator>> values = new ArrayList<>(expressionList.size()); + List<OperatorNode<ExpressionOperator>> values = Lists.newArrayListWithExpectedSize(expressionList.size()); for (Constant_expressionContext expr : expressionList) { values.add(convertExpr(expr, scope)); } @@ -657,7 +668,7 @@ final class ProgramParser { } case yqlplusParser.RULE_call_expression: { List<ArgumentContext> args = ((ArgumentsContext) firstChild.getChild(1)).argument(); - List<OperatorNode<ExpressionOperator>> arguments = new ArrayList<>(args.size()); + List<OperatorNode<ExpressionOperator>> arguments = Lists.newArrayListWithExpectedSize(args.size()); for (ArgumentContext argContext : args) { arguments.add(convertExpr(argContext.expression(),scope)); } @@ -878,7 +889,7 @@ final class ProgramParser { if (elements.size() == 1 && scope.getParser().isArrayParameter(firldElement)) { return convertExpr(firldElement, scope); } else { - List<OperatorNode<ExpressionOperator>> values = new ArrayList<>(elements.size()); + List<OperatorNode<ExpressionOperator>> values = Lists.newArrayListWithExpectedSize(elements.size()); for (Literal_elementContext child : elements) { values.add(convertExpr(child.getChild(0), scope)); } @@ -895,10 +906,10 @@ final class ProgramParser { String text = literal.getChild(0).getText(); switch(parseTreeIndex) { case yqlplusParser.INT: - long as_long = Long.parseLong(text); - int as_int = (int)as_long; + Long as_long = Long.valueOf(text); + int as_int = as_long.intValue(); if (as_int == as_long) { - return as_int; + return Integer.valueOf(as_int); } else { return as_long; } @@ -932,7 +943,11 @@ final class ProgramParser { } case ARRAY: { List<OperatorNode<ExpressionOperator>> exprs = node.getArgument(0); - return exprs.stream().map(expr -> readConstantExpression(expr)).toList(); + ImmutableList.Builder<Object> lst = ImmutableList.builder(); + for (OperatorNode<ExpressionOperator> expr : exprs) { + lst.add(readConstantExpression(expr)); + } + return lst.build(); } case VARREF: { return node; // must be dereferenced in YqlParser when we have userQuery @@ -952,7 +967,7 @@ final class ProgramParser { } private OperatorNode<ExpressionOperator> readConjOp(ExpressionOperator op, List<Equality_expressionContext> nodes, Scope scope) { - List<OperatorNode<ExpressionOperator>> arguments = new ArrayList<>(nodes.size()); + List<OperatorNode<ExpressionOperator>> arguments = Lists.newArrayListWithExpectedSize(nodes.size()); for (ParseTree child : nodes) { arguments.add(convertExpr(child, scope)); } @@ -961,13 +976,13 @@ final class ProgramParser { private OperatorNode<ExpressionOperator> readConjOrOp(ExpressionOperator op, Logical_OR_expressionContext node, Scope scope) { List<Logical_AND_expressionContext> andExpressionList = node.logical_AND_expression(); - List<OperatorNode<ExpressionOperator>> arguments = new ArrayList<>(andExpressionList.size()); + List<OperatorNode<ExpressionOperator>> arguments = Lists.newArrayListWithExpectedSize(andExpressionList.size()); for (Logical_AND_expressionContext child : andExpressionList) { List<Equality_expressionContext> equalities = child.equality_expression(); if (equalities.size() == 1) { arguments.add(convertExpr(equalities.get(0), scope)); } else { - List<OperatorNode<ExpressionOperator>> andArguments = new ArrayList<>(equalities.size()); + List<OperatorNode<ExpressionOperator>> andArguments = Lists.newArrayListWithExpectedSize(equalities.size()); for (Equality_expressionContext subTreeChild:equalities) { andArguments.add(convertExpr(subTreeChild, scope)); } @@ -1000,17 +1015,19 @@ final class ProgramParser { * @return list of READ_FIELD expressions */ private List<OperatorNode<ExpressionOperator>> getReadFieldExpressions(OperatorNode<ExpressionOperator> in) { - List<OperatorNode<ExpressionOperator>> readFieldList = new ArrayList<>(); + List<OperatorNode<ExpressionOperator>> readFieldList = Lists.newArrayList(); switch (in.getOperator()) { - case READ_FIELD -> readFieldList.add(in); - case CALL -> { + case READ_FIELD: + readFieldList.add(in); + break; + case CALL: List<OperatorNode<ExpressionOperator>> callArgs = in.getArgument(1); for (OperatorNode<ExpressionOperator> callArg : callArgs) { if (callArg.getOperator() == ExpressionOperator.READ_FIELD) { readFieldList.add(callArg); } } - } + break; } return readFieldList; } |