use of org.apache.flink.table.catalog.DataTypeFactory in project flink by splunk.
the class BridgingSqlFunction method of.
/**
* Creates an instance of a scalar or table function during translation.
*/
public static BridgingSqlFunction of(FlinkContext context, FlinkTypeFactory typeFactory, ContextResolvedFunction resolvedFunction) {
final DataTypeFactory dataTypeFactory = context.getCatalogManager().getDataTypeFactory();
final TypeInference typeInference = resolvedFunction.getDefinition().getTypeInference(dataTypeFactory);
return of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
}
use of org.apache.flink.table.catalog.DataTypeFactory in project flink by splunk.
the class CommonExecLookupJoin method createSyncLookupJoin.
private StreamOperatorFactory<RowData> createSyncLookupJoin(RelOptTable temporalTable, ExecNodeConfig config, Map<Integer, LookupJoinUtil.LookupKey> allLookupKeys, TableFunction<?> syncLookupFunction, RelBuilder relBuilder, RowType inputRowType, RowType tableSourceRowType, RowType resultRowType, boolean isLeftOuterJoin, boolean isObjectReuseEnabled) {
DataTypeFactory dataTypeFactory = ShortcutUtils.unwrapContext(relBuilder).getCatalogManager().getDataTypeFactory();
int[] orderedLookupKeys = LookupJoinUtil.getOrderedLookupKeys(allLookupKeys.keySet());
GeneratedFunction<FlatMapFunction<RowData, RowData>> generatedFetcher = LookupJoinCodeGenerator.generateSyncLookupFunction(config.getTableConfig(), dataTypeFactory, inputRowType, tableSourceRowType, resultRowType, allLookupKeys, orderedLookupKeys, syncLookupFunction, StringUtils.join(temporalTable.getQualifiedName(), "."), isObjectReuseEnabled);
RowType rightRowType = Optional.ofNullable(temporalTableOutputType).map(FlinkTypeFactory::toLogicalRowType).orElse(tableSourceRowType);
CodeGeneratorContext ctx = new CodeGeneratorContext(config.getTableConfig());
GeneratedCollector<TableFunctionCollector<RowData>> generatedCollector = LookupJoinCodeGenerator.generateCollector(ctx, inputRowType, rightRowType, resultRowType, JavaScalaConversionUtil.toScala(Optional.ofNullable(joinCondition)), JavaScalaConversionUtil.toScala(Optional.empty()), true);
ProcessFunction<RowData, RowData> processFunc;
if (existCalcOnTemporalTable) {
// a projection or filter after table source scan
GeneratedFunction<FlatMapFunction<RowData, RowData>> generatedCalc = LookupJoinCodeGenerator.generateCalcMapFunction(config.getTableConfig(), JavaScalaConversionUtil.toScala(projectionOnTemporalTable), filterOnTemporalTable, temporalTableOutputType, tableSourceRowType);
processFunc = new LookupJoinWithCalcRunner(generatedFetcher, generatedCalc, generatedCollector, isLeftOuterJoin, rightRowType.getFieldCount());
} else {
// right type is the same as table source row type, because no calc after temporal table
processFunc = new LookupJoinRunner(generatedFetcher, generatedCollector, isLeftOuterJoin, rightRowType.getFieldCount());
}
return SimpleOperatorFactory.of(new ProcessOperator<>(processFunc));
}
use of org.apache.flink.table.catalog.DataTypeFactory in project flink by splunk.
the class TypeInfoDataTypeConverter method convertToStructuredType.
private static DataType convertToStructuredType(DataTypeFactory dataTypeFactory, CompositeType<?> compositeType, boolean forceNullability) {
final int arity = compositeType.getArity();
final String[] fieldNames = compositeType.getFieldNames();
final Class<?> typeClass = compositeType.getTypeClass();
final Map<String, DataType> fieldDataTypes = new LinkedHashMap<>();
IntStream.range(0, arity).forEachOrdered(pos -> fieldDataTypes.put(fieldNames[pos], toDataType(dataTypeFactory, compositeType.getTypeAt(pos))));
final List<String> fieldNamesReordered;
final boolean isNullable;
// for POJOs and Avro records
if (compositeType instanceof PojoTypeInfo) {
final PojoTypeInfo<?> pojoTypeInfo = (PojoTypeInfo<?>) compositeType;
final List<Field> pojoFields = IntStream.range(0, arity).mapToObj(pojoTypeInfo::getPojoFieldAt).map(PojoField::getField).collect(Collectors.toList());
// POJO serializer supports top-level nulls
isNullable = true;
// based on type information all fields are boxed classes,
// therefore we need to check the reflective field for more details
fieldDataTypes.replaceAll((name, dataType) -> {
final Class<?> fieldClass = pojoFields.stream().filter(f -> f.getName().equals(name)).findFirst().orElseThrow(IllegalStateException::new).getType();
if (fieldClass.isPrimitive()) {
return dataType.notNull().bridgedTo(fieldClass);
}
// serializer supports nullable fields
return dataType.nullable();
});
// best effort extraction of the field order, if it fails we use the default order of
// PojoTypeInfo which is alphabetical
fieldNamesReordered = extractStructuredTypeFieldOrder(typeClass, pojoFields);
} else // for tuples and case classes
{
// serializers don't support top-level nulls
isNullable = forceNullability;
// based on type information all fields are boxed classes,
// but case classes might contain primitives
fieldDataTypes.replaceAll((name, dataType) -> {
try {
final Class<?> fieldClass = getStructuredField(typeClass, name).getType();
if (fieldClass.isPrimitive()) {
return dataType.notNull().bridgedTo(fieldClass);
}
} catch (Throwable t) {
// ignore extraction errors and keep the original conversion class
}
return dataType;
});
// field order from type information is correct
fieldNamesReordered = null;
}
final DataTypes.Field[] structuredFields;
if (fieldNamesReordered != null) {
structuredFields = fieldNamesReordered.stream().map(name -> DataTypes.FIELD(name, fieldDataTypes.get(name))).toArray(DataTypes.Field[]::new);
} else {
structuredFields = fieldDataTypes.entrySet().stream().map(e -> DataTypes.FIELD(e.getKey(), e.getValue())).toArray(DataTypes.Field[]::new);
}
final DataType structuredDataType = DataTypes.STRUCTURED(typeClass, structuredFields);
if (isNullable) {
return structuredDataType.nullable();
} else {
return structuredDataType.notNull();
}
}
use of org.apache.flink.table.catalog.DataTypeFactory in project flink by splunk.
the class RelDataTypeJsonSerializer method serialize.
@Override
public void serialize(RelDataType relDataType, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
final SerdeContext serdeContext = SerdeContext.get(serializerProvider);
final DataTypeFactory dataTypeFactory = serdeContext.getFlinkContext().getCatalogManager().getDataTypeFactory();
// Conversion to LogicalType also ensures that Calcite's type system is materialized
// so data types like DECIMAL will receive a concrete precision and scale (not unspecified
// anymore).
final LogicalType logicalType = LogicalRelDataTypeConverter.toLogicalType(relDataType, dataTypeFactory);
serializerProvider.defaultSerializeValue(logicalType, jsonGenerator);
}
use of org.apache.flink.table.catalog.DataTypeFactory in project flink by splunk.
the class BuiltInFunctionTestBase method testFunction.
@Test
public void testFunction() {
final TableEnvironment env = TableEnvironment.create(EnvironmentSettings.newInstance().build());
env.getConfig().addConfiguration(configuration());
testSpec.functions.forEach(f -> env.createTemporarySystemFunction(f.getSimpleName(), f));
final DataTypeFactory dataTypeFactory = ((TableEnvironmentInternal) env).getCatalogManager().getDataTypeFactory();
final Table inputTable;
if (testSpec.fieldDataTypes == null) {
inputTable = env.fromValues(Row.of(testSpec.fieldData));
} else {
final DataTypes.UnresolvedField[] fields = IntStream.range(0, testSpec.fieldDataTypes.length).mapToObj(i -> DataTypes.FIELD("f" + i, testSpec.fieldDataTypes[i])).toArray(DataTypes.UnresolvedField[]::new);
inputTable = env.fromValues(DataTypes.ROW(fields), Row.of(testSpec.fieldData));
}
for (TestItem testItem : testSpec.testItems) {
try {
if (testItem instanceof ResultTestItem<?>) {
testResult(dataTypeFactory, env, inputTable, (ResultTestItem<?>) testItem);
} else if (testItem instanceof ErrorTestItem<?>) {
testError(env, inputTable, (ErrorTestItem<?>) testItem);
}
} catch (Throwable t) {
throw new AssertionError("Failing test item: " + testItem, t);
}
}
}
Aggregations