Search in sources :

Example 21 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class HeuristicIndexFilter method matches.

@Override
public boolean matches(Object expression) {
    // Only push ComparisonExpression to the actual indices
    if (expression instanceof CallExpression) {
        return matchAny((CallExpression) expression);
    }
    if (expression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) expression;
        switch(specialForm.getForm()) {
            case BETWEEN:
                Signature sigLeft = Signature.internalOperator(OperatorType.GREATER_THAN_OR_EQUAL, specialForm.getType().getTypeSignature(), specialForm.getArguments().get(1).getType().getTypeSignature());
                Signature sigRight = Signature.internalOperator(OperatorType.LESS_THAN_OR_EQUAL, specialForm.getType().getTypeSignature(), specialForm.getArguments().get(2).getType().getTypeSignature());
                // todo remote udf, we should get FunctionHandle from FunctionAndTypeManager
                CallExpression left = new CallExpression(OperatorType.GREATER_THAN_OR_EQUAL.name(), new BuiltInFunctionHandle(sigLeft), specialForm.getType(), ImmutableList.of(specialForm.getArguments().get(0), specialForm.getArguments().get(1)), Optional.empty());
                CallExpression right = new CallExpression(OperatorType.LESS_THAN_OR_EQUAL.name(), new BuiltInFunctionHandle(sigRight), specialForm.getType(), ImmutableList.of(specialForm.getArguments().get(0), specialForm.getArguments().get(2)), Optional.empty());
                // break it to (>= left and <= right)
                return matches(left) && matches(right);
            case IN:
                Signature sigEqual = Signature.internalOperator(OperatorType.EQUAL, specialForm.getType().getTypeSignature(), specialForm.getArguments().get(1).getType().getTypeSignature());
                for (RowExpression exp : specialForm.getArguments().subList(1, specialForm.getArguments().size())) {
                    // todo remote udf, we should get FunctionHandle from FunctionAndTypeManager
                    if (matches(new CallExpression(OperatorType.EQUAL.name(), new BuiltInFunctionHandle(sigEqual), specialForm.getType(), ImmutableList.of(specialForm.getArguments().get(0), exp), Optional.empty()))) {
                        return true;
                    }
                }
                // None of the values in the IN-valueList matches any index
                return false;
            case AND:
                return matches(specialForm.getArguments().get(0)) && matches(specialForm.getArguments().get(1));
            case OR:
                return matches(specialForm.getArguments().get(0)) || matches(specialForm.getArguments().get(1));
        }
    }
    // Not able to apply index filtering, just don't filter
    return true;
}
Also used : Signature(io.prestosql.spi.function.Signature) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 22 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class TestBTreeIndex method testBetween.

@Test
public void testBetween() throws IOException, IndexLookUpException {
    BTreeIndex index = new BTreeIndex();
    for (int i = 0; i < 20; i++) {
        List<Pair> pairs = new ArrayList<>();
        Long key = Long.valueOf(100 + i);
        String value = "value" + i;
        pairs.add(new Pair(key, value));
        Pair pair = new Pair("dummyCol", pairs);
        index.addKeyValues(Collections.singletonList(pair));
    }
    File file = getFile();
    index.serialize(new FileOutputStream(file));
    BTreeIndex readIndex = new BTreeIndex();
    readIndex.deserialize(new FileInputStream(file));
    RowExpression betweenPredicate = new SpecialForm(SpecialForm.Form.BETWEEN, BOOLEAN, new VariableReferenceExpression("dummyCol", VARCHAR), new ConstantExpression(111L, BIGINT), new ConstantExpression(114L, BIGINT));
    Iterator<String> result = readIndex.lookUp(betweenPredicate);
    assertNotNull(result, "Result shouldn't be null");
    assertTrue(result.hasNext());
    for (int i = 11; i <= 14; i++) {
        assertEquals("value" + i, result.next());
    }
    assertFalse(result.hasNext());
    index.close();
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) FileInputStream(java.io.FileInputStream) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SpecialForm(io.prestosql.spi.relation.SpecialForm) Pair(io.prestosql.spi.heuristicindex.Pair) Test(org.testng.annotations.Test)

