Search in sources :

Example 6 with DataTypeFactory

use of org.apache.flink.table.catalog.DataTypeFactory in project flink by apache.

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);
        }
    }
}
Also used : DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Expression(org.apache.flink.table.expressions.Expression) AtomicReference(java.util.concurrent.atomic.AtomicReference) MiniClusterResourceConfiguration(org.apache.flink.runtime.testutils.MiniClusterResourceConfiguration) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) FlinkAssertions.anyCauseMatches(org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches) Parameterized(org.junit.runners.Parameterized) Nullable(javax.annotation.Nullable) AbstractDataType(org.apache.flink.table.types.AbstractDataType) MiniClusterWithClientResource(org.apache.flink.test.util.MiniClusterWithClientResource) TableEnvironment(org.apache.flink.table.api.TableEnvironment) Iterator(java.util.Iterator) Parameter(org.junit.runners.Parameterized.Parameter) Configuration(org.apache.flink.configuration.Configuration) DataTypes(org.apache.flink.table.api.DataTypes) UserDefinedFunction(org.apache.flink.table.functions.UserDefinedFunction) Test(org.junit.Test) Table(org.apache.flink.table.api.Table) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) EnvironmentSettings(org.apache.flink.table.api.EnvironmentSettings) TableResult(org.apache.flink.table.api.TableResult) Row(org.apache.flink.types.Row) TableEnvironmentInternal(org.apache.flink.table.api.internal.TableEnvironmentInternal) Table(org.apache.flink.table.api.Table) TableEnvironment(org.apache.flink.table.api.TableEnvironment) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) DataTypes(org.apache.flink.table.api.DataTypes) Test(org.junit.Test)

Example 7 with DataTypeFactory

use of org.apache.flink.table.catalog.DataTypeFactory in project flink by apache.

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);
}
Also used : LogicalType(org.apache.flink.table.types.logical.LogicalType) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 8 with DataTypeFactory

use of org.apache.flink.table.catalog.DataTypeFactory in project flink-mirror by flink-ci.

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();
    }
}
Also used : PojoTypeInfo(org.apache.flink.api.java.typeutils.PojoTypeInfo) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) DataTypeQueryable(org.apache.flink.table.types.DataTypeQueryable) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) ExtractionUtils.getStructuredField(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredField) BasicArrayTypeInfo(org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo) StructuredType(org.apache.flink.table.types.logical.StructuredType) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) ExtractionUtils.extractAssigningConstructor(org.apache.flink.table.types.extraction.ExtractionUtils.extractAssigningConstructor) ObjectArrayTypeInfo(org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo) RowTypeInfo(org.apache.flink.api.java.typeutils.RowTypeInfo) RowType(org.apache.flink.table.types.logical.RowType) LinkedHashMap(java.util.LinkedHashMap) ListTypeInfo(org.apache.flink.api.java.typeutils.ListTypeInfo) BigDecimal(java.math.BigDecimal) RawType(org.apache.flink.table.types.logical.RawType) Map(java.util.Map) LocalTime(java.time.LocalTime) MapTypeInfo(org.apache.flink.api.java.typeutils.MapTypeInfo) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Nullable(javax.annotation.Nullable) Types(org.apache.flink.api.common.typeinfo.Types) GenericTypeInfo(org.apache.flink.api.java.typeutils.GenericTypeInfo) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) DataTypes(org.apache.flink.table.api.DataTypes) PojoField(org.apache.flink.api.java.typeutils.PojoField) Field(java.lang.reflect.Field) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) PrimitiveArrayTypeInfo(org.apache.flink.api.common.typeinfo.PrimitiveArrayTypeInfo) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) LocalDate(java.time.LocalDate) Internal(org.apache.flink.annotation.Internal) Row(org.apache.flink.types.Row) MultisetTypeInfo(org.apache.flink.api.java.typeutils.MultisetTypeInfo) ExtractionUtils(org.apache.flink.table.types.extraction.ExtractionUtils) TupleTypeInfoBase(org.apache.flink.api.java.typeutils.TupleTypeInfoBase) PojoTypeInfo(org.apache.flink.api.java.typeutils.PojoTypeInfo) LinkedHashMap(java.util.LinkedHashMap) ExtractionUtils.getStructuredField(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredField) PojoField(org.apache.flink.api.java.typeutils.PojoField) Field(java.lang.reflect.Field) DataType(org.apache.flink.table.types.DataType)

Example 9 with DataTypeFactory

use of org.apache.flink.table.catalog.DataTypeFactory in project flink-mirror by flink-ci.

the class SqlAggFunctionVisitor method createSqlAggFunction.

