|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.flink.ml.common.dataproc.vector; |
| 21 | + |
| 22 | +import org.apache.flink.api.common.typeinfo.TypeInformation; |
| 23 | +import org.apache.flink.api.common.typeinfo.Types; |
| 24 | +import org.apache.flink.ml.api.misc.param.Params; |
| 25 | +import org.apache.flink.ml.common.linalg.DenseVector; |
| 26 | +import org.apache.flink.ml.common.linalg.SparseVector; |
| 27 | +import org.apache.flink.ml.common.utils.RowCollector; |
| 28 | +import org.apache.flink.ml.params.dataproc.vector.VectorToColumnsParams; |
| 29 | +import org.apache.flink.table.api.TableSchema; |
| 30 | +import org.apache.flink.types.Row; |
| 31 | + |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | + |
| 38 | +/** |
| 39 | + * Unit test for VectorToColumnsMapper. |
| 40 | + */ |
| 41 | +public class VectorToColumnsMapperTest { |
| 42 | + @Test |
| 43 | + public void test1() throws Exception { |
| 44 | + TableSchema schema = new TableSchema(new String[]{"vec"}, new TypeInformation<?>[]{Types.STRING}); |
| 45 | + |
| 46 | + Params params = new Params() |
| 47 | + .set(VectorToColumnsParams.SELECTED_COL, "vec") |
| 48 | + .set(VectorToColumnsParams.OUTPUT_COLS, new String[]{"f0", "f1"}); |
| 49 | + |
| 50 | + VectorToColumnsMapper mapper = new VectorToColumnsMapper(schema, params); |
| 51 | + RowCollector collector = new RowCollector(); |
| 52 | + mapper.flatMap(Row.of(new DenseVector(new double[]{3.0, 4.0})), collector); |
| 53 | + List<Row> rows = collector.getRows(); |
| 54 | + assertEquals(rows.get(0).getField(1), 3.0); |
| 55 | + assertEquals(rows.get(0).getField(2), 4.0); |
| 56 | + assertEquals(mapper.getOutputSchema(), new TableSchema(new String[]{"vec", "f0", "f1"}, |
| 57 | + new TypeInformation<?>[]{Types.STRING, Types.DOUBLE, Types.DOUBLE})); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void test2() throws Exception { |
| 63 | + TableSchema schema = new TableSchema(new String[]{"vec"}, new TypeInformation<?>[]{Types.STRING}); |
| 64 | + |
| 65 | + Params params = new Params() |
| 66 | + .set(VectorToColumnsParams.SELECTED_COL, "vec") |
| 67 | + .set(VectorToColumnsParams.RESERVED_COLS, new String[]{}) |
| 68 | + .set(VectorToColumnsParams.OUTPUT_COLS, new String[]{"f0", "f1"}); |
| 69 | + |
| 70 | + VectorToColumnsMapper mapper = new VectorToColumnsMapper(schema, params); |
| 71 | + |
| 72 | + RowCollector collector = new RowCollector(); |
| 73 | + mapper.flatMap(Row.of(new DenseVector(new double[]{3.0, 4.0})), collector); |
| 74 | + List<Row> rows = collector.getRows(); |
| 75 | + assertEquals(rows.get(0).getField(0), 3.0); |
| 76 | + assertEquals(rows.get(0).getField(1), 4.0); |
| 77 | + assertEquals(mapper.getOutputSchema(), new TableSchema(new String[]{"f0", "f1"}, |
| 78 | + new TypeInformation<?>[]{Types.DOUBLE, Types.DOUBLE})); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void test3() throws Exception { |
| 83 | + TableSchema schema = new TableSchema(new String[]{"vec"}, new TypeInformation<?>[]{Types.STRING}); |
| 84 | + |
| 85 | + Params params = new Params() |
| 86 | + .set(VectorToColumnsParams.SELECTED_COL, "vec") |
| 87 | + .set(VectorToColumnsParams.OUTPUT_COLS, new String[]{"f0", "f1", "f2"}); |
| 88 | + |
| 89 | + VectorToColumnsMapper mapper = new VectorToColumnsMapper(schema, params); |
| 90 | + |
| 91 | + RowCollector collector = new RowCollector(); |
| 92 | + mapper.flatMap(Row.of(new SparseVector(3, new int[]{1, 2}, new double[]{3.0, 4.0})), collector); |
| 93 | + List<Row> rows = collector.getRows(); |
| 94 | + assertEquals(rows.get(0).getField(0), new SparseVector(3, new int[]{1, 2}, new double[]{3.0, 4.0})); |
| 95 | + assertEquals(rows.get(0).getField(1), 0.0); |
| 96 | + assertEquals(rows.get(0).getField(2), 3.0); |
| 97 | + assertEquals(rows.get(0).getField(3), 4.0); |
| 98 | + assertEquals(mapper.getOutputSchema(), new TableSchema(new String[]{"vec", "f0", "f1", "f2"}, |
| 99 | + new TypeInformation<?>[]{Types.STRING, Types.DOUBLE, Types.DOUBLE, Types.DOUBLE})); |
| 100 | + } |
| 101 | +} |
0 commit comments