raminqaf commented on code in PR #29512: URL: https://github.com/apache/flink/pull/29512#discussion_r4323404884
########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CbrtFunctionITCase.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.planner.functions; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; + +import java.math.BigDecimal; +import java.util.stream.Stream; + +import static org.apache.flink.table.api.Expressions.$; +import static org.apache.flink.table.api.Expressions.lit; + +/** SQL, Table API, coercion, and constant-folding tests for CBRT. */ +class CbrtFunctionITCase extends BuiltInFunctionTestBase { Review Comment: Does CBRT needs to be in a separate file? Are there any other IT cases where we can add this function too? How about `MathFunctionsITCase`? ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CbrtFunctionITCase.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.planner.functions; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; + +import java.math.BigDecimal; +import java.util.stream.Stream; + +import static org.apache.flink.table.api.Expressions.$; +import static org.apache.flink.table.api.Expressions.lit; + +/** SQL, Table API, coercion, and constant-folding tests for CBRT. */ +class CbrtFunctionITCase extends BuiltInFunctionTestBase { + + @Override + Stream<TestSetSpec> getTestSetSpecs() { + return Stream.concat( + Stream.of( + numericTypes(), + boundaries(), + TestSetSpec.forFunction(BuiltInFunctionDefinitions.CBRT, "constant folding") Review Comment: Please move the last one also into a function ########## flink-python/pyflink/table/expression.py: ########## @@ -1043,6 +1043,17 @@ def end(self) -> 'Expression': """ return _unary_op("end")(self) + @property + def cbrt(self) -> 'Expression[float]': + """ + Returns the cube root of a numeric value. + For positive numbers, returns a positive cube root. For negative numbers, returns a negative cube root. + Zero returns zero. Returns null if the argument is null. + + For example, CBRT(27) returns 3.0, CBRT(-8) returns -2.0, and CBRT(0) returns 0.0. + """ + return _unary_op("bitCount")(self) Review Comment: What is `bitCount`? is this correct? ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CbrtFunctionITCase.java: ########## @@ -0,0 +1,160 @@ +/* + * 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.planner.functions; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; + +import java.math.BigDecimal; +import java.util.stream.Stream; + +import static org.apache.flink.table.api.Expressions.$; +import static org.apache.flink.table.api.Expressions.lit; + +/** SQL, Table API, coercion, and constant-folding tests for CBRT. */ +class CbrtFunctionITCase extends BuiltInFunctionTestBase { + + @Override + Stream<TestSetSpec> getTestSetSpecs() { + return Stream.concat( + Stream.of( + numericTypes(), + boundaries(), + TestSetSpec.forFunction(BuiltInFunctionDefinitions.CBRT, "constant folding") + .onFieldsWithData(0) + .withConstantFoldingEnabled() + .testResult( + resultSpec( + lit(27).cbrt(), + "CBRT(27)", + 3.0, + DataTypes.DOUBLE().notNull()), + resultSpec( + lit(null, DataTypes.DOUBLE()).cbrt(), + "CBRT(CAST(NULL AS DOUBLE))", + null, + DataTypes.DOUBLE())) + .testSqlResult("CBRT(NULL)", null, DataTypes.DOUBLE()), + TestSetSpec.forFunction(BuiltInFunctionDefinitions.CBRT, "argument count") + .onFieldsWithData(0) + .testSqlValidationError( + "CBRT()", "No match found for function signature CBRT().") + .testSqlValidationError( + "CBRT(1, 2)", + "No match found for function signature CBRT(<NUMERIC>, <NUMERIC>).")), + Stream.of( + DataTypes.BOOLEAN(), + DataTypes.STRING(), + DataTypes.BYTES(), + DataTypes.ARRAY(DataTypes.INT())) + .map( + type -> + TestSetSpec.forFunction( + BuiltInFunctionDefinitions.CBRT, + "reject " + type) + .onFieldsWithData(0) + .testTableApiValidationError( + lit(null, type).cbrt(), + "Invalid input arguments") + .testSqlValidationError( + "CBRT(CAST(NULL AS " + type + "))", + "Invalid input arguments"))); + } + + private static TestSetSpec numericTypes() { + return TestSetSpec.forFunction(BuiltInFunctionDefinitions.CBRT, "numeric coercion") + .onFieldsWithData((byte) 1, (short) 1, 1, 1L, 1.0f, 1.0, new BigDecimal("1.00")) + .andDataTypes( + DataTypes.TINYINT().notNull(), + DataTypes.SMALLINT(), + DataTypes.INT(), + DataTypes.BIGINT(), + DataTypes.FLOAT(), + DataTypes.DOUBLE(), + DataTypes.DECIMAL(10, 2)) + .testResult( + resultSpec($("f0").cbrt(), "CBRT(f0)", 1.0, DataTypes.DOUBLE().notNull()), + resultSpec($("f1").cbrt(), "CBRT(f1)", 1.0, DataTypes.DOUBLE()), + resultSpec($("f2").cbrt(), "CBRT(f2)", 1.0, DataTypes.DOUBLE()), + resultSpec($("f3").cbrt(), "CBRT(f3)", 1.0, DataTypes.DOUBLE()), + resultSpec($("f4").cbrt(), "CBRT(f4)", 1.0, DataTypes.DOUBLE()), + resultSpec($("f5").cbrt(), "CBRT(f5)", 1.0, DataTypes.DOUBLE()), + resultSpec($("f6").cbrt(), "CBRT(f6)", 1.0, DataTypes.DOUBLE())); + } + + private static TestSetSpec boundaries() { + return TestSetSpec.forFunction(BuiltInFunctionDefinitions.CBRT, "boundaries and nulls") + .onFieldsWithData( + 0.0, + -0.0, + 27.0, + -27.0, + 8.0, + -8.0, Review Comment: are these boundaries? ########## flink-python/pyflink/table/tests/test_expression.py: ########## @@ -125,6 +125,7 @@ def test_expression(self): self.assertEqual('in(a, b, c, d)', str(expr1.in_(expr2, expr3, expr4))) self.assertEqual('start(a)', str(expr1.start)) self.assertEqual('end(a)', str(expr1.end)) + self.assertEqual('CBRT(a)', str(expr1.bin)) Review Comment: You are not calling cbrt here, but `expr1.bin` 🤔 ########## docs/data/sql_functions.yml: ########## @@ -240,6 +240,9 @@ arithmetic: - sql: UUID_V7() table: uuidV7() description: Returns a time-ordered RFC 9562 version 7 UUID value, generated from the current timestamp and a random component. Compared to UUID(), this function returns a value of the UUID data type. + - sql: CBRT(numeric) + table: NUMERIC.cbrt() + description: Returns the cube root as DOUBLE, including for negative values. Returns NULL for NULL input. E.g., CBRT(-27) returns -3.0. Review Comment: Should we move this (and all the functions) closer to `SQRT` function? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
