org.apache.flink.table.api.ValidationException Java Examples

The following examples show how to use org.apache.flink.table.api.ValidationException. 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: DataType.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * This method should catch the most common errors. However, another validation is required in
 * deeper layers as we don't know whether the data type is used for input or output declaration.
 */
private static <C> Class<C> performEarlyClassValidation(
		LogicalType logicalType,
		Class<C> candidate) {

	if (candidate != null &&
			!logicalType.supportsInputConversion(candidate) &&
			!logicalType.supportsOutputConversion(candidate)) {
		throw new ValidationException(
			String.format(
				"Logical type '%s' does not support a conversion from or to class '%s'.",
				logicalType.asSummaryString(),
				candidate.getName()));
	}
	return candidate;
}
 
Example #2
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void registerTableSourceInternal(String name, TableSource<?> tableSource) {
	validateTableSource(tableSource);
	Optional<CatalogBaseTable> table = getCatalogTable(catalogManager.getBuiltInCatalogName(),
		catalogManager.getBuiltInDatabaseName(), name);

	if (table.isPresent()) {
		if (table.get() instanceof ConnectorCatalogTable<?, ?>) {
			ConnectorCatalogTable<?, ?> sourceSinkTable = (ConnectorCatalogTable<?, ?>) table.get();
			if (sourceSinkTable.getTableSource().isPresent()) {
				throw new ValidationException(String.format(
					"Table '%s' already exists. Please choose a different name.", name));
			} else {
				// wrapper contains only sink (not source)
				replaceTableInternal(
					name,
					ConnectorCatalogTable
						.sourceAndSink(tableSource, sourceSinkTable.getTableSink().get(), !IS_STREAM_TABLE));
			}
		} else {
			throw new ValidationException(String.format(
				"Table '%s' already exists. Please choose a different name.", name));
		}
	} else {
		registerTableInternal(name, ConnectorCatalogTable.source(tableSource, !IS_STREAM_TABLE));
	}
}
 
Example #3
Source File: AggregateOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private FieldReferenceExpression getValidatedTimeAttribute(GroupWindow window, ExpressionResolver resolver) {
	List<ResolvedExpression> timeFieldExprs = resolver.resolve(singletonList(window.getTimeField()));

	if (timeFieldExprs.size() != 1) {
		throw new ValidationException("A group window only supports a single time field column.");
	}

	Expression timeFieldExpr = timeFieldExprs.get(0);
	if (!(timeFieldExpr instanceof FieldReferenceExpression)) {
		throw new ValidationException("A group window expects a time attribute for grouping.");
	}

	FieldReferenceExpression timeField = (FieldReferenceExpression) timeFieldExpr;

	final LogicalType timeFieldType = timeField.getOutputDataType().getLogicalType();

	validateTimeAttributeType(timeFieldType);

	return timeField;
}
 
Example #4
Source File: DecimalType.java    From flink with Apache License 2.0 6 votes vote down vote up
public DecimalType(boolean isNullable, int precision, int scale) {
	super(isNullable, LogicalTypeRoot.DECIMAL);
	if (precision < MIN_PRECISION || precision > MAX_PRECISION) {
		throw new ValidationException(
			String.format(
				"Decimal precision must be between %d and %d (both inclusive).",
				MIN_PRECISION,
				MAX_PRECISION));
	}
	if (scale < MIN_SCALE || scale > precision) {
		throw new ValidationException(
			String.format(
				"Decimal scale must be between %d and the precision %d (both inclusive).",
				MIN_SCALE,
				precision));
	}
	this.precision = precision;
	this.scale = scale;
}
 
