use of com.facebook.presto.spi.relation.SpecialFormExpression in project urban-eureka by errir503.
the class RowExpressionVerifier method visitSimpleCaseExpression.
@Override
protected Boolean visitSimpleCaseExpression(SimpleCaseExpression expected, RowExpression actual) {
if (!(actual instanceof SpecialFormExpression && ((SpecialFormExpression) actual).getForm().equals(SWITCH))) {
return false;
}
SpecialFormExpression actualCase = (SpecialFormExpression) actual;
if (!process(expected.getOperand(), actualCase.getArguments().get(0))) {
return false;
}
List<RowExpression> whenClauses;
Optional<RowExpression> elseValue;
RowExpression last = actualCase.getArguments().get(actualCase.getArguments().size() - 1);
if (last instanceof SpecialFormExpression && ((SpecialFormExpression) last).getForm().equals(WHEN)) {
whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size());
elseValue = Optional.empty();
} else {
whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size() - 1);
elseValue = Optional.of(last);
}
if (!process(expected.getWhenClauses(), whenClauses)) {
return false;
}
return process(expected.getDefaultValue(), elseValue);
}
use of com.facebook.presto.spi.relation.SpecialFormExpression in project urban-eureka by errir503.
the class RowExpressionVerifier method visitDereferenceExpression.
@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
if (!(actual instanceof SpecialFormExpression) || !(((SpecialFormExpression) actual).getForm().equals(DEREFERENCE))) {
return false;
}
SpecialFormExpression actualDereference = (SpecialFormExpression) actual;
if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
Object value = LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actualDereference.getArguments().get(1));
checkState(value instanceof Long);
long index = (Long) value;
checkState(index >= 0 && index < rowType.getFields().size());
RowType.Field field = rowType.getFields().get(toIntExact(index));
checkState(field.getName().isPresent());
return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
}
return false;
}
use of com.facebook.presto.spi.relation.SpecialFormExpression in project urban-eureka by errir503.
the class TestLogicalRowExpressions method testDuplicateIsNullExpressions.
@Test
public void testDuplicateIsNullExpressions() {
SpecialFormExpression isNullExpression = new SpecialFormExpression(IS_NULL, BOOLEAN, a);
List<RowExpression> arguments = Arrays.asList(new SpecialFormExpression[] { isNullExpression, isNullExpression });
SpecialFormExpression duplicateIsNullExpression = new SpecialFormExpression(OR, BOOLEAN, arguments);
logicalRowExpressions.minimalNormalForm(duplicateIsNullExpression);
}
use of com.facebook.presto.spi.relation.SpecialFormExpression in project urban-eureka by errir503.
the class TestCursorProcessorCompiler method testRewriteRowExpressionWithCSE.
@Test
public void testRewriteRowExpressionWithCSE() {
CursorProcessorCompiler cseCursorCompiler = new CursorProcessorCompiler(METADATA, true, emptyMap());
ClassDefinition cursorProcessorClassDefinition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(CursorProcessor.class.getSimpleName()), type(Object.class), type(CursorProcessor.class));
RowExpression filter = new SpecialFormExpression(AND, BIGINT, ADD_X_Y_GREATER_THAN_2);
List<RowExpression> projections = ImmutableList.of(ADD_X_Y_Z);
List<RowExpression> rowExpressions = ImmutableList.<RowExpression>builder().addAll(projections).add(filter).build();
Map<Integer, Map<RowExpression, VariableReferenceExpression>> commonSubExpressionsByLevel = collectCSEByLevel(rowExpressions);
Map<VariableReferenceExpression, CommonSubExpressionRewriter.CommonSubExpressionFields> cseFields = declareCommonSubExpressionFields(cursorProcessorClassDefinition, commonSubExpressionsByLevel);
Map<RowExpression, VariableReferenceExpression> commonSubExpressions = commonSubExpressionsByLevel.values().stream().flatMap(m -> m.entrySet().stream()).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
// X+Y as CSE
assertEquals(1, cseFields.size());
VariableReferenceExpression cseVariable = cseFields.keySet().iterator().next();
RowExpression rewrittenFilter = cseCursorCompiler.rewriteRowExpressionsWithCSE(ImmutableList.of(filter), commonSubExpressions).get(0);
List<RowExpression> rewrittenProjections = cseCursorCompiler.rewriteRowExpressionsWithCSE(projections, commonSubExpressions);
// X+Y+Z contains CSE X+Y
assertTrue(((CallExpression) rewrittenProjections.get(0)).getArguments().contains(cseVariable));
// X+Y > 2 consists CSE X+Y
assertTrue(((CallExpression) ((SpecialFormExpression) rewrittenFilter).getArguments().get(0)).getArguments().contains(cseVariable));
}
use of com.facebook.presto.spi.relation.SpecialFormExpression in project urban-eureka by errir503.
the class TestCursorProcessorCompiler method testCompilerWithCSE.
@Test
public void testCompilerWithCSE() {
PageFunctionCompiler functionCompiler = new PageFunctionCompiler(METADATA, 0);
ExpressionCompiler expressionCompiler = new ExpressionCompiler(METADATA, functionCompiler);
RowExpression filter = new SpecialFormExpression(AND, BIGINT, ADD_X_Y_GREATER_THAN_2, ADD_X_Y_LESS_THAN_10);
List<? extends RowExpression> projections = createIfProjectionList(5);
Supplier<CursorProcessor> cseCursorProcessorSupplier = expressionCompiler.compileCursorProcessor(SESSION.getSqlFunctionProperties(), Optional.of(filter), projections, "key", true);
Supplier<CursorProcessor> noCseSECursorProcessorSupplier = expressionCompiler.compileCursorProcessor(SESSION.getSqlFunctionProperties(), Optional.of(filter), projections, "key", false);
Page input = createLongBlockPage(2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Type> types = ImmutableList.of(BIGINT, BIGINT);
PageBuilder pageBuilder = new PageBuilder(projections.stream().map(RowExpression::getType).collect(toList()));
RecordSet recordSet = new PageRecordSet(types, input);
cseCursorProcessorSupplier.get().process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), recordSet.cursor(), pageBuilder);
Page pageFromCSE = pageBuilder.build();
pageBuilder.reset();
noCseSECursorProcessorSupplier.get().process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), recordSet.cursor(), pageBuilder);
Page pageFromNoCSE = pageBuilder.build();
checkPageEqual(pageFromCSE, pageFromNoCSE);
}
Aggregations