use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class DruidAggregationProjectConverter method handleDateTruncationViaDateTruncation.
private DruidExpression handleDateTruncationViaDateTruncation(CallExpression function, Map<VariableReferenceExpression, DruidQueryGeneratorContext.Selection> context) {
RowExpression timeInputParameter = function.getArguments().get(1);
String inputColumn;
String inputTimeZone;
String inputFormat;
CallExpression timeConversion = getExpressionAsFunction(timeInputParameter, timeInputParameter);
if (!timeConversion.getDisplayName().toLowerCase(ENGLISH).equals(FROM_UNIXTIME)) {
throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Unsupported time function: " + timeConversion.getDisplayName() + " to pushdown for Druid connector.");
}
inputColumn = timeConversion.getArguments().get(0).accept(this, context).getDefinition();
inputTimeZone = timeConversion.getArguments().size() > 1 ? getStringFromConstant(timeConversion.getArguments().get(1)) : DateTimeZone.UTC.getID();
inputFormat = "seconds";
RowExpression intervalParameter = function.getArguments().get(0);
if (!(intervalParameter instanceof ConstantExpression)) {
throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Unsupported interval unit: " + intervalParameter + " to pushdown for Druid connector.");
}
return derived("dateTrunc(" + inputColumn + "," + inputFormat + ", " + inputTimeZone + ", " + getStringFromConstant(intervalParameter) + ")");
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class PageFunctionCompiler method compileProjections.
public List<Supplier<PageProjectionWithOutputs>> compileProjections(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, List<? extends RowExpression> projections, boolean isOptimizeCommonSubExpression, Optional<String> classNameSuffix) {
if (isOptimizeCommonSubExpression) {
ImmutableList.Builder<Supplier<PageProjectionWithOutputs>> pageProjections = ImmutableList.builder();
ImmutableMap.Builder<RowExpression, Integer> expressionsWithPositionBuilder = ImmutableMap.builder();
for (int i = 0; i < projections.size(); i++) {
RowExpression projection = projections.get(i);
if (projection instanceof ConstantExpression || projection instanceof InputReferenceExpression) {
pageProjections.add(toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projection, classNameSuffix), new int[] { i }));
} else {
expressionsWithPositionBuilder.put(projection, i);
}
}
Map<RowExpression, Integer> expressionsWithPosition = expressionsWithPositionBuilder.build();
Map<List<RowExpression>, Boolean> projectionsPartitionedByCSE = getExpressionsPartitionedByCSE(expressionsWithPosition.keySet(), MAX_PROJECTION_GROUP_SIZE);
for (Map.Entry<List<RowExpression>, Boolean> entry : projectionsPartitionedByCSE.entrySet()) {
if (entry.getValue()) {
pageProjections.add(toPageProjectionWithOutputs(compileProjectionCached(sqlFunctionProperties, sessionFunctions, entry.getKey(), true, classNameSuffix), toIntArray(entry.getKey().stream().map(expressionsWithPosition::get).collect(toImmutableList()))));
} else {
verify(entry.getKey().size() == 1, "Expect non-cse expression list to only have one element");
RowExpression projection = entry.getKey().get(0);
pageProjections.add(toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projection, classNameSuffix), new int[] { expressionsWithPosition.get(projection) }));
}
}
return pageProjections.build();
}
return IntStream.range(0, projections.size()).mapToObj(outputChannel -> toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projections.get(outputChannel), classNameSuffix), new int[] { outputChannel })).collect(toImmutableList());
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class LambdaBytecodeGenerator method variableReferenceCompiler.
private static RowExpressionVisitor<BytecodeNode, Scope> variableReferenceCompiler(Map<String, ParameterAndType> parameterMap) {
return new RowExpressionVisitor<BytecodeNode, Scope>() {
@Override
public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
throw new UnsupportedOperationException();
}
@Override
public BytecodeNode visitCall(CallExpression call, Scope scope) {
throw new UnsupportedOperationException();
}
@Override
public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
throw new UnsupportedOperationException();
}
@Override
public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
throw new UnsupportedOperationException();
}
@Override
public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
ParameterAndType parameterAndType = parameterMap.get(reference.getName());
Parameter parameter = parameterAndType.getParameter();
Class<?> type = parameterAndType.getType();
return new BytecodeBlock().append(parameter).append(unboxPrimitiveIfNecessary(context, type));
}
@Override
public BytecodeNode visitSpecialForm(SpecialFormExpression specialForm, Scope context) {
throw new UnsupportedOperationException();
}
};
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class SwitchCodeGenerator method generateExpression.
@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) {
Scope scope = generatorContext.getScope();
BytecodeNode elseValue;
List<RowExpression> whenClauses;
RowExpression last = arguments.get(arguments.size() - 1);
if (last instanceof SpecialFormExpression && ((SpecialFormExpression) last).getForm().equals(WHEN)) {
whenClauses = arguments.subList(1, arguments.size());
elseValue = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pushJavaDefault(returnType.getJavaType());
} else {
whenClauses = arguments.subList(1, arguments.size() - 1);
elseValue = generatorContext.generate(last, Optional.empty());
}
// determine the type of the value and result
RowExpression value = arguments.get(0);
Class<?> valueType = value.getType().getJavaType();
// We generate SearchedCase as CASE TRUE WHEN p1 THEN v1 WHEN p2 THEN p2...
boolean searchedCase = (value instanceof ConstantExpression && ((ConstantExpression) value).getType() == BOOLEAN && ((ConstantExpression) value).getValue() == Boolean.TRUE);
// evaluate the value and store it in a variable
LabelNode elseLabel = new LabelNode("else");
LabelNode endLabel = new LabelNode("end");
BytecodeBlock block = new BytecodeBlock();
Optional<BytecodeNode> getTempVariableNode;
if (!searchedCase) {
BytecodeNode valueBytecode = generatorContext.generate(value, Optional.empty());
Variable tempVariable = scope.createTempVariable(valueType);
block.append(valueBytecode).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, elseLabel, void.class, valueType)).putVariable(tempVariable);
getTempVariableNode = Optional.of(VariableInstruction.loadVariable(tempVariable));
} else {
getTempVariableNode = Optional.empty();
}
Variable wasNull = generatorContext.wasNull();
block.putVariable(wasNull, false);
Map<RowExpression, LabelNode> resultLabels = new HashMap<>();
// We already know the P1 .. Pn are all boolean just call them and search for true (false/null don't matter).
for (RowExpression clause : whenClauses) {
checkArgument(clause instanceof SpecialFormExpression && ((SpecialFormExpression) clause).getForm().equals(WHEN));
RowExpression operand = ((SpecialFormExpression) clause).getArguments().get(0);
BytecodeNode operandBytecode;
if (searchedCase) {
operandBytecode = generatorContext.generate(operand, Optional.empty());
} else {
// call equals(value, operandBytecode)
FunctionHandle equalsFunction = generatorContext.getFunctionManager().resolveOperator(EQUAL, fromTypes(value.getType(), operand.getType()));
operandBytecode = generatorContext.generateCall(EQUAL.name(), generatorContext.getFunctionManager().getJavaScalarFunctionImplementation(equalsFunction), ImmutableList.of(generatorContext.generate(operand, Optional.empty()), getTempVariableNode.get()));
}
block.append(operandBytecode);
IfStatement ifWasNull = new IfStatement().condition(wasNull);
ifWasNull.ifTrue().putVariable(wasNull, false).pop(// pop the result of the predicate eval
Boolean.class);
// Here the TOS is the result of the predicate.
RowExpression result = ((SpecialFormExpression) clause).getArguments().get(1);
LabelNode target = resultLabels.get(result);
if (target == null) {
target = new LabelNode(RESULT_LABEL_PREFIX + resultLabels.size());
resultLabels.put(result, target);
}
ifWasNull.ifFalse().ifTrueGoto(target);
block.append(ifWasNull);
}
// Here we evaluate the else result.
block.visitLabel(elseLabel).append(elseValue).gotoLabel(endLabel);
// Now generate the result expression code.
for (Map.Entry<RowExpression, LabelNode> resultLabel : resultLabels.entrySet()) {
block.visitLabel(resultLabel.getValue()).append(generatorContext.generate(resultLabel.getKey(), Optional.empty())).gotoLabel(endLabel);
}
block.visitLabel(endLabel);
outputBlockVariable.ifPresent(output -> block.append(generateWrite(generatorContext, returnType, output)));
return block;
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class TestJdbcComputePushdown method testJdbcComputePushdownWithConstants.
@Test
public void testJdbcComputePushdownWithConstants() {
String table = "test_table";
String schema = "test_schema";
String expression = "(c1 + c2) = 3";
TypeProvider typeProvider = TypeProvider.copyOf(ImmutableMap.of("c1", BIGINT, "c2", BIGINT));
RowExpression rowExpression = sqlToRowExpressionTranslator.translateAndOptimize(expression(expression), typeProvider);
Set<ColumnHandle> columns = Stream.of("c1", "c2").map(TestJdbcComputePushdown::integerJdbcColumnHandle).collect(Collectors.toSet());
PlanNode original = filter(jdbcTableScan(schema, table, BIGINT, "c1", "c2"), rowExpression);
JdbcTableHandle jdbcTableHandle = new JdbcTableHandle(CONNECTOR_ID, new SchemaTableName(schema, table), CATALOG_NAME, schema, table);
ConnectorSession session = new TestingConnectorSession(ImmutableList.of());
JdbcTableLayoutHandle jdbcTableLayoutHandle = new JdbcTableLayoutHandle(session.getSqlFunctionProperties(), jdbcTableHandle, TupleDomain.none(), Optional.of(new JdbcExpression("(('c1' + 'c2') = ?)", ImmutableList.of(new ConstantExpression(Long.valueOf(3), INTEGER)))));
PlanNode actual = this.jdbcComputePushdown.optimize(original, session, null, ID_ALLOCATOR);
assertPlanMatch(actual, PlanMatchPattern.filter(expression, JdbcTableScanMatcher.jdbcTableScanPattern(jdbcTableLayoutHandle, columns)));
}
Aggregations