Search in sources :

Example 1 with ExpressionNode

use of io.questdb.griffin.model.ExpressionNode in project questdb by bluestreak01.

the class PostOrderTreeTraversalAlgo method traverse.

public void traverse(ExpressionNode node, Visitor visitor) throws SqlException {
    backup();
    try {
        // post-order iterative tree traversal
        // see http://en.wikipedia.org/wiki/Tree_traversal
        stack.clear();
        indexStack.clear();
        ExpressionNode lastVisited = null;
        while (!stack.isEmpty() || node != null) {
            if (node != null) {
                stack.push(node);
                indexStack.push(0);
                node = node.rhs;
            } else {
                ExpressionNode peek = stack.peek();
                assert peek != null;
                if (peek.paramCount < 3) {
                    if (peek.lhs != null && lastVisited != peek.lhs) {
                        node = peek.lhs;
                    } else {
                        visitor.visit(peek);
                        lastVisited = stack.poll();
                        indexStack.pop();
                    }
                } else {
                    int index = indexStack.peek();
                    if (index < peek.paramCount) {
                        node = peek.args.getQuick(index);
                        indexStack.update(index + 1);
                    } else {
                        visitor.visit(peek);
                        lastVisited = stack.poll();
                        indexStack.pop();
                    }
                }
            }
        }
    } finally {
        restore();
    }
}
Also used : ExpressionNode(io.questdb.griffin.model.ExpressionNode)

Example 2 with ExpressionNode

use of io.questdb.griffin.model.ExpressionNode in project questdb by bluestreak01.

the class SampleByFillValueRecordCursorFactory method createPlaceholderFunctions.

@NotNull
public static ObjList<Function> createPlaceholderFunctions(ObjList<Function> recordFunctions, @Transient IntList recordFunctionPositions, @NotNull @Transient ObjList<ExpressionNode> fillValues) throws SqlException {
    final ObjList<Function> placeholderFunctions = new ObjList<>();
    int fillIndex = 0;
    final int fillValueCount = fillValues.size();
    for (int i = 0, n = recordFunctions.size(); i < n; i++) {
        Function function = recordFunctions.getQuick(i);
        if (function instanceof GroupByFunction) {
            if (fillIndex == fillValueCount) {
                throw SqlException.position(0).put("not enough values");
            }
            ExpressionNode fillNode = fillValues.getQuick(fillIndex++);
            try {
                switch(ColumnType.tagOf(function.getType())) {
                    case ColumnType.INT:
                        placeholderFunctions.add(IntConstant.newInstance(Numbers.parseInt(fillNode.token)));
                        break;
                    case ColumnType.LONG:
                        placeholderFunctions.add(LongConstant.newInstance(Numbers.parseLong(fillNode.token)));
                        break;
                    case ColumnType.FLOAT:
                        placeholderFunctions.add(FloatConstant.newInstance(Numbers.parseFloat(fillNode.token)));
                        break;
                    case ColumnType.DOUBLE:
                        placeholderFunctions.add(DoubleConstant.newInstance(Numbers.parseDouble(fillNode.token)));
                        break;
                    case ColumnType.SHORT:
                        placeholderFunctions.add(ShortConstant.newInstance((short) Numbers.parseInt(fillNode.token)));
                        break;
                    case ColumnType.BYTE:
                        placeholderFunctions.add(ByteConstant.newInstance((byte) Numbers.parseInt(fillNode.token)));
                        break;
                    default:
                        throw SqlException.$(recordFunctionPositions.getQuick(i), "Unsupported type: ").put(ColumnType.nameOf(function.getType()));
                }
            } catch (NumericException e) {
                throw SqlException.position(fillNode.position).put("invalid number: ").put(fillNode.token);
            }
        } else {
            placeholderFunctions.add(function);
        }
    }
    return placeholderFunctions;
}
Also used : GroupByFunction(io.questdb.griffin.engine.functions.GroupByFunction) GroupByFunction(io.questdb.griffin.engine.functions.GroupByFunction) Function(io.questdb.cairo.sql.Function) ExpressionNode(io.questdb.griffin.model.ExpressionNode) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ExpressionNode

use of io.questdb.griffin.model.ExpressionNode in project questdb by bluestreak01.

the class WhereClauseParser method analyzeNotIn.