Example 23 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class TestBTreeIndex method testIn.

@Test
public void testIn() throws IOException, IndexLookUpException {
    BTreeIndex index = new BTreeIndex();
    for (int i = 0; i < 20; i++) {
        List<Pair> pairs = new ArrayList<>();
        Long key = Long.valueOf(100 + i);
        String value = "value" + i;
        pairs.add(new Pair(key, value));
        Pair pair = new Pair("dummyCol", pairs);
        index.addKeyValues(Collections.singletonList(pair));
    }
    File file = getFile();
    index.serialize(new FileOutputStream(file));
    BTreeIndex readIndex = new BTreeIndex();
    readIndex.deserialize(new FileInputStream(file));
    RowExpression inPredicate = new SpecialForm(SpecialForm.Form.IN, BOOLEAN, new VariableReferenceExpression("dummyCol", VARCHAR), new ConstantExpression(111L, BIGINT), new ConstantExpression(115L, BIGINT), new ConstantExpression(118L, BIGINT), new ConstantExpression(150L, BIGINT));
    Iterator<String> result = readIndex.lookUp(inPredicate);
    assertNotNull(result, "Result shouldn't be null");
    assertTrue(result.hasNext());
    assertEquals("value11", result.next());
    assertEquals("value15", result.next());
    assertEquals("value18", result.next());
    assertFalse(result.hasNext());
    index.close();
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) FileInputStream(java.io.FileInputStream) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SpecialForm(io.prestosql.spi.relation.SpecialForm) Pair(io.prestosql.spi.heuristicindex.Pair) Test(org.testng.annotations.Test)

Example 24 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class LogicalRowExpressions method binaryExpression.

public static RowExpression binaryExpression(SpecialForm.Form form, Collection<RowExpression> expressions) {
    requireNonNull(form, "operator is null");
    requireNonNull(expressions, "expressions is null");
    if (expressions.isEmpty()) {
        switch(form) {
            case AND:
                return TRUE_CONSTANT;
            case OR:
                return FALSE_CONSTANT;
            default:
                throw new IllegalArgumentException("Unsupported binary expression operator");
        }
    }
    // Build balanced tree for efficient recursive processing that
    // preserves the evaluation order of the input expressions.
    // 
    // The tree is built bottom up by combining pairs of elements into
    // binary AND expressions.
    // 
    // Example:
    // 
    // Initial state:
    // a b c d e
    // 
    // First iteration:
    // 
    // /\    /\   e
    // a  b  c  d
    // 
    // Second iteration:
    // 
    // / \    e
    // /\   /\
    // a  b c  d
    // 
    // 
    // Last iteration:
    // 
    // / \
    // / \  e
    // /\   /\
    // a  b c  d
    Queue<RowExpression> queue = new ArrayDeque<>(expressions);
    while (queue.size() > 1) {
        Queue<RowExpression> buffer = new ArrayDeque<>();
        // combine pairs of elements
        while (queue.size() >= 2) {
            List<RowExpression> arguments = asList(queue.remove(), queue.remove());
            buffer.add(new SpecialForm(form, BOOLEAN, arguments));
        }
        // if there's and odd number of elements, just append the last one
        if (!queue.isEmpty()) {
            buffer.add(queue.remove());
        }
        // continue processing the pairs that were just built
        queue = buffer;
    }
    return queue.remove();
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) ArrayDeque(java.util.ArrayDeque) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 25 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project boostkit-bigdata by kunpengcompute.

the class OmniRowExpressionUtil method generateOmniExpr.

/**
 * Generates a RowExpression to ensure like query compatibility with omniruntime
 *
 * @param staticExpr Expression from openLookeng
 * @param translatedExpr RowExpression from staticFilter conversion
 * @return new Optional<RowExpression> with new, regex-syntax arguments for like
 * queries
 */
