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