twalthr commented on a change in pull request #11290: [FLINK-16379][table] 
Introduce fromValues in TableEnvironment
URL: https://github.com/apache/flink/pull/11290#discussion_r404129825
 
 

 ##########
 File path: 
flink-table/flink-table-api-java/src/test/java/org/apache/flink/table/operations/utils/ValuesOperationTreeBuilderTest.java
 ##########
 @@ -0,0 +1,324 @@
+/*
+ * 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.operations.utils;
+
+import org.apache.flink.table.annotation.DataTypeHint;
+import org.apache.flink.table.annotation.FunctionHint;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.TableSchema;
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.ResolvedExpression;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionIdentifier;
+import org.apache.flink.table.functions.ScalarFunction;
+import org.apache.flink.table.operations.ValuesQueryOperation;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.DataTypeFactoryMock;
+import org.apache.flink.table.utils.FunctionLookupMock;
+import org.apache.flink.types.Row;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import javax.annotation.Nullable;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static org.apache.flink.table.api.Expressions.call;
+import static org.apache.flink.table.api.Expressions.row;
+import static 
org.apache.flink.table.expressions.ApiExpressionUtils.typeLiteral;
+import static 
org.apache.flink.table.expressions.ApiExpressionUtils.valueLiteral;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link OperationTreeBuilder#values}.
+ */
+@RunWith(Parameterized.class)
+public class ValuesOperationTreeBuilderTest {
+
+       @Parameterized.Parameters(name = "{0}")
+       public static Collection<TestSpec> parameters() {
+               return asList(
+                       TestSpec.test("Flattening row constructor")
+                       .values(row(1, "ABC"), row(2, "EFG"))
+                       .equalTo(new ValuesQueryOperation(
+                               asList(
+                                       asList(valueLiteral(1), 
valueLiteral("ABC")),
+                                       asList(valueLiteral(2), 
valueLiteral("EFG"))
+                               ),
+                               TableSchema.builder()
+                                       .field("f0", DataTypes.INT().notNull())
+                                       .field("f1", 
DataTypes.CHAR(3).notNull())
+                                       .build()
+                       )),
+
+                       TestSpec.test("Finding common type")
+                               .values(row(1L, "ABC"), row(3.1f, "DEFG"))
+                               .equalTo(new ValuesQueryOperation(
+                                       asList(
+                                               asList(valueLiteral(1f), 
valueLiteral("ABC", DataTypes.VARCHAR(4).notNull())),
+                                               asList(valueLiteral(3.1f), 
valueLiteral("DEFG", DataTypes.VARCHAR(4).notNull()))
+                                       ),
+                                       TableSchema.builder()
+                                               .field("f0", 
DataTypes.FLOAT().notNull())
+                                               .field("f1", 
DataTypes.VARCHAR(4).notNull())
+                                               .build()
+                               )),
+
+                       TestSpec.test("Explicit common type")
+                               .values(
+                                       DataTypes.ROW(
+                                               DataTypes.FIELD("id", 
DataTypes.DECIMAL(10, 2)),
+                                               DataTypes.FIELD("name", 
DataTypes.STRING())),
+                                       row(1L, "ABC"),
+                                       row(3.1f, "DEFG")
+                               )
+                               .equalTo(new ValuesQueryOperation(
+                                       asList(
+                                               asList(
+                                                       
valueLiteral(BigDecimal.valueOf(1L), DataTypes.DECIMAL(10, 2)),
+                                                       valueLiteral("ABC", 
DataTypes.STRING())
+                                               ),
+                                               asList(
+                                                       valueLiteral(new 
BigDecimal(String.valueOf(3.1f)), DataTypes.DECIMAL(10, 2)),
+                                                       valueLiteral("DEFG", 
DataTypes.STRING())
+                                               )
+                                       ),
+                                       TableSchema.builder()
+                                               .field("id", 
DataTypes.DECIMAL(10, 2))
+                                               .field("name", 
DataTypes.STRING())
+                                               .build()
+                               )),
+
+                       TestSpec.test("Finding common type for nested rows")
+                               .values(
+                                       row(1L, row(1L, "ABC")),
+                                       row(3.1f, row(3.1f, "DEFG"))
+                               )
+                               .equalTo(
+                                       new ValuesQueryOperation(
+                                               asList(
+                                                       asList(
+                                                               
valueLiteral(1f),
+                                                               cast(
+                                                                       rowCtor(
+                                                                               
DataTypes.ROW(
+                                                                               
        DataTypes.FIELD("f0", DataTypes.BIGINT().notNull()),
+                                                                               
        DataTypes.FIELD("f1", DataTypes.CHAR(3).notNull())),
+                                                                               
valueLiteral(1L, DataTypes.BIGINT().notNull()),
+                                                                               
valueLiteral("ABC", DataTypes.CHAR(3).notNull())
+                                                                       ),
+                                                                       
DataTypes.ROW(
+                                                                               
DataTypes.FIELD("f0", DataTypes.FLOAT().notNull()),
+                                                                               
DataTypes.FIELD("f1", DataTypes.VARCHAR(4).notNull()))
+                                                               )
+                                                       ),
+                                                       asList(
+                                                               
valueLiteral(3.1f),
+                                                               cast(
+                                                                       rowCtor(
+                                                                               
DataTypes.ROW(
+                                                                               
        DataTypes.FIELD("f0", DataTypes.FLOAT().notNull()),
+                                                                               
        DataTypes.FIELD("f1", DataTypes.CHAR(4).notNull())),
+                                                                               
valueLiteral(3.1f, DataTypes.FLOAT().notNull()),
+                                                                               
valueLiteral("DEFG", DataTypes.CHAR(4).notNull())
+                                                                       ),
+                                                                       
DataTypes.ROW(
+                                                                               
DataTypes.FIELD("f0", DataTypes.FLOAT().notNull()),
+                                                                               
DataTypes.FIELD("f1", DataTypes.VARCHAR(4).notNull()))
+                                                               )
+                                                       )
+                                               ),
+                                               TableSchema.builder()
+                                                       .field("f0", 
DataTypes.FLOAT().notNull())
+                                                       .field(
+                                                               "f1",
+                                                               DataTypes.ROW(
+                                                                       
DataTypes.FIELD("f0", DataTypes.FLOAT().notNull()),
+                                                                       
DataTypes.FIELD("f1", DataTypes.VARCHAR(4).notNull())))
+                                                       .build()
+                                       )),
+
+                       TestSpec.test("Finding common type. Insert cast for 
calls")
+                               .values(call(new IntScalarFunction()), 
row(3.1f))
+                               .equalTo(new ValuesQueryOperation(
+                                       asList(
+                                               singletonList(
+                                                       cast(
+                                                               new 
CallExpression(new IntScalarFunction(), Collections.emptyList(), 
DataTypes.INT()),
+                                                               
DataTypes.FLOAT()
+                                                       )),
+                                               
singletonList(valueLiteral(3.1f, DataTypes.FLOAT().nullable()))
+                                       ),
+                                       TableSchema.builder()
+                                               .field("f0", DataTypes.FLOAT())
+                                               .build()
+                               )),
+
+                       TestSpec.test("Row in a function result is not 
flattened")
+                               .values(call(new RowScalarFunction()))
+                               .equalTo(new ValuesQueryOperation(
+                                       singletonList(
+                                               singletonList(new 
CallExpression(
+                                                       new RowScalarFunction(),
+                                                       Collections.emptyList(),
+                                                       DataTypes.ROW(
+                                                               
DataTypes.FIELD("f0", DataTypes.INT()),
+                                                               
DataTypes.FIELD("f1", DataTypes.STRING()))))
+                                       ),
+                                       TableSchema.builder()
+                                               .field("f0", DataTypes.ROW(
+                                                       DataTypes.FIELD("f0", 
DataTypes.INT()),
+                                                       DataTypes.FIELD("f1", 
DataTypes.STRING())))
+                                               .build()
+                               ))
+               );
+       }
+
+       /**
+        * A simple function that returns a ROW.
+        */
+       @FunctionHint(
+               output = @DataTypeHint("ROW<f0 INT, f1 STRING>")
+       )
+       public static class RowScalarFunction extends ScalarFunction {
+               public Row eval() {
+                       return Row.of(1, "ABC");
+               }
+
+               @Override
+               public int hashCode() {
+                       return 0;
+               }
+
+               @Override
+               public boolean equals(Object obj) {
+                       return obj instanceof RowScalarFunction;
+               }
+       }
+
+       /**
+        * A simple function that returns an int.
+        */
+       public static class IntScalarFunction extends ScalarFunction {
+               public Integer eval() {
+                       return 1;
+               }
+
+               @Override
+               public int hashCode() {
+                       return 0;
+               }
+
+               @Override
+               public boolean equals(Object obj) {
+                       return obj instanceof IntScalarFunction;
+               }
+       }
+
+       private static ResolvedExpression rowCtor(DataType dataType, 
ResolvedExpression... expression) {
+               return new CallExpression(
+                       FunctionIdentifier.of("row"),
+                       BuiltInFunctionDefinitions.ROW,
+                       Arrays.asList(expression),
+                       dataType);
+       }
+
+       private static ResolvedExpression cast(ResolvedExpression expression, 
DataType dataType) {
 
 Review comment:
   nit: sort public/private static/non-static components in this class

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to