Search in sources :

Example 11 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class IndexResolver method prepareSingleColumnSearchCandidateComparison.

@SuppressWarnings({ "ConstantConditions", "UnstableApiUsage" })
private static IndexComponentCandidate prepareSingleColumnSearchCandidateComparison(RexNode exp, RexNode operand1, RexNode operand2) {
    // SARG is supported only for literals, not for dynamic parameters
    if (operand1.getKind() != SqlKind.INPUT_REF || operand2.getKind() != SqlKind.LITERAL) {
        return null;
    }
    int columnIndex = ((RexInputRef) operand1).getIndex();
    RexLiteral literal = (RexLiteral) operand2;
    QueryDataType hazelcastType = HazelcastTypeUtils.toHazelcastType(literal.getType());
    RangeSet<?> rangeSet = RexToExpression.extractRangeFromSearch(literal);
    if (rangeSet == null) {
        return null;
    }
    Set<? extends Range<?>> ranges = rangeSet.asRanges();
    IndexFilter indexFilter;
    if (ranges.size() == 1) {
        indexFilter = createIndexFilterForSingleRange(Iterables.getFirst(ranges, null), hazelcastType);
    } else if (ranges.stream().allMatch(IndexResolver::isSingletonRange)) {
        indexFilter = new IndexInFilter(toList(ranges, range -> createIndexFilterForSingleRange(range, hazelcastType)));
    } else {
        // No support for IndexInFilter with multiple IndexFilterForSingleRanges
        return null;
    }
    return new IndexComponentCandidate(exp, columnIndex, indexFilter);
}
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) RexLiteral(org.apache.calcite.rex.RexLiteral) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RexInputRef(org.apache.calcite.rex.RexInputRef) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter)

Example 12 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class SqlDataSerializerHook method createFactory.

