Search in sources :

Example 51 with QueryDataType

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

the class KvMetadataJavaResolver method resolveObjectMetadata.

private KvMetadata resolveObjectMetadata(boolean isKey, List<MappingField> resolvedFields, Map<QueryPath, MappingField> fieldsByPath, Class<?> clazz) {
    Map<String, Class<?>> typesByNames = FieldsUtil.resolveClass(clazz);
    List<TableField> fields = new ArrayList<>();
    Map<String, String> typeNamesByPaths = new HashMap<>();
    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));
        if (path.getPath() != null && typesByNames.get(path.getPath()) != null) {
            typeNamesByPaths.put(path.getPath(), typesByNames.get(path.getPath()).getName());
        }
    }
    maybeAddDefaultField(isKey, resolvedFields, fields, QueryDataType.OBJECT);
    return new KvMetadata(fields, GenericQueryTargetDescriptor.DEFAULT, new PojoUpsertTargetDescriptor(clazz.getName(), typeNamesByPaths));
}
Also used : HashMap(java.util.HashMap) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) PojoUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.PojoUpsertTargetDescriptor) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MappingField(com.hazelcast.sql.impl.schema.MappingField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) QueryPath(com.hazelcast.sql.impl.extract.QueryPath)

Example 52 with QueryDataType

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

the class KvMetadataJavaResolver method resolveMetadata.

public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Class<?> clazz) {
    QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(clazz);
    Map<QueryPath, MappingField> fields = extractFields(resolvedFields, isKey);
    if (type != QueryDataType.OBJECT) {
        return resolvePrimitiveMetadata(isKey, resolvedFields, fields, type);
    } else {
        return resolveObjectMetadata(isKey, resolvedFields, fields, clazz);
    }
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 53 with QueryDataType

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

the class CompactUpsertTarget method createInjector.

@Override
@SuppressWarnings("checkstyle:ReturnCount")
public UpsertInjector createInjector(@Nullable String path, QueryDataType queryDataType) {
    if (path == null) {
        return FAILING_TOP_LEVEL_INJECTOR;
    }
    boolean hasField = schema.hasField(path);
    if (!hasField) {
        return value -> {
            throw QueryException.error("Unable to inject a non-null value to \"" + path + "\"");
        };
    }
    FieldKind kind = schema.getField(path).getKind();
    switch(kind) {
        case STRING:
            return value -> builder.setString(path, (String) value);
        case NULLABLE_BOOLEAN:
            return value -> builder.setNullableBoolean(path, (Boolean) value);
        case NULLABLE_INT8:
            return value -> builder.setNullableInt8(path, (Byte) value);
        case NULLABLE_INT16:
            return value -> builder.setNullableInt16(path, (Short) value);
        case NULLABLE_INT32:
            return value -> builder.setNullableInt32(path, (Integer) value);
        case NULLABLE_INT64:
            return value -> builder.setNullableInt64(path, (Long) value);
        case DECIMAL:
            return value -> builder.setDecimal(path, (BigDecimal) value);
        case NULLABLE_FLOAT32:
            return value -> builder.setNullableFloat32(path, (Float) value);
        case NULLABLE_FLOAT64:
            return value -> builder.setNullableFloat64(path, (Double) value);
        case TIME:
            return value -> builder.setTime(path, (LocalTime) value);
        case DATE:
            return value -> builder.setDate(path, (LocalDate) value);
        case TIMESTAMP:
            return value -> builder.setTimestamp(path, (LocalDateTime) value);
        case TIMESTAMP_WITH_TIMEZONE:
            return value -> builder.setTimestampWithTimezone(path, (OffsetDateTime) value);
        default:
            throw QueryException.error(kind + " kind is not supported in SQL with Compact format!");
    }
}
Also used : GenericRecord(com.hazelcast.nio.serialization.GenericRecord) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) LocalDateTime(java.time.LocalDateTime) Schema(com.hazelcast.internal.serialization.impl.compact.Schema) BigDecimal(java.math.BigDecimal) FieldKind(com.hazelcast.nio.serialization.FieldKind) OffsetDateTime(java.time.OffsetDateTime) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) GenericRecordBuilder(com.hazelcast.nio.serialization.GenericRecordBuilder) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) DeserializedSchemaBoundGenericRecordBuilder(com.hazelcast.internal.serialization.impl.compact.DeserializedSchemaBoundGenericRecordBuilder) QueryException(com.hazelcast.sql.impl.QueryException) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) FAILING_TOP_LEVEL_INJECTOR(com.hazelcast.jet.sql.impl.inject.UpsertInjector.FAILING_TOP_LEVEL_INJECTOR) FieldKind(com.hazelcast.nio.serialization.FieldKind)

Example 54 with QueryDataType

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

the class OptUtils method convert.

/**
 * Converts a {@link TableField} to {@link RelDataType}.
 */
public static RelDataType convert(TableField field, RelDataTypeFactory typeFactory) {
    QueryDataType fieldType = field.getType();
    SqlTypeName sqlTypeName = HazelcastTypeUtils.toCalciteType(fieldType);
    if (sqlTypeName == null) {
        throw new IllegalStateException("Unsupported type family: " + fieldType + ", getSqlTypeName should never return null.");
    }
    if (sqlTypeName == SqlTypeName.OTHER) {
        return convertCustomType(fieldType);
    } else {
        RelDataType relType = typeFactory.createSqlType(sqlTypeName);
        return typeFactory.createTypeWithNullability(relType, true);
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 55 with QueryDataType

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

the class KvMetadataAvroResolver method schema.

private Schema schema(List<TableField> fields) {
    QueryPath[] paths = paths(fields);
    QueryDataType[] types = types(fields);
    FieldAssembler<Schema> schema = SchemaBuilder.record("jet.sql").fields();
    for (int i = 0; i < fields.size(); i++) {
        String path = paths[i].getPath();
        if (path == null) {
            continue;
        }
        QueryDataType type = types[i];
        switch(type.getTypeFamily()) {
            case BOOLEAN:
                schema = schema.name(path).type().unionOf().nullType().and().booleanType().endUnion().nullDefault();
                break;
            case TINYINT:
            case SMALLINT:
            case INTEGER:
                schema = schema.name(path).type().unionOf().nullType().and().intType().endUnion().nullDefault();
                break;
            case BIGINT:
                schema = schema.name(path).type().unionOf().nullType().and().longType().endUnion().nullDefault();
                break;
            case REAL:
                schema = schema.name(path).type().unionOf().nullType().and().floatType().endUnion().nullDefault();
                break;
            case DOUBLE:
                schema = schema.name(path).type().unionOf().nullType().and().doubleType().endUnion().nullDefault();
                break;
            case DECIMAL:
            case TIME:
            case DATE:
            case TIMESTAMP:
            case TIMESTAMP_WITH_TIME_ZONE:
            case VARCHAR:
                schema = schema.name(path).type().unionOf().nullType().and().stringType().endUnion().nullDefault();
                break;
            case OBJECT:
                schema = schema.name(path).type().unionOf().nullType().and().booleanType().and().intType().and().longType().and().floatType().and().doubleType().and().stringType().endUnion().nullDefault();
                break;
            default:
                throw new IllegalArgumentException("Unknown type: " + type.getTypeFamily());
        }
    }
    return schema.endRecord();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) Schema(org.apache.avro.Schema)

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