Java Code Examples for org.apache.flink.table.types.utils.TypeConversions#fromLogicalToDataType()

The following examples show how to use org.apache.flink.table.types.utils.TypeConversions#fromLogicalToDataType() . 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: ValuesOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private DataType findCommonTypeAtPosition(List<List<ResolvedExpression>> resolvedRows, int i) {
	List<LogicalType> typesAtIPosition = extractLogicalTypesAtPosition(resolvedRows, i);

	LogicalType logicalType = LogicalTypeMerging.findCommonType(typesAtIPosition)
		.orElseThrow(() -> {
			Set<DataType> columnTypes = resolvedRows.stream()
				.map(row -> row.get(i).getOutputDataType())
				.collect(Collectors.toCollection(LinkedHashSet::new));

			return new ValidationException(String.format(
				"Types in fromValues(...) must have a common super type. Could not find a common type" +
					" for all rows at column %d.\n" +
					"Could not find a common super type for types: %s",
				i,
				columnTypes));
		});

	return TypeConversions.fromLogicalToDataType(logicalType);
}
 
Example 2
Source File: OperatorBindingCallContext.java    From flink with Apache License 2.0 6 votes vote down vote up
public OperatorBindingCallContext(
		DataTypeFactory dataTypeFactory,
		FunctionDefinition definition,
		SqlOperatorBinding binding) {
	super(
		dataTypeFactory,
		definition,
		binding.getOperator().getNameAsId().toString());

	this.binding = binding;
	this.argumentDataTypes = new AbstractList<DataType>() {
		@Override
		public DataType get(int pos) {
			final LogicalType logicalType = FlinkTypeFactory.toLogicalType(binding.getOperandType(pos));
			return TypeConversions.fromLogicalToDataType(logicalType);
		}

		@Override
		public int size() {
			return binding.getOperandCount();
		}
	};
}
 
Example 3
Source File: MapInputTypeStrategy.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<List<DataType>> inferInputTypes(CallContext callContext, boolean throwOnFailure) {
	List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
	if (argumentDataTypes.size() == 0) {
		return Optional.empty();
	}

	List<LogicalType> keyTypes = new ArrayList<>();
	List<LogicalType> valueTypes = new ArrayList<>();

	for (int i = 0; i < argumentDataTypes.size(); i++) {
		LogicalType logicalType = argumentDataTypes.get(i).getLogicalType();
		if (i % 2 == 0) {
			keyTypes.add(logicalType);
		} else {
			valueTypes.add(logicalType);
		}
	}
	Optional<LogicalType> commonKeyType = LogicalTypeMerging.findCommonType(keyTypes);
	Optional<LogicalType> commonValueType = LogicalTypeMerging.findCommonType(valueTypes);

	if (!commonKeyType.isPresent() || !commonValueType.isPresent()) {
		return Optional.empty();
	}

	DataType keyType = TypeConversions.fromLogicalToDataType(commonKeyType.get());
	DataType valueType = TypeConversions.fromLogicalToDataType(commonValueType.get());
	return Optional.of(IntStream.range(0, argumentDataTypes.size())
		.mapToObj(idx -> {
			if (idx % 2 == 0) {
				return keyType;
			} else {
				return valueType;
			}
		})
		.collect(Collectors.toList()));
}
 
Example 4
Source File: OperationConverterUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static TableColumn toTableColumn(SqlTableColumn sqlTableColumn, SqlValidator sqlValidator) {
	String name = sqlTableColumn.getName().getSimple();
	SqlDataTypeSpec typeSpec = sqlTableColumn.getType();
	LogicalType logicalType = FlinkTypeFactory.toLogicalType(
			typeSpec.deriveType(sqlValidator, typeSpec.getNullable()));
	DataType dataType = TypeConversions.fromLogicalToDataType(logicalType);
	return TableColumn.of(name, dataType);
}
 
Example 5
Source File: CallBindingCallContext.java    From flink with Apache License 2.0 5 votes vote down vote up
public CallBindingCallContext(
		DataTypeFactory dataTypeFactory,
		FunctionDefinition definition,
		SqlCallBinding binding,
		@Nullable RelDataType outputType) {
	super(
		dataTypeFactory,
		definition,
		binding.getOperator().getNameAsId().toString());

	this.adaptedArguments = binding.operands(); // reorders the operands
	this.argumentDataTypes = new AbstractList<DataType>() {
		@Override
		public DataType get(int pos) {
			final RelDataType relDataType = binding.getValidator().deriveType(
				binding.getScope(),
				adaptedArguments.get(pos));
			final LogicalType logicalType = FlinkTypeFactory.toLogicalType(relDataType);
			return TypeConversions.fromLogicalToDataType(logicalType);
		}

		@Override
		public int size() {
			return binding.getOperandCount();
		}
	};
	this.outputType = convertOutputType(binding, outputType);
}
 
Example 6
Source File: CallBindingCallContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private static @Nullable DataType convertOutputType(SqlCallBinding binding, @Nullable RelDataType returnType) {
	if (returnType == null || returnType.equals(binding.getValidator().getUnknownType())) {
		return null;
	} else {
		final LogicalType logicalType = FlinkTypeFactory.toLogicalType(returnType);
		return TypeConversions.fromLogicalToDataType(logicalType);
	}
}
 
