use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.
the class StreamTable method items.
StreamSource<JetSqlRow> items(Expression<Boolean> predicate, List<Expression<?>> projections) {
List<Expression<?>> argumentExpressions = this.argumentExpressions;
return SourceBuilder.stream("stream", ctx -> {
ExpressionEvalContext evalContext = ExpressionEvalContext.from(ctx);
Integer rate = evaluate(argumentExpressions.get(0), evalContext);
if (rate == null) {
throw QueryException.error("Invalid argument of a call to function GENERATE_STREAM" + " - rate cannot be null");
}
if (rate < 0) {
throw QueryException.error("Invalid argument of a call to function GENERATE_STREAM" + " - rate cannot be less than zero");
}
return new DataGenerator(rate, predicate, projections, evalContext);
}).fillBufferFn(DataGenerator::fillBuffer).build();
}
use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.
the class SlidingWindow method windowPolicyProvider.
public final FunctionEx<ExpressionEvalContext, SlidingWindowPolicy> windowPolicyProvider() {
QueryParameterMetadata parameterMetadata = ((HazelcastRelOptCluster) getCluster()).getParameterMetadata();
RexToExpressionVisitor visitor = new RexToExpressionVisitor(FAILING_FIELD_TYPE_PROVIDER, parameterMetadata);
if (operator() == HazelcastSqlOperatorTable.TUMBLE) {
Expression<?> windowSizeExpression = operand(2).accept(visitor);
return context -> tumblingWinPolicy(WindowUtils.extractMillis(windowSizeExpression, context));
} else if (operator() == HazelcastSqlOperatorTable.HOP) {
Expression<?> windowSizeExpression = operand(2).accept(visitor);
Expression<?> slideSizeExpression = operand(3).accept(visitor);
return context -> slidingWinPolicy(WindowUtils.extractMillis(windowSizeExpression, context), WindowUtils.extractMillis(slideSizeExpression, context));
} else {
throw new IllegalArgumentException();
}
}
use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.
the class CreateDagVisitor method onSlidingWindowAggregate.
public Vertex onSlidingWindowAggregate(SlidingWindowAggregatePhysicalRel rel) {
FunctionEx<JetSqlRow, ?> groupKeyFn = rel.groupKeyFn();
AggregateOperation<?, JetSqlRow> aggregateOperation = rel.aggrOp();
Expression<?> timestampExpression = rel.timestampExpression();
ToLongFunctionEx<JetSqlRow> timestampFn = row -> WindowUtils.extractMillis(timestampExpression.eval(row.getRow(), MOCK_EEC));
SlidingWindowPolicy windowPolicy = rel.windowPolicyProvider().apply(MOCK_EEC);
KeyedWindowResultFunction<? super Object, ? super JetSqlRow, ?> resultMapping = rel.outputValueMapping();
if (rel.numStages() == 1) {
Vertex vertex = dag.newUniqueVertex("Sliding-Window-AggregateByKey", Processors.aggregateToSlidingWindowP(singletonList(groupKeyFn), singletonList(timestampFn), TimestampKind.EVENT, windowPolicy, 0, aggregateOperation, resultMapping));
connectInput(rel.getInput(), vertex, edge -> edge.distributeTo(localMemberAddress).allToOne(""));
return vertex;
} else {
assert rel.numStages() == 2;
Vertex vertex1 = dag.newUniqueVertex("Sliding-Window-AccumulateByKey", Processors.accumulateByFrameP(singletonList(groupKeyFn), singletonList(timestampFn), TimestampKind.EVENT, windowPolicy, aggregateOperation));
Vertex vertex2 = dag.newUniqueVertex("Sliding-Window-CombineByKey", Processors.combineToSlidingWindowP(windowPolicy, aggregateOperation, resultMapping));
connectInput(rel.getInput(), vertex1, edge -> edge.partitioned(groupKeyFn));
dag.edge(between(vertex1, vertex2).distributed().partitioned(entryKey()));
return vertex2;
}
}
use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.
the class SlidingWindowAggregatePhysicalRel method timestampExpression.
public Expression<?> timestampExpression() {
QueryParameterMetadata parameterMetadata = ((HazelcastRelOptCluster) getCluster()).getParameterMetadata();
RexVisitor<Expression<?>> visitor = OptUtils.createRexToExpressionVisitor(schema(parameterMetadata), parameterMetadata);
return timestampExpression.accept(visitor);
}
use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.
the class IndexResolver method composeEqualsFilter.
/**
* Composes an equality filter from multiple single-column components.
* <p>
* If the number of single-column filters is equal to the number of index components, the resulting filter is a composite
* equality filter.
* <p>
* If the number of single-column filters is less than the number of index components, the resulting filter is a range
* filter, with missing components filled with negative/positive infinities for the left and right bounds respectively.
* <p>
* If the range filter is required, and the target index type is not {@link IndexType#SORTED}, the result is {@code null}.
* <p>
* Examples:
* <ul>
* <li>SORTED(a, b), {a=1, b=2} => EQUALS(1), EQUALS(2) </li>
* <li>HASH(a, b), {a=1, b=2} => EQUALS(1), EQUALS(2) </li>
* <li>SORTED(a, b), {a=1} => EQUALS(1), RANGE(INF) </li>
* <li>HASH(a, b), {a=1} => null </li>
* </ul>
*
* @return composite filter or {@code null}
*/
private static IndexFilter composeEqualsFilter(List<IndexFilter> filters, IndexEqualsFilter lastFilter, IndexType indexType, int indexComponentsCount) {
// Flatten all known values.
List<Expression> components = new ArrayList<>(filters.size());
List<Boolean> allowNulls = new ArrayList<>(filters.size());
fillNonTerminalComponents(filters, components, allowNulls);
components.addAll(lastFilter.getValue().getComponents());
allowNulls.addAll(lastFilter.getValue().getAllowNulls());
if (indexComponentsCount == components.size()) {
// If there is a full match, then leave it as equals filter
return new IndexEqualsFilter(new IndexFilterValue(components, allowNulls));
} else {
// Otherwise convert it to a range request
if (indexType == HASH) {
return null;
}
List<Expression> fromComponents = components;
List<Expression> toComponents = new ArrayList<>(components);
List<Boolean> fromAllowNulls = allowNulls;
List<Boolean> toAllowNulls = new ArrayList<>(fromAllowNulls);
addInfiniteRanges(fromComponents, fromAllowNulls, true, toComponents, toAllowNulls, true, indexComponentsCount);
return new IndexRangeFilter(new IndexFilterValue(fromComponents, fromAllowNulls), true, new IndexFilterValue(toComponents, toAllowNulls), true);
}
}
Aggregations