|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.druid; |
| 15 | + |
| 16 | +import com.facebook.airlift.http.client.HttpClient; |
| 17 | +import com.facebook.airlift.http.client.HttpStatus; |
| 18 | +import com.facebook.airlift.http.client.testing.TestingHttpClient; |
| 19 | +import com.facebook.airlift.http.client.testing.TestingResponse; |
| 20 | +import com.facebook.presto.common.Page; |
| 21 | +import com.facebook.presto.common.block.Block; |
| 22 | +import com.facebook.presto.spi.ColumnHandle; |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | +import com.google.common.collect.ImmutableListMultimap; |
| 25 | +import com.google.common.collect.ListMultimap; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | + |
| 30 | +import static com.facebook.presto.common.type.BigintType.BIGINT; |
| 31 | +import static com.facebook.presto.common.type.DoubleType.DOUBLE; |
| 32 | +import static com.facebook.presto.common.type.VarcharType.VARCHAR; |
| 33 | +import static org.testng.Assert.assertTrue; |
| 34 | + |
| 35 | +public class TestDruidPageSourceNullHandling |
| 36 | +{ |
| 37 | + @Test |
| 38 | + public void testNullAndMissingColumns() |
| 39 | + { |
| 40 | + String jsonRows = |
| 41 | + "{\"region.Id\":1,\"city\":\"Boston\",\"fare\":10.0}\n" + |
| 42 | + "{\"region.Id\":2,\"city\":null,\"fare\":20.0}\n" + // city column is having null value |
| 43 | + "{\"region.Id\":3,\"fare\":30.0}\n" + // missing city column |
| 44 | + "\n"; |
| 45 | + |
| 46 | + ListMultimap<String, String> headers = ImmutableListMultimap.of( |
| 47 | + "Content-Type", "application/json"); |
| 48 | + TestingResponse response = new TestingResponse( |
| 49 | + HttpStatus.OK, |
| 50 | + headers, |
| 51 | + jsonRows.getBytes(StandardCharsets.UTF_8)); |
| 52 | + HttpClient httpClient = new TestingHttpClient(request -> response); |
| 53 | + |
| 54 | + DruidConfig druidConfig = new DruidConfig() |
| 55 | + .setDruidSchema("default") |
| 56 | + .setDruidCoordinatorUrl("http://localhost:8081") |
| 57 | + .setDruidBrokerUrl("http://localhost:8082"); |
| 58 | + |
| 59 | + ImmutableList<ColumnHandle> columnHandles = ImmutableList.of(new DruidColumnHandle("region.Id", BIGINT), |
| 60 | + new DruidColumnHandle("city", VARCHAR), |
| 61 | + new DruidColumnHandle("fare", DOUBLE)); |
| 62 | + |
| 63 | + DruidBrokerPageSource pageSource = new DruidBrokerPageSource( |
| 64 | + new DruidQueryGenerator.GeneratedDql("testTable", "SELECT region.Id, city, fare FROM test", true), |
| 65 | + columnHandles, |
| 66 | + new DruidClient(druidConfig, httpClient)); |
| 67 | + |
| 68 | + Page page; |
| 69 | + boolean foundNull = false; |
| 70 | + boolean foundMissing = false; |
| 71 | + |
| 72 | + while ((page = pageSource.getNextPage()) != null) { |
| 73 | + Block cityBlock = page.getBlock(1); |
| 74 | + for (int i = 0; i < cityBlock.getPositionCount(); i++) { |
| 75 | + if (cityBlock.isNull(i)) { |
| 76 | + if (i == 1) { |
| 77 | + foundNull = true; // row with "city":null |
| 78 | + } |
| 79 | + if (i == 2) { |
| 80 | + foundMissing = true; // row missing "city" |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + assertTrue(foundNull, "Expected null value in column 'city'"); |
| 87 | + assertTrue(foundMissing, "Expected missing column to be treated as null"); |
| 88 | + } |
| 89 | +} |
0 commit comments