Example 7
Source File: ParserImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public ResolvedExpression parseSqlExpression(String sqlExpression, TableSchema inputSchema) {
	SqlExprToRexConverter sqlExprToRexConverter = sqlExprToRexConverterCreator.apply(inputSchema);
	RexNode rexNode = sqlExprToRexConverter.convertToRexNode(sqlExpression);
	LogicalType logicalType = FlinkTypeFactory.toLogicalType(rexNode.getType());
	return new RexNodeExpression(rexNode, TypeConversions.fromLogicalToDataType(logicalType));
}
 
Example 8
Source File: ArrayFieldReader.java    From flink with Apache License 2.0 5 votes vote down vote up
private Class<?> getElementClass(LogicalType elementType) {
	DataType dataType = TypeConversions.fromLogicalToDataType(elementType);
	if (elementType instanceof TimestampType) {
		// the default conversion class is java.time.LocalDateTime
		dataType = dataType.bridgedTo(Timestamp.class);
	} else if (elementType instanceof DateType) {
		// the default conversion class is java.time.LocalDate
		dataType = dataType.bridgedTo(Date.class);
	} else if (elementType instanceof TimeType) {
		// the default conversion class is java.time.LocalTime
		dataType = dataType.bridgedTo(Time.class);
	}
	return dataType.getConversionClass();
}
 
Example 9
Source File: ArrowSourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
	fieldTypes.add(new VarCharType());
	List<RowType.RowField> rowFields = new ArrayList<>();
	for (int i = 0; i < fieldTypes.size(); i++) {
		rowFields.add(new RowType.RowField("f" + i, fieldTypes.get(i)));
	}
	rowType = new RowType(rowFields);
	dataType = TypeConversions.fromLogicalToDataType(rowType);
	serializer = new RowDataSerializer(
		new ExecutionConfig(), fieldTypes.toArray(new LogicalType[0]));
	allocator = ArrowUtils.getRootAllocator().newChildAllocator("stdout", 0, Long.MAX_VALUE);
}
 
Example 10
Source File: RowArrowSourceFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() {
	fieldTypes.add(new VarCharType());
	List<RowType.RowField> rowFields = new ArrayList<>();
	for (int i = 0; i < fieldTypes.size(); i++) {
		rowFields.add(new RowType.RowField("f" + i, fieldTypes.get(i)));
	}
	rowType = new RowType(rowFields);
	dataType = TypeConversions.fromLogicalToDataType(rowType);
	allocator = ArrowUtils.getRootAllocator().newChildAllocator("stdout", 0, Long.MAX_VALUE);
}
 
Example 11
Source File: LogicalTypeDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static DataType fromLogicalTypeToDataType(LogicalType logicalType) {
	return TypeConversions.fromLogicalToDataType(logicalType);
}
 
Example 12
Source File: LogicalTypeDataTypeConverter.java    From flink with Apache License 2.0 4 votes vote down vote up
public static DataType fromLogicalTypeToDataType(LogicalType logicalType) {
	return TypeConversions.fromLogicalToDataType(logicalType);
}
 
Example 13
Source File: CatalogTableSchemaResolver.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve the computed column's type for the given schema.
 *
 * @param tableSchema Table schema to derive table field names and data types
 * @return the resolved TableSchema
 */
public TableSchema resolve(TableSchema tableSchema) {
	final String rowtime;
	if (!tableSchema.getWatermarkSpecs().isEmpty()) {
		// TODO: [FLINK-14473] we only support top-level rowtime attribute right now
		rowtime = tableSchema.getWatermarkSpecs().get(0).getRowtimeAttribute();
		if (rowtime.contains(".")) {
			throw new ValidationException(
					String.format("Nested field '%s' as rowtime attribute is not supported right now.", rowtime));
		}
	} else {
		rowtime = null;
	}

	String[] fieldNames = tableSchema.getFieldNames();
	DataType[] fieldTypes = tableSchema.getFieldDataTypes();

	TableSchema.Builder builder = TableSchema.builder();
	for (int i = 0; i < tableSchema.getFieldCount(); ++i) {
		TableColumn tableColumn = tableSchema.getTableColumns().get(i);
		DataType fieldType = fieldTypes[i];

		if (tableColumn.isGenerated()) {
			fieldType = resolveExpressionDataType(tableColumn.getExpr().get(), tableSchema);
			if (isProctime(fieldType)) {
				if (fieldNames[i].equals(rowtime)) {
					throw new TableException("Watermark can not be defined for a processing time attribute column.");
				}
			}
		}

		if (isStreamingMode && fieldNames[i].equals(rowtime)) {
			TimestampType originalType = (TimestampType) fieldType.getLogicalType();
			LogicalType rowtimeType = new TimestampType(
					originalType.isNullable(),
					TimestampKind.ROWTIME,
					originalType.getPrecision());
			fieldType = TypeConversions.fromLogicalToDataType(rowtimeType);
		}

		if (tableColumn.isGenerated()) {
			builder.field(fieldNames[i], fieldType, tableColumn.getExpr().get());
		} else {
			builder.field(fieldNames[i], fieldType);
		}
	}

	tableSchema.getWatermarkSpecs().forEach(builder::watermark);
	tableSchema.getPrimaryKey().ifPresent(
			pk -> builder.primaryKey(pk.getName(), pk.getColumns().toArray(new String[0])));
	return builder.build();
}