Search in sources :

Example 1 with QueryParameterMetadata

use of com.hazelcast.sql.impl.QueryParameterMetadata 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();
    }
}
Also used : SlidingWindowPolicy.slidingWinPolicy(com.hazelcast.jet.core.SlidingWindowPolicy.slidingWinPolicy) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) FAILING_FIELD_TYPE_PROVIDER(com.hazelcast.sql.impl.plan.node.PlanNodeFieldTypeProvider.FAILING_FIELD_TYPE_PROVIDER) SlidingWindowPolicy(com.hazelcast.jet.core.SlidingWindowPolicy) HazelcastRelOptCluster(org.apache.calcite.plan.HazelcastRelOptCluster) RelColumnMapping(org.apache.calcite.rel.metadata.RelColumnMapping) RexNode(org.apache.calcite.rex.RexNode) SqlOperator(org.apache.calcite.sql.SqlOperator) RexToExpressionVisitor(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor) Expression(com.hazelcast.sql.impl.expression.Expression) Preconditions.checkTrue(com.hazelcast.internal.util.Preconditions.checkTrue) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptCluster(org.apache.calcite.plan.RelOptCluster) FunctionEx(com.hazelcast.function.FunctionEx) TableFunctionScan(org.apache.calcite.rel.core.TableFunctionScan) RelDataType(org.apache.calcite.rel.type.RelDataType) Set(java.util.Set) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) Type(java.lang.reflect.Type) HazelcastSqlOperatorTable(com.hazelcast.jet.sql.impl.validate.HazelcastSqlOperatorTable) SlidingWindowPolicy.tumblingWinPolicy(com.hazelcast.jet.core.SlidingWindowPolicy.tumblingWinPolicy) WindowUtils(com.hazelcast.jet.sql.impl.aggregate.WindowUtils) RexCall(org.apache.calcite.rex.RexCall) HazelcastRelOptCluster(org.apache.calcite.plan.HazelcastRelOptCluster) RexToExpressionVisitor(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor) Expression(com.hazelcast.sql.impl.expression.Expression) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata)

Example 2 with QueryParameterMetadata

use of com.hazelcast.sql.impl.QueryParameterMetadata in project hazelcast by hazelcast.

the class QueryParser method parse0.

private QueryParseResult parse0(String sql) throws SqlParseException {
    SqlParser parser = SqlParser.create(sql, CONFIG);
    SqlNodeList statements = parser.parseStmtList();
    if (statements.size() != 1) {
        throw QueryException.error(SqlErrorCode.PARSING, "The command must contain a single statement");
    }
    SqlNode topNode = statements.get(0);
    SqlNode node = validator.validate(topNode);
    SqlVisitor<Void> visitor = new UnsupportedOperationVisitor();
    node.accept(visitor);
    return new QueryParseResult(node, new QueryParameterMetadata(validator.getParameterConverters(node)), validator.isInfiniteRows());
}
Also used : UnsupportedOperationVisitor(com.hazelcast.jet.sql.impl.validate.UnsupportedOperationVisitor) HazelcastSqlParser(com.hazelcast.jet.sql.impl.calcite.parser.HazelcastSqlParser) SqlParser(org.apache.calcite.sql.parser.SqlParser) SqlNodeList(org.apache.calcite.sql.SqlNodeList) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with QueryParameterMetadata

use of com.hazelcast.sql.impl.QueryParameterMetadata 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);
}
Also used : HazelcastRelOptCluster(org.apache.calcite.plan.HazelcastRelOptCluster) Expression(com.hazelcast.sql.impl.expression.Expression) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata)

Example 4 with QueryParameterMetadata

use of com.hazelcast.sql.impl.QueryParameterMetadata in project hazelcast by hazelcast.

the class OptimizerTestSupport method context.

