use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class TestLogicalRowExpressions method testEliminateDuplicate.
@Test
public void testEliminateDuplicate() {
RowExpression nd = call("random", functionAndTypeManager.lookupFunction("random", fromTypes()), DOUBLE);
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(and(TRUE_CONSTANT, a), and(b, b))), or(a, b));
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(and(a, b), and(a, b))), and(a, b));
// we will prefer most simplified expression than correct conjunctive/disjunctive form
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(or(and(a, b), and(a, b))), and(a, b));
// eliminate duplicated items with different order, prefers the ones appears first.
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(and(b, a), and(a, b))), and(b, a));
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(or(and(b, a), and(a, b))), and(b, a));
// (b && a) || a
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(and(b, a), a)), a);
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(or(and(b, a), a)), a);
// (b || a) && a
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(a, or(b, a))), a);
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(and(a, or(b, a))), a);
// (b && a) || (a && b && c) -> b && a (should keep b && a instead of a && b as it appears first)
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(or(and(b, a), and(and(a, b), c))), and(b, a));
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(or(and(b, a), and(and(a, b), c))), and(b, a));
// (b || a) && (a || b) && (a || b || c || d) || (a || b || c) -> b || a (should keep b || a instead of a || b as it appears first)
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(or(b, a), and(or(a, b), and(or(or(c, d), or(a, b)), or(a, or(b, c)))))), or(b, a));
// (b || a) && (a || b || c) && (a || b) && (a || b || nd) && e
// we cannot eliminate nd because it is non-deterministic
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(and(or(b, a), and(or(a, or(b, c)), and(or(a, b), or(or(a, b), nd)))), e)), and(and(or(b, a), or(or(a, b), nd)), e));
// we cannot convert to disjunctive form because nd is non-deterministic
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(and(and(or(b, a), and(or(a, or(b, c)), and(or(a, b), or(or(a, b), nd)))), e)), and(and(or(b, a), or(or(a, b), nd)), e));
// (b || a) && (a || b || c) && (a || b) && (a || b || d) && e
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(and(or(b, a), and(or(a, or(b, c)), and(or(a, b), or(or(a, b), d)))), e)), and(or(b, a), e));
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(and(and(or(b, a), and(or(a, or(b, c)), and(or(a, b), or(or(a, b), d)))), e)), or(and(b, e), and(a, e)));
// (b || a || c) && (a || b || d) && (a || b || e) && (a || b || f)
// already conjunctive form
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(or(or(b, a), c), and(or(d, or(a, b)), and(or(or(a, b), e), or(or(a, b), f))))), and(and(or(or(b, a), c), or(or(b, a), d)), and(or(or(b, a), e), or(or(b, a), f))));
// (b || a || c) && (a || b || d) && (a || b || f) -> b || a || (c && d && e && f)
// can be simplified by extract common predicates
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(and(or(or(b, a), c), and(or(d, or(a, b)), and(or(or(a, b), e), or(or(a, b), f))))), or(or(b, a), and(and(c, d), and(e, f))));
// de-duplicate nested expression
// ((a && b) || (a && c) || (a && d)) && ((b && c) || (c && a) || (c && d)) -> a && c
assertEquals(logicalRowExpressions.convertToConjunctiveNormalForm(and(or(and(a, b), and(a, c), and(a, d)), or(and(c, b), and(c, a), and(c, d)))), and(a, c));
assertEquals(logicalRowExpressions.convertToDisjunctiveNormalForm(and(or(and(a, b), and(a, c), and(a, d)), or(and(c, b), and(c, a), and(c, d)))), and(a, c));
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class TestRowExpressionTranslator method testMissingFunctionTranslator.
@Test
public void testMissingFunctionTranslator() {
String untranslated = "ABS(col1)";
TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", DOUBLE));
RowExpression specialForm = sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
TranslatedExpression translatedExpression = translateWith(specialForm, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
assertFalse(translatedExpression.getTranslated().isPresent());
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class PinotFilterExpressionConverter method handleDateAndTimestampMagicLiteralFunction.
private PinotExpression handleDateAndTimestampMagicLiteralFunction(CallExpression timestamp, Function<VariableReferenceExpression, Selection> context) {
if (timestamp.getArguments().size() == 1) {
RowExpression input = timestamp.getArguments().get(0);
Type expectedType = timestamp.getType();
if (typeManager.canCoerce(input.getType(), expectedType) || input.getType() == BigintType.BIGINT || input.getType() == IntegerType.INTEGER) {
return input.accept(this, context);
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Non implicit Date/Timestamp Literal is not supported: " + timestamp);
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), format("The Date/Timestamp Literal is not supported. Received: %s", timestamp));
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class PinotFilterExpressionConverter method handleCast.
private PinotExpression handleCast(CallExpression cast, Function<VariableReferenceExpression, Selection> context) {
if (cast.getArguments().size() == 1) {
RowExpression input = cast.getArguments().get(0);
Type expectedType = cast.getType();
if (typeManager.canCoerce(input.getType(), expectedType)) {
return input.accept(this, context);
}
// `TIMESTAMP` type is cast correctly to milliseconds value.
if (expectedType == DateType.DATE) {
try {
PinotExpression expression = input.accept(this, context);
if (input.getType() == TimestampType.TIMESTAMP || input.getType() == TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) {
return expression;
}
// It converts ISO DateTime to daysSinceEpoch value so Pinot could understand this.
if (input.getType() == VarcharType.VARCHAR) {
// Remove the leading & trailing quote then parse
Integer daysSinceEpoch = (int) TimeUnit.MILLISECONDS.toDays(DATE_FORMATTER.parseMillis(expression.getDefinition().substring(1, expression.getDefinition().length() - 1)));
return new PinotExpression(daysSinceEpoch.toString(), expression.getOrigin());
}
} catch (Exception e) {
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Cast date value expression is not supported: " + cast);
}
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Non implicit casts not supported: " + cast);
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), format("This type of CAST operator not supported. Received: %s", cast));
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class PinotFilterExpressionConverter method handleBetween.
private PinotExpression handleBetween(CallExpression between, Function<VariableReferenceExpression, Selection> context) {
if (between.getArguments().size() == 3) {
RowExpression value = between.getArguments().get(0);
RowExpression min = between.getArguments().get(1);
RowExpression max = between.getArguments().get(2);
return derived(format("(%s BETWEEN %s AND %s)", value.accept(this, context).getDefinition(), min.accept(this, context).getDefinition(), max.accept(this, context).getDefinition()));
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), format("Between operator not supported: %s", between));
}
Aggregations