You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by mliesenberg <gi...@git.apache.org> on 2015/09/06 22:23:15 UTC
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
GitHub user mliesenberg opened a pull request:
https://github.com/apache/flink/pull/1097
[FLINK-2017] Add predefined required parameters to ParameterTool
- adds Option class to represent required parameter
- adds RequiredParameter class to interact with ParameterTool to check and/or validate parameters
- adds corresponding unit tests
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/mliesenberg/flink feature/FLINK-2017
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/flink/pull/1097.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #1097
----
commit 81c2ad7f792acb7ed4e01057fc5e807f752f902e
Author: Martin Liesenberg <ma...@retresco.de>
Date: 2015-09-06T20:18:13Z
[FLINK-2017] Add predefined required parameters to ParameterTool
- adds Option class to represent required parameter
- adds RequiredParameter class to interact with ParameterTool to check and/or validate parameters
- adds corresponding unit tests
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-152144589
Ok, should I rebase on the current master and squash the commits into one before it will be merged?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157648517
thanks for the feedback.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158321550
No, I think once that's resolved, the PR is good to merge.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45178470
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/Option.java ---
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Internal representation of a parameter passed to a user defined function.
+ */
+public class Option {
+
+ private String longName;
+ private String shortName;
+
+ private String defaultValue;
+ private Set<String> choices;
+
+ private String helpText;
+ private OptionType type = OptionType.STRING;
+
+ public Option(String name) {
+ this.longName = name;
+ this.choices = new HashSet<>();
+ }
+
+ /**
+ * Define a alternative / short name of the Parameter.
+ *
+ * @param shortName - short version of the parameter name
+ * @return the updated Option
+ */
+ public Option alt(String shortName) {
--- End diff --
Okay, thank you for adding a comment.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-151791276
Thanks for the update!
Except for my comments, the only remaining question is whether to offer the `add(Option)` method or not, right?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157719256
Okay, I think its a good idea.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-151804263
Yes, that's right.
I'll address the comments tonight. If the `add(Option)` issue is blocking the merge, we can just drop the function it, if you want.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-155437914
Hi @rmetzger, do you want to give it another look and check if it does what you intended with FLINK-2017?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158761960
the build failure is due to a test in streaming, so most likely not related.
the test failure has been observed before and is captured in issue FLINK-2348
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157717077
I think it should not be too hard to collect all the issues in a list and then throw the exception in the end.
What do you mean by the optional list? Only listing those arguments which are still missing?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42943777
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
--- End diff --
sure, I can change that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39496768
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
The RequiredParameter tool should check for each Option that:
- the ParameterTool contains a value for either the full or short key (if short key is defined)
- the ParameterTool does not contain values for both full and short key (if short key is defined)
- if the value is set on the short key, it should be copied to the full key
The checkAndPopulate method should only add the default value to the full key
That way, the value can be retrieved using the full key.
What do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39933413
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
+ throw new RequiredParameterException("Required parameter " + o.getName() + " not present.");
+ }
+ }
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied. If not use the default values
+ * which have been supplied. If no default value is supplied for a missing parameter, an exception is thrown.
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void checkAndPopulate(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ String key = o.getName();
--- End diff --
Based on the discussion above, I left this as is.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43101847
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
--- End diff --
Sure, it's always good to have my eyes looking over code. Esp. for API issues.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39492448
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/Option.java ---
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Internal representation of a parameter passed to a user defined function.
+ */
+public class Option<T> {
+
+ private String longName;
+ private String shortName;
+
+ private T defaultValue;
+ private Set<T> choices;
+
+ private String helpText;
+
+ public Option(String name) {
+ this.longName = name;
+ this.choices = new HashSet<>();
+ }
+
+ /**
+ * Define a alternative / short name of the Parameter.
+ *
+ * @param shortName - short version of the parameter name
+ * @return the updated Option
+ */
+ public Option<T> alt(String shortName) {
+ this.shortName = shortName;
+ return this;
+ }
+
+ /**
+ * Define a default value for the option.
+ *
+ * Throws an exception if the list of possible values for the parameter is not empty and the default value passed
+ * is not in the list.
+ *
+ * @param defaultValue - the default value
+ * @return the updated Option
+ */
+ public Option<T> defaultValue(T defaultValue) {
+ if (this.choices.isEmpty()) {
+ return this.setDefaultValue(defaultValue);
+ } else {
+ if (this.choices.contains(defaultValue)) {
+ return this.setDefaultValue(defaultValue);
+ } else {
+ throw new IllegalArgumentException("defaultValue passed is not in the list of expected values.");
+ }
+ }
+ }
+
+ /**
+ * Restrict the list of possible values of the parameter.
+ *
+ * @param choices - the allowed values of the parameter.
+ * @return the updated Option
+ */
+ public Option<T> choices(T... choices) {
+ Collections.addAll(this.choices, choices);
--- End diff --
Add check that `defaultValue` (if already set) is in choices.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-140348478
Ok, I will add that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by uce <gi...@git.apache.org>.
Github user uce commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-138229357
Thanks for the PR! I am sure that the mentioned test failure is unrelated to your changes. If you like, you can create a new issue with the label test-instability for the failing test.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45172709
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * - does the value provided in the parameterTool adhere to the choices defined in the option
+ *
+ * For any check which is not passed, a RequiredParametersException is thrown
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void applyTo(ParameterTool parameterTool) throws RequiredParametersException {
--- End diff --
I think since the exception is an important part of the functionality, can you add a ` @throws` Javadoc comment to the method?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158121645
I think the test cases are not good enough.
I tried it out with this code again:
```java
RequiredParameters rq = new RequiredParameters();
rq.add("input");
rq.add(new Option("parallelism").alt("p").defaultValue("1").type(OptionType.INTEGER));
ParameterTool pt = ParameterTool.fromArgs(args);
rq.applyTo(pt);
```
and got the following error:
```
Exception in thread "main" org.apache.flink.api.java.utils.RequiredParametersException: Value null is not in the list of valid choices
at org.apache.flink.api.java.utils.RequiredParameters.applyTo(RequiredParameters.java:126)
at org.apache.flink.examples.java.wordcount.WordCount.main(WordCount.java:64)
```
Passing `--input abc` will result in `RequiredParametersException: Value abc is not in the list of valid choices`.
The output of the help method looks better now
```
Required Parameters:
--input
-p, --parallelism Set a parallelism for the job default: 1
--ouput This is my help
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-138189037
One of the builds failed because of a test failure in:
CoStreamCheckpointingITCase>StreamFaultToleranceTestBase.runCheckpointedProgram:105->postSubmit:126 Restore was never called on counting Map function.
I will check it out and update the pull request if necessary.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173689
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/OptionType.java ---
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.flink.api.java.utils;
+
+/**
+ * Types the parameters of UDFs can take.
--- End diff --
User Defined Function. As I understood it, the ParameterTool and the RequiredParameters are used primarily for that purpose.
If I recall correctly, the ticket which 2017 is related to, called it that. I can remove that if it is misleading.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158766866
yep, based on the discussion above with fabian the default value is set only on the long key.
I'll go ahead and set it on the short key as well, if nothing else is defined.
I think the issue above is something else though:
when a value is found for the short key, but not the long key, it is copied to the long key and removed from the short key - also based on my understanding of the discussion above. rereading it and seeing your use case, it is probably better to drop the removal from the short key.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/flink/pull/1097
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158345168
Oha. that is embarrassing, I thought I had it covered. I'll look into it. Sorry for the mess.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39495292
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
Both names full and short should be treated equally, no?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43100940
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
--- End diff --
`req.add("name").type(...).values(...)`
oh, somehow I managed to miss that one.
I will add a String based version, maybe we can get a third opinion on the removal? I'd be in favor of giving the user the option to directly add an `Option` as well.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42648726
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
--- End diff --
Why not changing the signature of this method to `public Option add(String name)`?
This would relieve the user to instantiate the Option object.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43236711
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * -
+ *
+ * For any check which is not passed, a RequiredParametersException is thrown
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void applyTo(ParameterTool parameterTool) throws RequiredParametersException {
+ for (Option o : data.values()) {
+ String key = o.getName();
+ String shortKey = o.getAlt();
+
+ boolean longKeyUndefined = keyIsUndefined(key, parameterTool.data);
+ boolean shortKeyUndefined = keyIsUndefined(shortKey, parameterTool.data);
+
+ // check that the parameterTool does not contain values for both full and short key
+ if (parameterTool.data.containsKey(key) && parameterTool.data.containsKey(shortKey)) {
+ throw new RequiredParametersException("Required parameter " + key +
+ " is defined twice: on short and long version.");
+ }
+
+ // if the shortKey is not undefined
+ if (!shortKeyUndefined && longKeyUndefined) {
+ parameterTool.data.put(key, parameterTool.data.get(shortKey));
+ parameterTool.data.remove(shortKey);
+ }
+
+ // key is defined and has value, now check if it adheres to the type specified.
+ if (o.hasType() && !o.isCastableToDefinedType(parameterTool.data.get(key))) {
--- End diff --
Shouldn't this check be moved after the `if (longkeyUndefined)` check?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42943773
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
--- End diff --
We could add an additional method, though then we do have an implicit coupling between that add method and the constructor of Option. If, and that is probably a big if, the constructor were to change, we would have to update the add method as well.
Only providing a String based version of the add method would lead to code like that:
```
Option option = req.add("name")
option.type(...).values(...)
```
which in my opinion is a little bit awkward compared to instantiation, setting the right type and values and then adding the object.
what do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45172895
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/Option.java ---
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Internal representation of a parameter passed to a user defined function.
+ */
+public class Option {
+
+ private String longName;
+ private String shortName;
+
+ private String defaultValue;
+ private Set<String> choices;
+
+ private String helpText;
+ private OptionType type = OptionType.STRING;
+
+ public Option(String name) {
+ this.longName = name;
+ this.choices = new HashSet<>();
+ }
+
+ /**
+ * Define a alternative / short name of the Parameter.
+ *
+ * @param shortName - short version of the parameter name
+ * @return the updated Option
+ */
+ public Option alt(String shortName) {
--- End diff --
Maybe we should add a comment here that only one alternative is allowed.
or we add it as a feature to allow multiple alternatives ;)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-142684451
The test failure seems unrelated to my changes. It's connected to the YARNSessionFIFOITCase test.
FLINK-2700 has been opened a couple of days ago with the same error.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mxm <gi...@git.apache.org>.
Github user mxm commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-152135619
I think it's fine to have an `add(Option)` method in addition to `add(String)`. There might be cases where you want to create the Options first and add them later on.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-140347951
If you add a type field (or enum) to the option, you can check if you can cast the string into the requested type. Since we would only support a fixed set of types (the primitives I listed above) this should be quite easy.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-151439931
I do see your point about the uselessness of only `check`, so I'll go ahead and implement `applyTo`.
I'll update the PR tonight.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39493200
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
Check alternative name as well?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by chiwanpark <gi...@git.apache.org>.
Github user chiwanpark commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r40297666
--- Diff: flink-java/src/test/java/org/apache/flink/api/java/utils/RequiredParameterTest.java ---
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for RequiredParameter class and its interactions with ParameterTool
+ */
+public class RequiredParameterTest {
+
+ @Test(expected = RequiredParameterException.class)
+ public void testCheckWithMissingParameterValues() throws RequiredParameterException {
+ ParameterTool parameter = ParameterTool.fromArgs(new String[]{"--berlin"});
+ RequiredParameter required = new RequiredParameter();
+ required.add(new Option("berlin"));
+ required.check(parameter);
+ }
+
+ @Test
+ public void testCheckWithValidParameters() throws RequiredParameterException {
+ ParameterTool parameter = ParameterTool.fromArgs(new String[]{"--berlin", "value"});
+ RequiredParameter required = new RequiredParameter();
+ required.add(new Option("berlin"));
+ required.check(parameter);
+ }
--- End diff --
Please use tabs as indent instead of spaces.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158939653
Thank you for addressing the issues so timely.
I'll merge the change now, thank you!
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157648501
I'll address the comments and rework the getHelp method to produce more useful output (and cover that with more tests).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157722421
cool. will update the PR today.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173470
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * - does the value provided in the parameterTool adhere to the choices defined in the option
+ *
+ * For any check which is not passed, a RequiredParametersException is thrown
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void applyTo(ParameterTool parameterTool) throws RequiredParametersException {
--- End diff --
sure.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157660361
Thank you for reacting so quickly to my feedback.
I'm sorry for the delays during the PR review.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39495024
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
That would only be necessary and applicable, if the check fails, right?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158164663
Oha. yes. Found the problem causing the exception, will fix it and make the tests more specific as to which exceptions they need to catch.
While I am at it, anything else which needs change or are we good to go?
I'll update the PR tomorrow.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-140347682
@fhueske Yes, I will add the checks.
Regarding the type checks: Does that refer to the interaction with the ParameterTool class? I thought about that as well, but there the values are stored as strings and only upon retrieval the expected types are used.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157719075
optional in the sense of, if the list is passed, then the missing arguments are added to the help text in an extra paragraph at the end.
realized probably via method overloading.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39492715
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
--- End diff --
Check overwriting of existing option with same name?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173702
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
--- End diff --
sure, will add that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-140343995
Thanks for the PR @mliesenberg!
Can you add the checks (and tests to verify the checks are working) that I mentioned in my comments?
The original JIRA issue also included type checks. I think we could restrict those to Java primitives (Integer, Long, Double, Float, Boolean).
I would be OK with adding this PR without type checks (and opening another JIRA for that) but it would be a cool feature if you'd like to add that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42650529
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParameterException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ String key = o.getName();
+ String shortKey = o.getAlt();
+
+ boolean longKeyUndefined = keyIsUndefined(key, parameterTool.data);
+ boolean shortKeyUndefined = keyIsUndefined(shortKey, parameterTool.data);
+ // check if the ParameterTool contains a value for either the full or short key (if short key is defined)
+ if (longKeyUndefined && shortKeyUndefined) {
+ throw new RequiredParameterException("Required parameter " + key + " is not present.");
+ }
+
+ // check if the ParameterTool does not contain values for both full and short key (if short key is defined)
+ if (parameterTool.data.containsKey(key) && parameterTool.data.containsKey(shortKey)) {
+ throw new RequiredParameterException("Required parameter " + key +
+ " is defined twice: on short and long version.");
+ }
+
+ if (!shortKeyUndefined) {
+ parameterTool.data.put(key, parameterTool.data.get(shortKey));
+ parameterTool.data.remove(shortKey);
+ }
+
+ // key is defined and has value, now check if it adheres to the type specified.
+ if (o.hasType() && !o.isCastableToDefinedType(parameterTool.data.get(key))) {
+ throw new RequiredParameterException("Parameter " + key + " cannot be cast to the specified type.");
+ }
+ }
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied. If not use the default values
+ * which have been supplied. If no default value is supplied for a missing parameter, an exception is thrown.
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void checkAndPopulate(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
--- End diff --
Shouldn't `checkAndPopulate` do what `check` is doing and a bit more (populating)?
Actually, I am a bit confused about the purpose of having two separate methods.
Is it possible to merge both into a single method and call it for example `applyTo`?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43241539
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * -
--- End diff --
good point. I'll update that.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39497481
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
Sounds good to me.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42993834
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) throws RequiredParameterException {
--- End diff --
You can also do:
`req.add("name").type(...).values(...)`
just like proposed in the FLINK-2017 and users do not need to care about the `Option` class which is very nice, IMO.
In some cases, alternatives can raise more questions and confusion than help. So I would rather remove `add(Option)` but that's just my opinion.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173590
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/Option.java ---
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Internal representation of a parameter passed to a user defined function.
+ */
+public class Option {
+
+ private String longName;
+ private String shortName;
+
+ private String defaultValue;
+ private Set<String> choices;
+
+ private String helpText;
+ private OptionType type = OptionType.STRING;
+
+ public Option(String name) {
+ this.longName = name;
+ this.choices = new HashSet<>();
+ }
+
+ /**
+ * Define a alternative / short name of the Parameter.
+ *
+ * @param shortName - short version of the parameter name
+ * @return the updated Option
+ */
+ public Option alt(String shortName) {
--- End diff --
I don't see a reason why more than one alternative should be helpful. I'll add a comment to point out that only one alternative is allowed.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158329310
Cool. Pushed my latest changes which fix the bug and add more test cases.
Btw., is there any way to see a test coverage report?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158784004
ok. done.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157713303
Sure, no problem.
One comment concerning the comment:
The `RequiredParametersException` could list the missing arguments.
At the moment the validation in `applyTo` fails with the first missing parameter. To list all missing arguments would require to change that, but it might be worth it.
What do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43241508
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * -
+ *
+ * For any check which is not passed, a RequiredParametersException is thrown
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void applyTo(ParameterTool parameterTool) throws RequiredParametersException {
+ for (Option o : data.values()) {
+ String key = o.getName();
+ String shortKey = o.getAlt();
+
+ boolean longKeyUndefined = keyIsUndefined(key, parameterTool.data);
+ boolean shortKeyUndefined = keyIsUndefined(shortKey, parameterTool.data);
+
+ // check that the parameterTool does not contain values for both full and short key
+ if (parameterTool.data.containsKey(key) && parameterTool.data.containsKey(shortKey)) {
+ throw new RequiredParametersException("Required parameter " + key +
+ " is defined twice: on short and long version.");
+ }
+
+ // if the shortKey is not undefined
+ if (!shortKeyUndefined && longKeyUndefined) {
+ parameterTool.data.put(key, parameterTool.data.get(shortKey));
+ parameterTool.data.remove(shortKey);
+ }
+
+ // key is defined and has value, now check if it adheres to the type specified.
+ if (o.hasType() && !o.isCastableToDefinedType(parameterTool.data.get(key))) {
--- End diff --
I'll look into it. Just to be sure, the comment refers to line 89?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45178597
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/OptionType.java ---
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.flink.api.java.utils;
+
+/**
+ * Types the parameters of UDFs can take.
--- End diff --
Its one of the use cases of the parameter tool, but I think most users use it just for parsing the input.
I think its misleading.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-152136905
Thanks @mxm for the feedback. Then let's keep this PR as it is :-)
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r42648906
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
--- End diff --
Shouldn't it be `RequiredParameters` (plural)?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-151142957
Going through FLINK-2017, I interpreted the semantics of `check` and `checkAndPopulate` as follows:
- `check` verifies that all required parameters are provided and valid (type, choice, etc.)
- `checkAndPopulate` copied the default values of the options into the `ParameterTool`
I think a method that is called `checkAndPopulate` should perform the same checks as `check` and do the *populate* in addition. Moreover, I do not see why `check` alone would be useful. Why would somebody define required parameters with default values without enforcing them, i.e., why you call `check` without `checkAndPopulate`.
If we have a single method, we could also give it a simpler name such as `applyTo`.
What do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39493290
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
+ throw new RequiredParameterException("Required parameter " + o.getName() + " not present.");
+ }
+ }
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied. If not use the default values
+ * which have been supplied. If no default value is supplied for a missing parameter, an exception is thrown.
+ *
+ * @param parameterTool - parameters supplied by the user.
+ */
+ public void checkAndPopulate(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ String key = o.getName();
--- End diff --
Check for alternative / short key as well
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158340342
I'm wondering whether you are trying to run the examples I'm posting here:
```java
RequiredParameters rq = new RequiredParameters();
rq.add("input");
rq.add(new Option("parallelism").alt("p").defaultValue("1").type(OptionType.INTEGER));
ParameterTool pt = ParameterTool.fromArgs(args);
rq.applyTo(pt);
```
with `--input abc`
leads to:
```
Exception in thread "main" org.apache.flink.api.java.utils.RequiredParametersException: Value for parameter parallelism cannot be cast to type INTEGER
at org.apache.flink.api.java.utils.RequiredParameters.applyTo(RequiredParameters.java:123)
at org.apache.flink.examples.java.wordcount.WordCount.main(WordCount.java:65)
```
I didn't provide any value for parallelism and I was expecting it to be set automatically to 1.
I would recommend you to at least add the examples I'm posting here as test cases.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-152147439
Yes, that would be nice. Thanks
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158775559
Oh, I see. I have to admit I didn't closely follow the discussion with Fabian.
Would be nice if you could change the behavior.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-157645907
Thank you for reviewing the pull request while I was away @fhueske.
I've just tried it out with this code:
```java
RequiredParameters rp = new RequiredParameters();
rp.add("input");
rp.add("output");
rp.add(new Option("parallelism").alt("p").help("Set the parallelism for all operators").type(OptionType.INTEGER));
ParameterTool pt = ParameterTool.fromArgs(args);
try {
rp.applyTo(pt);
} catch(RequiredParametersException rpe) {
System.out.println(rp.getHelp());
System.exit(1);
}
```
and the output is the following:
```
Required Parameter
name, short name, help text, default valueinput null
output null
parallelism p Set the parallelism for all operators
```
I think the `getHelp` method is not producing helpful output here.
How about something like:
```
Required Parameters:
--input
--output
-p, --parallelism Set the parallelism for all operators
```
The `RequiredParametersException` could list the missing arguments.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r43236812
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
+ if (!this.data.containsKey(name)) {
+ Option option = new Option(name);
+ this.data.put(name, option);
+ return option;
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + name);
+ }
+ }
+
+ public void add(Option option) throws RequiredParametersException {
+ if (!this.data.containsKey(option.getName())) {
+ this.data.put(option.getName(), option);
+ } else {
+ throw new RequiredParametersException("Option with passed key already exists. " + option.getName());
+ }
+ }
+
+ /**
+ * Check for all parameters in the parameterTool passed to the function:
+ * - has a value been passed
+ * - if not, does the parameter have an associated default value
+ * - does the type of the parameter match the one defined in RequiredParameters
+ * -
--- End diff --
It is not checked whether the parameter is within the choices list (if choices are defined).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-142857451
I'll soon review this pull request. Sorry for the delay.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158335983
Yes, in IntelliJ the button right to the Debug button will generate a test coverage report.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39496043
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) throws RequiredParameterException {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
--- End diff --
Mhm. Makes sense. :)
So in the end, the user can retrieve the parameter both with the short and the long name?
So it basically needs to be added twice if there is a short and a long name?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-158765143
I've tested the PR again.
I found a minor issue with the alternative arguments.
input: `--input abc --p 12`,
code:
```java
RequiredParameters rq = new RequiredParameters();
rq.add("input");
rq.add(new Option("parallelism").alt("p").defaultValue("1").type(OptionType.INTEGER));
ParameterTool pt = ParameterTool.fromArgs(args);
rq.applyTo(pt);
String in = pt.get("input");
System.out.println("In = "+in + " parallelism = "+pt.getInt("parallelism") + " p="+pt.getInt("p"));
System.exit(0);
```
error
```
Exception in thread "main" java.lang.RuntimeException: No data for required key 'p'
at org.apache.flink.api.java.utils.ParameterTool.getRequired(ParameterTool.java:236)
at org.apache.flink.api.java.utils.ParameterTool.getInt(ParameterTool.java:272)
at org.apache.flink.examples.java.wordcount.WordCount.main(WordCount.java:69)
```
I think we need to set the (default) value for the long and the short variant.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-138243384
OK, cool. I have created a separate ticket. https://issues.apache.org/jira/browse/FLINK-2628
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173029
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/OptionType.java ---
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.flink.api.java.utils;
+
+/**
+ * Types the parameters of UDFs can take.
--- End diff --
What do you mean by UDFs here?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-149961803
Looks like good progress! As pointed out in one of my comments, I think we should make the use a bit more intuitive. Having two methods which are named so similar but which do different things is not a good idea. IMO it would be better to have one method that applies (and checks) the required options to the parsed parameters.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r45173067
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameters.java ---
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameters {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameters() {
+ this.data = new HashMap<>();
+ }
+
+ public Option add(String name) throws RequiredParametersException {
--- End diff --
I think the two add methods are missing javadocs.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-151441146
Thanks!
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/1097#discussion_r39068191
--- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/RequiredParameter.java ---
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.java.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Facility to manage required parameters in user defined functions.
+ */
+public class RequiredParameter {
+
+ private static final String HELP_TEXT_PARAM_DELIMITER = "\t";
+ private static final String HELP_TEXT_LINE_DELIMITER = "\n";
+
+ private HashMap<String, Option> data;
+
+ public RequiredParameter() {
+ this.data = new HashMap<>();
+ }
+
+ public void add(Option option) {
+ this.data.put(option.getName(), option);
+ }
+
+ /**
+ * Check if all parameters defined as required have been supplied.
+ *
+ * @param parameterTool - parameters supplied by user.
+ */
+ public void check(ParameterTool parameterTool) {
+ for (Option o : data.values()) {
+ // if the parameter is not present or its value is undefined, throw a RuntimeException.
+ if (!parameterTool.data.containsKey(o.getName()) || keyIsUndefined(o.getName(), parameterTool.data)) {
+ throw new RuntimeException("Required parameter " + o.getName() + " not present.");
--- End diff --
I think it would be better to throw a checked exception here
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
[GitHub] flink pull request: [FLINK-2017] Add predefined required parameter...
Posted by mliesenberg <gi...@git.apache.org>.
Github user mliesenberg commented on the pull request:
https://github.com/apache/flink/pull/1097#issuecomment-150490748
I think that might just boil down to the question whether or not there is a common enough use case for one without the other.
If so, we could change it to one method for check and one for populate which would untangle the code a bit as well.
If there is no use case for one without the other, I'd agree to get rid of the check.
I am unfortunately a bit pressed for time at the moment, but hope to find time to address the other comments during the weekend and.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---