Search in sources :

Example 16 with SpecialForm

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

the class LambdaBytecodeGenerator method variableReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
    return new RowExpressionVisitor<BytecodeNode, Scope>() {

        @Override
        public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitCall(CallExpression call, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitSpecialForm(SpecialForm specialForm, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
            ParameterAndType parameterAndType = parameterMap.get(reference.getName());
            Parameter parameter = parameterAndType.getParameter();
            Class<?> type = parameterAndType.getType();
            return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
        }
    };
}
Also used : InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Scope(io.airlift.bytecode.Scope) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpressionVisitor(io.prestosql.spi.relation.RowExpressionVisitor) Parameter(io.airlift.bytecode.Parameter) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 17 with SpecialForm

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

the class TestSplitFiltering method testIsSplitFilterApplicableForOperators.

/**
 * Test that split filter is applicable for different operators
 */
@Test
public void testIsSplitFilterApplicableForOperators() {
    // EQUAL is supported
    testIsSplitFilterApplicableForOperator(PlanBuilder.comparison(OperatorType.EQUAL, new VariableReferenceExpression("a", VarcharType.VARCHAR), new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR)), true);
    // GREATER_THAN is supported
    testIsSplitFilterApplicableForOperator(PlanBuilder.comparison(OperatorType.GREATER_THAN, new VariableReferenceExpression("a", VarcharType.VARCHAR), new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR)), true);
    // LESS_THAN is supported
    testIsSplitFilterApplicableForOperator(PlanBuilder.comparison(OperatorType.LESS_THAN, new VariableReferenceExpression("a", VarcharType.VARCHAR), new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR)), true);
    // GREATER_THAN_OR_EQUAL is supported
    testIsSplitFilterApplicableForOperator(PlanBuilder.comparison(OperatorType.GREATER_THAN_OR_EQUAL, new VariableReferenceExpression("a", VarcharType.VARCHAR), new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR)), true);
    // LESS_THAN_OR_EQUAL is supported
    testIsSplitFilterApplicableForOperator(PlanBuilder.comparison(OperatorType.LESS_THAN_OR_EQUAL, new VariableReferenceExpression("a", VarcharType.VARCHAR), new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR)), true);
    FunctionResolution functionResolution = new FunctionResolution(METADATA.getFunctionAndTypeManager());
    // NOT is not supported
    testIsSplitFilterApplicableForOperator(Expressions.call("NOT", functionResolution.notFunction(), BOOLEAN, new VariableReferenceExpression("a", VarcharType.VARCHAR)), true);
    // Test multiple supported predicates
    // AND is supported
    RowExpression castExpressionA = Expressions.call(OperatorType.CAST.name(), functionResolution.castFunction(BIGINT.getTypeSignature(), BIGINT.getTypeSignature()), BIGINT, new VariableReferenceExpression("a", BIGINT));
    RowExpression castExpressionB = Expressions.call(OperatorType.CAST.name(), functionResolution.castFunction(fromTypes(BIGINT).get(0).getTypeSignature(), fromTypes(BIGINT).get(0).getTypeSignature()), BIGINT, new VariableReferenceExpression("b", BIGINT));
    RowExpression expr1 = PlanBuilder.comparison(OperatorType.EQUAL, new VariableReferenceExpression("a", BIGINT), castExpressionA);
    RowExpression expr2 = PlanBuilder.comparison(OperatorType.EQUAL, new VariableReferenceExpression("b", BIGINT), castExpressionB);
    RowExpression andLbExpression = new SpecialForm(SpecialForm.Form.AND, BIGINT, expr1, expr2);
    testIsSplitFilterApplicableForOperator(andLbExpression, true);
    // OR is supported
    RowExpression orLbExpression = new SpecialForm(SpecialForm.Form.OR, BIGINT, expr1, expr2);
    testIsSplitFilterApplicableForOperator(orLbExpression, true);
    // IN is supported
    RowExpression item0 = new ConstantExpression(utf8Slice("a"), VarcharType.VARCHAR);
    RowExpression item1 = new ConstantExpression(utf8Slice("hello"), VarcharType.VARCHAR);
    RowExpression item2 = new ConstantExpression(utf8Slice("hello2"), VarcharType.VARCHAR);
    RowExpression inExpression = new SpecialForm(SpecialForm.Form.IN, BOOLEAN, ImmutableList.of(item0, item1, item2));
    testIsSplitFilterApplicableForOperator(inExpression, true);
}
Also used : VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowExpression(io.prestosql.spi.relation.RowExpression) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) SpecialForm(io.prestosql.spi.relation.SpecialForm) Test(org.testng.annotations.Test)

Example 18 with SpecialForm

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

the class BTreeIndex method lookUp.

@Override
public Iterator<String> lookUp(Object expression) throws IndexLookUpException {
    Collection<String> lookUpResults = Collections.emptyList();
    if (expression instanceof CallExpression) {
        CallExpression callExp = (CallExpression) expression;
        Object key = extractValueFromRowExpression(callExp.getArguments().get(1));
        BuiltInFunctionHandle builtInFunctionHandle;
        if (callExp.getFunctionHandle() instanceof BuiltInFunctionHandle) {
            builtInFunctionHandle = (BuiltInFunctionHandle) callExp.getFunctionHandle();
        } else {
            throw new UnsupportedOperationException("Unsupported function: " + callExp.getDisplayName());
        }
        Optional<OperatorType> operatorOptional = Signature.getOperatorType(builtInFunctionHandle.getSignature().getNameSuffix());
        if (operatorOptional.isPresent()) {
            OperatorType operator = operatorOptional.get();
            switch(operator) {
                case EQUAL:
                    if (dataMap.containsKey(key)) {
                        lookUpResults = Collections.singleton(dataMap.get(key));
                    }
                    break;
                case LESS_THAN:
                    lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, false);
                    break;
                case LESS_THAN_OR_EQUAL:
                    lookUpResults = rangeLookUp(dataMap.firstKey(), true, key, true);
                    break;
                case GREATER_THAN:
                    lookUpResults = rangeLookUp(key, false, dataMap.lastKey(), true);
                    break;
                case GREATER_THAN_OR_EQUAL:
                    lookUpResults = rangeLookUp(key, true, dataMap.lastKey(), true);
                    break;
                default:
                    throw new UnsupportedOperationException("Expression not supported");
            }
        }
    } else if (expression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) expression;
        switch(specialForm.getForm()) {
            case BETWEEN:
                Object left = extractValueFromRowExpression(specialForm.getArguments().get(1));
                Object right = extractValueFromRowExpression(specialForm.getArguments().get(2));
                lookUpResults = rangeLookUp(left, true, right, true);
                break;
            case IN:
                lookUpResults = new ArrayList<>();
                for (RowExpression exp : specialForm.getArguments().subList(1, specialForm.getArguments().size())) {
                    Object key = extractValueFromRowExpression(exp);
                    if (dataMap.containsKey(key)) {
                        lookUpResults.add(dataMap.get(key));
                    }
                }
                break;
            default:
                throw new UnsupportedOperationException("Expression not supported");
        }
    } else {
        throw new UnsupportedOperationException("Expression not supported");
    }
    Set<String> symbolSet = new HashSet<>();
    // break lookUp results to symbols and keep in a set. e.g. ["1,2,2,4","2,3"] -> set{"1", "2", "3", "4"}
    for (String res : lookUpResults) {
        Collections.addAll(symbolSet, res.split(","));
    }
    // translate the symbols to actual data values
    List<String> translated = new ArrayList<>(symbolSet.size());
    for (String sym : symbolSet) {
        translated.add(symbolTable != null ? symbolTable.get(sym) : sym);
    }
    translated.sort(String::compareTo);
    return translated.iterator();
}
Also used : ArrayList(java.util.ArrayList) TypeUtils.extractValueFromRowExpression(io.prestosql.spi.heuristicindex.TypeUtils.extractValueFromRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) OperatorType(io.prestosql.spi.function.OperatorType) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) HashSet(java.util.HashSet)

Example 19 with SpecialForm

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

the class HeuristicIndexFilter method lookUp.

@Override
public <I extends Comparable<I>> Iterator<I> lookUp(Object expression) throws IndexLookUpException {
    if (expression instanceof CallExpression) {
        return lookUpAll((RowExpression) expression);
    }
    if (expression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) expression;
        switch(specialForm.getForm()) {
            case IN:
            case BETWEEN:
                return lookUpAll((RowExpression) expression);
            case AND:
                Iterator<I> iteratorAnd1 = lookUp(specialForm.getArguments().get(0));
                Iterator<I> iteratorAnd2 = lookUp(specialForm.getArguments().get(1));
                if (iteratorAnd1 == null && iteratorAnd2 == null) {
                    return null;
                } else if (iteratorAnd1 == null) {
                    return iteratorAnd2;
                } else if (iteratorAnd2 == null) {
                    return iteratorAnd1;
                } else {
                    return SequenceUtils.intersect(iteratorAnd1, iteratorAnd2);
                }
            case OR:
                Iterator<I> iteratorOr1 = lookUp(specialForm.getArguments().get(0));
                Iterator<I> iteratorOr2 = lookUp(specialForm.getArguments().get(1));
                if (iteratorOr1 == null || iteratorOr2 == null) {
                    throw new IndexLookUpException();
                }
                return SequenceUtils.union(iteratorOr1, iteratorOr2);
        }
    }
    throw new IndexLookUpException();
}
Also used : CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) IndexLookUpException(io.prestosql.spi.heuristicindex.IndexLookUpException)

Example 20 with SpecialForm

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

the class HeuristicIndexFilter method lookUpAll.

/**
 * Lookup all index available according to the expression and union the result.
 * <p>
 * It returns {@code null} as the special value for "universe" result U such that,
 * for any other results A: U \and A == A, U \or A == U.
 * <p>
 * If any of the index throws {@code IndexLookUpException} during lookup, it immediately break and return null.
 */
private <T extends Comparable<T>> Iterator<T> lookUpAll(RowExpression expression) {
    RowExpression varRef = null;
    if (expression instanceof CallExpression) {
        varRef = ((CallExpression) expression).getArguments().get(0);
    }
    if (expression instanceof SpecialForm && (((SpecialForm) expression).getForm() == SpecialForm.Form.BETWEEN || ((SpecialForm) expression).getForm() == SpecialForm.Form.IN)) {
        varRef = ((SpecialForm) expression).getArguments().get(0);
    }
    if (!(varRef instanceof VariableReferenceExpression)) {
        return null;
    }
    List<IndexMetadata> selectedIndex = HeuristicIndexSelector.select(expression, indices.get(((VariableReferenceExpression) varRef).getName()));
    if (selectedIndex.isEmpty()) {
        return null;
    }
    try {
        List<Iterator<T>> iterators = selectedIndex.parallelStream().map(indexMetadata -> {
            try {
                return (Iterator<T>) indexMetadata.getIndex().lookUp(expression);
            } catch (IndexLookUpException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());
        return SequenceUtils.union(iterators);
    } catch (RuntimeException re) {
        return null;
    }
}
Also used : Iterator(java.util.Iterator) IndexFilter(io.prestosql.spi.heuristicindex.IndexFilter) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Collectors(java.util.stream.Collectors) CallExpression(io.prestosql.spi.relation.CallExpression) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SequenceUtils(io.hetu.core.common.algorithm.SequenceUtils) OperatorType(io.prestosql.spi.function.OperatorType) Map(java.util.Map) RowExpression(io.prestosql.spi.relation.RowExpression) Optional(java.util.Optional) Signature(io.prestosql.spi.function.Signature) IndexLookUpException(io.prestosql.spi.heuristicindex.IndexLookUpException) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) SpecialForm(io.prestosql.spi.relation.SpecialForm) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) Iterator(java.util.Iterator) RowExpression(io.prestosql.spi.relation.RowExpression) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) IndexLookUpException(io.prestosql.spi.heuristicindex.IndexLookUpException)

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