Search in sources :

Example 6 with QueryDataType

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

the class KvMetadataAvroResolver method resolveMetadata.

@Override
public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> fieldsByPath = extractFields(resolvedFields, isKey);
    List<TableField> fields = new ArrayList<>();
    for (Entry<QueryPath, MappingField> entry : fieldsByPath.entrySet()) {
        QueryPath path = entry.getKey();
        QueryDataType type = entry.getValue().type();
        String name = entry.getValue().name();
        fields.add(new MapTableField(name, type, false, path));
    }
    maybeAddDefaultField(isKey, resolvedFields, fields, QueryDataType.OBJECT);
    return new KvMetadata(fields, AvroQueryTargetDescriptor.INSTANCE, new AvroUpsertTargetDescriptor(schema(fields).toString()));
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) AvroUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.AvroUpsertTargetDescriptor) MappingField(com.hazelcast.sql.impl.schema.MappingField) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField)

Example 7 with QueryDataType

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

the class KvMetadataJavaResolver method resolveAndValidateObjectFields.

private Stream<MappingField> resolveAndValidateObjectFields(boolean isKey, List<MappingField> userFields, Class<?> clazz) {
    Map<QueryPath, MappingField> userFieldsByPath = extractFields(userFields, isKey);
    for (Entry<String, Class<?>> classField : FieldsUtil.resolveClass(clazz).entrySet()) {
        QueryPath path = new QueryPath(classField.getKey(), isKey);
        QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(classField.getValue());
        MappingField userField = userFieldsByPath.get(path);
        if (userField != null && !type.getTypeFamily().equals(userField.type().getTypeFamily())) {
            throw QueryException.error("Mismatch between declared and resolved type for field '" + userField.name() + "'. Declared: " + userField.type().getTypeFamily() + ", resolved: " + type.getTypeFamily());
        }
    }
    return userFieldsByPath.values().stream();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 8 with QueryDataType

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

the class KvMetadataJavaResolver method resolveObjectFields.

private Stream<MappingField> resolveObjectFields(boolean isKey, Class<?> clazz) {
    Map<String, Class<?>> fieldsInClass = FieldsUtil.resolveClass(clazz);
    if (fieldsInClass.isEmpty()) {
        // we didn't find any non-object fields in the class, map the whole value (e.g. in java.lang.Object)
        String name = isKey ? KEY : VALUE;
        return Stream.of(new MappingField(name, QueryDataType.OBJECT, name));
    }
    return fieldsInClass.entrySet().stream().map(classField -> {
        QueryPath path = new QueryPath(classField.getKey(), isKey);
        QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(classField.getValue());
        String name = classField.getKey();
        return new MappingField(name, type, path.toString());
    });
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 9 with QueryDataType

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

the class JsonResolver method resolveFields.

static List<MappingField> resolveFields(Map<String, Object> json) {
    Map<String, MappingField> fields = new LinkedHashMap<>();
    for (Entry<String, Object> entry : json.entrySet()) {
        String name = entry.getKey();
        QueryDataType type = resolveType(entry.getValue());
        MappingField field = new MappingField(name, type);
        fields.putIfAbsent(field.name(), field);
    }
    return new ArrayList<>(fields.values());
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) MappingField(com.hazelcast.sql.impl.schema.MappingField) LinkedHashMap(java.util.LinkedHashMap)

Example 10 with QueryDataType

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

the class AggregateAbstractPhysicalRule method aggregateOperation.

protected static AggregateOperation<?, JetSqlRow> aggregateOperation(RelDataType inputType, ImmutableBitSet groupSet, List<AggregateCall> aggregateCalls) {
    List<QueryDataType> operandTypes = OptUtils.schema(inputType).getTypes();
    List<SupplierEx<SqlAggregation>> aggregationProviders = new ArrayList<>();
    List<FunctionEx<JetSqlRow, Object>> valueProviders = new ArrayList<>();
    for (Integer groupIndex : groupSet.toList()) {
        aggregationProviders.add(ValueSqlAggregation::new);
        // getMaybeSerialized is safe for ValueAggr because it only passes the value on
        valueProviders.add(new RowGetMaybeSerializedFn(groupIndex));
    }
    for (AggregateCall aggregateCall : aggregateCalls) {
        boolean distinct = aggregateCall.isDistinct();
        List<Integer> aggregateCallArguments = aggregateCall.getArgList();
        SqlKind kind = aggregateCall.getAggregation().getKind();
        switch(kind) {
            case COUNT:
                if (distinct) {
                    int countIndex = aggregateCallArguments.get(0);
                    aggregationProviders.add(new AggregateCountSupplier(true, true));
                    // getMaybeSerialized is safe for COUNT because the aggregation only looks whether it is null or not
                    valueProviders.add(new RowGetMaybeSerializedFn(countIndex));
                } else if (aggregateCallArguments.size() == 1) {
                    int countIndex = aggregateCallArguments.get(0);
                    aggregationProviders.add(new AggregateCountSupplier(true, false));
                    valueProviders.add(new RowGetMaybeSerializedFn(countIndex));
                } else {
                    aggregationProviders.add(new AggregateCountSupplier(false, false));
                    valueProviders.add(NullFunction.INSTANCE);
                }
                break;
            case MIN:
                int minIndex = aggregateCallArguments.get(0);
                aggregationProviders.add(MinSqlAggregation::new);
                valueProviders.add(new RowGetFn(minIndex));
                break;
            case MAX:
                int maxIndex = aggregateCallArguments.get(0);
                aggregationProviders.add(MaxSqlAggregation::new);
                valueProviders.add(new RowGetFn(maxIndex));
                break;
            case SUM:
                int sumIndex = aggregateCallArguments.get(0);
                QueryDataType sumOperandType = operandTypes.get(sumIndex);
                aggregationProviders.add(new AggregateSumSupplier(distinct, sumOperandType));
                valueProviders.add(new RowGetFn(sumIndex));
                break;
            case AVG:
                int avgIndex = aggregateCallArguments.get(0);
                QueryDataType avgOperandType = operandTypes.get(avgIndex);
                aggregationProviders.add(new AggregateAvgSupplier(distinct, avgOperandType));
                valueProviders.add(new RowGetFn(avgIndex));
                break;
            default:
                throw QueryException.error("Unsupported aggregation function: " + kind);
        }
    }
    return AggregateOperation.withCreate(new AggregateCreateSupplier(aggregationProviders)).andAccumulate(new AggregateAccumulateFunction(valueProviders)).andCombine(AggregateCombineFunction.INSTANCE).andExportFinish(AggregateExportFinishFunction.INSTANCE);
}
Also used : MaxSqlAggregation(com.hazelcast.jet.sql.impl.aggregate.MaxSqlAggregation) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) SupplierEx(com.hazelcast.function.SupplierEx) SqlKind(org.apache.calcite.sql.SqlKind) ValueSqlAggregation(com.hazelcast.jet.sql.impl.aggregate.ValueSqlAggregation) AggregateCall(org.apache.calcite.rel.core.AggregateCall) MinSqlAggregation(com.hazelcast.jet.sql.impl.aggregate.MinSqlAggregation) FunctionEx(com.hazelcast.function.FunctionEx)

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