Example #5
Source File: FunctionCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
private void registerTemporarySystemFunction(
		String name,
		CatalogFunction function,
		boolean ignoreIfExists) {
	final String normalizedName = FunctionIdentifier.normalizeName(name);

	try {
		validateAndPrepareFunction(function);
	} catch (Throwable t) {
		throw new ValidationException(
			String.format(
				"Could not register temporary system function '%s' due to implementation errors.",
				name),
			t);
	}

	if (!tempSystemFunctions.containsKey(normalizedName)) {
		tempSystemFunctions.put(normalizedName, function);
	} else if (!ignoreIfExists) {
		throw new ValidationException(
			String.format(
				"Could not register temporary system function. A function named '%s' does already exist.",
				name));
	}
}
 
Example #6
Source File: FunctionITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidUseOfScalarFunction() {
	tEnv().executeSql("CREATE TABLE SinkTable(s STRING) WITH ('connector' = 'COLLECTION')");

	tEnv().createTemporarySystemFunction("PrimitiveScalarFunction", PrimitiveScalarFunction.class);
	try {
		tEnv().executeSql(
			"INSERT INTO SinkTable " +
			"SELECT * FROM TABLE(PrimitiveScalarFunction(1, 2, '3'))");
		fail();
	} catch (ValidationException e) {
		assertThat(
			e,
			hasMessage(
				containsString(
					"No match found for function signature PrimitiveScalarFunction(<NUMERIC>, <NUMERIC>, <CHARACTER>)")));
	}
}
 
Example #7
Source File: CsvRowFormatFactoryTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDisableQuoteCharacterException() {
	thrown.expect(ValidationException.class);
	thrown.expectMessage("Format cannot define a quote character and disabled quote character at the same time.");
	final Map<String, String> properties = new Csv()
		.schema(SCHEMA)
		.fieldDelimiter(';')
		.lineDelimiter("\r\n")
		.allowComments()
		.ignoreParseErrors()
		.arrayElementDelimiter("|")
		.escapeCharacter('\\')
		.nullLiteral("n/a")
		.quoteCharacter('#')
		.disableQuoteCharacter()
		.toProperties();

	TableFactoryService
		.find(SerializationSchemaFactory.class, properties)
		.createSerializationSchema(properties);
}
 
Example #8
Source File: YearMonthIntervalType.java    From flink with Apache License 2.0 6 votes vote down vote up
public YearMonthIntervalType(boolean isNullable, YearMonthResolution resolution, int yearPrecision) {
	super(isNullable, LogicalTypeRoot.INTERVAL_YEAR_MONTH);
	Preconditions.checkNotNull(resolution);
	if (resolution == YearMonthResolution.MONTH && yearPrecision != DEFAULT_PRECISION) {
		throw new ValidationException(
			String.format(
				"Year precision of sub-year intervals must be equal to the default precision %d.",
				DEFAULT_PRECISION));
	}
	if (yearPrecision < MIN_PRECISION || yearPrecision > MAX_PRECISION) {
		throw new ValidationException(
			String.format(
				"Year precision of year-month intervals must be between %d and %d (both inclusive).",
				MIN_PRECISION,
				MAX_PRECISION));
	}
	this.resolution = resolution;
	this.yearPrecision = yearPrecision;
}
 
Example #9
Source File: KafkaDynamicTableFactoryTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMissingStartupTimestamp() {
	// Construct table source using DDL and table source factory
	ObjectIdentifier objectIdentifier = ObjectIdentifier.of(
			"default",
			"default",
			"scanTable");
	final Map<String, String> modifiedOptions = getModifiedOptions(
			getFullSourceOptions(),
			options -> {
				options.put("scan.startup.mode", "timestamp");
			});
	CatalogTable catalogTable = createKafkaSourceCatalogTable(modifiedOptions);

	thrown.expect(ValidationException.class);
	thrown.expect(containsCause(new ValidationException("'scan.startup.timestamp-millis' "
			+ "is required in 'timestamp' startup mode but missing.")));
	FactoryUtil.createTableSource(null,
			objectIdentifier,
			catalogTable,
			new Configuration(),
			Thread.currentThread().getContextClassLoader());
}
 