@SuppressWarnings("unchecked")
@Override
public DataSerializableFactory createFactory() {
    ConstructorFunction<Integer, IdentifiedDataSerializable>[] constructors = new ConstructorFunction[LEN];
    constructors[QUERY_DATA_TYPE] = arg -> new QueryDataType();
    constructors[QUERY_ID] = arg -> new QueryId();
    constructors[ROW_HEAP] = arg -> new HeapRow();
    constructors[ROW_EMPTY] = arg -> EmptyRow.INSTANCE;
    constructors[LAZY_TARGET] = arg -> new LazyTarget();
    constructors[INDEX_FILTER_VALUE] = arg -> new IndexFilterValue();
    constructors[INDEX_FILTER_EQUALS] = arg -> new IndexEqualsFilter();
    constructors[INDEX_FILTER_RANGE] = arg -> new IndexRangeFilter();
    constructors[INDEX_FILTER_IN] = arg -> new IndexInFilter();
    constructors[EXPRESSION_COLUMN] = arg -> new ColumnExpression<>();
    constructors[EXPRESSION_IS_NULL] = arg -> new IsNullPredicate();
    constructors[TARGET_DESCRIPTOR_GENERIC] = arg -> GenericQueryTargetDescriptor.DEFAULT;
    constructors[QUERY_PATH] = arg -> new QueryPath();
    constructors[EXPRESSION_CONSTANT] = arg -> new ConstantExpression<>();
    constructors[EXPRESSION_PARAMETER] = arg -> new ParameterExpression<>();
    constructors[EXPRESSION_CAST] = arg -> new CastExpression<>();
    constructors[EXPRESSION_DIVIDE] = arg -> new DivideFunction<>();
    constructors[EXPRESSION_MINUS] = arg -> new MinusFunction<>();
    constructors[EXPRESSION_MULTIPLY] = arg -> new MultiplyFunction<>();
    constructors[EXPRESSION_PLUS] = arg -> new PlusFunction<>();
    constructors[EXPRESSION_UNARY_MINUS] = arg -> new UnaryMinusFunction<>();
    constructors[EXPRESSION_AND] = arg -> new AndPredicate();
    constructors[EXPRESSION_OR] = arg -> new OrPredicate();
    constructors[EXPRESSION_NOT] = arg -> new NotPredicate();
    constructors[EXPRESSION_COMPARISON] = arg -> new ComparisonPredicate();
    constructors[EXPRESSION_IS_TRUE] = arg -> new IsTruePredicate();
    constructors[EXPRESSION_IS_NOT_TRUE] = arg -> new IsNotTruePredicate();
    constructors[EXPRESSION_IS_FALSE] = arg -> new IsFalsePredicate();
    constructors[EXPRESSION_IS_NOT_FALSE] = arg -> new IsNotFalsePredicate();
    constructors[EXPRESSION_IS_NOT_NULL] = arg -> new IsNotNullPredicate();
    constructors[EXPRESSION_ABS] = arg -> new AbsFunction<>();
    constructors[EXPRESSION_SIGN] = arg -> new SignFunction<>();
    constructors[EXPRESSION_RAND] = arg -> new RandFunction();
    constructors[EXPRESSION_DOUBLE] = arg -> new DoubleFunction();
    constructors[EXPRESSION_FLOOR_CEIL] = arg -> new FloorCeilFunction<>();
    constructors[EXPRESSION_ROUND_TRUNCATE] = arg -> new RoundTruncateFunction<>();
    constructors[INTERVAL_YEAR_MONTH] = arg -> new SqlYearMonthInterval();
    constructors[INTERVAL_DAY_SECOND] = arg -> new SqlDaySecondInterval();
    constructors[EXPRESSION_ASCII] = arg -> new AsciiFunction();
    constructors[EXPRESSION_CHAR_LENGTH] = arg -> new CharLengthFunction();
    constructors[EXPRESSION_INITCAP] = arg -> new InitcapFunction();
    constructors[EXPRESSION_LOWER] = arg -> new LowerFunction();
    constructors[EXPRESSION_UPPER] = arg -> new UpperFunction();
    constructors[EXPRESSION_CONCAT] = arg -> new ConcatFunction();
    constructors[EXPRESSION_LIKE] = arg -> new LikeFunction();
    constructors[EXPRESSION_SUBSTRING] = arg -> new SubstringFunction();
    constructors[EXPRESSION_TRIM] = arg -> new TrimFunction();
    constructors[EXPRESSION_REPLACE] = arg -> new ReplaceFunction();
    constructors[EXPRESSION_POSITION] = arg -> new PositionFunction();
    constructors[EXPRESSION_REMAINDER] = arg -> new RemainderFunction<>();
    constructors[EXPRESSION_CONCAT_WS] = arg -> new ConcatWSFunction();
    constructors[EXPRESSION_CASE] = arg -> new CaseExpression<>();
    constructors[EXPRESSION_EXTRACT] = arg -> new ExtractFunction();
    constructors[EXPRESSION_DOUBLE_DOUBLE] = arg -> new DoubleBiFunction();
    constructors[EXPRESSION_TO_TIMESTAMP_TZ] = arg -> new ToTimestampTzFunction();
    constructors[EXPRESSION_TO_EPOCH_MILLIS] = arg -> new ToEpochMillisFunction();
    constructors[MAPPING] = arg -> new Mapping();
    constructors[MAPPING_FIELD] = arg -> new MappingField();
    constructors[EXPRESSION_SEARCHABLE] = arg -> new SearchableExpression<>();
    constructors[EXPRESSION_SEARCH] = arg -> new SearchPredicate();
    constructors[VIEW] = arg -> new View();
    return new ArrayDataSerializableFactory(constructors);
}
Also used : ConcatWSFunction(com.hazelcast.sql.impl.expression.string.ConcatWSFunction) LowerFunction(com.hazelcast.sql.impl.expression.string.LowerFunction) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) IsFalsePredicate(com.hazelcast.sql.impl.expression.predicate.IsFalsePredicate) Mapping(com.hazelcast.sql.impl.schema.Mapping) HeapRow(com.hazelcast.sql.impl.row.HeapRow) SearchPredicate(com.hazelcast.sql.impl.expression.predicate.SearchPredicate) PositionFunction(com.hazelcast.sql.impl.expression.string.PositionFunction) ConcatFunction(com.hazelcast.sql.impl.expression.string.ConcatFunction) IsNotTruePredicate(com.hazelcast.sql.impl.expression.predicate.IsNotTruePredicate) OrPredicate(com.hazelcast.sql.impl.expression.predicate.OrPredicate) ExtractFunction(com.hazelcast.sql.impl.expression.datetime.ExtractFunction) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) IsNullPredicate(com.hazelcast.sql.impl.expression.predicate.IsNullPredicate) UpperFunction(com.hazelcast.sql.impl.expression.string.UpperFunction) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField) DoubleBiFunction(com.hazelcast.sql.impl.expression.math.DoubleBiFunction) TrimFunction(com.hazelcast.sql.impl.expression.string.TrimFunction) ToEpochMillisFunction(com.hazelcast.sql.impl.expression.datetime.ToEpochMillisFunction) RandFunction(com.hazelcast.sql.impl.expression.math.RandFunction) CharLengthFunction(com.hazelcast.sql.impl.expression.string.CharLengthFunction) SqlYearMonthInterval(com.hazelcast.sql.impl.type.SqlYearMonthInterval) LikeFunction(com.hazelcast.sql.impl.expression.string.LikeFunction) ReplaceFunction(com.hazelcast.sql.impl.expression.string.ReplaceFunction) IsNotFalsePredicate(com.hazelcast.sql.impl.expression.predicate.IsNotFalsePredicate) ComparisonPredicate(com.hazelcast.sql.impl.expression.predicate.ComparisonPredicate) SubstringFunction(com.hazelcast.sql.impl.expression.string.SubstringFunction) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) ToTimestampTzFunction(com.hazelcast.sql.impl.expression.datetime.ToTimestampTzFunction) InitcapFunction(com.hazelcast.sql.impl.expression.string.InitcapFunction) AndPredicate(com.hazelcast.sql.impl.expression.predicate.AndPredicate) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) SqlDaySecondInterval(com.hazelcast.sql.impl.type.SqlDaySecondInterval) NotPredicate(com.hazelcast.sql.impl.expression.predicate.NotPredicate) View(com.hazelcast.sql.impl.schema.view.View) ConstructorFunction(com.hazelcast.internal.util.ConstructorFunction) IsNotNullPredicate(com.hazelcast.sql.impl.expression.predicate.IsNotNullPredicate) DoubleFunction(com.hazelcast.sql.impl.expression.math.DoubleFunction) AsciiFunction(com.hazelcast.sql.impl.expression.string.AsciiFunction) IsTruePredicate(com.hazelcast.sql.impl.expression.predicate.IsTruePredicate) ArrayDataSerializableFactory(com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory)

