Java Code Examples for org.apache.calcite.rel.type.RelDataTypeFactory#createArrayType()

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeFactory#createArrayType() . 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: AbstractNamespace.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected RelDataType convertToStruct(RelDataType type) {
  // "MULTISET [<expr>, ...]" needs to be wrapped in a record if
  // <expr> has a scalar type.
  // For example, "MULTISET [8, 9]" has type
  // "RECORD(INTEGER EXPR$0 NOT NULL) NOT NULL MULTISET NOT NULL".
  final RelDataType componentType = type.getComponentType();
  if (componentType == null || componentType.isStruct()) {
    return type;
  }
  final RelDataTypeFactory typeFactory = validator.getTypeFactory();
  final RelDataType structType = toStruct(componentType, getNode());
  final RelDataType collectionType;
  switch (type.getSqlTypeName()) {
  case ARRAY:
    collectionType = typeFactory.createArrayType(structType, -1);
    break;
  case MULTISET:
    collectionType = typeFactory.createMultisetType(structType, -1);
    break;
  default:
    throw new AssertionError(type);
  }
  return typeFactory.createTypeWithNullability(collectionType,
      type.isNullable());
}
 
Example 2
Source File: AbstractNamespace.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelDataType convertToStruct(RelDataType type) {
  // "MULTISET [<expr>, ...]" needs to be wrapped in a record if
  // <expr> has a scalar type.
  // For example, "MULTISET [8, 9]" has type
  // "RECORD(INTEGER EXPR$0 NOT NULL) NOT NULL MULTISET NOT NULL".
  final RelDataType componentType = type.getComponentType();
  if (componentType == null || componentType.isStruct()) {
    return type;
  }
  final RelDataTypeFactory typeFactory = validator.getTypeFactory();
  final RelDataType structType = toStruct(componentType, getNode());
  final RelDataType collectionType;
  switch (type.getSqlTypeName()) {
  case ARRAY:
    collectionType = typeFactory.createArrayType(structType, -1);
    break;
  case MULTISET:
    collectionType = typeFactory.createMultisetType(structType, -1);
    break;
  default:
    throw new AssertionError(type);
  }
  return typeFactory.createTypeWithNullability(collectionType,
      type.isNullable());
}
 
Example 3
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RelDataType createArrayType(
    RelDataTypeFactory typeFactory,
    RelDataType type,
    boolean nullable) {
  RelDataType ret = typeFactory.createArrayType(type, -1);
  return typeFactory.createTypeWithNullability(ret, nullable);
}
 
Example 4
Source File: OLAPTable.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static RelDataType createSqlType(RelDataTypeFactory typeFactory, DataType dataType, boolean isNullable) {
    SqlTypeName sqlTypeName = SQLTYPE_MAPPING.get(dataType.getName());
    if (sqlTypeName == null) {
        for (String reg : REGEX_SQLTYPE_MAPPING.keySet()) {
            Pattern pattern = Pattern.compile(reg);
            if (pattern.matcher(dataType.getName()).matches()) {
                sqlTypeName = REGEX_SQLTYPE_MAPPING.get(reg);
                break;
            }
        }
    }

    if (sqlTypeName == null)
        throw new IllegalArgumentException("Unrecognized data type " + dataType);

    int precision = dataType.getPrecision();
    int scale = dataType.getScale();

    RelDataType result;
    if (sqlTypeName == SqlTypeName.ARRAY) {
        String innerTypeName = dataType.getName().split("<|>")[1];
        result = typeFactory.createArrayType(createSqlType(typeFactory, DataType.getType(innerTypeName), false),
                -1);
    } else if (precision >= 0 && scale >= 0)
        result = typeFactory.createSqlType(sqlTypeName, precision, scale);
    else if (precision >= 0)
        result = typeFactory.createSqlType(sqlTypeName, precision);
    else
        result = typeFactory.createSqlType(sqlTypeName);

    // due to left join and uncertain data quality, dimension value can be null
    if (isNullable) {
        result = typeFactory.createTypeWithNullability(result, true);
    } else {
        result = typeFactory.createTypeWithNullability(result, false);
    }

    return result;
}
 
