You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "dylanhz (via GitHub)" <gi...@apache.org> on 2024/08/19 09:00:47 UTC

[PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

dylanhz opened a new pull request, #25216:
URL: https://github.com/apache/flink/pull/25216

   ## What is the purpose of the change
   
   Add the built-in function TO_NUMBER.
   Examples:
   ```SQL
   -- The format expects:
   --  * an optional sign at the beginning,
   --  * followed by a dollar sign,
   --  * followed by a number between 3 and 6 digits long,
   --  * thousands separators,
   --  * up to two dight beyond the decimal point.
   > SELECT TO_NUMBER('-$12,345.67', 'S$999,099.99');
    -12345.67
   
   -- Plus is optional, and so are fractional digits.
   > SELECT TO_NUMBER('$345', 'S$999,099.99');
    345.00
   
   -- The format requires at least three digits.
   > SELECT TO_NUMBER('$45', 'S$999,099.99');
    NULL
   
   -- The format requires at least three digits
   > SELECT TO_NUMBER('$045', 'S$999,099.99');
    45.00
   
   -- Using brackets to denote negative values
   > SELECT TO_NUMBER('<1234>', '999999PR');
    -1234
   ```
   
   ## Brief change log
   
   - [FLINK-36025](https://issues.apache.org/jira/browse/FLINK-36025)
   - Add `ToNumberTypeStrategy`
   - Add `NumberParser`
   - Fix doc issue of function PRINTF
   
   ## Verifying this change
   
   - `StringFunctionsITCase#toNumberTestCases`
   - `TypeStrategiesTest#testData`
   - `NumberParserTest`
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (yes)
     - If yes, how is the feature documented? (docs)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25216:
URL: https://github.com/apache/flink/pull/25216#issuecomment-2861919708

   This PR has been closed since it has not had any activity in 120 days. 
   If you feel like this was a mistake, or you would like to continue working on it, 
   please feel free to re-open the PR and ask for a review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #25216: [FLINK-36025][table] Add the built-in function TO_NUMBER
URL: https://github.com/apache/flink/pull/25216


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "superdiaodiao (via GitHub)" <gi...@apache.org>.
superdiaodiao commented on code in PR #25216:
URL: https://github.com/apache/flink/pull/25216#discussion_r1724604184


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ToNumberFunction.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+import org.apache.flink.table.utils.NumberParser;
+import org.apache.flink.table.utils.ThreadLocalCache;
+
+import javax.annotation.Nullable;
+
+import java.util.Optional;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#TO_NUMBER}. */
+@Internal
+public class ToNumberFunction extends BuiltInScalarFunction {
+
+    private static final ThreadLocalCache<String, Optional<NumberParser>> NUMBER_PARSER_CACHE =
+            new ThreadLocalCache<String, Optional<NumberParser>>() {
+                @Override
+                public Optional<NumberParser> getNewInstance(String key) {
+                    return NumberParser.of(key);
+                }
+            };
+
+    public ToNumberFunction(SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.TO_NUMBER, context);
+    }
+
+    public @Nullable DecimalData eval(@Nullable StringData expr, StringData format) {
+        if (expr == null) {
+            return null;
+        }
+        Optional<NumberParser> parser = NUMBER_PARSER_CACHE.get(format.toString());
+        return parser.map(numberParser -> numberParser.parse(expr)).orElse(null);
+    }

Review Comment:
   What if the `format` equals to empty string(`' '`)? Since the test cases haven't include it either.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #25216:
URL: https://github.com/apache/flink/pull/25216#issuecomment-2782155194

   This PR is being marked as stale since it has not had any activity in the last 90 days. 
   If you would like to keep this PR alive, please leave a comment asking for a review. 
   If the PR has merge conflicts, update it with the latest from the base branch.
   
   If you are having difficulty finding a reviewer, please reach out to the 
   community, contact details can be found here: https://flink.apache.org/what-is-flink/community/
   
   If this PR is no longer valid or desired, please feel free to close it. 
   If no activity occurs in the next 30 days, it will be automatically closed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "dylanhz (via GitHub)" <gi...@apache.org>.
dylanhz commented on code in PR #25216:
URL: https://github.com/apache/flink/pull/25216#discussion_r1724736554


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ToNumberFunction.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+import org.apache.flink.table.utils.NumberParser;
+import org.apache.flink.table.utils.ThreadLocalCache;
+
+import javax.annotation.Nullable;
+
+import java.util.Optional;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#TO_NUMBER}. */
+@Internal
+public class ToNumberFunction extends BuiltInScalarFunction {
+
+    private static final ThreadLocalCache<String, Optional<NumberParser>> NUMBER_PARSER_CACHE =
+            new ThreadLocalCache<String, Optional<NumberParser>>() {
+                @Override
+                public Optional<NumberParser> getNewInstance(String key) {
+                    return NumberParser.of(key);
+                }
+            };
+
+    public ToNumberFunction(SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.TO_NUMBER, context);
+    }
+
+    public @Nullable DecimalData eval(@Nullable StringData expr, StringData format) {
+        if (expr == null) {
+            return null;
+        }
+        Optional<NumberParser> parser = NUMBER_PARSER_CACHE.get(format.toString());
+        return parser.map(numberParser -> numberParser.parse(expr)).orElse(null);
+    }

Review Comment:
   A format string of `" "` will fail the first checking in `isValidFormat()` which requests one `Token.DIGIT_GROUP` at least, so the validator will throw an error in compile phase because it can't infer an output type.
   The behavior is actually the same as format string `"$"`, but I'll add another case for this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "superdiaodiao (via GitHub)" <gi...@apache.org>.
superdiaodiao commented on code in PR #25216:
URL: https://github.com/apache/flink/pull/25216#discussion_r1724604184


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/scalar/ToNumberFunction.java:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.table.runtime.functions.scalar;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+import org.apache.flink.table.utils.NumberParser;
+import org.apache.flink.table.utils.ThreadLocalCache;
+
+import javax.annotation.Nullable;
+
+import java.util.Optional;
+
+/** Implementation of {@link BuiltInFunctionDefinitions#TO_NUMBER}. */
+@Internal
+public class ToNumberFunction extends BuiltInScalarFunction {
+
+    private static final ThreadLocalCache<String, Optional<NumberParser>> NUMBER_PARSER_CACHE =
+            new ThreadLocalCache<String, Optional<NumberParser>>() {
+                @Override
+                public Optional<NumberParser> getNewInstance(String key) {
+                    return NumberParser.of(key);
+                }
+            };
+
+    public ToNumberFunction(SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.TO_NUMBER, context);
+    }
+
+    public @Nullable DecimalData eval(@Nullable StringData expr, StringData format) {
+        if (expr == null) {
+            return null;
+        }
+        Optional<NumberParser> parser = NUMBER_PARSER_CACHE.get(format.toString());
+        return parser.map(numberParser -> numberParser.parse(expr)).orElse(null);
+    }

Review Comment:
   What if the `format` equals to empty string(`' '`)? Since the test cases haven't include it either, or did I miss?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [FLINK-36025][table] Add the built-in function TO_NUMBER [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #25216:
URL: https://github.com/apache/flink/pull/25216#issuecomment-2296050722

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "db36054ff1ca735e262487a9cf9803945250409a",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "db36054ff1ca735e262487a9cf9803945250409a",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * db36054ff1ca735e262487a9cf9803945250409a UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org