Example #10
Source File: TypeInferenceUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
private static AdaptedCallContext inferInputTypes(
		TypeInference typeInference,
		CallContext callContext,
		@Nullable DataType outputType,
		boolean throwOnFailure) {

	final AdaptedCallContext adaptedCallContext = new AdaptedCallContext(callContext, outputType);

	// typed arguments have highest priority
	typeInference.getTypedArguments().ifPresent(adaptedCallContext::setExpectedArguments);

	final List<DataType> inferredDataTypes = typeInference.getInputTypeStrategy()
		.inferInputTypes(adaptedCallContext, throwOnFailure)
		.orElse(null);

	if (inferredDataTypes != null) {
		adaptedCallContext.setExpectedArguments(inferredDataTypes);
	} else if (throwOnFailure) {
		throw new ValidationException("Invalid input arguments.");
	}

	return adaptedCallContext;
}
 
Example #11
Source File: ElasticsearchUpsertTableSinkBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TableSink<Tuple2<Boolean, Row>> configure(String[] fieldNames, TypeInformation<?>[] fieldTypes) {
	if (!Arrays.equals(getFieldNames(), fieldNames) || !Arrays.equals(getFieldTypes(), fieldTypes)) {
		throw new ValidationException("Reconfiguration with different fields is not allowed. " +
			"Expected: " + Arrays.toString(getFieldNames()) + " / " + Arrays.toString(getFieldTypes()) + ". " +
			"But was: " + Arrays.toString(fieldNames) + " / " + Arrays.toString(fieldTypes));
	}
	return copy(
		isAppendOnly,
		schema,
		hosts,
		index,
		docType,
		keyDelimiter,
		keyNullLiteral,
		serializationSchema,
		contentType,
		failureHandler,
		sinkOptions,
		requestFactory);
}
 
Example #12
Source File: InputTypeStrategiesTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testStrategy() {
	if (testSpec.expectedSignature != null) {
		assertThat(
			generateSignature(),
			equalTo(testSpec.expectedSignature));
	}
	if (testSpec.expectedErrorMessage != null) {
		thrown.expect(ValidationException.class);
		thrown.expectCause(containsCause(new ValidationException(testSpec.expectedErrorMessage)));
	}
	for (List<DataType> actualArgumentTypes : testSpec.actualArgumentTypes) {
		TypeInferenceUtil.Result result = runTypeInference(actualArgumentTypes);
		if (testSpec.expectedArgumentTypes != null) {
			assertThat(result.getExpectedArgumentTypes(), equalTo(testSpec.expectedArgumentTypes));
		}
	}
}
 
Example #13
Source File: DescriptorProperties.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Validation for fixed indexed properties.
 *
 * <p>For example:
 *
 * <pre>
 *     schema.fields.0.type = INT, schema.fields.0.name = test
 *     schema.fields.1.type = LONG, schema.fields.1.name = test2
 * </pre>
 *
 * <p>The subKeyValidation map must define e.g. "type" and "name" and a validation logic for the given full key.
 */
public void validateFixedIndexedProperties(String key, boolean allowEmpty, Map<String, Consumer<String>> subKeyValidation) {
	// determine max index
	final int maxIndex = extractMaxIndex(key, "\\.(.*)");

	if (maxIndex < 0 && !allowEmpty) {
		throw new ValidationException("Property key '" + key + "' must not be empty.");
	}

	// validate
	for (int i = 0; i <= maxIndex; i++) {
		for (Map.Entry<String, Consumer<String>> subKey : subKeyValidation.entrySet()) {
			final String fullKey = key + '.' + i + '.' + subKey.getKey();
			if (properties.containsKey(fullKey)) {
				// run validation logic
				subKey.getValue().accept(fullKey);
			} else {
				throw new ValidationException("Required property key '" + fullKey + "' is missing.");
			}
		}
	}
}
 