private boolean analyzeNotIn(AliasTranslator translator, IntrinsicModel model, ExpressionNode notNode, RecordMetadata m, FunctionParser functionParser, RecordMetadata metadata, SqlExecutionContext executionContext) throws SqlException {
    ExpressionNode node = notNode.rhs;
    if (node.paramCount < 2) {
        throw SqlException.$(node.position, "Too few arguments for 'in'");
    }
    ExpressionNode col = node.paramCount < 3 ? node.lhs : node.args.getLast();
    if (col.type != ExpressionNode.LITERAL) {
        throw SqlException.$(col.position, "Column name expected");
    }
    CharSequence column = translator.translateAlias(col.token);
    if (m.getColumnIndexQuiet(column) == -1) {
        throw SqlException.invalidColumn(col.position, col.token);
    }
    boolean ok = analyzeInInterval(model, col, node, true, functionParser, metadata, executionContext);
    if (ok) {
        notNode.intrinsicValue = IntrinsicModel.TRUE;
    } else {
        analyzeNotListOfValues(model, column, m, node, notNode);
    }
    return ok;
}
Also used : ExpressionNode(io.questdb.griffin.model.ExpressionNode) FlyweightCharSequence(io.questdb.std.str.FlyweightCharSequence)

Example 4 with ExpressionNode

use of io.questdb.griffin.model.ExpressionNode in project questdb by bluestreak01.

the class WhereClauseParser method extractWithin.

ExpressionNode extractWithin(AliasTranslator translator, ExpressionNode node, RecordMetadata metadata, FunctionParser functionParser, SqlExecutionContext executionContext, LongList prefixes) throws SqlException {
    prefixes.clear();
    if (node == null)
        return null;
    if (removeWithin(translator, node, metadata, functionParser, executionContext, prefixes)) {
        return collapseWithinNodes(node);
    }
    ExpressionNode root = node;
    while (!stack.isEmpty() || node != null) {
        if (node != null) {
            if (isAndKeyword(node.token) || isOrKeyword(node.token)) {
                if (!removeWithin(translator, node.rhs, metadata, functionParser, executionContext, prefixes)) {
                    stack.push(node.rhs);
                }
                node = removeWithin(translator, node.lhs, metadata, functionParser, executionContext, prefixes) ? null : node.lhs;
            } else {
                node = stack.poll();
            }
        } else {
            node = stack.poll();
        }
    }
    return collapseWithinNodes(root);
}
Also used : ExpressionNode(io.questdb.griffin.model.ExpressionNode)

Example 5 with ExpressionNode

use of io.questdb.griffin.model.ExpressionNode in project questdb by bluestreak01.

the class WhereClauseParser method removeNodes.

private void removeNodes(ExpressionNode b, ObjList<ExpressionNode> nodes) {
    tempNodes.clear();
    for (int i = 0, size = nodes.size(); i < size; i++) {
        ExpressionNode expressionNode = nodes.get(i);
        if ((expressionNode.lhs != null && Chars.equals(expressionNode.lhs.token, b.token)) || (expressionNode.rhs != null && Chars.equals(expressionNode.rhs.token, b.token))) {
            expressionNode.intrinsicValue = IntrinsicModel.TRUE;
            tempNodes.add(expressionNode);
        }
    }
    for (int i = 0, size = tempNodes.size(); i < size; i++) {
        nodes.remove(tempNodes.get(i));
    }
}
Also used : ExpressionNode(io.questdb.griffin.model.ExpressionNode)

Aggregations

ExpressionNode (io.questdb.griffin.model.ExpressionNode)22 QueryColumn (io.questdb.griffin.model.QueryColumn)6 FlyweightCharSequence (io.questdb.std.str.FlyweightCharSequence)6 Function (io.questdb.cairo.sql.Function)4 GroupByFunction (io.questdb.griffin.engine.functions.GroupByFunction)3 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)2 SqlException (io.questdb.griffin.SqlException)2 SymbolFunction (io.questdb.griffin.engine.functions.SymbolFunction)2 ObjList (io.questdb.std.ObjList)2 Test (org.junit.Test)2 AbstractGeoHashFunction (io.questdb.griffin.engine.functions.AbstractGeoHashFunction)1 IntrinsicModel (io.questdb.griffin.model.IntrinsicModel)1 QueryModel (io.questdb.griffin.model.QueryModel)1 NotNull (org.jetbrains.annotations.NotNull)1