Example 5
Source File: OLAPTable.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static RelDataType createSqlType(RelDataTypeFactory typeFactory, DataType dataType, boolean isNullable) {
    SqlTypeName sqlTypeName = SQLTYPE_MAPPING.get(dataType.getName());
    if (sqlTypeName == null) {
        for (String reg : REGEX_SQLTYPE_MAPPING.keySet()) {
            Pattern pattern = Pattern.compile(reg);
            if (pattern.matcher(dataType.getName()).matches()) {
                sqlTypeName = REGEX_SQLTYPE_MAPPING.get(reg);
                break;
            }
        }
    }

    if (sqlTypeName == null)
        throw new IllegalArgumentException("Unrecognized data type " + dataType);

    int precision = dataType.getPrecision();
    int scale = dataType.getScale();

    RelDataType result;
    if (sqlTypeName == SqlTypeName.ARRAY) {
        String innerTypeName = dataType.getName().split("<|>")[1];
        result = typeFactory.createArrayType(createSqlType(typeFactory, DataType.getType(innerTypeName), false),
                -1);
    } else if (precision >= 0 && scale >= 0)
        result = typeFactory.createSqlType(sqlTypeName, precision, scale);
    else if (precision >= 0)
        result = typeFactory.createSqlType(sqlTypeName, precision);
    else
        result = typeFactory.createSqlType(sqlTypeName);

    // due to left join and uncertain data quality, dimension value can be null
    if (isNullable) {
        result = typeFactory.createTypeWithNullability(result, true);
    } else {
        result = typeFactory.createTypeWithNullability(result, false);
    }

    return result;
}
 
Example 6
Source File: ExtendedSqlCollectionTypeNameSpec.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create collection data type.
 *
 * @param elementType Type of the collection element
 * @param typeFactory Type factory
 * @return The collection data type, or throw exception if the collection
 *         type name does not belong to {@code SqlTypeName} enumerations
 */
private RelDataType createCollectionType(RelDataType elementType,
		RelDataTypeFactory typeFactory) {
	switch (collectionTypeName) {
	case MULTISET:
		return typeFactory.createMultisetType(elementType, -1);
	case ARRAY:
		return typeFactory.createArrayType(elementType, -1);

	default:
		throw Util.unexpected(collectionTypeName);
	}
}
 
Example 7
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a type in Java format to a SQL-oriented type. */
public static RelDataType toSql(final RelDataTypeFactory typeFactory,
    RelDataType type) {
  if (type instanceof RelRecordType) {
    return typeFactory.createTypeWithNullability(
        typeFactory.createStructType(
            type.getFieldList()
                .stream()
                .map(field -> toSql(typeFactory, field.getType()))
                .collect(Collectors.toList()),
            type.getFieldNames()),
        type.isNullable());
  } else if (type instanceof JavaType) {
    SqlTypeName sqlTypeName = type.getSqlTypeName();
    final RelDataType relDataType;
    if (SqlTypeUtil.isArray(type)) {
      // Transform to sql type, take care for two cases:
      // 1. type.getJavaClass() is collection with erased generic type
      // 2. ElementType returned by JavaType is also of JavaType,
      // and needs conversion using typeFactory
      final RelDataType elementType = toSqlTypeWithNullToAny(
          typeFactory, type.getComponentType());
      relDataType = typeFactory.createArrayType(elementType, -1);
    } else if (SqlTypeUtil.isMap(type)) {
      final RelDataType keyType = toSqlTypeWithNullToAny(
          typeFactory, type.getKeyType());
      final RelDataType valueType = toSqlTypeWithNullToAny(
          typeFactory, type.getValueType());
      relDataType = typeFactory.createMapType(keyType, valueType);
    } else {
      relDataType = typeFactory.createSqlType(sqlTypeName);
    }
    return typeFactory.createTypeWithNullability(relDataType, type.isNullable());
  }
  return type;
}
 
Example 8
Source File: SqlCollectionTypeNameSpec.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Create collection data type.
 *
 * @param elementType Type of the collection element
 * @param typeFactory Type factory
 * @return The collection data type, or throw exception if the collection
 *         type name does not belong to {@code SqlTypeName} enumerations
 */
private RelDataType createCollectionType(RelDataType elementType,
    RelDataTypeFactory typeFactory) {
  switch (collectionTypeName) {
  case MULTISET:
    return typeFactory.createMultisetType(elementType, -1);
  case ARRAY:
    return typeFactory.createArrayType(elementType, -1);

  default:
    throw Util.unexpected(collectionTypeName);
  }
}
 
Example 9
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RelDataType createArrayType(
    RelDataTypeFactory typeFactory,
    RelDataType type,
    boolean nullable) {
  RelDataType ret = typeFactory.createArrayType(type, -1);
  return typeFactory.createTypeWithNullability(ret, nullable);
}
 
