use of io.trino.metadata.ResolvedFunction in project trino by trinodb.
the class TestPushdownLimitIntoWindow method assertWindowNotOrdered.
private void assertWindowNotOrdered(String rankingFunctionName) {
ResolvedFunction ranking = tester().getMetadata().resolveFunction(tester().getSession(), QualifiedName.of(rankingFunctionName), fromTypes());
tester().assertThat(new PushdownLimitIntoWindow()).on(p -> {
Symbol a = p.symbol("a");
Symbol rowNumberSymbol = p.symbol("row_number_1");
return p.limit(3, p.window(new WindowNode.Specification(ImmutableList.of(a), Optional.empty()), ImmutableMap.of(rowNumberSymbol, newWindowNodeFunction(ranking, a)), p.values(a)));
}).doesNotFire();
}
use of io.trino.metadata.ResolvedFunction in project trino by trinodb.
the class LogicalPlanner method noTruncationCast.
/*
According to the standard, for the purpose of store assignment (INSERT),
no non-space characters of a character string, and no non-zero octets
of a binary string must be lost when the inserted value is truncated to
fit in the target column type.
The following method returns a cast from source type to target type
with a guarantee of no illegal truncation.
TODO Once BINARY and parametric VARBINARY types are supported, they should be handled here.
TODO This workaround is insufficient to handle structural types
*/
private Expression noTruncationCast(Expression expression, Type fromType, Type toType) {
if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
return new Cast(expression, toSqlType(toType));
}
int targetLength;
if (toType instanceof VarcharType) {
if (((VarcharType) toType).isUnbounded()) {
return new Cast(expression, toSqlType(toType));
}
targetLength = ((VarcharType) toType).getBoundedLength();
} else {
targetLength = ((CharType) toType).getLength();
}
checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(session, QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
ResolvedFunction fail = metadata.resolveFunction(session, QualifiedName.of("fail"), fromTypes(VARCHAR));
return new IfExpression(// check if the trimmed value fits in the target type
new ComparisonExpression(GREATER_THAN_OR_EQUAL, new GenericLiteral("BIGINT", Integer.toString(targetLength)), new CoalesceExpression(new FunctionCall(spaceTrimmedLength.toQualifiedName(), ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))), new GenericLiteral("BIGINT", "0"))), new Cast(expression, toSqlType(toType)), new Cast(new FunctionCall(fail.toQualifiedName(), ImmutableList.of(new Cast(new StringLiteral(format("Cannot truncate non-space characters when casting from %s to %s on INSERT", fromType.getDisplayName(), toType.getDisplayName())), toSqlType(VARCHAR)))), toSqlType(toType)));
}
use of io.trino.metadata.ResolvedFunction in project trino by trinodb.
the class StatisticsAggregationPlanner method createAggregation.
private ColumnStatisticsAggregation createAggregation(QualifiedName functionName, SymbolReference input, Type inputType, Type outputType) {
ResolvedFunction resolvedFunction = metadata.resolveFunction(session, functionName, fromTypes(inputType));
Type resolvedType = getOnlyElement(resolvedFunction.getSignature().getArgumentTypes());
verify(resolvedType.equals(inputType), "resolved function input type does not match the input type: %s != %s", resolvedType, inputType);
return new ColumnStatisticsAggregation(new AggregationNode.Aggregation(resolvedFunction, ImmutableList.of(input), false, Optional.empty(), Optional.empty(), Optional.empty()), outputType);
}
use of io.trino.metadata.ResolvedFunction in project trino by trinodb.
the class TestDeterminismEvaluator method testDeterminismEvaluator.
@Test
public void testDeterminismEvaluator() {
TestingFunctionResolution functionResolution = new TestingFunctionResolution();
CallExpression random = new CallExpression(functionResolution.resolveFunction(QualifiedName.of("random"), fromTypes(BIGINT)), singletonList(constant(10L, BIGINT)));
assertFalse(isDeterministic(random));
InputReferenceExpression col0 = field(0, BIGINT);
ResolvedFunction lessThan = functionResolution.resolveOperator(LESS_THAN, ImmutableList.of(BIGINT, BIGINT));
CallExpression lessThanExpression = new CallExpression(lessThan, ImmutableList.of(col0, constant(10L, BIGINT)));
assertTrue(isDeterministic(lessThanExpression));
CallExpression lessThanRandomExpression = new CallExpression(lessThan, ImmutableList.of(col0, random));
assertFalse(isDeterministic(lessThanRandomExpression));
}
use of io.trino.metadata.ResolvedFunction in project trino by trinodb.
the class StatisticAggregations method createPartialAggregations.
public Parts createPartialAggregations(SymbolAllocator symbolAllocator, PlannerContext plannerContext) {
ImmutableMap.Builder<Symbol, Aggregation> partialAggregation = ImmutableMap.builder();
ImmutableMap.Builder<Symbol, Aggregation> finalAggregation = ImmutableMap.builder();
ImmutableMap.Builder<Symbol, Symbol> mappings = ImmutableMap.builder();
for (Map.Entry<Symbol, Aggregation> entry : aggregations.entrySet()) {
Aggregation originalAggregation = entry.getValue();
ResolvedFunction resolvedFunction = originalAggregation.getResolvedFunction();
AggregationFunctionMetadata functionMetadata = plannerContext.getMetadata().getAggregationFunctionMetadata(resolvedFunction);
List<Type> intermediateTypes = functionMetadata.getIntermediateTypes().stream().map(plannerContext.getTypeManager()::getType).collect(toImmutableList());
Type intermediateType = intermediateTypes.size() == 1 ? intermediateTypes.get(0) : RowType.anonymous(intermediateTypes);
Symbol partialSymbol = symbolAllocator.newSymbol(resolvedFunction.getSignature().getName(), intermediateType);
mappings.put(entry.getKey(), partialSymbol);
partialAggregation.put(partialSymbol, new Aggregation(resolvedFunction, originalAggregation.getArguments(), originalAggregation.isDistinct(), originalAggregation.getFilter(), originalAggregation.getOrderingScheme(), originalAggregation.getMask()));
finalAggregation.put(entry.getKey(), new Aggregation(resolvedFunction, ImmutableList.of(partialSymbol.toSymbolReference()), false, Optional.empty(), Optional.empty(), Optional.empty()));
}
groupingSymbols.forEach(symbol -> mappings.put(symbol, symbol));
return new Parts(new StatisticAggregations(partialAggregation.buildOrThrow(), groupingSymbols), new StatisticAggregations(finalAggregation.buildOrThrow(), groupingSymbols), mappings.buildOrThrow());
}
Aggregations