yunfengzhou-hub commented on code in PR #82:
URL: https://github.com/apache/flink-ml/pull/82#discussion_r846977954


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/BucketizerTest.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.ml.feature;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.ml.common.param.HasHandleInvalid;
+import org.apache.flink.ml.feature.bucketizer.Bucketizer;
+import org.apache.flink.ml.util.StageTestUtils;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/** Tests the {@link Bucketizer}. */
+public class BucketizerTest extends AbstractTestBase {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+    private StreamTableEnvironment tEnv;
+    private Table inputTable;
+
+    private static final List<Row> inputData =
+            Arrays.asList(
+                    Row.of(1, -0.5, 0.0, 1.0),
+                    Row.of(2, Double.NEGATIVE_INFINITY, 1.0, 
Double.POSITIVE_INFINITY),
+                    Row.of(3, Double.NaN, -0.5, -0.5));
+
+    private static final Double[][] splitsArray =
+            new Double[][] {
+                new Double[] {-0.5, 0.0, 0.5},
+                new Double[] {-1.0, 0.0, 2.0},
+                new Double[] {Double.NEGATIVE_INFINITY, 10.0, 
Double.POSITIVE_INFINITY}
+            };
+
+    private final List<Row> expectedKeepResult =
+            Arrays.asList(Row.of(1, 0, 1, 0), Row.of(2, 2, 1, 1), Row.of(3, 2, 
0, 0));
+
+    private final List<Row> expectedSkipResult = 
Collections.singletonList(Row.of(1, 0, 1, 0));
+
+    @Before
+    public void before() {
+        Configuration config = new Configuration();
+        
config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, 
true);
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.setParallelism(4);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        inputTable = 
tEnv.fromDataStream(env.fromCollection(inputData)).as("id", "f1", "f2", "f3");
+    }
+
+    @SuppressWarnings("all")
+    private void verifyOutputResult(Table output, String[] outputCols, 
List<Row> expectedResult)
+            throws Exception {
+        List<Row> collectedResult =
+                
IteratorUtils.toList(tEnv.toDataStream(output).executeAndCollect());
+        List<Row> result = new ArrayList<>(collectedResult.size());
+
+        for (Row r : collectedResult) {
+            Row outRow = new Row(outputCols.length + 1);
+            outRow.setField(0, r.getField("id"));
+            for (int i = 0; i < outputCols.length; i++) {
+                outRow.setField(i + 1, r.getField(outputCols[i]));
+            }
+            result.add(outRow);
+        }
+
+        compareResultCollections(
+                expectedResult, result, Comparator.comparingInt(r -> 
((Integer) r.getField(0))));
+    }
+
+    @Test
+    public void testParam() {
+        Bucketizer bucketizer = new Bucketizer();
+        assertEquals(HasHandleInvalid.ERROR_INVALID, 
bucketizer.getHandleInvalid());
+
+        bucketizer
+                .setInputCols("f1", "f2", "f3")
+                .setOutputCols("o1", "o2", "o3")
+                .setHandleInvalid(HasHandleInvalid.SKIP_INVALID)
+                .setSplitsArray(splitsArray);

Review Comment:
   @lindong28 How do you like the idea to add APIs to `Bucketizer` so that 
users can define metadata in the following format?
   ```java
   bucketizer
       .addCol("f1", "o1", splitsArray[0])
       .addCol("f2", "o2", splitsArray[1]);
   ```
   It is different from existing practice from Spark or Alink, but the 
readability would better. It would also be OK for me if it is better to leave 
this discussion to future and to focus on functionality correctness for now.



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

Reply via email to