Example 13 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MapScanRow method create.

public static MapScanRow create(QueryTargetDescriptor keyDescriptor, QueryTargetDescriptor valueDescriptor, List<QueryPath> fieldPaths, List<QueryDataType> fieldTypes, Extractors extractors, InternalSerializationService serializationService) {
    QueryTarget keyTarget = keyDescriptor.create(serializationService, extractors, true);
    QueryTarget valueTarget = valueDescriptor.create(serializationService, extractors, false);
    QueryExtractor[] fieldExtractors = new QueryExtractor[fieldPaths.size()];
    for (int i = 0; i < fieldPaths.size(); i++) {
        QueryPath fieldPath = fieldPaths.get(i);
        QueryDataType fieldType = fieldTypes.get(i);
        fieldExtractors[i] = createExtractor(keyTarget, valueTarget, fieldPath, fieldType);
    }
    return new MapScanRow(keyTarget, valueTarget, fieldExtractors);
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryTarget(com.hazelcast.sql.impl.extract.QueryTarget) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) QueryExtractor(com.hazelcast.sql.impl.extract.QueryExtractor)

Example 14 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class FieldsUtil method resolvePortable.

/**
 * Resolve the list of fields from a portable {@link ClassDefinition},
 * along with their {@link QueryDataType}.
 */
@Nonnull
public static SortedMap<String, QueryDataType> resolvePortable(@Nonnull ClassDefinition clazz) {
    SortedMap<String, QueryDataType> fields = new TreeMap<>();
    // Add regular fields.
    for (String name : clazz.getFieldNames()) {
        FieldType portableType = clazz.getFieldType(name);
        QueryDataType type = resolveType(FieldTypeToFieldKind.toFieldKind(portableType));
        fields.putIfAbsent(name, type);
    }
    return fields;
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) TreeMap(java.util.TreeMap) FieldType(com.hazelcast.nio.serialization.FieldType) Nonnull(javax.annotation.Nonnull)

Example 15 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MapTableUtils method getPartitionedMapIndexes.

public static List<MapTableIndex> getPartitionedMapIndexes(MapContainer mapContainer, List<TableField> fields) {
    Map<QueryPath, Integer> pathToOrdinalMap = mapPathsToOrdinals(fields);
    if (mapContainer.getIndexes() == null) {
        return Collections.emptyList();
    }
    InternalIndex[] indexes = mapContainer.getIndexes().getIndexes();
    if (indexes == null || indexes.length == 0) {
        return Collections.emptyList();
    }
    List<MapTableIndex> res = new ArrayList<>(indexes.length);
    for (Index index : indexes) {
        IndexConfig indexConfig = index.getConfig();
        List<QueryDataType> resolvedFieldConverterTypes = indexConverterToSqlTypes(index.getConverter());
        List<String> indexAttributes = indexConfig.getAttributes();
        List<Integer> indexFieldOrdinals = new ArrayList<>(indexAttributes.size());
        List<QueryDataType> indexFieldConverterTypes = new ArrayList<>(indexAttributes.size());
        String[] components = index.getComponents();
        for (int i = 0; i < indexAttributes.size(); i++) {
            String attribute = indexAttributes.get(i);
            QueryPath attributePath = QueryPath.create(attribute);
            Integer ordinal = pathToOrdinalMap.get(attributePath);
            if (ordinal == null) {
                // No mapping for the field. Stop.
                break;
            }
            if (i >= resolvedFieldConverterTypes.size()) {
                // No more resolved converters. Stop.
                break;
            }
            QueryDataType fieldType = fields.get(ordinal).getType();
            QueryDataType converterType = resolvedFieldConverterTypes.get(i);
            if (!isCompatibleForIndexRequest(fieldType, converterType)) {
                // Field and converter types are not compatible (e.g. INT vs VARCHAR).
                break;
            }
            indexFieldOrdinals.add(ordinal);
            indexFieldConverterTypes.add(converterType);
        }
        MapTableIndex index0 = new MapTableIndex(indexConfig.getName(), indexConfig.getType(), components.length, indexFieldOrdinals, indexFieldConverterTypes);
        res.add(index0);
    }
    return res;
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) InternalIndex(com.hazelcast.query.impl.InternalIndex) Index(com.hazelcast.query.impl.Index) QueryPath(com.hazelcast.sql.impl.extract.QueryPath) InternalIndex(com.hazelcast.query.impl.InternalIndex) IndexConfig(com.hazelcast.config.IndexConfig)

Aggregations

QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)61 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)23 MappingField (com.hazelcast.sql.impl.schema.MappingField)17 ArrayList (java.util.ArrayList)16 TableField (com.hazelcast.sql.impl.schema.TableField)14 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)13 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)10 RelDataType (org.apache.calcite.rel.type.RelDataType)7 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)6 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)6 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)5 Nonnull (javax.annotation.Nonnull)5 IndexEqualsFilter (com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter)4 Test (org.junit.Test)4 IndexType (com.hazelcast.config.IndexType)3 KvMetadata (com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 HashMap (java.util.HashMap)3 List (java.util.List)3