Search in sources :

Example 21 with RowExpression

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));
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 22 with RowExpression

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());
}
Also used : TypeProvider(com.facebook.presto.sql.planner.TypeProvider) RowExpression(com.facebook.presto.spi.relation.RowExpression) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) Test(org.testng.annotations.Test)

Example 23 with RowExpression

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));
}
Also used : PinotException(com.facebook.presto.pinot.PinotException) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) OperatorType(com.facebook.presto.common.function.OperatorType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) DateType(com.facebook.presto.common.type.DateType) TimestampType(com.facebook.presto.common.type.TimestampType) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 24 with RowExpression

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));
}
Also used : PinotException(com.facebook.presto.pinot.PinotException) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) OperatorType(com.facebook.presto.common.function.OperatorType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) DateType(com.facebook.presto.common.type.DateType) TimestampType(com.facebook.presto.common.type.TimestampType) RowExpression(com.facebook.presto.spi.relation.RowExpression) PinotException(com.facebook.presto.pinot.PinotException)

Example 25 with RowExpression

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));
}
Also used : PinotException(com.facebook.presto.pinot.PinotException) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Aggregations

RowExpression (com.facebook.presto.spi.relation.RowExpression)237 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)97 Test (org.testng.annotations.Test)87 ImmutableList (com.google.common.collect.ImmutableList)58 CallExpression (com.facebook.presto.spi.relation.CallExpression)52 Map (java.util.Map)49 List (java.util.List)42 Type (com.facebook.presto.common.type.Type)41 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)40 ImmutableMap (com.google.common.collect.ImmutableMap)38 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)37 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)35 Optional (java.util.Optional)35 Expression (com.facebook.presto.sql.tree.Expression)31 ColumnHandle (com.facebook.presto.spi.ColumnHandle)27 Objects.requireNonNull (java.util.Objects.requireNonNull)27 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)24 Set (java.util.Set)24 ArrayList (java.util.ArrayList)23