Example #14
Source File: CalculatedTableFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private TableSchema adjustNames(
		TableSchema tableSchema,
		List<String> aliases,
		String functionName) {
	int aliasesSize = aliases.size();
	if (aliasesSize == 0) {
		return tableSchema;
	}

	int callArity = tableSchema.getFieldCount();
	if (callArity != aliasesSize) {
		throw new ValidationException(String.format(
			"List of column aliases must have same degree as table; " +
				"the returned table of function '%s' has " +
				"%d columns, whereas alias list has %d columns",
			functionName,
			callArity,
			aliasesSize));
	}

	return TableSchema.builder()
		.fields(aliases.toArray(new String[0]), tableSchema.getFieldDataTypes())
		.build();
}
 
Example #15
Source File: SqlCreateTableConverter.java    From flink with Apache License 2.0 6 votes vote down vote up
private CatalogTable lookupLikeSourceTable(SqlTableLike sqlTableLike) {
	UnresolvedIdentifier unresolvedIdentifier = UnresolvedIdentifier.of(sqlTableLike.getSourceTable()
		.toString());
	ObjectIdentifier identifier = catalogManager.qualifyIdentifier(unresolvedIdentifier);
	CatalogManager.TableLookupResult lookupResult = catalogManager.getTable(identifier)
		.orElseThrow(() -> new ValidationException(String.format(
			"Source table '%s' of the LIKE clause not found in the catalog, at %s",
			identifier,
			sqlTableLike.getSourceTable().getParserPosition())));
	if (!(lookupResult.getTable() instanceof CatalogTable)) {
		throw new ValidationException(String.format(
			"Source table '%s' of the LIKE clause can not be a VIEW, at %s",
			identifier,
			sqlTableLike.getSourceTable().getParserPosition()));
	}
	return (CatalogTable) lookupResult.getTable();
}
 
Example #16
Source File: FlinkPravegaTableFactoryTest.java    From flink-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * For sink, routingkey-field-name information is mandatory.
 */
@Test (expected = ValidationException.class)
public void testMissingRoutingKeyForWriter() {
    Pravega pravega = new Pravega();
    Stream stream = Stream.of(SCOPE, STREAM);

    pravega.tableSinkWriterBuilder()
            .forStream(stream)
            .withPravegaConfig(PRAVEGA_CONFIG);

    final TestTableDescriptor testDesc = new TestTableDescriptor(pravega)
            .withFormat(JSON)
            .withSchema(SCHEMA)
            .inAppendMode();

    final Map<String, String> propertiesMap = testDesc.toProperties();
    TableFactoryService.find(StreamTableSinkFactory.class, propertiesMap)
            .createStreamTableSink(propertiesMap);
    fail("routingKey field name validation failed");
}
 
Example #17
Source File: FactoryUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Discovers a {@link EncodingFormat} of the given type using the given option (if present) as factory
 * identifier.
 */
public <I, F extends EncodingFormatFactory<I>> Optional<EncodingFormat<I>> discoverOptionalEncodingFormat(
		Class<F> formatFactoryClass,
		ConfigOption<String> formatOption) {
	return discoverOptionalFormatFactory(formatFactoryClass, formatOption)
		.map(formatFactory -> {
			String formatPrefix = formatPrefix(formatFactory, formatOption);
			try {
				return formatFactory.createEncodingFormat(context, projectOptions(formatPrefix));
			} catch (Throwable t) {
				throw new ValidationException(
					String.format(
						"Error creating sink format '%s' in option space '%s'.",
						formatFactory.factoryIdentifier(),
						formatPrefix),
					t);
			}
		});
}
 
Example #18
Source File: AggregateOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private ResolvedExpression unwrapFromAlias(CallExpression call) {
	List<ResolvedExpression> children = call.getResolvedChildren();
	List<String> aliases = children.subList(1, children.size())
		.stream()
		.map(alias -> ExpressionUtils.extractValue(alias, String.class)
			.orElseThrow(() -> new ValidationException("Unexpected alias: " + alias)))
		.collect(toList());

	if (!isFunctionOfKind(children.get(0), TABLE_AGGREGATE)) {
		throw fail();
	}

	validateAlias(
		aliases,
		(TableAggregateFunctionDefinition) ((CallExpression) children.get(0)).getFunctionDefinition());
	alias = aliases;
	return children.get(0);
}
 