private static OptimizerContext context(HazelcastSchema schema, QueryDataType... parameterTypes) {
    OptimizerContext context = OptimizerContext.create(HazelcastSchemaUtils.createCatalog(schema), QueryUtils.prepareSearchPaths(null, null), emptyList(), 1, name -> null);
    ParameterConverter[] parameterConverters = IntStream.range(0, parameterTypes.length).mapToObj(i -> new StrictParameterConverter(i, SqlParserPos.ZERO, parameterTypes[i])).toArray(ParameterConverter[]::new);
    QueryParameterMetadata parameterMetadata = new QueryParameterMetadata(parameterConverters);
    context.setParameterMetadata(parameterMetadata);
    return context;
}
Also used : IntStream(java.util.stream.IntStream) OptimizerContext(com.hazelcast.jet.sql.impl.OptimizerContext) LogicalRules(com.hazelcast.jet.sql.impl.opt.logical.LogicalRules) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) PhysicalRules(com.hazelcast.jet.sql.impl.opt.physical.PhysicalRules) SCHEMA_NAME_PUBLIC(com.hazelcast.jet.sql.impl.schema.TableResolverImpl.SCHEMA_NAME_PUBLIC) RelOptUtil(org.apache.calcite.plan.RelOptUtil) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) ParameterConverter(com.hazelcast.sql.impl.ParameterConverter) Collectors.toMap(java.util.stream.Collectors.toMap) LogicalRel(com.hazelcast.jet.sql.impl.opt.logical.LogicalRel) Arrays.asList(java.util.Arrays.asList) ConstantTableStatistics(com.hazelcast.sql.impl.schema.ConstantTableStatistics) HazelcastSchemaUtils(com.hazelcast.jet.sql.impl.schema.HazelcastSchemaUtils) QueryUtils(com.hazelcast.sql.impl.QueryUtils) PartitionedMapTable(com.hazelcast.sql.impl.schema.map.PartitionedMapTable) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) StrictParameterConverter(com.hazelcast.jet.sql.impl.validate.param.StrictParameterConverter) HazelcastTableStatistic(com.hazelcast.jet.sql.impl.schema.HazelcastTableStatistic) Collections.emptyList(java.util.Collections.emptyList) SqlTestSupport(com.hazelcast.jet.sql.SqlTestSupport) SqlExplainLevel(org.apache.calcite.sql.SqlExplainLevel) RelNode(org.apache.calcite.rel.RelNode) HazelcastSchema(com.hazelcast.jet.sql.impl.schema.HazelcastSchema) Objects(java.util.Objects) TableField(com.hazelcast.sql.impl.schema.TableField) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) StringReader(java.io.StringReader) QueryParseResult(com.hazelcast.jet.sql.impl.parse.QueryParseResult) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) Function.identity(java.util.function.Function.identity) BufferedReader(java.io.BufferedReader) Arrays.stream(java.util.Arrays.stream) PhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.PhysicalRel) StrictParameterConverter(com.hazelcast.jet.sql.impl.validate.param.StrictParameterConverter) ParameterConverter(com.hazelcast.sql.impl.ParameterConverter) StrictParameterConverter(com.hazelcast.jet.sql.impl.validate.param.StrictParameterConverter) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) OptimizerContext(com.hazelcast.jet.sql.impl.OptimizerContext)

Example 5 with QueryParameterMetadata

use of com.hazelcast.sql.impl.QueryParameterMetadata in project hazelcast by hazelcast.

the class IndexResolver method prepareSingleColumnCandidates.

/**
 * Creates a map from the scan column ordinal to expressions that could be potentially used by indexes created over
 * this column.
 *
 * @param expressions             CNF nodes
 * @param allIndexedFieldOrdinals ordinals of all columns that have some indexes. Helps to filter out candidates that
 *                                definitely cannot be used earlier.
 */
