Search in sources :

Example 6 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class IndexResolver method buildCollationTrait.

/**
 * Builds a collation with collation fields re-mapped according to the table projections.
 *
 * @param scan  the logical map scan
 * @param index the index
 * @param ascs  the collation of index fields
 * @return the new collation trait
 */
private static RelCollation buildCollationTrait(FullScanLogicalRel scan, MapTableIndex index, List<Boolean> ascs) {
    if (index.getType() != SORTED) {
        return RelCollations.of(Collections.emptyList());
    }
    List<RelFieldCollation> fields = new ArrayList<>(index.getFieldOrdinals().size());
    HazelcastTable table = OptUtils.extractHazelcastTable(scan);
    // Extract those projections that are direct input field references. Only those can be used
    // for index access
    List<Integer> fieldProjects = table.getProjects().stream().filter(expr -> expr instanceof RexInputRef).map(inputRef -> ((RexInputRef) inputRef).getIndex()).collect(Collectors.toList());
    for (int i = 0; i < index.getFieldOrdinals().size(); ++i) {
        Integer indexFieldOrdinal = index.getFieldOrdinals().get(i);
        int remappedIndexFieldOrdinal = fieldProjects.indexOf(indexFieldOrdinal);
        if (remappedIndexFieldOrdinal == -1) {
            // The field is not used in the query
            break;
        }
        Direction direction = ascs.get(i) ? ASCENDING : DESCENDING;
        RelFieldCollation fieldCollation = new RelFieldCollation(remappedIndexFieldOrdinal, direction);
        fields.add(fieldCollation);
    }
    return RelCollations.of(fields);
}
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) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) Direction(org.apache.calcite.rel.RelFieldCollation.Direction)

Example 7 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class IndexResolver method createFullIndexScan.

/**
 * Creates an index scan without any filter.
 *
 * @param scan              the original scan operator
 * @param index             available indexes
 * @param ascs              the collation of index fields
 * @param nonEmptyCollation whether to filter out full index scan with no collation
 * @return index scan or {@code null}
 */
private static RelNode createFullIndexScan(FullScanLogicalRel scan, MapTableIndex index, List<Boolean> ascs, boolean nonEmptyCollation) {
    assert isIndexSupported(index);
    RexNode scanFilter = OptUtils.extractHazelcastTable(scan).getFilter();
    RelTraitSet traitSet = OptUtils.toPhysicalConvention(scan.getTraitSet());
    RelCollation relCollation = buildCollationTrait(scan, index, ascs);
    if (nonEmptyCollation && relCollation.getFieldCollations().size() == 0) {
        // Don't make a full scan with empty collation
        return null;
    }
    traitSet = OptUtils.traitPlus(traitSet, relCollation);
    HazelcastRelOptTable originalRelTable = (HazelcastRelOptTable) scan.getTable();
    HazelcastTable originalHazelcastTable = OptUtils.extractHazelcastTable(scan);
    RelOptTable newRelTable = createRelTable(originalRelTable.getDelegate().getQualifiedName(), originalHazelcastTable.withFilter(null), scan.getCluster().getTypeFactory());
    return new IndexScanMapPhysicalRel(scan.getCluster(), traitSet, newRelTable, index, null, null, scanFilter);
}
Also used : IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) RelCollation(org.apache.calcite.rel.RelCollation) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptTable(org.apache.calcite.plan.RelOptTable) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) RexNode(org.apache.calcite.rex.RexNode)

Example 8 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class SqlCreateIndexTest method checkPlan.

private void checkPlan(boolean withIndex, boolean sorted, String sql, String mapName) {
    List<QueryDataType> parameterTypes = asList(QueryDataType.INT, QueryDataType.INT);
    List<TableField> mapTableFields = asList(new MapTableField("__key", QueryDataType.INT, false, QueryPath.KEY_PATH), new MapTableField("this", QueryDataType.INT, false, QueryPath.VALUE_PATH));
    HazelcastTable table = partitionedTable(mapName, mapTableFields, getPartitionedMapIndexes(mapContainer(map), mapTableFields), map.size());
    assertThat(table.getProjects().size()).isEqualTo(2);
    OptimizerTestSupport.Result optimizationResult = optimizePhysical(sql, parameterTypes, table);
    if (sorted) {
        if (withIndex) {
            assertPlan(optimizationResult.getPhysical(), plan(planRow(0, IndexScanMapPhysicalRel.class)));
        } else {
            assertPlan(optimizationResult.getPhysical(), plan(planRow(0, SortPhysicalRel.class), planRow(1, FullScanPhysicalRel.class)));
        }
    } else {
        assertPlan(optimizationResult.getLogical(), plan(planRow(0, FullScanLogicalRel.class)));
        assertPlan(optimizationResult.getPhysical(), plan(planRow(0, withIndex ? IndexScanMapPhysicalRel.class : FullScanPhysicalRel.class)));
    }
}
Also used : IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) FullScanPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel) OptimizerTestSupport(com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField)

Example 9 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class LogicalUpdateTest method test_updateByKeyOrKey.

@Test
public void test_updateByKeyOrKey() {
    HazelcastTable table = partitionedTable("m", asList(field(KEY, INT), field(VALUE, VARCHAR)), 10);
    assertPlan(optimizeLogical("UPDATE m SET this = '2' WHERE __key = 1 OR __key = 2", table), plan(planRow(0, UpdateLogicalRel.class), planRow(1, FullScanLogicalRel.class)));
}
Also used : HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) Test(org.junit.Test)

Example 10 with HazelcastTable

use of com.hazelcast.jet.sql.impl.schema.HazelcastTable in project hazelcast by hazelcast.

the class LogicalUpdateTest method test_updateByKeyWithDynamicParam.

@Test
@Parameters(method = "types")
public void test_updateByKeyWithDynamicParam(QueryDataType type) {
    HazelcastTable table = partitionedTable("m", asList(field(KEY, type), field(VALUE, VARCHAR)), 1);
    assertPlan(optimizeLogical("UPDATE m SET this = '2' WHERE __key = ?", table), plan(planRow(0, UpdateByKeyMapLogicalRel.class)));
}
Also used : HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)70 Test (org.junit.Test)48 TableField (com.hazelcast.sql.impl.schema.TableField)8 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)8 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)8 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)7 RexNode (org.apache.calcite.rex.RexNode)7 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)6 Parameters (junitparams.Parameters)6 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)5 HazelcastRelOptTable (com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable)5 ArrayList (java.util.ArrayList)5 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)4 RelOptTable (org.apache.calcite.plan.RelOptTable)4 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 RelCollation (org.apache.calcite.rel.RelCollation)3 RelNode (org.apache.calcite.rel.RelNode)3 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)3 SqlValidatorTable (org.apache.calcite.sql.validate.SqlValidatorTable)3 RexBuilder (org.apache.calcite.rex.RexBuilder)2