Example #19
Source File: AggregateOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private ResolvedGroupWindow validateAndCreateSessionWindow(
		SessionWithGapOnTimeWithAlias window,
		String windowName,
		FieldReferenceExpression timeField) {
	ValueLiteralExpression windowGap = getAsValueLiteral(
		window.getGap(),
		"A session window expects a gap value literal.");

	final LogicalType windowGapType = windowGap.getOutputDataType().getLogicalType();

	if (!hasRoot(windowGapType, INTERVAL_DAY_TIME)) {
		throw new ValidationException("A session window expects a gap literal of a day-time interval type.");
	}

	return ResolvedGroupWindow.sessionWindow(
		windowName,
		timeField,
		windowGap);
}
 
Example #20
Source File: OperationTreeBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Void visit(UnresolvedCallExpression call) {
	if (ApiExpressionUtils.isFunctionOfKind(call, FunctionKind.AGGREGATE)) {
		throw new ValidationException(exceptionMessage);
	}
	call.getChildren().forEach(expr -> expr.accept(this));
	return null;
}
 
Example #21
Source File: FactoryUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends DynamicTableFactory> T getDynamicTableFactory(
		Class<T> factoryClass,
		@Nullable Catalog catalog,
		DefaultDynamicTableContext context) {
	// catalog factory has highest precedence
	if (catalog != null) {
		final Factory factory = catalog.getFactory()
			.filter(f -> factoryClass.isAssignableFrom(f.getClass()))
			.orElse(null);
		if (factory != null) {
			return (T) factory;
		}
	}

	// fallback to factory discovery
	final String connectorOption = context.getCatalogTable()
		.getOptions()
		.get(CONNECTOR.key());
	if (connectorOption == null) {
		throw new ValidationException(
			String.format(
				"Table options do not contain an option key '%s' for discovering a connector.",
				CONNECTOR.key()));
	}
	try {
		return discoverFactory(context.getClassLoader(), factoryClass, connectorOption);
	} catch (ValidationException e) {
		throw new ValidationException(
			String.format(
				"Cannot discover a connector using option '%s'.",
				stringifyOption(CONNECTOR.key(), connectorOption)),
			e);
	}
}
 
Example #22
Source File: LookupCallResolver.java    From flink with Apache License 2.0 5 votes vote down vote up
public Expression visit(LookupCallExpression lookupCall) {
	final FunctionLookup.Result result = functionLookup.lookupFunction(lookupCall.getUnresolvedName())
		.orElseThrow(() -> new ValidationException("Undefined function: " + lookupCall.getUnresolvedName()));

	return new UnresolvedCallExpression(
		result.getObjectIdentifier(),
		result.getFunctionDefinition(),
		resolveChildren(lookupCall.getChildren()));
}
 
Example #23
Source File: TypeMappingUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void verifyTimeAttributeType(TableColumn logicalColumn, String rowtimeOrProctime) {
	if (!hasFamily(logicalColumn.getType().getLogicalType(), LogicalTypeFamily.TIMESTAMP)) {
		throw new ValidationException(String.format(
			"%s field '%s' has invalid type %s. %s attributes must be of a Timestamp family.",
			rowtimeOrProctime,
			logicalColumn.getName(),
			logicalColumn.getType(),
			rowtimeOrProctime));
	}
}
 
Example #24
Source File: FunctionServiceTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = ValidationException.class)
public void testErrorConstructorClass() {
	FunctionDescriptor descriptor = new FunctionDescriptor()
			.fromClass(new ClassInstance()
					.of(ErrorConstructorClass.class.getName())
					.parameterString("arg"));

	FunctionService.createFunction(descriptor);
}
 
