use of io.trino.spi.expression.Variable in project trino by trinodb.
the class TestExpressionMatching method testMatchCall.
@Test
public void testMatchCall() {
ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
ExpressionPattern pattern = expressionPattern("add(foo: decimal(p, s), bar: bigint)");
Match match = pattern.getPattern().match(expression).collect(onlyElement());
MatchContext matchContext = new MatchContext();
pattern.resolve(match.captures(), matchContext);
assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo", "bar");
assertThat(matchContext.get("p")).isEqualTo(10L);
assertThat(matchContext.get("s")).isEqualTo(2L);
assertThat(matchContext.get("foo")).isEqualTo(new Variable("first", createDecimalType(10, 2)));
assertThat(matchContext.get("bar")).isEqualTo(new Variable("second", BIGINT));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class TestExpressionMatching method testExpressionCapture.
@Test
public void testExpressionCapture() {
ConnectorExpression expression = new Call(createDecimalType(21, 2), new FunctionName("add"), List.of(new Variable("first", createDecimalType(10, 2)), new Variable("second", BIGINT)));
ExpressionPattern pattern = expressionPattern("foo: decimal(p, s)");
Match match = pattern.getPattern().match(expression).collect(onlyElement());
MatchContext matchContext = new MatchContext();
pattern.resolve(match.captures(), matchContext);
assertThat(matchContext.keys()).containsExactlyInAnyOrder("p", "s", "foo");
assertThat(matchContext.get("p")).isEqualTo(21L);
assertThat(matchContext.get("s")).isEqualTo(2L);
assertThat(matchContext.get("foo")).isSameAs(expression);
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class TestPruneTableScanColumns method mockApplyProjection.
private Optional<ProjectionApplicationResult<ConnectorTableHandle>> mockApplyProjection(ConnectorSession session, ConnectorTableHandle tableHandle, List<ConnectorExpression> projections, Map<String, ColumnHandle> assignments) {
MockConnectorTableHandle handle = (MockConnectorTableHandle) tableHandle;
List<Variable> variables = projections.stream().map(Variable.class::cast).collect(toImmutableList());
List<ColumnHandle> newColumns = variables.stream().map(variable -> assignments.get(variable.getName())).collect(toImmutableList());
if (handle.getColumns().isPresent() && newColumns.equals(handle.getColumns().get())) {
return Optional.empty();
}
return Optional.of(new ProjectionApplicationResult<>(new MockConnectorTableHandle(handle.getTableName(), handle.getConstraint(), Optional.of(newColumns)), projections, variables.stream().map(variable -> new Assignment(variable.getName(), assignments.get(variable.getName()), ((MockConnectorColumnHandle) assignments.get(variable.getName())).getType())).collect(toImmutableList()), false));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class BaseImplementAvgBigint method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
Variable argument = captures.get(this.argument);
verify(aggregateFunction.getOutputType() == DOUBLE);
return Optional.of(new JdbcExpression(format(getRewriteFormatExpression(), context.rewriteExpression(argument).orElseThrow()), new JdbcTypeHandle(Types.DOUBLE, Optional.of("double"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())));
}
use of io.trino.spi.expression.Variable in project trino by trinodb.
the class ImplementAvgDecimal method rewrite.
@Override
public Optional<JdbcExpression> rewrite(AggregateFunction aggregateFunction, Captures captures, RewriteContext<String> context) {
Variable argument = captures.get(ARGUMENT);
JdbcColumnHandle columnHandle = (JdbcColumnHandle) context.getAssignment(argument.getName());
DecimalType type = (DecimalType) columnHandle.getColumnType();
verify(aggregateFunction.getOutputType().equals(type));
return Optional.of(new JdbcExpression(format("CAST(avg(%s) AS decimal(%s, %s))", context.rewriteExpression(argument).orElseThrow(), type.getPrecision(), type.getScale()), columnHandle.getJdbcTypeHandle()));
}
Aggregations