|
| 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.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | +import org.apache.wayang.basic.data.Record; |
| 24 | +import org.apache.wayang.core.api.Configuration; |
| 25 | +import org.apache.wayang.core.api.Job; |
| 26 | +import org.apache.wayang.core.function.PredicateDescriptor; |
| 27 | +import org.apache.wayang.core.optimizer.OptimizationContext; |
| 28 | +import org.apache.wayang.core.plan.executionplan.ExecutionTask; |
| 29 | +import org.apache.wayang.core.plan.executionplan.ExecutionStage; |
| 30 | +import org.apache.wayang.core.plan.wayangplan.ExecutionOperator; |
| 31 | +import org.apache.wayang.core.plan.wayangplan.OutputSlot; |
| 32 | +import org.apache.wayang.core.platform.ChannelInstance; |
| 33 | +import org.apache.wayang.core.platform.CrossPlatformExecutor; |
| 34 | +import org.apache.wayang.core.profiling.FullInstrumentationStrategy; |
| 35 | +import org.apache.wayang.java.channels.StreamChannel; |
| 36 | +import org.apache.wayang.java.execution.JavaExecutor; |
| 37 | +import org.apache.wayang.java.platform.JavaPlatform; |
| 38 | +import org.apache.wayang.jdbc.channels.SqlQueryChannel; |
| 39 | +import org.apache.wayang.jdbc.test.HsqldbJoinOperator; |
| 40 | +import org.apache.wayang.jdbc.test.HsqldbPlatform; |
| 41 | +import org.apache.wayang.jdbc.test.HsqldbTableSource; |
| 42 | +import org.apache.wayang.core.function.TransformationDescriptor; |
| 43 | +import org.apache.wayang.jdbc.execution.JdbcExecutor; |
| 44 | +import org.apache.wayang.core.profiling.NoInstrumentationStrategy; |
| 45 | +import org.apache.wayang.core.optimizer.DefaultOptimizationContext; |
| 46 | + |
| 47 | +import java.sql.Connection; |
| 48 | +import java.sql.SQLException; |
| 49 | +import java.sql.Statement; |
| 50 | +import java.util.Arrays; |
| 51 | +import java.util.List; |
| 52 | +import java.util.stream.Collectors; |
| 53 | +import java.util.Collections; |
| 54 | + |
| 55 | +import static org.mockito.Mockito.mock; |
| 56 | +import static org.mockito.Mockito.when; |
| 57 | + |
| 58 | +/** |
| 59 | + * Test suite for {@link SqlToStreamOperator}. |
| 60 | + */ |
| 61 | +public class JdbcJoinOperatorTest extends OperatorTestBase { |
| 62 | + @Test |
| 63 | + public void testWithHsqldb() throws SQLException { |
| 64 | + Configuration configuration = new Configuration(); |
| 65 | + |
| 66 | + Job job = mock(Job.class); |
| 67 | + when(job.getConfiguration()).thenReturn(configuration); |
| 68 | + when(job.getCrossPlatformExecutor()).thenReturn(new CrossPlatformExecutor(job, new NoInstrumentationStrategy())); |
| 69 | + SqlQueryChannel.Descriptor sqlChannelDescriptor = HsqldbPlatform.getInstance().getSqlQueryChannelDescriptor(); |
| 70 | + |
| 71 | + HsqldbPlatform hsqldbPlatform = new HsqldbPlatform(); |
| 72 | + |
| 73 | + ExecutionStage sqlStage = mock(ExecutionStage.class); |
| 74 | + |
| 75 | + // Create some test data. |
| 76 | + try (Connection jdbcConnection = hsqldbPlatform.createDatabaseDescriptor(configuration).createJdbcConnection()) { |
| 77 | + final Statement statement = jdbcConnection.createStatement(); |
| 78 | + statement.execute("CREATE TABLE testA (a INT, b VARCHAR(6));"); |
| 79 | + statement.execute("INSERT INTO testA VALUES (0, 'zero');"); |
| 80 | + statement.execute("CREATE TABLE testB (a INT, b INT);"); |
| 81 | + statement.execute("INSERT INTO testB VALUES (0, 100);"); |
| 82 | + } |
| 83 | + |
| 84 | + JdbcTableSource tableSourceA = new HsqldbTableSource("testA"); |
| 85 | + JdbcTableSource tableSourceB = new HsqldbTableSource("testB"); |
| 86 | + |
| 87 | + ExecutionTask tableSourceATask = new ExecutionTask(tableSourceA); |
| 88 | + tableSourceATask.setOutputChannel(0, new SqlQueryChannel(sqlChannelDescriptor, tableSourceA.getOutput(0))); |
| 89 | + tableSourceATask.setStage(sqlStage); |
| 90 | + |
| 91 | + ExecutionTask tableSourceBTask = new ExecutionTask(tableSourceB); |
| 92 | + tableSourceBTask.setOutputChannel(0, new SqlQueryChannel(sqlChannelDescriptor, tableSourceB.getOutput(0))); |
| 93 | + tableSourceBTask.setStage(sqlStage); |
| 94 | + |
| 95 | + final ExecutionOperator joinOperator = new HsqldbJoinOperator<Integer>( |
| 96 | + new TransformationDescriptor<Record, Integer>( |
| 97 | + (record) -> (Integer) record.getField(0), |
| 98 | + Record.class, |
| 99 | + Integer.class |
| 100 | + ).withSqlImplementation("testA", "a"), |
| 101 | + new TransformationDescriptor<Record, Integer>( |
| 102 | + (record) -> (Integer) record.getField(0), |
| 103 | + Record.class, |
| 104 | + Integer.class |
| 105 | + ).withSqlImplementation("testB", "a") |
| 106 | + ); |
| 107 | + |
| 108 | + ExecutionTask joinTask = new ExecutionTask(joinOperator); |
| 109 | + tableSourceATask.getOutputChannel(0).addConsumer(joinTask, 0); |
| 110 | + tableSourceBTask.getOutputChannel(0).addConsumer(joinTask, 1); |
| 111 | + joinTask.setOutputChannel(0, new SqlQueryChannel(sqlChannelDescriptor, joinOperator.getOutput(0))); |
| 112 | + joinTask.setStage(sqlStage); |
| 113 | + |
| 114 | + when(sqlStage.getStartTasks()).thenReturn(Collections.singleton(tableSourceATask)); |
| 115 | + when(sqlStage.getTerminalTasks()).thenReturn(Collections.singleton(joinTask)); |
| 116 | + |
| 117 | + ExecutionStage nextStage = mock(ExecutionStage.class); |
| 118 | + |
| 119 | + SqlToStreamOperator sqlToStreamOperator = new SqlToStreamOperator(HsqldbPlatform.getInstance()); |
| 120 | + ExecutionTask sqlToStreamTask = new ExecutionTask(sqlToStreamOperator); |
| 121 | + joinTask.getOutputChannel(0).addConsumer(sqlToStreamTask, 0); |
| 122 | + sqlToStreamTask.setStage(nextStage); |
| 123 | + |
| 124 | + JdbcExecutor executor = new JdbcExecutor(HsqldbPlatform.getInstance(), job); |
| 125 | + executor.execute(sqlStage, new DefaultOptimizationContext(job), job.getCrossPlatformExecutor()); |
| 126 | + |
| 127 | + SqlQueryChannel.Instance sqlQueryChannelInstance = |
| 128 | + (SqlQueryChannel.Instance) job.getCrossPlatformExecutor().getChannelInstance(sqlToStreamTask.getInputChannel(0)); |
| 129 | + |
| 130 | + System.out.println(); |
| 131 | + |
| 132 | + Assert.assertEquals( |
| 133 | + "SELECT * FROM testA JOIN testA ON testB.a=testA.a;", |
| 134 | + sqlQueryChannelInstance.getSqlQuery() |
| 135 | + ); |
| 136 | + } |
| 137 | +} |
0 commit comments