use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class HashGenerationOptimizer method getHashExpression.
public static Optional<RowExpression> getHashExpression(FunctionAndTypeManager functionAndTypeManager, List<VariableReferenceExpression> variables) {
if (variables.isEmpty()) {
return Optional.empty();
}
RowExpression result = constant(INITIAL_HASH_VALUE, BIGINT);
for (VariableReferenceExpression variable : variables) {
RowExpression hashField = call(functionAndTypeManager, HASH_CODE, BIGINT, variable);
hashField = orNullHashCode(hashField);
result = call(functionAndTypeManager, "combine_hash", BIGINT, result, hashField);
}
return Optional.of(result);
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class PruneValuesColumns method pushDownProjectOff.
@Override
protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, PlanVariableAllocator variableAllocator, ValuesNode valuesNode, Set<VariableReferenceExpression> referencedOutputs) {
List<VariableReferenceExpression> newOutputs = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
List<VariableReferenceExpression> newOutputVariables = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
// for each output of project, the corresponding column in the values node
int[] mapping = new int[newOutputs.size()];
for (int i = 0; i < mapping.length; i++) {
mapping[i] = valuesNode.getOutputVariables().indexOf(newOutputs.get(i));
}
ImmutableList.Builder<List<RowExpression>> rowsBuilder = ImmutableList.builder();
for (List<RowExpression> row : valuesNode.getRows()) {
rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
}
return Optional.of(new ValuesNode(valuesNode.getSourceLocation(), valuesNode.getId(), newOutputVariables, rowsBuilder.build()));
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class RewriteAggregationIfToFilter method shouldRewriteAggregation.
private boolean shouldRewriteAggregation(Aggregation aggregation, ProjectNode sourceProject) {
if (functionAndTypeManager.getFunctionMetadata(aggregation.getFunctionHandle()).isCalledOnNullInput()) {
// This rewrite will filter out the null values. It could change the behavior if the aggregation is also applied on NULLs.
return false;
}
if (!(aggregation.getArguments().size() == 1 && aggregation.getArguments().get(0) instanceof VariableReferenceExpression)) {
// Currently we only handle aggregation with a single VariableReferenceExpression. The detailed expressions are in a project node below this aggregation.
return false;
}
if (aggregation.getFilter().isPresent() || aggregation.getMask().isPresent()) {
// Do not rewrite the aggregation if it already has a filter or mask.
return false;
}
RowExpression sourceExpression = sourceProject.getAssignments().get((VariableReferenceExpression) aggregation.getArguments().get(0));
if (sourceExpression instanceof CallExpression) {
CallExpression callExpression = (CallExpression) sourceExpression;
if (callExpression.getArguments().size() == 1 && standardFunctionResolution.isCastFunction(callExpression.getFunctionHandle())) {
// If the expression is CAST(), check the expression inside.
sourceExpression = callExpression.getArguments().get(0);
}
}
if (!(sourceExpression instanceof SpecialFormExpression) || !rowExpressionDeterminismEvaluator.isDeterministic(sourceExpression)) {
return false;
}
SpecialFormExpression expression = (SpecialFormExpression) sourceExpression;
// Only rewrite the aggregation if the else branch is not present or the else result is NULL.
return expression.getForm() == IF && Expressions.isNull(expression.getArguments().get(2));
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class TestRowExpressionFormatter method testConstants.
@Test
public void testConstants() {
// null
RowExpression constantExpression = constantNull(UNKNOWN);
assertEquals(format(constantExpression), "null");
// boolean
constantExpression = constant(true, BOOLEAN);
assertEquals(format(constantExpression), "BOOLEAN'true'");
// double
constantExpression = constant(1.1, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'1.1'");
constantExpression = constant(Double.NaN, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'NaN'");
constantExpression = constant(Double.POSITIVE_INFINITY, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'Infinity'");
// real
constantExpression = constant((long) floatToIntBits(1.1f), REAL);
assertEquals(format(constantExpression), "REAL'1.1'");
constantExpression = constant((long) floatToIntBits(Float.NaN), REAL);
assertEquals(format(constantExpression), "REAL'NaN'");
constantExpression = constant((long) floatToIntBits(Float.POSITIVE_INFINITY), REAL);
assertEquals(format(constantExpression), "REAL'Infinity'");
// string
constantExpression = constant(utf8Slice("abcde"), VARCHAR);
assertEquals(format(constantExpression), "VARCHAR'abcde'");
constantExpression = constant(utf8Slice("fgh"), createCharType(3));
assertEquals(format(constantExpression), "CHAR'fgh'");
// integer
constantExpression = constant(1L, TINYINT);
assertEquals(format(constantExpression), "TINYINT'1'");
constantExpression = constant(1L, SMALLINT);
assertEquals(format(constantExpression), "SMALLINT'1'");
constantExpression = constant(1L, INTEGER);
assertEquals(format(constantExpression), "INTEGER'1'");
constantExpression = constant(1L, BIGINT);
assertEquals(format(constantExpression), "BIGINT'1'");
// varbinary
Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456AB"));
constantExpression = constant(value, VARBINARY);
assertEquals(format(constantExpression), "X'12 34 56 ab'");
// color
constantExpression = constant(256L, COLOR);
assertEquals(format(constantExpression), "COLOR'256'");
// long and short decimals
constantExpression = constant(decimal("1.2345678910"), DecimalType.createDecimalType(11, 10));
assertEquals(format(constantExpression), "DECIMAL'1.2345678910'");
constantExpression = constant(decimal("1.281734081274028174012432412423134"), DecimalType.createDecimalType(34, 33));
assertEquals(format(constantExpression), "DECIMAL'1.281734081274028174012432412423134'");
// time
constantExpression = constant(662727600000L, TIMESTAMP);
assertEquals(format(constantExpression), "TIMESTAMP'1991-01-01 00:00:00.000'");
constantExpression = constant(7670L, DATE);
assertEquals(format(constantExpression), "DATE'1991-01-01'");
// interval
constantExpression = constant(24L, INTERVAL_DAY_TIME);
assertEquals(format(constantExpression), "INTERVAL DAY TO SECOND'0 00:00:00.024'");
constantExpression = constant(25L, INTERVAL_YEAR_MONTH);
assertEquals(format(constantExpression), "INTERVAL YEAR TO MONTH'2-1'");
// block
constantExpression = constant(new LongArrayBlockBuilder(null, 4).writeLong(1L).writeLong(2).build(), new ArrayType(BIGINT));
assertEquals(format(constantExpression), "[Block: position count: 2; size: 96 bytes]");
}
use of com.facebook.presto.spi.relation.RowExpression in project presto by prestodb.
the class TestRowExpressionFormatter method testComplex.
@Test
public void testComplex() {
RowExpression complexExpression;
RowExpression expression = createCallExpression(ADD);
complexExpression = call(SUBTRACT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBTRACT, fromTypes(BIGINT, BIGINT)), BIGINT, C_BIGINT, expression);
assertEquals(format(complexExpression), "(c_bigint) - ((c_bigint) + (BIGINT'5'))");
RowExpression expression1 = createCallExpression(ADD);
RowExpression expression2 = call(MULTIPLY.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(MULTIPLY, fromTypes(BIGINT, BIGINT)), BIGINT, expression1, C_BIGINT);
RowExpression expression3 = createCallExpression(GREATER_THAN);
complexExpression = new SpecialFormExpression(OR, BOOLEAN, expression2, expression3);
assertEquals(format(complexExpression), "(((c_bigint) + (BIGINT'5')) * (c_bigint)) OR ((c_bigint) > (BIGINT'5'))");
ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
Type elementType = arrayType.getElementType();
expression1 = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(5L, INTEGER)));
expression2 = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression1.getType())), expression1.getType(), expression1);
expression3 = call(ADD.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(ADD, fromTypes(expression2.getType(), BIGINT)), BIGINT, expression2, constant(5L, BIGINT));
assertEquals(format(expression3), "(-(c_bigint_array[INTEGER'5'])) + (BIGINT'5')");
}
Aggregations