private SqlAggFunction createSqlAggFunction(CallExpression call) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // legacy
    if (definition instanceof AggregateFunctionDefinition) {
        return createLegacySqlAggregateFunction(call.getFunctionIdentifier().orElse(null), (AggregateFunctionDefinition) definition);
    } else if (definition instanceof TableAggregateFunctionDefinition) {
        return createLegacySqlTableAggregateFunction(call.getFunctionIdentifier().orElse(null), (TableAggregateFunctionDefinition) definition);
    }
    // new stack
    final DataTypeFactory dataTypeFactory = ShortcutUtils.unwrapContext(relBuilder).getCatalogManager().getDataTypeFactory();
    final TypeInference typeInference = definition.getTypeInference(dataTypeFactory);
    return BridgingSqlAggFunction.of(dataTypeFactory, ShortcutUtils.unwrapTypeFactory(relBuilder), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 10 with DataTypeFactory

use of org.apache.flink.table.catalog.DataTypeFactory in project flink-mirror by flink-ci.

the class StructuredObjectConverter method createOrError.

/**
 * Creates a {@link DataStructureConverter} for the given structured type.
 *
 * <p>Note: We do not perform validation if data type and structured type implementation match.
 * This must have been done earlier in the {@link DataTypeFactory}.
 */
@SuppressWarnings("RedundantCast")
private static StructuredObjectConverter<?> createOrError(DataType dataType) {
    final List<DataType> fields = dataType.getChildren();
    final DataStructureConverter<Object, Object>[] fieldConverters = fields.stream().map(dt -> (DataStructureConverter<Object, Object>) DataStructureConverters.getConverter(dt)).toArray(DataStructureConverter[]::new);
    final RowData.FieldGetter[] fieldGetters = IntStream.range(0, fields.size()).mapToObj(pos -> RowData.createFieldGetter(fields.get(pos).getLogicalType(), pos)).toArray(RowData.FieldGetter[]::new);
    final Class<?>[] fieldClasses = fields.stream().map(DataType::getConversionClass).toArray(Class[]::new);
    final StructuredType structuredType = (StructuredType) dataType.getLogicalType();
    final Class<?> implementationClass = structuredType.getImplementationClass().orElseThrow(IllegalStateException::new);
    final int uniqueClassId = nextUniqueClassId.getAndIncrement();
    final String converterName = String.format("%s$%s$Converter", implementationClass.getName().replace('.', '$'), uniqueClassId);
    final String converterCode = generateCode(converterName, implementationClass, getFieldNames(structuredType).toArray(new String[0]), fieldClasses);
    return new StructuredObjectConverter<>(fieldConverters, fieldGetters, converterName, converterCode);
}
Also used : DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) RowData(org.apache.flink.table.data.RowData) CompileUtils(org.apache.flink.table.runtime.generated.CompileUtils) ExtractionUtils.getStructuredField(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredField) LogicalTypeChecks.getFieldNames(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getFieldNames) ExtractionUtils.getStructuredFieldGetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldGetter) ExtractionUtils.isStructuredFieldDirectlyWritable(org.apache.flink.table.types.extraction.ExtractionUtils.isStructuredFieldDirectlyWritable) StructuredType(org.apache.flink.table.types.logical.StructuredType) TableException(org.apache.flink.table.api.TableException) Field(java.lang.reflect.Field) ExtractionUtils.hasInvokableConstructor(org.apache.flink.table.types.extraction.ExtractionUtils.hasInvokableConstructor) List(java.util.List) GenericRowData(org.apache.flink.table.data.GenericRowData) ExtractionUtils.getStructuredFieldSetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldSetter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExtractionUtils.isStructuredFieldDirectlyReadable(org.apache.flink.table.types.extraction.ExtractionUtils.isStructuredFieldDirectlyReadable) ExtractionUtils.primitiveToWrapper(org.apache.flink.table.types.extraction.ExtractionUtils.primitiveToWrapper) Internal(org.apache.flink.annotation.Internal) Method(java.lang.reflect.Method) ExtractionUtils.getStructuredFieldGetter(org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredFieldGetter) StructuredType(org.apache.flink.table.types.logical.StructuredType) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) DataType(org.apache.flink.table.types.DataType)

Aggregations

DataTypeFactory (org.apache.flink.table.catalog.DataTypeFactory)36 DataType (org.apache.flink.table.types.DataType)15 TypeInference (org.apache.flink.table.types.inference.TypeInference)12 List (java.util.List)9 IntStream (java.util.stream.IntStream)9 DataTypes (org.apache.flink.table.api.DataTypes)9 RowData (org.apache.flink.table.data.RowData)9 RowType (org.apache.flink.table.types.logical.RowType)9 Row (org.apache.flink.types.Row)9 Field (java.lang.reflect.Field)6 LocalDate (java.time.LocalDate)6 Collectors (java.util.stream.Collectors)5 Nullable (javax.annotation.Nullable)5 Internal (org.apache.flink.annotation.Internal)5 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)5 BridgingUtils.createSqlOperandTypeInference (org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlOperandTypeInference)5 BridgingUtils.createSqlReturnTypeInference (org.apache.flink.table.planner.functions.bridging.BridgingUtils.createSqlReturnTypeInference)5 ExtractionUtils.getStructuredField (org.apache.flink.table.types.extraction.ExtractionUtils.getStructuredField)5 LogicalType (org.apache.flink.table.types.logical.LogicalType)5 StructuredType (org.apache.flink.table.types.logical.StructuredType)5