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


##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {
+
+    private StreamTableEnvironment tEnv;
+    private Table inputDataTable;
+
+    private static final List<Row> INPUT_DATA =
+            Arrays.asList(
+                    Row.of(
+                            0,
+                            Vectors.dense(2.1, 3.1, 2.3, 3.4, 5.3, 5.1),
+                            Vectors.sparse(5, new int[] {1, 3, 4}, new 
double[] {0.1, 0.2, 0.3})),
+                    Row.of(
+                            1,
+                            Vectors.dense(2.3, 4.1, 1.3, 2.4, 5.1, 4.1),
+                            Vectors.sparse(5, new int[] {1, 2, 4}, new 
double[] {0.1, 0.2, 0.3})));
+
+    private static final DenseVector EXPECTED_OUTPUT_DATA_1 = 
Vectors.dense(2.1, 3.1, 2.3);
+    private static final DenseVector EXPECTED_OUTPUT_DATA_2 = 
Vectors.dense(2.3, 4.1, 1.3);
+
+    private static final SparseVector EXPECTED_OUTPUT_DATA_3 =
+            Vectors.sparse(3, new int[] {1}, new double[] {0.1});
+    private static final SparseVector EXPECTED_OUTPUT_DATA_4 =
+            Vectors.sparse(3, new int[] {1, 2}, new double[] {0.1, 0.2});
+
+    @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);
+        DataStream<Row> dataStream = env.fromCollection(INPUT_DATA);
+        inputDataTable = tEnv.fromDataStream(dataStream).as("id", "vec", 
"sparseVec");
+    }
+
+    private void verifyOutputResult(Table output, String outputCol, boolean 
isSparse)
+            throws Exception {
+        DataStream<Row> dataStream = tEnv.toDataStream(output);
+        List<Row> results = 
IteratorUtils.toList(dataStream.executeAndCollect());
+        assertEquals(2, results.size());
+        for (Row result : results) {
+            if (result.getField(0) == (Object) 0) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_3, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_1, 
result.getField(outputCol));
+                }
+            } else if (result.getField(0) == (Object) 1) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_4, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_2, 
result.getField(outputCol));
+                }
+            } else {
+                throw new RuntimeException("Result id value is error, it must 
be 0 or 1.");
+            }
+        }
+    }
+
+    @Test
+    public void testParam() {
+        VectorSlicer vectorSlicer = new VectorSlicer();
+        assertEquals("input", vectorSlicer.getInputCol());
+        assertEquals("output", vectorSlicer.getOutputCol());
+        vectorSlicer.setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 
1, 2);
+        assertEquals("vec", vectorSlicer.getInputCol());
+        assertEquals("sliceVec", vectorSlicer.getOutputCol());
+        assertArrayEquals(new Integer[] {0, 1, 2}, vectorSlicer.getIndices());
+    }
+
+    @Test
+    public void testSaveLoadAndTransform() throws Exception {
+        VectorSlicer vectorSlicer =
+                new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 1, 2);
+        VectorSlicer loadedVectorSlicer =
+                TestUtils.saveAndReload(
+                        tEnv, vectorSlicer, 
TEMPORARY_FOLDER.newFolder().getAbsolutePath());
+        Table output = loadedVectorSlicer.transform(inputDataTable)[0];
+        verifyOutputResult(output, loadedVectorSlicer.getOutputCol(), false);
+    }
+
+    @Test
+    public void testEmptyIndices() {

Review Comment:
   It seems that spark `VectorSlicer` treats an empty array as a valid input 
for indices. Can we also follow this convention?



##########
flink-ml-python/pyflink/ml/core/param.py:
##########
@@ -228,6 +228,22 @@ def validate(self, value: Tuple[T]) -> bool:
 
         return NonEmptyArray()
 
+    @staticmethod
+    def numerical_array_gt_eq(lower_bound: int) -> ParamValidator[Tuple[int]]:

Review Comment:
   This method can be removed now.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {

Review Comment:
   Let's also add a test case to verify when input indices are unordered.



##########
flink-ml-python/pyflink/ml/lib/feature/vectorslicer.py:
##########
@@ -0,0 +1,71 @@
+################################################################################
+#  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.
+################################################################################
+
+from typing import Tuple
+from pyflink.ml.core.wrapper import JavaWithParams
+from pyflink.ml.core.param import IntArrayParam, ParamValidators
+from pyflink.ml.lib.feature.common import JavaFeatureTransformer
+from pyflink.ml.lib.param import HasInputCol, HasOutputCol, Param
+
+
+class _VectorSlicerParams(
+    JavaWithParams,
+    HasInputCol,
+    HasOutputCol
+):
+    """
+    Params for :class:`VectorSlicer`.
+    """
+
+    INDICES: Param[Tuple[int, ...]] = IntArrayParam(
+        "indices",
+        "An array of indices to select features from a vector column.",
+        None,
+        ParamValidators.numerical_array_gt_eq(0))
+
+    def __init__(self, java_params):
+        super(_VectorSlicerParams, self).__init__(java_params)
+
+    def set_indices(self, *ind: int):
+        return self.set(self.INDICES, ind)
+
+    def get_indices(self) -> Tuple[int, ...]:
+        return self.get(self.INDICES)
+
+    @property
+    def indices(self) -> Tuple[int, ...]:
+        return self.get_indices()
+
+
+class VectorSlicer(JavaFeatureTransformer, _VectorSlicerParams):
+    """
+    VectorSlicer is a transformer that transforms a vector to a new one with a 
sub-array of the
+    original features. It is useful for extracting features from a given 
vector. If the max
+    indices are larger than the size of the input vector, it will throw an 
IllegalArgumentException.

Review Comment:
   nit: This document is inconsistent with that in Java.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {
+
+    private StreamTableEnvironment tEnv;
+    private Table inputDataTable;
+
+    private static final List<Row> INPUT_DATA =
+            Arrays.asList(
+                    Row.of(
+                            0,
+                            Vectors.dense(2.1, 3.1, 2.3, 3.4, 5.3, 5.1),
+                            Vectors.sparse(5, new int[] {1, 3, 4}, new 
double[] {0.1, 0.2, 0.3})),
+                    Row.of(
+                            1,
+                            Vectors.dense(2.3, 4.1, 1.3, 2.4, 5.1, 4.1),
+                            Vectors.sparse(5, new int[] {1, 2, 4}, new 
double[] {0.1, 0.2, 0.3})));
+
+    private static final DenseVector EXPECTED_OUTPUT_DATA_1 = 
Vectors.dense(2.1, 3.1, 2.3);
+    private static final DenseVector EXPECTED_OUTPUT_DATA_2 = 
Vectors.dense(2.3, 4.1, 1.3);
+
+    private static final SparseVector EXPECTED_OUTPUT_DATA_3 =
+            Vectors.sparse(3, new int[] {1}, new double[] {0.1});
+    private static final SparseVector EXPECTED_OUTPUT_DATA_4 =
+            Vectors.sparse(3, new int[] {1, 2}, new double[] {0.1, 0.2});
+
+    @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);
+        DataStream<Row> dataStream = env.fromCollection(INPUT_DATA);
+        inputDataTable = tEnv.fromDataStream(dataStream).as("id", "vec", 
"sparseVec");
+    }
+
+    private void verifyOutputResult(Table output, String outputCol, boolean 
isSparse)
+            throws Exception {
+        DataStream<Row> dataStream = tEnv.toDataStream(output);
+        List<Row> results = 
IteratorUtils.toList(dataStream.executeAndCollect());
+        assertEquals(2, results.size());
+        for (Row result : results) {
+            if (result.getField(0) == (Object) 0) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_3, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_1, 
result.getField(outputCol));
+                }
+            } else if (result.getField(0) == (Object) 1) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_4, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_2, 
result.getField(outputCol));
+                }
+            } else {
+                throw new RuntimeException("Result id value is error, it must 
be 0 or 1.");
+            }
+        }
+    }
+
+    @Test
+    public void testParam() {
+        VectorSlicer vectorSlicer = new VectorSlicer();
+        assertEquals("input", vectorSlicer.getInputCol());
+        assertEquals("output", vectorSlicer.getOutputCol());
+        vectorSlicer.setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 
1, 2);
+        assertEquals("vec", vectorSlicer.getInputCol());
+        assertEquals("sliceVec", vectorSlicer.getOutputCol());
+        assertArrayEquals(new Integer[] {0, 1, 2}, vectorSlicer.getIndices());
+    }
+
+    @Test
+    public void testSaveLoadAndTransform() throws Exception {
+        VectorSlicer vectorSlicer =
+                new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 1, 2);
+        VectorSlicer loadedVectorSlicer =
+                TestUtils.saveAndReload(
+                        tEnv, vectorSlicer, 
TEMPORARY_FOLDER.newFolder().getAbsolutePath());
+        Table output = loadedVectorSlicer.transform(inputDataTable)[0];
+        verifyOutputResult(output, loadedVectorSlicer.getOutputCol(), false);
+    }
+
+    @Test
+    public void testEmptyIndices() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices();
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesLargerThanVectorSize() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, 2, 10);
+            Table output = vectorSlicer.transform(inputDataTable)[0];
+            DataStream<Row> dataStream = tEnv.toDataStream(output);
+            IteratorUtils.toList(dataStream.executeAndCollect());
+            Assert.fail("Expected RuntimeException");
+        } catch (Exception e) {
+            assertEquals(
+                    "Index value 10 is greater than vector size:6",
+                    
e.getCause().getCause().getCause().getCause().getCause().getMessage());
+            assertEquals(
+                    IllegalArgumentException.class,
+                    
e.getCause().getCause().getCause().getCause().getCause().getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesSallerThanZero() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, -2);
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {1,-2}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());
+        }
+    }
+
+    @Test
+    public void testDuplicateIndices() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, 1, 3);
+            vectorSlicer.transform(inputDataTable);

Review Comment:
   We can remove this line because an exception would be thrown before 
`transform()` is invoked. Same for other test cases.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {
+
+    private StreamTableEnvironment tEnv;
+    private Table inputDataTable;
+
+    private static final List<Row> INPUT_DATA =
+            Arrays.asList(
+                    Row.of(
+                            0,
+                            Vectors.dense(2.1, 3.1, 2.3, 3.4, 5.3, 5.1),
+                            Vectors.sparse(5, new int[] {1, 3, 4}, new 
double[] {0.1, 0.2, 0.3})),
+                    Row.of(
+                            1,
+                            Vectors.dense(2.3, 4.1, 1.3, 2.4, 5.1, 4.1),
+                            Vectors.sparse(5, new int[] {1, 2, 4}, new 
double[] {0.1, 0.2, 0.3})));
+
+    private static final DenseVector EXPECTED_OUTPUT_DATA_1 = 
Vectors.dense(2.1, 3.1, 2.3);
+    private static final DenseVector EXPECTED_OUTPUT_DATA_2 = 
Vectors.dense(2.3, 4.1, 1.3);
+
+    private static final SparseVector EXPECTED_OUTPUT_DATA_3 =
+            Vectors.sparse(3, new int[] {1}, new double[] {0.1});
+    private static final SparseVector EXPECTED_OUTPUT_DATA_4 =
+            Vectors.sparse(3, new int[] {1, 2}, new double[] {0.1, 0.2});
+
+    @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);
+        DataStream<Row> dataStream = env.fromCollection(INPUT_DATA);
+        inputDataTable = tEnv.fromDataStream(dataStream).as("id", "vec", 
"sparseVec");
+    }
+
+    private void verifyOutputResult(Table output, String outputCol, boolean 
isSparse)
+            throws Exception {
+        DataStream<Row> dataStream = tEnv.toDataStream(output);
+        List<Row> results = 
IteratorUtils.toList(dataStream.executeAndCollect());
+        assertEquals(2, results.size());
+        for (Row result : results) {
+            if (result.getField(0) == (Object) 0) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_3, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_1, 
result.getField(outputCol));
+                }
+            } else if (result.getField(0) == (Object) 1) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_4, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_2, 
result.getField(outputCol));
+                }
+            } else {
+                throw new RuntimeException("Result id value is error, it must 
be 0 or 1.");
+            }
+        }
+    }
+
+    @Test
+    public void testParam() {
+        VectorSlicer vectorSlicer = new VectorSlicer();
+        assertEquals("input", vectorSlicer.getInputCol());
+        assertEquals("output", vectorSlicer.getOutputCol());
+        vectorSlicer.setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 
1, 2);
+        assertEquals("vec", vectorSlicer.getInputCol());
+        assertEquals("sliceVec", vectorSlicer.getOutputCol());
+        assertArrayEquals(new Integer[] {0, 1, 2}, vectorSlicer.getIndices());
+    }
+
+    @Test
+    public void testSaveLoadAndTransform() throws Exception {
+        VectorSlicer vectorSlicer =
+                new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 1, 2);
+        VectorSlicer loadedVectorSlicer =
+                TestUtils.saveAndReload(
+                        tEnv, vectorSlicer, 
TEMPORARY_FOLDER.newFolder().getAbsolutePath());
+        Table output = loadedVectorSlicer.transform(inputDataTable)[0];
+        verifyOutputResult(output, loadedVectorSlicer.getOutputCol(), false);
+    }
+
+    @Test
+    public void testEmptyIndices() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices();
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesLargerThanVectorSize() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, 2, 10);
+            Table output = vectorSlicer.transform(inputDataTable)[0];
+            DataStream<Row> dataStream = tEnv.toDataStream(output);
+            IteratorUtils.toList(dataStream.executeAndCollect());
+            Assert.fail("Expected RuntimeException");
+        } catch (Exception e) {
+            assertEquals(
+                    "Index value 10 is greater than vector size:6",
+                    
e.getCause().getCause().getCause().getCause().getCause().getMessage());
+            assertEquals(
+                    IllegalArgumentException.class,
+                    
e.getCause().getCause().getCause().getCause().getCause().getClass());

Review Comment:
   nit: `ExceptionUtils.getRootCause(e)`



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {
+
+    private StreamTableEnvironment tEnv;
+    private Table inputDataTable;
+
+    private static final List<Row> INPUT_DATA =
+            Arrays.asList(
+                    Row.of(
+                            0,
+                            Vectors.dense(2.1, 3.1, 2.3, 3.4, 5.3, 5.1),
+                            Vectors.sparse(5, new int[] {1, 3, 4}, new 
double[] {0.1, 0.2, 0.3})),
+                    Row.of(
+                            1,
+                            Vectors.dense(2.3, 4.1, 1.3, 2.4, 5.1, 4.1),
+                            Vectors.sparse(5, new int[] {1, 2, 4}, new 
double[] {0.1, 0.2, 0.3})));
+
+    private static final DenseVector EXPECTED_OUTPUT_DATA_1 = 
Vectors.dense(2.1, 3.1, 2.3);
+    private static final DenseVector EXPECTED_OUTPUT_DATA_2 = 
Vectors.dense(2.3, 4.1, 1.3);
+
+    private static final SparseVector EXPECTED_OUTPUT_DATA_3 =
+            Vectors.sparse(3, new int[] {1}, new double[] {0.1});
+    private static final SparseVector EXPECTED_OUTPUT_DATA_4 =
+            Vectors.sparse(3, new int[] {1, 2}, new double[] {0.1, 0.2});
+
+    @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);
+        DataStream<Row> dataStream = env.fromCollection(INPUT_DATA);
+        inputDataTable = tEnv.fromDataStream(dataStream).as("id", "vec", 
"sparseVec");
+    }
+
+    private void verifyOutputResult(Table output, String outputCol, boolean 
isSparse)
+            throws Exception {
+        DataStream<Row> dataStream = tEnv.toDataStream(output);
+        List<Row> results = 
IteratorUtils.toList(dataStream.executeAndCollect());
+        assertEquals(2, results.size());
+        for (Row result : results) {
+            if (result.getField(0) == (Object) 0) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_3, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_1, 
result.getField(outputCol));
+                }
+            } else if (result.getField(0) == (Object) 1) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_4, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_2, 
result.getField(outputCol));
+                }
+            } else {
+                throw new RuntimeException("Result id value is error, it must 
be 0 or 1.");
+            }
+        }
+    }
+
+    @Test
+    public void testParam() {
+        VectorSlicer vectorSlicer = new VectorSlicer();
+        assertEquals("input", vectorSlicer.getInputCol());
+        assertEquals("output", vectorSlicer.getOutputCol());
+        vectorSlicer.setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 
1, 2);
+        assertEquals("vec", vectorSlicer.getInputCol());
+        assertEquals("sliceVec", vectorSlicer.getOutputCol());
+        assertArrayEquals(new Integer[] {0, 1, 2}, vectorSlicer.getIndices());
+    }
+
+    @Test
+    public void testSaveLoadAndTransform() throws Exception {
+        VectorSlicer vectorSlicer =
+                new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 1, 2);
+        VectorSlicer loadedVectorSlicer =
+                TestUtils.saveAndReload(
+                        tEnv, vectorSlicer, 
TEMPORARY_FOLDER.newFolder().getAbsolutePath());
+        Table output = loadedVectorSlicer.transform(inputDataTable)[0];
+        verifyOutputResult(output, loadedVectorSlicer.getOutputCol(), false);
+    }
+
+    @Test
+    public void testEmptyIndices() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices();
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesLargerThanVectorSize() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, 2, 10);
+            Table output = vectorSlicer.transform(inputDataTable)[0];
+            DataStream<Row> dataStream = tEnv.toDataStream(output);
+            IteratorUtils.toList(dataStream.executeAndCollect());
+            Assert.fail("Expected RuntimeException");
+        } catch (Exception e) {
+            assertEquals(
+                    "Index value 10 is greater than vector size:6",
+                    
e.getCause().getCause().getCause().getCause().getCause().getMessage());
+            assertEquals(
+                    IllegalArgumentException.class,
+                    
e.getCause().getCause().getCause().getCause().getCause().getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesSallerThanZero() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, -2);
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {1,-2}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());

Review Comment:
   nit: we may not need to cast `e` to `Throwable` before invoking 
`getClass()`. Same for other places.



##########
flink-ml-lib/src/test/java/org/apache/flink/ml/feature/VectorSlicerTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.feature.vectorslicer.VectorSlicer;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.SparseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import org.apache.flink.ml.util.TestUtils;
+import org.apache.flink.streaming.api.datastream.DataStream;
+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.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/** Tests {@link VectorSlicer}. */
+public class VectorSlicerTest extends AbstractTestBase {
+
+    private StreamTableEnvironment tEnv;
+    private Table inputDataTable;
+
+    private static final List<Row> INPUT_DATA =
+            Arrays.asList(
+                    Row.of(
+                            0,
+                            Vectors.dense(2.1, 3.1, 2.3, 3.4, 5.3, 5.1),
+                            Vectors.sparse(5, new int[] {1, 3, 4}, new 
double[] {0.1, 0.2, 0.3})),
+                    Row.of(
+                            1,
+                            Vectors.dense(2.3, 4.1, 1.3, 2.4, 5.1, 4.1),
+                            Vectors.sparse(5, new int[] {1, 2, 4}, new 
double[] {0.1, 0.2, 0.3})));
+
+    private static final DenseVector EXPECTED_OUTPUT_DATA_1 = 
Vectors.dense(2.1, 3.1, 2.3);
+    private static final DenseVector EXPECTED_OUTPUT_DATA_2 = 
Vectors.dense(2.3, 4.1, 1.3);
+
+    private static final SparseVector EXPECTED_OUTPUT_DATA_3 =
+            Vectors.sparse(3, new int[] {1}, new double[] {0.1});
+    private static final SparseVector EXPECTED_OUTPUT_DATA_4 =
+            Vectors.sparse(3, new int[] {1, 2}, new double[] {0.1, 0.2});
+
+    @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);
+        DataStream<Row> dataStream = env.fromCollection(INPUT_DATA);
+        inputDataTable = tEnv.fromDataStream(dataStream).as("id", "vec", 
"sparseVec");
+    }
+
+    private void verifyOutputResult(Table output, String outputCol, boolean 
isSparse)
+            throws Exception {
+        DataStream<Row> dataStream = tEnv.toDataStream(output);
+        List<Row> results = 
IteratorUtils.toList(dataStream.executeAndCollect());
+        assertEquals(2, results.size());
+        for (Row result : results) {
+            if (result.getField(0) == (Object) 0) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_3, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_1, 
result.getField(outputCol));
+                }
+            } else if (result.getField(0) == (Object) 1) {
+                if (isSparse) {
+                    assertEquals(EXPECTED_OUTPUT_DATA_4, 
result.getField(outputCol));
+                } else {
+                    assertEquals(EXPECTED_OUTPUT_DATA_2, 
result.getField(outputCol));
+                }
+            } else {
+                throw new RuntimeException("Result id value is error, it must 
be 0 or 1.");
+            }
+        }
+    }
+
+    @Test
+    public void testParam() {
+        VectorSlicer vectorSlicer = new VectorSlicer();
+        assertEquals("input", vectorSlicer.getInputCol());
+        assertEquals("output", vectorSlicer.getOutputCol());
+        vectorSlicer.setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 
1, 2);
+        assertEquals("vec", vectorSlicer.getInputCol());
+        assertEquals("sliceVec", vectorSlicer.getOutputCol());
+        assertArrayEquals(new Integer[] {0, 1, 2}, vectorSlicer.getIndices());
+    }
+
+    @Test
+    public void testSaveLoadAndTransform() throws Exception {
+        VectorSlicer vectorSlicer =
+                new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices(0, 1, 2);
+        VectorSlicer loadedVectorSlicer =
+                TestUtils.saveAndReload(
+                        tEnv, vectorSlicer, 
TEMPORARY_FOLDER.newFolder().getAbsolutePath());
+        Table output = loadedVectorSlicer.transform(inputDataTable)[0];
+        verifyOutputResult(output, loadedVectorSlicer.getOutputCol(), false);
+    }
+
+    @Test
+    public void testEmptyIndices() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new 
VectorSlicer().setInputCol("vec").setOutputCol("sliceVec").setIndices();
+            vectorSlicer.transform(inputDataTable);
+            Assert.fail("Expected IllegalArgumentException");
+        } catch (Exception e) {
+            assertEquals("Parameter indices is given an invalid value {}", 
e.getMessage());
+            assertEquals(IllegalArgumentException.class, ((Throwable) 
e).getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesLargerThanVectorSize() {
+        try {
+            VectorSlicer vectorSlicer =
+                    new VectorSlicer()
+                            .setInputCol("vec")
+                            .setOutputCol("sliceVec")
+                            .setIndices(1, 2, 10);
+            Table output = vectorSlicer.transform(inputDataTable)[0];
+            DataStream<Row> dataStream = tEnv.toDataStream(output);
+            IteratorUtils.toList(dataStream.executeAndCollect());
+            Assert.fail("Expected RuntimeException");
+        } catch (Exception e) {
+            assertEquals(
+                    "Index value 10 is greater than vector size:6",
+                    
e.getCause().getCause().getCause().getCause().getCause().getMessage());
+            assertEquals(
+                    IllegalArgumentException.class,
+                    
e.getCause().getCause().getCause().getCause().getCause().getClass());
+        }
+    }
+
+    @Test
+    public void testIndicesSallerThanZero() {

Review Comment:
   nit: smaller



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to