|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.wayang.jdbc.operators; |
| 20 | + |
| 21 | +import org.apache.wayang.basic.data.Record; |
| 22 | +import org.apache.wayang.core.api.Configuration; |
| 23 | +import org.apache.wayang.core.api.Job; |
| 24 | +import org.apache.wayang.core.function.PredicateDescriptor; |
| 25 | +import org.apache.wayang.core.optimizer.OptimizationContext; |
| 26 | +import org.apache.wayang.core.plan.executionplan.ExecutionTask; |
| 27 | +import org.apache.wayang.core.plan.wayangplan.ExecutionOperator; |
| 28 | +import org.apache.wayang.core.plan.wayangplan.OutputSlot; |
| 29 | +import org.apache.wayang.core.platform.ChannelInstance; |
| 30 | +import org.apache.wayang.core.platform.CrossPlatformExecutor; |
| 31 | +import org.apache.wayang.core.profiling.FullInstrumentationStrategy; |
| 32 | +import org.apache.wayang.jdbc.channels.SqlQueryChannel; |
| 33 | +import org.apache.wayang.jdbc.test.HsqldbFilterOperator; |
| 34 | +import org.apache.wayang.jdbc.test.HsqldbPlatform; |
| 35 | +import org.apache.wayang.spark.channels.RddChannel; |
| 36 | +import org.apache.wayang.spark.execution.SparkExecutor; |
| 37 | +import org.apache.wayang.spark.platform.SparkPlatform; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | +import java.sql.Connection; |
| 42 | +import java.sql.SQLException; |
| 43 | +import java.sql.Statement; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +import static org.mockito.Mockito.mock; |
| 48 | +import static org.mockito.Mockito.when; |
| 49 | + |
| 50 | +public class SqlToRddOperatorTest extends OperatorTestBase { |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testWithHsqldb() throws SQLException { |
| 54 | + Configuration configuration = new Configuration(); |
| 55 | + |
| 56 | + Job job = mock(Job.class); |
| 57 | + when(job.getConfiguration()).thenReturn(configuration); |
| 58 | + |
| 59 | + CrossPlatformExecutor cpe = new CrossPlatformExecutor(job, new FullInstrumentationStrategy()); |
| 60 | + when(job.getCrossPlatformExecutor()).thenReturn(cpe); |
| 61 | + final SparkExecutor sparkExecutor = new SparkExecutor(SparkPlatform.getInstance(), job); |
| 62 | + |
| 63 | + HsqldbPlatform hsqldbPlatform = new HsqldbPlatform(); |
| 64 | + |
| 65 | + // Create some test data. |
| 66 | + try (Connection jdbcConnection = hsqldbPlatform.createDatabaseDescriptor(configuration).createJdbcConnection()) { |
| 67 | + final Statement statement = jdbcConnection.createStatement(); |
| 68 | + statement.execute("CREATE TABLE testSqlToRddWithHsqldb (a INT, b VARCHAR(6));"); |
| 69 | + statement.execute("INSERT INTO testSqlToRddWithHsqldb VALUES (0, 'zero');"); |
| 70 | + statement.execute("INSERT INTO testSqlToRddWithHsqldb VALUES (1, 'one');"); |
| 71 | + statement.execute("INSERT INTO testSqlToRddWithHsqldb VALUES (2, 'two');"); |
| 72 | + } |
| 73 | + |
| 74 | + final ExecutionOperator filterOperator = new HsqldbFilterOperator( |
| 75 | + new PredicateDescriptor<>(x -> false, Record.class) |
| 76 | + ); |
| 77 | + final SqlQueryChannel sqlQueryChannel = new SqlQueryChannel( |
| 78 | + HsqldbPlatform.getInstance().getSqlQueryChannelDescriptor(), |
| 79 | + filterOperator.getOutput(0) |
| 80 | + ); |
| 81 | + SqlQueryChannel.Instance sqlQueryChannelInstance = sqlQueryChannel.createInstance( |
| 82 | + hsqldbPlatform.createExecutor(job), |
| 83 | + mock(OptimizationContext.OperatorContext.class), |
| 84 | + 0 |
| 85 | + ); |
| 86 | + sqlQueryChannelInstance.setSqlQuery("SELECT * FROM testSqlToRddWithHsqldb;"); |
| 87 | + ExecutionTask producer = new ExecutionTask(filterOperator); |
| 88 | + producer.setOutputChannel(0, sqlQueryChannel); |
| 89 | + |
| 90 | + RddChannel.Instance rddChannelInstance = |
| 91 | + new RddChannel(RddChannel.UNCACHED_DESCRIPTOR, mock(OutputSlot.class)).createInstance( |
| 92 | + sparkExecutor, |
| 93 | + mock(OptimizationContext.OperatorContext.class), |
| 94 | + 0 |
| 95 | + ); |
| 96 | + |
| 97 | + SqlToRddOperator sqlToRddOperator = new SqlToRddOperator(HsqldbPlatform.getInstance()); |
| 98 | + evaluate( |
| 99 | + sqlToRddOperator, |
| 100 | + new ChannelInstance[]{sqlQueryChannelInstance}, |
| 101 | + new ChannelInstance[]{rddChannelInstance} |
| 102 | + ); |
| 103 | + |
| 104 | + List<Record> output = rddChannelInstance.<Record>provideRdd().collect(); |
| 105 | + List<Record> expected = Arrays.asList( |
| 106 | + new Record(0, "zero"), |
| 107 | + new Record(1, "one"), |
| 108 | + new Record(2, "two") |
| 109 | + ); |
| 110 | + |
| 111 | + Assert.assertEquals(expected, output); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testWithEmptyHsqldb() throws SQLException { |
| 116 | + Configuration configuration = new Configuration(); |
| 117 | + |
| 118 | + Job job = mock(Job.class); |
| 119 | + when(job.getConfiguration()).thenReturn(configuration); |
| 120 | + |
| 121 | + CrossPlatformExecutor cpe = new CrossPlatformExecutor(job, new FullInstrumentationStrategy()); |
| 122 | + when(job.getCrossPlatformExecutor()).thenReturn(cpe); |
| 123 | + final SparkExecutor sparkExecutor = new SparkExecutor(SparkPlatform.getInstance(), job); |
| 124 | + |
| 125 | + HsqldbPlatform hsqldbPlatform = new HsqldbPlatform(); |
| 126 | + |
| 127 | + // Create some test data. |
| 128 | + try (Connection jdbcConnection = hsqldbPlatform.createDatabaseDescriptor(configuration).createJdbcConnection()) { |
| 129 | + final Statement statement = jdbcConnection.createStatement(); |
| 130 | + statement.execute("CREATE TABLE testSqlToRddWithEmptyHsqldb (a INT, b VARCHAR(6));"); |
| 131 | + } |
| 132 | + |
| 133 | + final ExecutionOperator filterOperator = new HsqldbFilterOperator( |
| 134 | + new PredicateDescriptor<>(x -> false, Record.class) |
| 135 | + ); |
| 136 | + final SqlQueryChannel sqlQueryChannel = new SqlQueryChannel( |
| 137 | + HsqldbPlatform.getInstance().getSqlQueryChannelDescriptor(), |
| 138 | + filterOperator.getOutput(0) |
| 139 | + ); |
| 140 | + SqlQueryChannel.Instance sqlQueryChannelInstance = sqlQueryChannel.createInstance( |
| 141 | + hsqldbPlatform.createExecutor(job), |
| 142 | + mock(OptimizationContext.OperatorContext.class), |
| 143 | + 0 |
| 144 | + ); |
| 145 | + sqlQueryChannelInstance.setSqlQuery("SELECT * FROM testSqlToRddWithEmptyHsqldb;"); |
| 146 | + ExecutionTask producer = new ExecutionTask(filterOperator); |
| 147 | + producer.setOutputChannel(0, sqlQueryChannel); |
| 148 | + |
| 149 | + RddChannel.Instance rddChannelInstance = |
| 150 | + new RddChannel(RddChannel.UNCACHED_DESCRIPTOR, mock(OutputSlot.class)).createInstance( |
| 151 | + sparkExecutor, |
| 152 | + mock(OptimizationContext.OperatorContext.class), |
| 153 | + 0 |
| 154 | + ); |
| 155 | + |
| 156 | + SqlToRddOperator sqlToRddOperator = new SqlToRddOperator(HsqldbPlatform.getInstance()); |
| 157 | + evaluate( |
| 158 | + sqlToRddOperator, |
| 159 | + new ChannelInstance[]{sqlQueryChannelInstance}, |
| 160 | + new ChannelInstance[]{rddChannelInstance} |
| 161 | + ); |
| 162 | + |
| 163 | + List<Record> output = rddChannelInstance.<Record>provideRdd().collect(); |
| 164 | + Assert.assertTrue(output.isEmpty()); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments