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));
}
};
}
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);
}
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();
}
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();
}
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;
}
}
Aggregations