Search in sources :

Example 36 with QueryDataType

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

the class HazelcastTypeCoercion method rowTypeElementCoercion.

@SuppressWarnings("checkstyle:BooleanExpressionComplexity")
public boolean rowTypeElementCoercion(SqlValidatorScope scope, SqlNode rowElement, RelDataType targetType, Consumer<SqlNode> replaceFn) {
    if (targetType.equals(scope.getValidator().getUnknownType())) {
        return false;
    }
    RelDataType sourceType = validator.deriveType(scope, rowElement);
    QueryDataType sourceHzType = HazelcastTypeUtils.toHazelcastType(sourceType);
    QueryDataType targetHzType = HazelcastTypeUtils.toHazelcastType(targetType);
    if (sourceHzType.getTypeFamily() == targetHzType.getTypeFamily() || targetHzType == OBJECT) {
        // Do nothing.
        return true;
    }
    boolean valid = sourceAndTargetAreNumeric(targetHzType, sourceHzType) || sourceAndTargetAreTemporalAndSourceCanBeConvertedToTarget(targetHzType, sourceHzType) || targetIsTemporalAndSourceIsVarcharLiteral(targetHzType, sourceHzType, rowElement) || sourceHzType.getTypeFamily() == QueryDataTypeFamily.NULL || sourceHzType.getTypeFamily() == QueryDataTypeFamily.VARCHAR && targetHzType.getTypeFamily() == QueryDataTypeFamily.JSON;
    if (!valid) {
        // Types cannot be converted to each other, fail to coerce
        return false;
    }
    // Types are in the same group, cast source to target.
    coerceNode(scope, rowElement, targetType, replaceFn);
    return true;
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 37 with QueryDataType

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

the class FieldsUtil method resolveCompact.

/**
 * Resolve the list of fields from a schema {@link com.hazelcast.internal.serialization.impl.compact.Schema},
 * along with their {@link QueryDataType}.
 */
@Nonnull
public static SortedMap<String, QueryDataType> resolveCompact(@Nonnull Schema schema) {
    SortedMap<String, QueryDataType> fields = new TreeMap<>();
    // Add regular fields.
    for (String name : schema.getFieldNames()) {
        FieldKind compactKind = schema.getField(name).getKind();
        QueryDataType type = resolveType(compactKind);
        fields.putIfAbsent(name, type);
    }
    return fields;
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) FieldTypeToFieldKind(com.hazelcast.internal.serialization.impl.portable.FieldTypeToFieldKind) FieldKind(com.hazelcast.nio.serialization.FieldKind) TreeMap(java.util.TreeMap) Nonnull(javax.annotation.Nonnull)

Example 38 with QueryDataType

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

the class MapIndexScanExecIterator method validateConverterTypes.

private void validateConverterTypes(InternalIndex index, String mapName, List<QueryDataType> expectedConverterTypes, List<QueryDataType> actualConverterTypes) {
    for (int i = 0; i < Math.min(expectedConverterTypes.size(), actualConverterTypes.size()); i++) {
        QueryDataType expected = expectedConverterTypes.get(i);
        QueryDataType actual = actualConverterTypes.get(i);
        if (!expected.equals(actual)) {
            String component = index.getComponents()[i];
            throw QueryException.error(SqlErrorCode.INDEX_INVALID, "Cannot use the index \"" + index.getName() + "\" of the IMap \"" + mapName + "\" because it has component \"" + component + "\" of type " + actual.getTypeFamily() + ", but " + expected.getTypeFamily() + " was expected");
        }
    }
    if (expectedConverterTypes.size() > actualConverterTypes.size()) {
        QueryDataType expected = expectedConverterTypes.get(actualConverterTypes.size());
        String component = index.getComponents()[actualConverterTypes.size()];
        throw QueryException.error(SqlErrorCode.INDEX_INVALID, "Cannot use the index \"" + index.getName() + "\" of the IMap \"" + mapName + "\" because it does not have suitable converter for component \"" + component + "\" (expected " + expected.getTypeFamily() + ")");
    }
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType)

Example 39 with QueryDataType

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

the class ColumnExpression method create.

public static ColumnExpression<?> create(int index, QueryDataType type) {
    // Canonicalize the column type: currently values of non-canonical types,
    // like QueryDataType.VARCHAR_CHARACTER, are canonicalized to values of
    // some other canonical type, like QueryDataType.VARCHAR. That kind of
    // changes the observed type of a column to a canonical one.
    Class<?> canonicalClass = type.getConverter().getNormalizedValueClass();
    QueryDataType canonicalType = QueryDataTypeUtils.resolveTypeForClass(canonicalClass);
    return new ColumnExpression<>(index, canonicalType);
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType)

Example 40 with QueryDataType

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

the class MapTableUtils method indexConverterToSqlTypes.

public static List<QueryDataType> indexConverterToSqlTypes(TypeConverter converter) {
    if (converter == null) {
        return Collections.emptyList();
    }
    if (converter instanceof CompositeConverter) {
        CompositeConverter converter0 = ((CompositeConverter) converter);
        List<QueryDataType> res = new ArrayList<>(converter0.getComponentCount());
        for (int i = 0; i < converter0.getComponentCount(); i++) {
            QueryDataType type = indexConverterToSqlType(converter0.getComponentConverter(i));
            if (type == null) {
                break;
            } else {
                res.add(type);
            }
        }
        if (!res.isEmpty()) {
            return res;
        }
    } else {
        QueryDataType type = indexConverterToSqlType(converter);
        if (type != null) {
            return Collections.singletonList(type);
        }
    }
    return Collections.emptyList();
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) CompositeConverter(com.hazelcast.query.impl.CompositeConverter)

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