You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/07/05 03:54:11 UTC

[GitHub] [beam] ZijieSong946 opened a new pull request #12174: [Work in Progress] Supporting NUMERIC type

ZijieSong946 opened a new pull request #12174:
URL: https://github.com/apache/beam/pull/12174


   Currently two test cases passed. (NUMERIC literal and NUMERIC add)
   However, It seems that ZetaSQL treat it the same as DOUBLE.
   
   cc: @robinyqiu 
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
    - [ ] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/#make-reviewers-job-easier).
   
   Post-Commit Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   
   Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
   --- | --- | --- | --- | --- | --- | ---
   Go | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
   Java | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [![Build Status](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/badge/icon)](https://builds.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
   Python | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python2/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python2/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python35/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python35/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Python2_PVR_Flink_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Python2_PVR_Flink_Cron/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python35_VR_Flink/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python35_VR_Flink/lastCompletedBuild/) | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
   XLang | --- | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
   
   Pre-Commit Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   
   --- |Java | Python | Go | Website
   --- | --- | --- | --- | ---
   Non-portable | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/)
   Portable | --- | [![Build Status](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/badge/icon)](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | ---
   
   See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
   


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454653663



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BeamBigQuerySqlDialect.java
##########
@@ -156,7 +156,11 @@ public void unparseCall(
         break;
       case OTHER_FUNCTION:
         String funName = call.getOperator().getName();
-        if (FUNCTIONS_USING_INTERVAL.contains(funName)) {
+        if (funName.equals("numeric_literal")) {

Review comment:
       Done. Thanks.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r455235874



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be

Review comment:
       > It might be worth writing up a mini design doc...
   
   That's a great idea!
   
   > I suspect this could also fix the Infinity and NaN issues...
   
   Yes, I agree. We have discussed that a bit offline and we believe this approach could fix that problem (and other similar problems, if any, due to different value representation between ZetaSQL and Calcite). I think Zijie will talk about that in more details in his design doc.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-659090461


   > Internally we got 398 tests passing.
   > 
   > Note that this PR turned on `LanguageFeature.FEATURE_NUMERIC_TYPE` so there are new tests enabled but are not currently supported.
   > 
   > I will merge this PR for now. @ZijieSong946 Please link to your mini design doc from comment once that is finished.
   
   Acknowledged.


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 edited a comment on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 edited a comment on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-659090461


   > Internally we got 398 tests passing.
   > 
   > Note that this PR turned on `LanguageFeature.FEATURE_NUMERIC_TYPE` so there are new tests enabled but are not currently supported.
   > 
   > I will merge this PR for now. @ZijieSong946 Please link to your mini design doc from comment once that is finished.
   
   Acknowledged. Thanks for testing.


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r455234860



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be
+        // unparsed to the string representation of the BigDecimal itself (e.g. "SELECT NUMERIC '0'"
+        // will be unparsed to "SELECT 0E-9"), and Calcite does not allow customize unparsing of
+        // SqlNumericLiteral. So we create a wrapper function here such that we can later recognize
+        // it and customize its unparsing in BeamBigQuerySqlDialect.
+        ret =
+            rexBuilder()
+                .makeCall(
+                    SqlOperators.createSimpleSqlFunction(
+                        "numeric_literal", ZetaSqlCalciteTranslationUtils.toCalciteTypeName(kind)),

Review comment:
       OK. Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-657806602


   Merge conflict solved.


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454653552



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.beam.sdk.extensions.sql.zetasql;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Utils to deal with ZetaSQL type generation. */
+@Internal
+public class ZetaSqlTypesUtils {

Review comment:
       I got it. Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r455233394



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be
+        // unparsed to the string representation of the BigDecimal itself (e.g. "SELECT NUMERIC '0'"
+        // will be unparsed to "SELECT 0E-9"), and Calcite does not allow customize unparsing of
+        // SqlNumericLiteral. So we create a wrapper function here such that we can later recognize
+        // it and customize its unparsing in BeamBigQuerySqlDialect.
+        ret =
+            rexBuilder()
+                .makeCall(
+                    SqlOperators.createSimpleSqlFunction(
+                        "numeric_literal", ZetaSqlCalciteTranslationUtils.toCalciteTypeName(kind)),

Review comment:
       Zijie, here you can refer to `NUMERIC_LITERAL_FUNCTION` (of course you need to make it public) you defined in the other file.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu merged pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu merged pull request #12174:
URL: https://github.com/apache/beam/pull/12174


   


----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454526705



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -808,9 +808,11 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
       case TYPE_NUMERIC:
-        // As ZetaSQL NUMERIC literal would be unparsed as DOUBLE through Calcite makeExactLiteral
-        // method, we design a function call specifically associate with ZetaSQL NUMERIC literal
-        // unparsing
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERICE type because later it will be

Review comment:
       Typo here `NUMERIC`

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.beam.sdk.extensions.sql.zetasql;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Utils to deal with ZetaSQL type generation. */
+@Internal
+public class ZetaSqlTypesUtils {
+
+  private ZetaSqlTypesUtils() {}
+
+  /**
+   * Creating a ZetaSQL NUMERIC value, which is java.math.BigDecimal with scale 9, from a string.

Review comment:
       I hope the comment to include some information that people cannot get from reading the function. For example, ZetaSQL NUMERIC type definition. How about we add:
   
   Create a ZetaSQL NUMERIC value represented as BigDecimal.
   
   ZetaSQL NUMERIC type is an exact numeric value with 38 digits of precision and 9 decimal digits of scale. Precision is the number of digits that the number contains. Scale is how many of these digits appear after the decimal point.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r453958885



##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/TestInput.java
##########
@@ -258,6 +259,14 @@
           .addRows(LocalTime.of(15, 30, 0), "s")
           .addRows(LocalTime.of(23, 35, 59), "s");
 
+  private static final Schema TABLE_WTH_NUMERIC_SCHEMA =
+      Schema.builder().addDecimalField("numeric_field").addStringField("str_field").build();
+  public static final TestBoundedTable TABLE_WITH_NUMERIC =
+      TestBoundedTable.of(TABLE_WTH_NUMERIC_SCHEMA)
+          .addRows(new BigDecimal("123.4567").setScale(9), "str1")

Review comment:
       `new BigDecimal("some string").setScale(9)` is used a lot of times in this file and the test file. I would create a utility function for better style.
   
   How about we create a new file `ZetaSqlTypesUtils.java` in /main and make a `BigDecimal bigDecimalAsNumeric(String s)`?

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,22 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // As ZetaSQL NUMERIC literal would be unparsed as DOUBLE through Calcite makeExactLiteral

Review comment:
       Technically speaking, it is not "unparsed as DOUBLE". Also, I would update the comment to be more descriptive, like:
   
   Cannot simply call makeExactLiteral() because later it will be unparsed to the string representation of the BigDecimal itself (e.g. "SELECT NUMERIC '0'" will be unparsed to "SELECT 0E-9"), and Calcite does not allow customize unparsing of SqlNumericLiteral. So we create a wrapper function here such that we can later recognize it and customize its unparsing in BeamBigQuerySqlDialect. 

##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlDialectSpecTest.java
##########
@@ -2174,6 +2175,246 @@ public void testZetaSQLNestedQueryFive() {
     pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
   }
 
+  /////////////////////////////////////////////////////////////////////////////
+  // NUMERIC type tests
+  /////////////////////////////////////////////////////////////////////////////
+
+  @Test
+  public void testNumericLiteral() {
+    String sql =
+        "SELECT NUMERIC '0', "
+            + "NUMERIC '123456', "
+            + "NUMERIC '-3.14', "
+            + "NUMERIC '-0.54321', "
+            + "NUMERIC '1.23456e05', "
+            + "NUMERIC '-9.876e-3', "
+            + "NUMERIC '-99999999999999999999999999999.999999999', "

Review comment:
       Add comment on these are the min/max values that can be represented as ZetaSQL NUMERIC?

##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlDialectSpecTest.java
##########
@@ -2174,6 +2175,246 @@ public void testZetaSQLNestedQueryFive() {
     pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
   }
 
+  /////////////////////////////////////////////////////////////////////////////
+  // NUMERIC type tests
+  /////////////////////////////////////////////////////////////////////////////
+
+  @Test
+  public void testNumericLiteral() {
+    String sql =
+        "SELECT NUMERIC '0', "
+            + "NUMERIC '123456', "
+            + "NUMERIC '-3.14', "
+            + "NUMERIC '-0.54321', "
+            + "NUMERIC '1.23456e05', "
+            + "NUMERIC '-9.876e-3', "
+            + "NUMERIC '-99999999999999999999999999999.999999999', "
+            + "NUMERIC '99999999999999999999999999999.999999999'";
+    ;
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .addDecimalField("f_numeric3")
+                        .addDecimalField("f_numeric4")
+                        .addDecimalField("f_numeric5")
+                        .addDecimalField("f_numeric6")
+                        .addDecimalField("f_numeric7")
+                        .addDecimalField("f_numeric8")
+                        .build())
+                .addValues(new BigDecimal("0").setScale(9))
+                .addValues(new BigDecimal("123456").setScale(9))
+                .addValues(new BigDecimal("-3.14").setScale(9))
+                .addValues(new BigDecimal("-0.54321").setScale(9))
+                .addValues(new BigDecimal("123456").setScale(9))
+                .addValues(new BigDecimal("-0.009876").setScale(9))
+                .addValues(new BigDecimal("-99999999999999999999999999999.999999999"))
+                .addValues(new BigDecimal("99999999999999999999999999999.999999999"))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testUnaryMinusNumeric() {
+    String sql = "SELECT - NUMERIC '1.23456e05'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("-123456").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testAddNumeric() {
+    String sql = "SELECT NUMERIC '1.23456e05' + NUMERIC '9.876e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123456.009876").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testSubNumeric() {
+    String sql = "SELECT NUMERIC '1.23456e05' - NUMERIC '-9.876e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123456.009876").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testMultiNumeric() {
+    String sql = "SELECT NUMERIC '1.23e02' * NUMERIC '-1.001e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("-0.123123").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testDivNumeric() {
+    String sql = "SELECT NUMERIC '-1.23123e-1' / NUMERIC '-1.001e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testModNumeric() {
+    String sql = "SELECT MOD(NUMERIC '1.23456e05', NUMERIC '5')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("1").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testFloorNumeric() {
+    String sql = "SELECT FLOOR(NUMERIC '1.23456e04'), FLOOR(NUMERIC '-1.23456e04')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .build())
+                .addValues(new BigDecimal("12345").setScale(9))
+                .addValues(new BigDecimal("-12346").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testCeilNumeric() {
+    String sql = "SELECT CEIL(NUMERIC '1.23456e04'), CEIL(NUMERIC '-1.23456e04')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .build())
+                .addValues(new BigDecimal("12346").setScale(9))
+                .addValues(new BigDecimal("-12345").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testNumericColumn() {

Review comment:
       Nit: move this below `testNumericLiteral()` as we did in other places?




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-656913542


   You will need to rebase this PR on the newest upstream/master.


----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r455231610



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.beam.sdk.extensions.sql.zetasql;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Utils to deal with ZetaSQL type generation. */
+@Internal
+public class ZetaSqlTypesUtils {

Review comment:
       I was thinking of moving some other util functions to this file later. But for now it is only used for test, so I am fine with it in test.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-659049262


   Internally we got 398 tests passing.
   
   Note that this PR turned on `LanguageFeature.FEATURE_NUMERIC_TYPE` so there are new tests enabled but are not currently supported.
   
   I will merge this PR for now. @ZijieSong946 Please link to your mini design doc from comment once that is finished.


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454663699



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be

Review comment:
       Make sense. Maybe a design doc is needed for detailed illustration.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454555513



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.beam.sdk.extensions.sql.zetasql;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Utils to deal with ZetaSQL type generation. */
+@Internal
+public class ZetaSqlTypesUtils {
+
+  private ZetaSqlTypesUtils() {}
+
+  /**
+   * Creating a ZetaSQL NUMERIC value, which is java.math.BigDecimal with scale 9, from a string.

Review comment:
       Make sense. Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454079645



##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/TestInput.java
##########
@@ -258,6 +259,14 @@
           .addRows(LocalTime.of(15, 30, 0), "s")
           .addRows(LocalTime.of(23, 35, 59), "s");
 
+  private static final Schema TABLE_WTH_NUMERIC_SCHEMA =
+      Schema.builder().addDecimalField("numeric_field").addStringField("str_field").build();
+  public static final TestBoundedTable TABLE_WITH_NUMERIC =
+      TestBoundedTable.of(TABLE_WTH_NUMERIC_SCHEMA)
+          .addRows(new BigDecimal("123.4567").setScale(9), "str1")

Review comment:
       Seems a great ideal. Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 edited a comment on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 edited a comment on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-675155207


   The detailed designs for overcoming the inconsistency between Calcite and ZetaSQL NUMERIC literal representation:
   https://docs.google.com/document/d/1veKaDTMttU2q2Izss--Y5Cagg2JUSEA1dS3NVxYjnYg/edit?usp=sharing


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454053171



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BeamBigQuerySqlDialect.java
##########
@@ -156,7 +156,14 @@ public void unparseCall(
         break;
       case OTHER_FUNCTION:
         String funName = call.getOperator().getName();
-        if (FUNCTIONS_USING_INTERVAL.contains(funName)) {
+        if (funName.equals("numeric_literal")) {
+          // self-designed function

Review comment:
       Done. Thanks for your opinion.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454663699



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be

Review comment:
       Make sense. Maybe a design doc is needed for detailed illustration. And Now I am working on 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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454554801



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -808,9 +808,11 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
       case TYPE_NUMERIC:
-        // As ZetaSQL NUMERIC literal would be unparsed as DOUBLE through Calcite makeExactLiteral
-        // method, we design a function call specifically associate with ZetaSQL NUMERIC literal
-        // unparsing
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERICE type because later it will be

Review comment:
       Fixed. Thanks.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on pull request #12174:
URL: https://github.com/apache/beam/pull/12174#issuecomment-675155207


   The detailed designs for overcoming the inconsistency between Calcite and ZetaSQL NUMERIC literal representation:
   https://docs.google.com/document/d/1veKaDTMttU2q2Izss--Y5Cagg2JUSEA1dS3NVxYjnYg/edit#


----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454044979



##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlDialectSpecTest.java
##########
@@ -2174,6 +2175,246 @@ public void testZetaSQLNestedQueryFive() {
     pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
   }
 
+  /////////////////////////////////////////////////////////////////////////////
+  // NUMERIC type tests
+  /////////////////////////////////////////////////////////////////////////////
+
+  @Test
+  public void testNumericLiteral() {
+    String sql =
+        "SELECT NUMERIC '0', "
+            + "NUMERIC '123456', "
+            + "NUMERIC '-3.14', "
+            + "NUMERIC '-0.54321', "
+            + "NUMERIC '1.23456e05', "
+            + "NUMERIC '-9.876e-3', "
+            + "NUMERIC '-99999999999999999999999999999.999999999', "

Review comment:
       OK. Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
robinyqiu commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r453995996



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BeamBigQuerySqlDialect.java
##########
@@ -156,7 +156,14 @@ public void unparseCall(
         break;
       case OTHER_FUNCTION:
         String funName = call.getOperator().getName();
-        if (FUNCTIONS_USING_INTERVAL.contains(funName)) {
+        if (funName.equals("numeric_literal")) {
+          // self-designed function

Review comment:
       Also, could you move this logic to a function `unparseNumericLiteralWrapperFunction()` like we do in other branches?




----------------------------------------------------------------
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.

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



[GitHub] [beam] apilloud commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
apilloud commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454615048



##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BeamBigQuerySqlDialect.java
##########
@@ -156,7 +156,11 @@ public void unparseCall(
         break;
       case OTHER_FUNCTION:
         String funName = call.getOperator().getName();
-        if (FUNCTIONS_USING_INTERVAL.contains(funName)) {
+        if (funName.equals("numeric_literal")) {

Review comment:
       Reverse the order here (`"numeric_literal".equals(funName)`) to avoid potential issues if funName is null. Also consider making "numeric_literal" a constant like the others.

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlTypesUtils.java
##########
@@ -0,0 +1,42 @@
+/*
+ * 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.beam.sdk.extensions.sql.zetasql;
+
+import java.math.BigDecimal;
+import org.apache.beam.sdk.annotations.Internal;
+
+/** Utils to deal with ZetaSQL type generation. */
+@Internal
+public class ZetaSqlTypesUtils {

Review comment:
       This class looks to be for tests only. Move it to '/src/test/' instead of '/src/main/'?

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be
+        // unparsed to the string representation of the BigDecimal itself (e.g. "SELECT NUMERIC '0'"
+        // will be unparsed to "SELECT 0E-9"), and Calcite does not allow customize unparsing of
+        // SqlNumericLiteral. So we create a wrapper function here such that we can later recognize
+        // it and customize its unparsing in BeamBigQuerySqlDialect.
+        ret =
+            rexBuilder()
+                .makeCall(
+                    SqlOperators.createSimpleSqlFunction(
+                        "numeric_literal", ZetaSqlCalciteTranslationUtils.toCalciteTypeName(kind)),

Review comment:
       I've seen this constant `numeric_literal` before somewhere...

##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,24 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // Cannot simply call makeExactLiteral() for ZetaSQL NUMERIC type because later it will be

Review comment:
       This is a somewhat interesting approach. It might be worth writing up a mini design doc on why this approach was chosen and literals didn't work (but ask @robinyqiu first). I suspect this could also fix the Infinity and NaN issues I told you about (which are actually in Double, not Numeric BEAM-8057).




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454044618



##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/ZetaSqlDialectSpecTest.java
##########
@@ -2174,6 +2175,246 @@ public void testZetaSQLNestedQueryFive() {
     pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
   }
 
+  /////////////////////////////////////////////////////////////////////////////
+  // NUMERIC type tests
+  /////////////////////////////////////////////////////////////////////////////
+
+  @Test
+  public void testNumericLiteral() {
+    String sql =
+        "SELECT NUMERIC '0', "
+            + "NUMERIC '123456', "
+            + "NUMERIC '-3.14', "
+            + "NUMERIC '-0.54321', "
+            + "NUMERIC '1.23456e05', "
+            + "NUMERIC '-9.876e-3', "
+            + "NUMERIC '-99999999999999999999999999999.999999999', "
+            + "NUMERIC '99999999999999999999999999999.999999999'";
+    ;
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .addDecimalField("f_numeric3")
+                        .addDecimalField("f_numeric4")
+                        .addDecimalField("f_numeric5")
+                        .addDecimalField("f_numeric6")
+                        .addDecimalField("f_numeric7")
+                        .addDecimalField("f_numeric8")
+                        .build())
+                .addValues(new BigDecimal("0").setScale(9))
+                .addValues(new BigDecimal("123456").setScale(9))
+                .addValues(new BigDecimal("-3.14").setScale(9))
+                .addValues(new BigDecimal("-0.54321").setScale(9))
+                .addValues(new BigDecimal("123456").setScale(9))
+                .addValues(new BigDecimal("-0.009876").setScale(9))
+                .addValues(new BigDecimal("-99999999999999999999999999999.999999999"))
+                .addValues(new BigDecimal("99999999999999999999999999999.999999999"))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testUnaryMinusNumeric() {
+    String sql = "SELECT - NUMERIC '1.23456e05'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("-123456").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testAddNumeric() {
+    String sql = "SELECT NUMERIC '1.23456e05' + NUMERIC '9.876e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123456.009876").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testSubNumeric() {
+    String sql = "SELECT NUMERIC '1.23456e05' - NUMERIC '-9.876e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123456.009876").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testMultiNumeric() {
+    String sql = "SELECT NUMERIC '1.23e02' * NUMERIC '-1.001e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("-0.123123").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testDivNumeric() {
+    String sql = "SELECT NUMERIC '-1.23123e-1' / NUMERIC '-1.001e-3'";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("123").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testModNumeric() {
+    String sql = "SELECT MOD(NUMERIC '1.23456e05', NUMERIC '5')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(Schema.builder().addDecimalField("f_numeric").build())
+                .addValues(new BigDecimal("1").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testFloorNumeric() {
+    String sql = "SELECT FLOOR(NUMERIC '1.23456e04'), FLOOR(NUMERIC '-1.23456e04')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .build())
+                .addValues(new BigDecimal("12345").setScale(9))
+                .addValues(new BigDecimal("-12346").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testCeilNumeric() {
+    String sql = "SELECT CEIL(NUMERIC '1.23456e04'), CEIL(NUMERIC '-1.23456e04')";
+
+    ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config);
+    BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql);
+    PCollection<Row> stream = BeamSqlRelUtils.toPCollection(pipeline, beamRelNode);
+
+    PAssert.that(stream)
+        .containsInAnyOrder(
+            Row.withSchema(
+                    Schema.builder()
+                        .addDecimalField("f_numeric1")
+                        .addDecimalField("f_numeric2")
+                        .build())
+                .addValues(new BigDecimal("12346").setScale(9))
+                .addValues(new BigDecimal("-12345").setScale(9))
+                .build());
+    pipeline.run().waitUntilFinish(Duration.standardMinutes(PIPELINE_EXECUTION_WAITTIME_MINUTES));
+  }
+
+  @Test
+  public void testNumericColumn() {

Review comment:
       Done.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454045168



##########
File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/ExpressionConverter.java
##########
@@ -805,6 +807,22 @@ private RexNode convertSimpleValueToRexNode(TypeKind kind, Value value) {
                 .makeLiteral(
                     value.getStringValue(), typeFactory().createSqlType(SqlTypeName.VARCHAR), true);
         break;
+      case TYPE_NUMERIC:
+        // As ZetaSQL NUMERIC literal would be unparsed as DOUBLE through Calcite makeExactLiteral

Review comment:
       OK. That's make sense.




----------------------------------------------------------------
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.

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



[GitHub] [beam] ZijieSong946 commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL

Posted by GitBox <gi...@apache.org>.
ZijieSong946 commented on a change in pull request #12174:
URL: https://github.com/apache/beam/pull/12174#discussion_r454079645



##########
File path: sdks/java/extensions/sql/zetasql/src/test/java/org/apache/beam/sdk/extensions/sql/zetasql/TestInput.java
##########
@@ -258,6 +259,14 @@
           .addRows(LocalTime.of(15, 30, 0), "s")
           .addRows(LocalTime.of(23, 35, 59), "s");
 
+  private static final Schema TABLE_WTH_NUMERIC_SCHEMA =
+      Schema.builder().addDecimalField("numeric_field").addStringField("str_field").build();
+  public static final TestBoundedTable TABLE_WITH_NUMERIC =
+      TestBoundedTable.of(TABLE_WTH_NUMERIC_SCHEMA)
+          .addRows(new BigDecimal("123.4567").setScale(9), "str1")

Review comment:
       Seems a great idea. Done.




----------------------------------------------------------------
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.

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