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: us...@infra.apache.org