Example 10
Source File: AvroDrillTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RelDataType getNullableRelDataTypeFromAvroType(
      RelDataTypeFactory typeFactory, Schema fieldSchema) {
    LogicalType logicalType = fieldSchema.getLogicalType();
    String logicalTypeName = logicalType != null ? logicalType.getName() : "";
    RelDataType relDataType = null;
    switch (fieldSchema.getType()) {
    case ARRAY:
      RelDataType eleType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getElementType());
      relDataType = typeFactory.createArrayType(eleType, -1);
      break;
    case BOOLEAN:
      relDataType = typeFactory.createSqlType(SqlTypeName.BOOLEAN);
      break;
    case BYTES:
      switch (logicalTypeName) {
        case "decimal":
          relDataType = typeFactory.createSqlType(SqlTypeName.DECIMAL);
          break;
        default:
          relDataType = typeFactory.createSqlType(SqlTypeName.BINARY);
      }
      break;
    case DOUBLE:
      relDataType = typeFactory.createSqlType(SqlTypeName.DOUBLE);
      break;
    case FIXED:
      switch (logicalTypeName) {
        case "decimal":
          relDataType = typeFactory.createSqlType(SqlTypeName.DECIMAL);
          break;
        default:
          logger.error("{} type not supported", fieldSchema.getType());
          throw UserException.unsupportedError().message("FIXED type not supported yet").build(logger);
      }
      break;
    case FLOAT:
      relDataType = typeFactory.createSqlType(SqlTypeName.FLOAT);
      break;
    case INT:
      switch (logicalTypeName) {
        case "date":
          relDataType = typeFactory.createSqlType(SqlTypeName.DATE);
          break;
        case "time-millis":
          relDataType = typeFactory.createSqlType(SqlTypeName.TIME);
          break;
        default:
          relDataType = typeFactory.createSqlType(SqlTypeName.INTEGER);
      }
      break;
    case LONG:
      switch (logicalTypeName) {
        case "date":
          relDataType = typeFactory.createSqlType(SqlTypeName.DATE);
          break;
        case "time-micros":
          relDataType = typeFactory.createSqlType(SqlTypeName.TIME);
          break;
        case "timestamp-millis":
        case "timestamp-micros":
          relDataType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
          break;
        default:
          relDataType = typeFactory.createSqlType(SqlTypeName.BIGINT);
      }
      break;
    case MAP:
      RelDataType valueType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getValueType());
      RelDataType keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
      relDataType = typeFactory.createMapType(keyType, valueType);
      break;
    case NULL:
      relDataType = typeFactory.createSqlType(SqlTypeName.NULL);
      break;
    case RECORD:
//      List<String> fieldNameList = Lists.newArrayList();
//      List<RelDataType> fieldRelDataTypeList = Lists.newArrayList();
//      for(Field field : fieldSchema.getFields()) {
//        fieldNameList.add(field.name());
//        fieldRelDataTypeList.add(getNullableRelDataTypeFromAvroType(typeFactory, field.schema()));
//      }
//      relDataType = typeFactory.createStructType(fieldRelDataTypeList, fieldNameList);

      //TODO This has to be mapped to struct type but because of calcite issue,
      //for now mapping it to map type.
      keyType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
      valueType = typeFactory.createSqlType(SqlTypeName.ANY);
      relDataType = typeFactory.createMapType(keyType, valueType);
      break;
    case ENUM:
    case STRING:
      relDataType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
      break;
    case UNION:
      RelDataType optinalType = getNullableRelDataTypeFromAvroType(typeFactory, fieldSchema.getTypes().get(1));
      relDataType = typeFactory.createTypeWithNullability(optinalType, true);
      break;
    }
    return relDataType;
  }
 
