|
| 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.paimon.table.source; |
| 20 | + |
| 21 | +import org.apache.paimon.catalog.TableQueryAuthResult; |
| 22 | +import org.apache.paimon.data.BinaryRow; |
| 23 | +import org.apache.paimon.table.FallbackReadFileStoreTable; |
| 24 | +import org.apache.paimon.table.FallbackReadFileStoreTable.FallbackDataSplit; |
| 25 | +import org.apache.paimon.table.FallbackReadFileStoreTable.FallbackSplit; |
| 26 | +import org.apache.paimon.table.FallbackReadFileStoreTable.FallbackSplitImpl; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | + |
| 32 | +import static org.apache.paimon.io.DataFileTestUtils.newFile; |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +/** Test for {@link Splits}. */ |
| 36 | +public class SplitsTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testDataSplitIsItsOwnUnderlyingSplit() { |
| 40 | + DataSplit split = dataSplit(); |
| 41 | + |
| 42 | + assertThat(Splits.underlying(split)).isSameAs(split); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testAuthorizationWrapperIsLookedThrough() { |
| 47 | + DataSplit split = dataSplit(); |
| 48 | + |
| 49 | + assertThat(Splits.underlying(withQueryAuth(split))).isSameAs(split); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testFallbackWrapperAroundAuthorizationWrapperIsLookedThrough() { |
| 54 | + DataSplit split = dataSplit(); |
| 55 | + Split nested = toFallbackSplit(withQueryAuth(split)); |
| 56 | + |
| 57 | + assertThat(nested).isInstanceOf(FallbackSplitImpl.class); |
| 58 | + assertThat(((FallbackSplit) nested).wrapped()).isInstanceOf(QueryAuthSplit.class); |
| 59 | + assertThat(Splits.underlying(nested)).isSameAs(split); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testFallbackDataSplitIsItsOwnUnderlyingSplit() { |
| 64 | + DataSplit split = dataSplit(); |
| 65 | + Split fallback = toFallbackSplit(split); |
| 66 | + |
| 67 | + assertThat(fallback).isInstanceOf(FallbackDataSplit.class); |
| 68 | + assertThat(Splits.underlying(fallback)).isSameAs(fallback); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testSplitThatIsNeitherADataSplitNorAWrapperIsItsOwnUnderlyingSplit() { |
| 73 | + ChainSplit split = chainSplit(); |
| 74 | + |
| 75 | + assertThat(Splits.underlying(split)).isSameAs(split); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testFallbackWrapperAroundASplitThatIsNotADataSplitIsLookedThrough() { |
| 80 | + ChainSplit split = chainSplit(); |
| 81 | + |
| 82 | + assertThat(Splits.underlying(toFallbackSplit(split))).isSameAs(split); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testAuthorizationWrapperAroundASplitThatIsNotADataSplitIsLookedThrough() { |
| 87 | + ChainSplit split = chainSplit(); |
| 88 | + |
| 89 | + assertThat(Splits.underlying(withQueryAuth(split))).isSameAs(split); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testAuthorizationWrapperAroundAFallbackDataSplitStopsAtTheFallbackDataSplit() { |
| 94 | + Split fallback = toFallbackSplit(dataSplit()); |
| 95 | + |
| 96 | + assertThat(Splits.underlying(withQueryAuth(fallback))).isSameAs(fallback); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testSplitCarryingNoAuthorizationHasNoAuthResult() { |
| 101 | + assertThat(Splits.authResult(dataSplit())).isNull(); |
| 102 | + assertThat(Splits.authResult(chainSplit())).isNull(); |
| 103 | + assertThat(Splits.authResult(toFallbackSplit(dataSplit()))).isNull(); |
| 104 | + assertThat(Splits.authResult(toFallbackSplit(chainSplit()))).isNull(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testAuthResultIsReadThroughTheAuthorizationWrapper() { |
| 109 | + TableQueryAuthResult authResult = authResult(); |
| 110 | + |
| 111 | + assertThat(Splits.authResult(new QueryAuthSplit(dataSplit(), authResult))) |
| 112 | + .isSameAs(authResult); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testAuthResultIsReadThroughAFallbackWrapper() { |
| 117 | + TableQueryAuthResult authResult = authResult(); |
| 118 | + Split nested = toFallbackSplit(new QueryAuthSplit(dataSplit(), authResult)); |
| 119 | + |
| 120 | + assertThat(Splits.authResult(nested)).isSameAs(authResult); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testAuthResultIsReadFromAWrapperAroundAFallbackDataSplit() { |
| 125 | + TableQueryAuthResult authResult = authResult(); |
| 126 | + Split split = new QueryAuthSplit(toFallbackSplit(dataSplit()), authResult); |
| 127 | + |
| 128 | + assertThat(Splits.authResult(split)).isSameAs(authResult); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testAuthorizationWrapperWithoutRulesHasNoAuthResult() { |
| 133 | + assertThat(Splits.authResult(new QueryAuthSplit(dataSplit(), null))).isNull(); |
| 134 | + } |
| 135 | + |
| 136 | + private static TableQueryAuthResult authResult() { |
| 137 | + return new TableQueryAuthResult( |
| 138 | + null, Collections.singletonMap("name", "{\"name\":\"NULL\"}")); |
| 139 | + } |
| 140 | + |
| 141 | + private static DataSplit dataSplit() { |
| 142 | + return DataSplit.builder() |
| 143 | + .withSnapshot(1) |
| 144 | + .withPartition(BinaryRow.EMPTY_ROW) |
| 145 | + .withBucket(0) |
| 146 | + .withBucketPath("bucket-0") |
| 147 | + .withDataFiles(Collections.singletonList(newFile(0L, 1L))) |
| 148 | + .build(); |
| 149 | + } |
| 150 | + |
| 151 | + private static ChainSplit chainSplit() { |
| 152 | + return new ChainSplit( |
| 153 | + BinaryRow.EMPTY_ROW, |
| 154 | + Collections.singletonList(newFile(0L, 1L)), |
| 155 | + Collections.emptyMap(), |
| 156 | + Collections.emptyMap(), |
| 157 | + null); |
| 158 | + } |
| 159 | + |
| 160 | + private static Split withQueryAuth(Split split) { |
| 161 | + return new QueryAuthSplit(split, new TableQueryAuthResult(null, null)); |
| 162 | + } |
| 163 | + |
| 164 | + private static Split toFallbackSplit(Split split) { |
| 165 | + return FallbackReadFileStoreTable.toFallbackSplit(split, true); |
| 166 | + } |
| 167 | +} |
0 commit comments