public static Optional<RowExpression> generateOmniExpr(Expression staticExpr, RowExpression translatedExpr) {
    // parse the expr
    if (staticExpr instanceof SearchedCaseExpression && translatedExpr instanceof SpecialForm) {
        return getWhenExpr((SearchedCaseExpression) staticExpr, (SpecialForm) translatedExpr);
    }
    if (translatedExpr instanceof SpecialForm) {
        SpecialForm specialExpr = (SpecialForm) translatedExpr;
        List<RowExpression> newArguments = new ArrayList<RowExpression>();
        for (int i = 0; i < specialExpr.getArguments().size(); i++) {
            RowExpression nestedExpr = specialExpr.getArguments().get(i);
            if (nestedExpr instanceof SpecialForm || nestedExpr instanceof CallExpression) {
                newArguments.add(generateOmniExpr((Expression) staticExpr.getChildren().get(i), nestedExpr).get());
            } else {
                newArguments.add(specialExpr.getArguments().get(i));
            }
        }
        Optional<RowExpression> newOmniFilter = Optional.of(new SpecialForm(specialExpr.getForm(), specialExpr.getType(), newArguments));
        return newOmniFilter;
    } else if (translatedExpr instanceof CallExpression) {
        CallExpression callExpr = (CallExpression) translatedExpr;
        List<RowExpression> newArguments = new ArrayList<RowExpression>();
        for (int i = 0; i < callExpr.getArguments().size(); i++) {
            RowExpression nestedExpr = callExpr.getArguments().get(i);
            if (nestedExpr instanceof SpecialForm || nestedExpr instanceof CallExpression) {
                newArguments.add(generateOmniExpr((Expression) staticExpr.getChildren().get(i), nestedExpr).get());
            } else {
                newArguments.add(callExpr.getArguments().get(i));
            }
        }
        Optional<RowExpression> newOmniFilter = Optional.of(new CallExpression(callExpr.getDisplayName(), callExpr.getFunctionHandle(), callExpr.getType(), newArguments));
        if ("LIKE".equals(((CallExpression) newOmniFilter.get()).getDisplayName().toUpperCase(Locale.ROOT))) {
            String sqlString = "";
            if (staticExpr.getChildren().get(1) instanceof Cast) {
                sqlString = ((StringLiteral) ((Cast) staticExpr.getChildren().get(1)).getExpression()).getValue();
            } else {
                sqlString = ((StringLiteral) staticExpr.getChildren().get(1)).getValue();
            }
            return generateLikeExpr(sqlString, newOmniFilter);
        }
        return newOmniFilter;
    }
    return Optional.of(translatedExpr);
}
Also used : Cast(io.prestosql.sql.tree.Cast) Optional(java.util.Optional) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Aggregations

SpecialForm (io.prestosql.spi.relation.SpecialForm)27 RowExpression (io.prestosql.spi.relation.RowExpression)20 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)14 CallExpression (io.prestosql.spi.relation.CallExpression)12 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)12 ArrayList (java.util.ArrayList)8 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)6 InputReferenceExpression (io.prestosql.spi.relation.InputReferenceExpression)6 Test (org.testng.annotations.Test)6 Signature (io.prestosql.spi.function.Signature)5 LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)5 OperatorType (io.prestosql.spi.function.OperatorType)4 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)3 Scope (io.airlift.bytecode.Scope)3 Variable (io.airlift.bytecode.Variable)3 FunctionHandle (io.prestosql.spi.function.FunctionHandle)3 IndexMetadata (io.prestosql.spi.heuristicindex.IndexMetadata)3 Type (io.prestosql.spi.type.Type)3 IfStatement (io.airlift.bytecode.control.IfStatement)2 LabelNode (io.airlift.bytecode.instruction.LabelNode)2