Search in sources :

Example 1 with PrecedenceClimbingParser

use of org.apache.calcite.util.PrecedenceClimbingParser in project calcite by apache.

the class SqlParserUtil method toTreeEx.

/**
 * Converts a list of {expression, operator, expression, ...} into a tree,
 * taking operator precedence and associativity into account.
 *
 * @param list        List of operands and operators. This list is modified as
 *                    expressions are reduced.
 * @param start       Position of first operand in the list. Anything to the
 *                    left of this (besides the immediately preceding operand)
 *                    is ignored. Generally use value 1.
 * @param minPrec     Minimum precedence to consider. If the method encounters
 *                    an operator of lower precedence, it doesn't reduce any
 *                    further.
 * @param stopperKind If not {@link SqlKind#OTHER}, stop reading the list if
 *                    we encounter a token of this kind.
 * @return the root node of the tree which the list condenses into
 */
public static SqlNode toTreeEx(SqlSpecialOperator.TokenSequence list, int start, final int minPrec, final SqlKind stopperKind) {
    final Predicate<PrecedenceClimbingParser.Token> predicate = new PredicateImpl<PrecedenceClimbingParser.Token>() {

        public boolean test(PrecedenceClimbingParser.Token t) {
            if (t instanceof PrecedenceClimbingParser.Op) {
                final SqlOperator op = ((ToTreeListItem) t.o).op;
                return stopperKind != SqlKind.OTHER && op.kind == stopperKind || minPrec > 0 && op.getLeftPrec() < minPrec;
            } else {
                return false;
            }
        }
    };
    PrecedenceClimbingParser parser = list.parser(start, predicate);
    final int beforeSize = parser.all().size();
    parser.partialParse();
    final int afterSize = parser.all().size();
    final SqlNode node = convert(parser.all().get(0));
    list.replaceSublist(start, start + beforeSize - afterSize + 1, node);
    return node;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) PredicateImpl(org.apache.calcite.runtime.PredicateImpl) PrecedenceClimbingParser(org.apache.calcite.util.PrecedenceClimbingParser) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

PredicateImpl (org.apache.calcite.runtime.PredicateImpl)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlOperator (org.apache.calcite.sql.SqlOperator)1 PrecedenceClimbingParser (org.apache.calcite.util.PrecedenceClimbingParser)1