use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.
the class JoinFilterFunctionCompiler method generateMethodsForLambdaAndTry.
private PreGeneratedExpressions generateMethodsForLambdaAndTry(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, int leftBlocksSize, RowExpression filter) {
Set<RowExpression> lambdaAndTryExpressions = ImmutableSet.copyOf(extractLambdaAndTryExpressions(filter));
ImmutableMap.Builder<CallExpression, MethodDefinition> tryMethodMap = ImmutableMap.builder();
ImmutableMap.Builder<LambdaDefinitionExpression, FieldDefinition> lambdaFieldMap = ImmutableMap.builder();
int counter = 0;
for (RowExpression expression : lambdaAndTryExpressions) {
if (expression instanceof CallExpression) {
CallExpression tryExpression = (CallExpression) expression;
verify(!Signatures.TRY.equals(tryExpression.getSignature().getName()));
Parameter session = arg("session", ConnectorSession.class);
Parameter leftPosition = arg("leftPosition", int.class);
Parameter leftBlocks = arg("leftBlocks", Block[].class);
Parameter rightPosition = arg("rightPosition", int.class);
Parameter rightBlocks = arg("rightBlocks", Block[].class);
BytecodeExpressionVisitor innerExpressionVisitor = new BytecodeExpressionVisitor(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder, leftPosition, leftBlocks, rightPosition, rightBlocks, leftBlocksSize), metadata.getFunctionRegistry(), new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build()));
List<Parameter> inputParameters = ImmutableList.<Parameter>builder().add(session).add(leftPosition).add(leftBlocks).add(rightPosition).add(rightBlocks).build();
MethodDefinition tryMethod = defineTryMethod(innerExpressionVisitor, containerClassDefinition, "try_" + counter, inputParameters, Primitives.wrap(tryExpression.getType().getJavaType()), tryExpression, callSiteBinder);
tryMethodMap.put(tryExpression, tryMethod);
} else if (expression instanceof LambdaDefinitionExpression) {
LambdaDefinitionExpression lambdaExpression = (LambdaDefinitionExpression) expression;
PreGeneratedExpressions preGeneratedExpressions = new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
FieldDefinition methodHandleField = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, "lambda_" + counter, containerClassDefinition, preGeneratedExpressions, callSiteBinder, cachedInstanceBinder, metadata.getFunctionRegistry());
lambdaFieldMap.put(lambdaExpression, methodHandleField);
} else {
throw new VerifyException(format("unexpected expression: %s", expression.toString()));
}
counter++;
}
return new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
}
use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.
the class CastCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
RowExpression argument = arguments.get(0);
Signature function = generatorContext.getRegistry().getCoercion(argument.getType(), returnType);
return generatorContext.generateCall(function.getName(), generatorContext.getRegistry().getScalarFunctionImplementation(function), ImmutableList.of(generatorContext.generate(argument)));
}
use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.
the class CoalesceCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
List<BytecodeNode> operands = new ArrayList<>();
for (RowExpression expression : arguments) {
operands.add(generatorContext.generate(expression));
}
Variable wasNull = generatorContext.wasNull();
BytecodeNode nullValue = new BytecodeBlock().append(wasNull.set(constantTrue())).pushJavaDefault(returnType.getJavaType());
// reverse list because current if statement builder doesn't support if/else so we need to build the if statements bottom up
for (BytecodeNode operand : Lists.reverse(operands)) {
IfStatement ifStatement = new IfStatement();
ifStatement.condition().append(operand).append(wasNull);
// if value was null, pop the null value, clear the null flag, and process the next operand
ifStatement.ifTrue().pop(returnType.getJavaType()).append(wasNull.set(constantFalse())).append(nullValue);
nullValue = ifStatement;
}
return nullValue;
}
use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.
the class ExpressionEquivalence method areExpressionsEquivalent.
public boolean areExpressionsEquivalent(Session session, Expression leftExpression, Expression rightExpression, Map<Symbol, Type> types) {
Map<Symbol, Integer> symbolInput = new HashMap<>();
Map<Integer, Type> inputTypes = new HashMap<>();
int inputId = 0;
for (Entry<Symbol, Type> entry : types.entrySet()) {
symbolInput.put(entry.getKey(), inputId);
inputTypes.put(inputId, entry.getValue());
inputId++;
}
RowExpression leftRowExpression = toRowExpression(session, leftExpression, symbolInput, inputTypes);
RowExpression rightRowExpression = toRowExpression(session, rightExpression, symbolInput, inputTypes);
RowExpression canonicalizedLeft = leftRowExpression.accept(CANONICALIZATION_VISITOR, null);
RowExpression canonicalizedRight = rightRowExpression.accept(CANONICALIZATION_VISITOR, null);
return canonicalizedLeft.equals(canonicalizedRight);
}
use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.
the class ExpressionEquivalence method toRowExpression.
private RowExpression toRowExpression(Session session, Expression expression, Map<Symbol, Integer> symbolInput, Map<Integer, Type> inputTypes) {
// replace qualified names with input references since row expressions do not support these
Expression expressionWithInputReferences = new SymbolToInputRewriter(symbolInput).rewrite(expression);
// determine the type of every expression
IdentityLinkedHashMap<Expression, Type> expressionTypes = getExpressionTypesFromInput(session, metadata, sqlParser, inputTypes, expressionWithInputReferences, emptyList());
// convert to row expression
return translate(expressionWithInputReferences, SCALAR, expressionTypes, metadata.getFunctionRegistry(), metadata.getTypeManager(), session, false);
}
Aggregations