|
| 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 | +package org.apache.beam.sdk.extensions.sql.meta.provider; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import org.apache.beam.sdk.schemas.FieldAccessDescriptor; |
| 24 | +import org.apache.beam.sdk.schemas.Schema; |
| 25 | +import org.apache.beam.sdk.schemas.io.PushdownProjector; |
| 26 | +import org.apache.beam.sdk.schemas.io.SchemaIO; |
| 27 | +import org.apache.beam.sdk.schemas.io.SchemaIOProvider; |
| 28 | +import org.apache.beam.sdk.schemas.transforms.Select; |
| 29 | +import org.apache.beam.sdk.transforms.Create; |
| 30 | +import org.apache.beam.sdk.transforms.PTransform; |
| 31 | +import org.apache.beam.sdk.values.PBegin; |
| 32 | +import org.apache.beam.sdk.values.PCollection; |
| 33 | +import org.apache.beam.sdk.values.PInput; |
| 34 | +import org.apache.beam.sdk.values.POutput; |
| 35 | +import org.apache.beam.sdk.values.Row; |
| 36 | +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting; |
| 37 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * A mock {@link org.apache.beam.sdk.extensions.sql.meta.provider.SchemaIOTableProviderWrapper} that |
| 41 | + * reads in-memory data for testing. |
| 42 | + */ |
| 43 | +@VisibleForTesting |
| 44 | +public class TestSchemaIOTableProviderWrapper extends SchemaIOTableProviderWrapper { |
| 45 | + private static final List<Row> rows = new ArrayList<>(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public SchemaIOProvider getSchemaIOProvider() { |
| 49 | + return new TestSchemaIOProvider(); |
| 50 | + } |
| 51 | + |
| 52 | + public static void addRows(Row... newRows) { |
| 53 | + rows.addAll(Arrays.asList(newRows)); |
| 54 | + } |
| 55 | + |
| 56 | + private class TestSchemaIOProvider implements SchemaIOProvider { |
| 57 | + @Override |
| 58 | + public String identifier() { |
| 59 | + return "TestSchemaIOProvider"; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Schema configurationSchema() { |
| 64 | + return Schema.of(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public SchemaIO from(String location, Row configuration, @Nullable Schema dataSchema) { |
| 69 | + return new TestSchemaIO(dataSchema); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean requiresDataSchema() { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public PCollection.IsBounded isBounded() { |
| 79 | + return PCollection.IsBounded.BOUNDED; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private class TestSchemaIO implements SchemaIO { |
| 84 | + private final Schema schema; |
| 85 | + |
| 86 | + TestSchemaIO(Schema schema) { |
| 87 | + this.schema = schema; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Schema schema() { |
| 92 | + return schema; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public PTransform<PBegin, PCollection<Row>> buildReader() { |
| 97 | + // Read all fields by default. |
| 98 | + return new TestPushdownProjector(schema, FieldAccessDescriptor.withAllFields()); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public PTransform<PCollection<Row>, ? extends POutput> buildWriter() { |
| 103 | + throw new UnsupportedOperationException(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@link PTransform} that reads in-memory data for testing. Simulates projection pushdown using |
| 109 | + * {@link Select}. |
| 110 | + */ |
| 111 | + private class TestPushdownProjector extends PTransform<PBegin, PCollection<Row>> |
| 112 | + implements PushdownProjector { |
| 113 | + /** The schema of the input data. */ |
| 114 | + private final Schema schema; |
| 115 | + /** The fields to be projected. */ |
| 116 | + private final FieldAccessDescriptor fieldAccessDescriptor; |
| 117 | + |
| 118 | + TestPushdownProjector(Schema schema, FieldAccessDescriptor fieldAccessDescriptor) { |
| 119 | + this.schema = schema; |
| 120 | + this.fieldAccessDescriptor = fieldAccessDescriptor; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public PTransform<? extends PInput, PCollection<Row>> withProjectionPushdown( |
| 125 | + FieldAccessDescriptor fieldAccessDescriptor) { |
| 126 | + return new TestPushdownProjector(schema, fieldAccessDescriptor); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean supportsFieldReordering() { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public PCollection<Row> expand(PBegin input) { |
| 136 | + // Simulate projection pushdown using Select. In a real IO, projection would be pushed down to |
| 137 | + // the source. |
| 138 | + return input |
| 139 | + .apply(Create.of(rows).withRowSchema(schema)) |
| 140 | + .apply(Select.fieldAccess(fieldAccessDescriptor)); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments