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()));
}
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();
}
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());
});
}
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());
}
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);
}
Aggregations