Example #25
Source File: FieldInfoUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
private static UnresolvedReferenceExpression getChildAsReference(Expression expression) {
	Expression child = expression.getChildren().get(0);
	if (child instanceof UnresolvedReferenceExpression) {
		return (UnresolvedReferenceExpression) child;
	}

	throw new ValidationException("Field reference expression expected.");
}
 
Example #26
Source File: DataGenTableSourceFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
private DataGenerator createDataGenerator(String name, DataType type, ReadableConfig options) {
	String genType = options.get(
			key(FIELDS + "." + name + "." + KIND).stringType().defaultValue(RANDOM));
	switch (genType) {
		case RANDOM:
			return createRandomGenerator(name, type, options);
		case SEQUENCE:
			return createSequenceGenerator(name, type, options);
		default:
			throw new ValidationException("Unsupported generator type: " + genType);
	}
}
 
Example #27
Source File: DescriptorProperties.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a character value under the given key if it exists.
 */
public Optional<Character> getOptionalCharacter(String key) {
	return optionalGet(key).map((c) -> {
		if (c.length() != 1) {
			throw new ValidationException("The value of '" + key + "' must only contain one character.");
		}
		return c.charAt(0);
	});
}
 
Example #28
Source File: UserDefinedFunctionHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Check whether this is a Scala object. Using Scala objects can lead to concurrency issues,
 * e.g., due to a shared collector.
 */
private static void validateNotSingleton(Class<?> clazz) {
	if (Arrays.stream(clazz.getFields()).anyMatch(f -> f.getName().equals("MODULE$"))) {
		throw new ValidationException(String.format(
			"Function implemented by class %s is a Scala object. This is forbidden because of concurrency" +
				" problems when using them.", clazz.getName()));
	}
}
 
Example #29
Source File: TableSourceValidation.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void validateLogicalTypeEqualsPhysical(
		String fieldName,
		DataType logicalType,
		TableSource<?> tableSource) {
	ResolvedField resolvedField = resolveField(fieldName, tableSource);
	if (!resolvedField.getType().equals(logicalType)) {
		throw new ValidationException(String.format(
			"Type %s of table field '%s' does not " +
				"match with type '%s; of the field '%s' of the TableSource return type.",
			logicalType,
			resolvedField.getType(),
			fieldName,
			resolvedField.getType()));
	}
}
 
Example #30
Source File: ElasticsearchValidationUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the table does not have primary key defined on illegal types.
 * In Elasticsearch the primary key is used to calculate the Elasticsearch document id,
 * which is a string of up to 512 bytes. It cannot have whitespaces. As of now it is calculated
 * by concatenating the fields. Certain types do not have a good string representation to be used
 * in this scenario. The illegal types are mostly {@link LogicalTypeFamily#COLLECTION} types and
 * {@link LogicalTypeRoot#RAW} type.
 */
public static void validatePrimaryKey(TableSchema schema) {
	schema.getPrimaryKey().ifPresent(
		key -> {
			List<LogicalTypeRoot> illegalTypes = key.getColumns()
				.stream()
				.map(fieldName -> {
					LogicalType logicalType = schema.getFieldDataType(fieldName).get().getLogicalType();
					if (hasRoot(logicalType, LogicalTypeRoot.DISTINCT_TYPE)) {
						return ((DistinctType) logicalType).getSourceType().getTypeRoot();
					} else {
						return logicalType.getTypeRoot();
					}
				})
				.filter(ILLEGAL_PRIMARY_KEY_TYPES::contains)
				.collect(Collectors.toList());

			if (!illegalTypes.isEmpty()) {
				throw new ValidationException(
					String.format(
						"The table has a primary key on columns of illegal types: %s.\n" +
							" Elasticsearch sink does not support primary keys on columns of types: %s.",
						illegalTypes,
						ILLEGAL_PRIMARY_KEY_TYPES));
			}
		}
	);
}