Search in sources :

Example 6 with TranslatedExpression

use of com.facebook.presto.expressions.translator.TranslatedExpression in project presto by prestodb.

the class TestRowExpressionTranslator method testUntranslatableSpecialForm.

@Test
public void testUntranslatableSpecialForm() {
    String untranslated = "col1 OR col2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", BOOLEAN, "col2", BOOLEAN));
    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 7 with TranslatedExpression

use of com.facebook.presto.expressions.translator.TranslatedExpression in project presto by prestodb.

the class TestRowExpressionTranslator method testBasicOperator.

@Test
public void testBasicOperator() {
    String untranslated = "col1 + col2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", BIGINT, "col2", BIGINT));
    RowExpression specialForm = sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
    TranslatedExpression translatedExpression = translateWith(specialForm, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
    assertTrue(translatedExpression.getTranslated().isPresent());
    assertEquals(translatedExpression.getTranslated().get(), "col1 -|- col2");
}
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 8 with TranslatedExpression

use of com.facebook.presto.expressions.translator.TranslatedExpression in project presto by prestodb.

the class TestRowExpressionTranslator method testEndToEndSpecialFormTranslation.

@Test
public void testEndToEndSpecialFormTranslation() {
    String untranslated = "col1 AND col2";
    TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("col1", BOOLEAN, "col2", BOOLEAN));
    RowExpression specialForm = sqlToRowExpressionTranslator.translate(expression(untranslated), typeProvider);
    TranslatedExpression translatedExpression = translateWith(specialForm, new TestFunctionTranslator(functionAndTypeManager, buildFunctionTranslator(ImmutableSet.of(TestFunctions.class))), emptyMap());
    assertTrue(translatedExpression.getTranslated().isPresent());
    assertEquals(translatedExpression.getTranslated().get(), "col1 TEST_AND col2");
}
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 9 with TranslatedExpression

use of com.facebook.presto.expressions.translator.TranslatedExpression in project presto by prestodb.

the class JdbcFilterToSqlTranslator method translateVariable.

@Override
public TranslatedExpression<JdbcExpression> translateVariable(VariableReferenceExpression variable, Map<VariableReferenceExpression, ColumnHandle> context, RowExpressionTreeTranslator<JdbcExpression, Map<VariableReferenceExpression, ColumnHandle>> rowExpressionTreeTranslator) {
    JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.get(variable);
    requireNonNull(columnHandle, format("Unrecognized variable %s", variable));
    return new TranslatedExpression<>(Optional.of(new JdbcExpression(quote + columnHandle.getColumnName().replace(quote, quote + quote) + quote)), variable, ImmutableList.of());
}
Also used : JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression)

Example 10 with TranslatedExpression

use of com.facebook.presto.expressions.translator.TranslatedExpression in project presto by prestodb.

the class JdbcFilterToSqlTranslator method translateCall.

@Override
public TranslatedExpression<JdbcExpression> translateCall(CallExpression call, Map<VariableReferenceExpression, ColumnHandle> context, RowExpressionTreeTranslator<JdbcExpression, Map<VariableReferenceExpression, ColumnHandle>> rowExpressionTreeTranslator) {
    List<TranslatedExpression<JdbcExpression>> translatedExpressions = call.getArguments().stream().map(expression -> rowExpressionTreeTranslator.rewrite(expression, context)).collect(toImmutableList());
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
    try {
        return functionTranslator.translate(functionMetadata, call, translatedExpressions);
    } catch (Throwable t) {
    // no-op
    }
    return untranslated(call, translatedExpressions);
}
Also used : TranslatedExpression.untranslated(com.facebook.presto.expressions.translator.TranslatedExpression.untranslated) BooleanType(com.facebook.presto.common.type.BooleanType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) IntegerType(com.facebook.presto.common.type.IntegerType) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) TimeWithTimeZoneType(com.facebook.presto.common.type.TimeWithTimeZoneType) CharType(com.facebook.presto.common.type.CharType) CallExpression(com.facebook.presto.spi.relation.CallExpression) RowExpressionTreeTranslator(com.facebook.presto.expressions.translator.RowExpressionTreeTranslator) FunctionMetadataManager(com.facebook.presto.spi.function.FunctionMetadataManager) Type(com.facebook.presto.common.type.Type) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) RowExpressionTranslator(com.facebook.presto.expressions.translator.RowExpressionTranslator) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) String.format(java.lang.String.format) TimeType(com.facebook.presto.common.type.TimeType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) SmallintType(com.facebook.presto.common.type.SmallintType) List(java.util.List) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Optional(java.util.Optional) DateType(com.facebook.presto.common.type.DateType) FunctionTranslator(com.facebook.presto.expressions.translator.FunctionTranslator) DoubleType(com.facebook.presto.common.type.DoubleType) TimestampType(com.facebook.presto.common.type.TimestampType) JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) Joiner(com.google.common.base.Joiner) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression)

Aggregations

TranslatedExpression (com.facebook.presto.expressions.translator.TranslatedExpression)10 Test (org.testng.annotations.Test)8 RowExpression (com.facebook.presto.spi.relation.RowExpression)7 TypeProvider (com.facebook.presto.sql.planner.TypeProvider)7 JdbcColumnHandle (com.facebook.presto.plugin.jdbc.JdbcColumnHandle)2 CallExpression (com.facebook.presto.spi.relation.CallExpression)2 BigintType (com.facebook.presto.common.type.BigintType)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 CharType (com.facebook.presto.common.type.CharType)1 DateType (com.facebook.presto.common.type.DateType)1 DoubleType (com.facebook.presto.common.type.DoubleType)1 IntegerType (com.facebook.presto.common.type.IntegerType)1 RealType (com.facebook.presto.common.type.RealType)1 SmallintType (com.facebook.presto.common.type.SmallintType)1 TimeType (com.facebook.presto.common.type.TimeType)1 TimeWithTimeZoneType (com.facebook.presto.common.type.TimeWithTimeZoneType)1 TimestampType (com.facebook.presto.common.type.TimestampType)1 TimestampWithTimeZoneType (com.facebook.presto.common.type.TimestampWithTimeZoneType)1 TinyintType (com.facebook.presto.common.type.TinyintType)1 Type (com.facebook.presto.common.type.Type)1