Example 11
Source File: FlinkSqlDataTypeSpec.java    From flink with Apache License 2.0 4 votes vote down vote up
private RelDataType getExtendedType(RelDataTypeFactory typeFactory, SqlIdentifier typeName) {
	// quick check.
	if (!(typeName instanceof ExtendedSqlType)) {
		return null;
	}
	if (typeName instanceof SqlBytesType) {
		return typeFactory.createSqlType(SqlTypeName.VARBINARY, Integer.MAX_VALUE);
	} else if (typeName instanceof SqlStringType) {
		return typeFactory.createSqlType(SqlTypeName.VARCHAR, Integer.MAX_VALUE);
	} else if (typeName instanceof SqlArrayType) {
		final SqlArrayType arrayType = (SqlArrayType) typeName;
		return typeFactory.createArrayType(arrayType.getElementType()
			.deriveType(typeFactory), -1);
	} else if (typeName instanceof SqlMultisetType) {
		final SqlMultisetType multiSetType = (SqlMultisetType) typeName;
		return typeFactory.createMultisetType(multiSetType.getElementType()
			.deriveType(typeFactory), -1);
	} else if (typeName instanceof SqlMapType) {
		final SqlMapType mapType = (SqlMapType) typeName;
		return typeFactory.createMapType(
			mapType.getKeyType().deriveType(typeFactory),
			mapType.getValType().deriveType(typeFactory));
	} else if (typeName instanceof SqlRowType) {
		final SqlRowType rowType = (SqlRowType) typeName;
		return typeFactory.createStructType(
			rowType.getFieldTypes().stream().map(ft -> ft.deriveType(typeFactory))
				.collect(Collectors.toList()),
			rowType.getFieldNames().stream().map(SqlIdentifier::getSimple)
				.collect(Collectors.toList()));
	} else if (typeName instanceof SqlTimeType) {
		final SqlTimeType zonedTimeType = (SqlTimeType) typeName;
		if (zonedTimeType.getPrecision() >= 0) {
			return typeFactory.createSqlType(zonedTimeType.getSqlTypeName(),
				zonedTimeType.getPrecision());
		} else {
			// Use default precision.
			return typeFactory.createSqlType(zonedTimeType.getSqlTypeName());
		}
	} else if (typeName instanceof SqlTimestampType) {
		final SqlTimestampType zonedTimestampType = (SqlTimestampType) typeName;
		if (zonedTimestampType.getPrecision() >= 0) {
			return typeFactory.createSqlType(zonedTimestampType.getSqlTypeName(),
				zonedTimestampType.getPrecision());
		} else {
			// Use default precision.
			return typeFactory.createSqlType(zonedTimestampType.getSqlTypeName());
		}
	}
	return null;
}
 
Example 12
Source File: TypeInferenceUtil.java    From marble with Apache License 2.0 4 votes vote down vote up
public static RelDataType getRelDataType(ObjectInspector oi,
      RelDataTypeFactory factory) {
    if (oi == null) {
      return factory.createTypeWithNullability(
          factory.createSqlType(SqlTypeName.ANY), true);
    }
    ObjectInspector.Category category = oi.getCategory();
    if (!category.equals(PRIMITIVE)) {
      if (category.equals(ObjectInspector.Category.LIST)) {
        StandardListObjectInspector standardListObjectInspector =
            (StandardListObjectInspector) oi;
        return factory.createArrayType(
            getRelDataType(
                standardListObjectInspector.getListElementObjectInspector(),
                factory), -1);
      } else {
        throw new IllegalArgumentException(
            "unsupported ObjectInspector category :" + category);
      }

    }
    PrimitiveObjectInspector primitiveObjectInspector =
        (PrimitiveObjectInspector) oi;
    PrimitiveTypeInfo primitiveTypeInfo =
        primitiveObjectInspector.getTypeInfo();

    SqlTypeName sqlTypeName = HIVE_TYPE_2_CALCITE_SQL_TYPE_INFO.get(
        primitiveTypeInfo);
    //handle DecimalTypeInfo special case
    if (primitiveTypeInfo instanceof DecimalTypeInfo) {
      sqlTypeName = SqlTypeName.DECIMAL;
//      return factory.createTypeWithNullability(
//          factory.createSqlType(
//              sqlTypeName, ((DecimalTypeInfo) primitiveTypeInfo).getPrecision(),
//              ((DecimalTypeInfo) primitiveTypeInfo).getScale()),
//          true);
    }
    if (sqlTypeName == null) {
      throw new RuntimeException(
          "can't get sqlType for hive primitiveTypeInfo:" + primitiveTypeInfo);
    }
    return factory.createTypeWithNullability(
        factory.createSqlType(
            sqlTypeName),
        true);

  }
 
Example 13
Source File: UdfTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
  return typeFactory.createArrayType(
      typeFactory.createSqlType(SqlTypeName.INTEGER), -1);
}
 
Example 14
Source File: UdfTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
  return typeFactory.createArrayType(
      typeFactory.createSqlType(SqlTypeName.DOUBLE), -1);
}
 
Example 15
Source File: MockSqlOperatorTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory =
      opBinding.getTypeFactory();
  return typeFactory.createArrayType(typeFactory.createSqlType(SqlTypeName.VARCHAR), -1);
}