org.apache.calcite.sql.type.JavaToSqlTypeConversionRules Java Examples

The following examples show how to use org.apache.calcite.sql.type.JavaToSqlTypeConversionRules. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SamzaSqlJavaTypeFactoryImpl.java    From samza with Apache License 2.0 6 votes vote down vote up
private static RelDataType convertToSql(final RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelRecordType) {
    return typeFactory.createStructType(
        Lists.transform(type.getFieldList(), a0 -> convertToSql(typeFactory, a0.getType())),
        type.getFieldNames());
  }
  if (type instanceof JavaType) {
    SqlTypeName typeName = JavaToSqlTypeConversionRules.instance().lookup(((JavaType) type).getJavaClass());
    // For unknown sql type names, return ANY sql type to make Calcite validation not fail.
    if (typeName == null) {
      typeName = SqlTypeName.ANY;
    }
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(typeName),
        type.isNullable());
  } else {
    return JavaTypeFactoryImpl.toSql(typeFactory, type);
  }
}
 
Example #2
Source File: RelDataTypeFactoryImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlTypeName getSqlTypeName() {
  final SqlTypeName typeName =
      JavaToSqlTypeConversionRules.instance().lookup(clazz);
  if (typeName == null) {
    return SqlTypeName.OTHER;
  }
  return typeName;
}
 
Example #3
Source File: StreamlineSqlTypeFactoryImpl.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType toSql(RelDataType type) {
    if (type instanceof JavaType) {
        JavaType javaType = (JavaType) type;
        SqlTypeName sqlTypeName = JavaToSqlTypeConversionRules.instance().lookup(javaType.getJavaClass());
        if (sqlTypeName == null) {
            sqlTypeName = SqlTypeName.ANY;
        }
        return createTypeWithNullability(createSqlType(sqlTypeName), type.isNullable());
    }
    return super.toSql(type);
}
 
Example #4
Source File: RelDataTypeFactoryImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlTypeName getSqlTypeName() {
  final SqlTypeName typeName =
      JavaToSqlTypeConversionRules.instance().lookup(clazz);
  if (typeName == null) {
    return SqlTypeName.OTHER;
  }
  return typeName;
}
 
Example #5
Source File: JavaTypeFactoryImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType createType(Type type) {
    if (type instanceof RelDataType) {
        return (RelDataType) type;
    }
    if (type instanceof SyntheticRecordType) {
        final SyntheticRecordType syntheticRecordType = (SyntheticRecordType) type;
        return syntheticRecordType.relType;
    }
    if (type instanceof Types.ArrayType) {
        final Types.ArrayType arrayType = (Types.ArrayType) type;
        final RelDataType componentRelType = createType(arrayType.getComponentType());
        return createArrayType(createTypeWithNullability(componentRelType, arrayType.componentIsNullable()),
                arrayType.maximumCardinality());
    }
    if (type instanceof Types.MapType) {
        final Types.MapType mapType = (Types.MapType) type;
        final RelDataType keyRelType = createType(mapType.getKeyType());
        final RelDataType valueRelType = createType(mapType.getValueType());
        return createMapType(createTypeWithNullability(keyRelType, mapType.keyIsNullable()),
                createTypeWithNullability(valueRelType, mapType.valueIsNullable()));
    }
    if (!(type instanceof Class)) {
        throw new UnsupportedOperationException("TODO: implement " + type);
    }
    final Class<?> clazz = (Class<?>) type;
    switch (Primitive.flavor(clazz)) {
    case PRIMITIVE:
        return createJavaType(clazz);
    case BOX:
        return createJavaType(Primitive.ofBox(clazz).boxClass);
    }
    if (JavaToSqlTypeConversionRules.instance().lookup(clazz) != null) {
        return createJavaType(clazz);
    } else if (clazz.isArray()) {
        return createMultisetType(createType(clazz.getComponentType()), -1);
    } else if (List.class.isAssignableFrom(clazz)) {
        return createArrayType(createTypeWithNullability(createSqlType(SqlTypeName.ANY), true), -1);
    } else if (Map.class.isAssignableFrom(clazz)) {
        return createMapType(createTypeWithNullability(createSqlType(SqlTypeName.ANY), true),
                createTypeWithNullability(createSqlType(SqlTypeName.ANY), true));
    } else {
        return createStructType(clazz);
    }
}
 
Example #6
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataType createType(Type type) {
  if (type instanceof RelDataType) {
    return (RelDataType) type;
  }
  if (type instanceof SyntheticRecordType) {
    final SyntheticRecordType syntheticRecordType =
        (SyntheticRecordType) type;
    return syntheticRecordType.relType;
  }
  if (type instanceof Types.ArrayType) {
    final Types.ArrayType arrayType = (Types.ArrayType) type;
    final RelDataType componentRelType =
        createType(arrayType.getComponentType());
    return createArrayType(
        createTypeWithNullability(componentRelType,
            arrayType.componentIsNullable()), arrayType.maximumCardinality());
  }
  if (type instanceof Types.MapType) {
    final Types.MapType mapType = (Types.MapType) type;
    final RelDataType keyRelType = createType(mapType.getKeyType());
    final RelDataType valueRelType = createType(mapType.getValueType());
    return createMapType(
        createTypeWithNullability(keyRelType, mapType.keyIsNullable()),
        createTypeWithNullability(valueRelType, mapType.valueIsNullable()));
  }
  if (!(type instanceof Class)) {
    throw new UnsupportedOperationException("TODO: implement " + type);
  }
  final Class clazz = (Class) type;
  switch (Primitive.flavor(clazz)) {
  case PRIMITIVE:
    return createJavaType(clazz);
  case BOX:
    return createJavaType(Primitive.ofBox(clazz).boxClass);
  }
  if (JavaToSqlTypeConversionRules.instance().lookup(clazz) != null) {
    return createJavaType(clazz);
  } else if (clazz.isArray()) {
    return createMultisetType(
        createType(clazz.getComponentType()), -1);
  } else if (List.class.isAssignableFrom(clazz)) {
    return createArrayType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true), -1);
  } else if (Map.class.isAssignableFrom(clazz)) {
    return createMapType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true),
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true));
  } else {
    return createStructType(clazz);
  }
}