[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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: us...@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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: us...@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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: us...@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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: us...@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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: us...@infra.apache.org
[GitHub] [beam] robinyqiu commented on a change in pull request #12174: [BEAM-10239] Support ZetaSQL NUMERIC type in BeamSQL
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 '-9.9', " 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 '-9.9', " ++ "NUMERIC '9.9'"; +; + +ZetaSQLQueryPlanner zetaSQLQueryPlanner = new ZetaSQLQueryPlanner(config); +BeamRelNode beamRelNode = zetaSQLQueryPlanner.convertToBeamRel(sql); +PCollection 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")