use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class ActualProperties method translateRowExpression.
public ActualProperties translateRowExpression(Map<VariableReferenceExpression, RowExpression> assignments, TypeProvider types) {
Map<VariableReferenceExpression, VariableReferenceExpression> inputToOutputVariables = new HashMap<>();
for (Map.Entry<VariableReferenceExpression, RowExpression> assignment : assignments.entrySet()) {
RowExpression expression = assignment.getValue();
if (isExpression(expression)) {
if (castToExpression(expression) instanceof SymbolReference) {
inputToOutputVariables.put(toVariableReference(castToExpression(expression), types), assignment.getKey());
}
} else {
if (expression instanceof VariableReferenceExpression) {
inputToOutputVariables.put((VariableReferenceExpression) expression, assignment.getKey());
}
}
}
Map<VariableReferenceExpression, ConstantExpression> translatedConstants = new HashMap<>();
for (Map.Entry<VariableReferenceExpression, ConstantExpression> entry : constants.entrySet()) {
if (inputToOutputVariables.containsKey(entry.getKey())) {
translatedConstants.put(inputToOutputVariables.get(entry.getKey()), entry.getValue());
}
}
ImmutableMap.Builder<VariableReferenceExpression, RowExpression> inputToOutputMappings = ImmutableMap.builder();
inputToOutputMappings.putAll(inputToOutputVariables);
constants.entrySet().stream().filter(entry -> !inputToOutputVariables.containsKey(entry.getKey())).forEach(inputToOutputMappings::put);
return builder().global(global.translateRowExpression(inputToOutputMappings.build(), assignments, types)).local(LocalProperties.translate(localProperties, variable -> Optional.ofNullable(inputToOutputVariables.get(variable)))).constants(translatedConstants).build();
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class Partitioning method translateToCoalesce.
// Maps VariableReferenceExpression in both partitions to an COALESCE expression, keeps constant arguments unchanged.
public Optional<Partitioning> translateToCoalesce(Partitioning other, Metadata metadata, Session session) {
checkArgument(arePartitionHandlesCompatibleForCoalesce(this.handle, other.handle, metadata, session), "incompatible partitioning handles: cannot coalesce %s and %s", this.handle, other.handle);
checkArgument(this.arguments.size() == other.arguments.size(), "incompatible number of partitioning arguments: %s != %s", this.arguments.size(), other.arguments.size());
ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder();
for (int i = 0; i < this.arguments.size(); i++) {
RowExpression leftArgument = this.arguments.get(i);
RowExpression rightArgument = other.arguments.get(i);
if (leftArgument instanceof ConstantExpression) {
arguments.add(leftArgument);
} else if (rightArgument instanceof ConstantExpression) {
arguments.add(rightArgument);
} else if (leftArgument instanceof VariableReferenceExpression && rightArgument instanceof VariableReferenceExpression) {
VariableReferenceExpression leftVariable = (VariableReferenceExpression) leftArgument;
VariableReferenceExpression rightVariable = (VariableReferenceExpression) rightArgument;
checkArgument(leftVariable.getType().equals(rightVariable.getType()), "incompatible types: %s != %s", leftVariable.getType(), rightVariable.getType());
arguments.add(new SpecialFormExpression(COALESCE, leftVariable.getType(), ImmutableList.of(leftVariable, rightVariable)));
} else {
return Optional.empty();
}
}
return Optional.of(new Partitioning(metadata.isRefinedPartitioningOver(session, other.handle, this.handle) ? this.handle : other.handle, arguments.build()));
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class TestRowExpressionSerde method assertStringLiteral.
private void assertStringLiteral(@Language("SQL") String sql, String expectedString, Type expectedType) {
RowExpression roundTrip = getRoundTrip(sql, true);
assertTrue(roundTrip instanceof ConstantExpression);
String roundTripValue = ((Slice) ((ConstantExpression) roundTrip).getValue()).toStringUtf8();
Type roundTripType = roundTrip.getType();
assertEquals(roundTripValue, expectedString);
assertEquals(roundTripType, expectedType);
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class TestRowExpressionSerde method testHllLiteral.
@Test
public void testHllLiteral() {
RowExpression rowExpression = getRoundTrip("empty_approx_set()", true);
assertTrue(rowExpression instanceof ConstantExpression);
Object value = ((ConstantExpression) rowExpression).getValue();
assertEquals(HyperLogLog.newInstance((Slice) value).cardinality(), 0);
}
use of com.facebook.presto.spi.relation.ConstantExpression in project presto by prestodb.
the class ValuesMatcher method detailMatches.
@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
ValuesNode valuesNode = (ValuesNode) node;
if (!expectedRows.map(rows -> rows.equals(valuesNode.getRows().stream().map(rowExpressions -> rowExpressions.stream().map(rowExpression -> {
if (isExpression(rowExpression)) {
return castToExpression(rowExpression);
}
ConstantExpression expression = (ConstantExpression) rowExpression;
if (expression.getType().getJavaType() == boolean.class) {
return new BooleanLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == long.class) {
return new LongLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == double.class) {
return new DoubleLiteral(String.valueOf(expression.getValue()));
}
if (expression.getType().getJavaType() == Slice.class) {
return new StringLiteral(((Slice) expression.getValue()).toStringUtf8());
}
return new GenericLiteral(expression.getType().toString(), String.valueOf(expression.getValue()));
}).collect(toImmutableList())).collect(toImmutableSet()))).orElse(true)) {
return NO_MATCH;
}
return match(SymbolAliases.builder().putAll(Maps.transformValues(outputSymbolAliases, index -> createSymbolReference(valuesNode.getOutputVariables().get(index)))).build());
}
Aggregations