private static Map<Integer, List<IndexComponentCandidate>> prepareSingleColumnCandidates(List<RexNode> expressions, QueryParameterMetadata parameterMetadata, Set<Integer> allIndexedFieldOrdinals) {
    Map<Integer, List<IndexComponentCandidate>> res = new HashMap<>();
    // Iterate over each CNF component of the expression.
    for (RexNode expression : expressions) {
        // Try creating a candidate for the expression. The candidate is created iff the expression could be used
        // by some index implementation (SORTED, HASH)
        IndexComponentCandidate candidate = prepareSingleColumnCandidate(expression, parameterMetadata);
        if (candidate == null) {
            // Expression cannot be used by any index implementation, skip
            continue;
        }
        if (!allIndexedFieldOrdinals.contains(candidate.getColumnIndex())) {
            // Therefore, the expression could not be used, skip
            continue;
        }
        // Group candidates by column. E.g. {a>1 AND a<3} is grouped into a single map entry: a->{>1},{<3}
        res.computeIfAbsent(candidate.getColumnIndex(), (k) -> new ArrayList<>()).add(candidate);
    }
    return res;
}
Also used : RangeSet(com.google.common.collect.RangeSet) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) OptUtils.getCluster(com.hazelcast.jet.sql.impl.opt.OptUtils.getCluster) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) Collections.singletonList(java.util.Collections.singletonList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) TypeConverters(com.hazelcast.query.impl.TypeConverters) RexUtil(org.apache.calcite.rex.RexUtil) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) QueryDataTypeUtils(com.hazelcast.sql.impl.type.QueryDataTypeUtils) HASH(com.hazelcast.config.IndexType.HASH) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) RexToExpressionVisitor(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor) PlanNodeFieldTypeProvider(com.hazelcast.sql.impl.plan.node.PlanNodeFieldTypeProvider) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlKind(org.apache.calcite.sql.SqlKind) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) Range(com.google.common.collect.Range) Set(java.util.Set) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) FullScanLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel) BoundType(com.google.common.collect.BoundType) RelCollation(org.apache.calcite.rel.RelCollation) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) TRUE(java.lang.Boolean.TRUE) RexCall(org.apache.calcite.rex.RexCall) OptUtils.createRelTable(com.hazelcast.jet.sql.impl.opt.OptUtils.createRelTable) Iterables(com.google.common.collect.Iterables) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) HashMap(java.util.HashMap) POSITIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.POSITIVE_INFINITY) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexType(com.hazelcast.config.IndexType) BiTuple(com.hazelcast.internal.util.BiTuple) Expression(com.hazelcast.sql.impl.expression.Expression) Nonnull(javax.annotation.Nonnull) ComparableIdentifiedDataSerializable(com.hazelcast.query.impl.ComparableIdentifiedDataSerializable) RelCollations(org.apache.calcite.rel.RelCollations) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FALSE(java.lang.Boolean.FALSE) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SORTED(com.hazelcast.config.IndexType.SORTED) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexBuilder(org.apache.calcite.rex.RexBuilder) OptUtils(com.hazelcast.jet.sql.impl.opt.OptUtils) IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) RelNode(org.apache.calcite.rel.RelNode) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) ASCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.ASCENDING) TreeMap(java.util.TreeMap) NEGATIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.NEGATIVE_INFINITY) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) DESCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.DESCENDING) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

QueryParameterMetadata (com.hazelcast.sql.impl.QueryParameterMetadata)5 List (java.util.List)3 RelNode (org.apache.calcite.rel.RelNode)3 RexToExpressionVisitor (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor)2 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)2 Expression (com.hazelcast.sql.impl.expression.Expression)2 MapTableIndex (com.hazelcast.sql.impl.schema.map.MapTableIndex)2 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)2 HazelcastRelOptCluster (org.apache.calcite.plan.HazelcastRelOptCluster)2 RelOptUtil (org.apache.calcite.plan.RelOptUtil)2 BoundType (com.google.common.collect.BoundType)1 Iterables (com.google.common.collect.Iterables)1 Range (com.google.common.collect.Range)1 RangeSet (com.google.common.collect.RangeSet)1 IndexType (com.hazelcast.config.IndexType)1 HASH (com.hazelcast.config.IndexType.HASH)1 SORTED (com.hazelcast.config.IndexType.SORTED)1 FunctionEx (com.hazelcast.function.FunctionEx)1 BiTuple (com.hazelcast.internal.util.BiTuple)1 Preconditions.checkTrue (com.hazelcast.internal.util.Preconditions.checkTrue)1