arina-ielchiieva commented on a change in pull request #1501: DRILL-6791: Scan projection framework URL: https://github.com/apache/drill/pull/1501#discussion_r233455585
########## File path: exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/scan/project/TestNullColumnLoader.java ########## @@ -0,0 +1,329 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill.exec.physical.impl.scan.project; + +import static org.junit.Assert.assertSame; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.drill.common.types.TypeProtos.DataMode; +import org.apache.drill.common.types.TypeProtos.MajorType; +import org.apache.drill.common.types.TypeProtos.MinorType; +import org.apache.drill.common.types.Types; +import org.apache.drill.exec.physical.impl.scan.project.NullColumnBuilder; +import org.apache.drill.exec.physical.impl.scan.project.NullColumnLoader; +import org.apache.drill.exec.physical.impl.scan.project.ResolvedNullColumn; +import org.apache.drill.exec.physical.rowSet.ResultVectorCache; +import org.apache.drill.exec.physical.rowSet.impl.NullResultVectorCacheImpl; +import org.apache.drill.exec.physical.rowSet.impl.ResultVectorCacheImpl; +import org.apache.drill.exec.record.BatchSchema; +import org.apache.drill.exec.record.VectorContainer; +import org.apache.drill.exec.vector.ValueVector; +import org.apache.drill.test.SubOperatorTest; +import org.apache.drill.test.rowSet.RowSet.SingleRowSet; +import org.apache.drill.test.rowSet.schema.SchemaBuilder; +import org.apache.drill.test.rowSet.RowSetComparison; +import org.junit.Test; + +/** + * Test the mechanism that handles all-null columns during projection. + * An all-null column is one projected in the query, but which does + * not actually exist in the underlying data source (or input + * operator.) + * <p> + * In anticipation of having type information, this mechanism + * can create the classic nullable Int null column, or one of + * any other type and mode. + */ + +public class TestNullColumnLoader extends SubOperatorTest { + + private ResolvedNullColumn makeNullCol(String name, MajorType nullType) { + + // For this test, we don't need the projection, so just + // set it to null. + + return new ResolvedNullColumn(name, nullType, null, 0); + } + + private ResolvedNullColumn makeNullCol(String name) { + return makeNullCol(name, null); + } + + /** + * Test the simplest case: default null type, nothing in the vector + * cache. Specify no column type, the special NULL type, or a + * predefined type. Output types should be set accordingly. + */ + + @Test + public void testBasics() { + + final List<ResolvedNullColumn> defns = new ArrayList<>(); + defns.add(makeNullCol("unspecified", null)); + defns.add(makeNullCol("nullType", Types.optional(MinorType.NULL))); + defns.add(makeNullCol("specifiedOpt", Types.optional(MinorType.VARCHAR))); + defns.add(makeNullCol("specifiedReq", Types.required(MinorType.VARCHAR))); + defns.add(makeNullCol("specifiedArray", Types.repeated(MinorType.VARCHAR))); + + final ResultVectorCache cache = new NullResultVectorCacheImpl(fixture.allocator()); + final NullColumnLoader staticLoader = new NullColumnLoader(cache, defns, null, false); + + // Create a batch + + final VectorContainer output = staticLoader.load(2); + + // Verify values and types + + final BatchSchema expectedSchema = new SchemaBuilder() + .add("unspecified", NullColumnLoader.DEFAULT_NULL_TYPE) + .add("nullType", NullColumnLoader.DEFAULT_NULL_TYPE) + .addNullable("specifiedOpt", MinorType.VARCHAR) + .addNullable("specifiedReq", MinorType.VARCHAR) + .addArray("specifiedArray", MinorType.VARCHAR) + .build(); + final SingleRowSet expected = fixture.rowSetBuilder(expectedSchema) + .addRow(null, null, null, null, new String[] {}) + .addRow(null, null, null, null, new String[] {}) + .build(); + + new RowSetComparison(expected) + .verifyAndClearAll(fixture.wrap(output)); + staticLoader.close(); + } + + /** + * Test the ability to use a type other than nullable INT for null + * columns. This occurs, for example, in the CSV reader where no + * column is ever INT (nullable or otherwise) and we want our null + * columns to be (non-nullable) VARCHAR. + */ + + @Test + public void testCustomNullType() { + + final List<ResolvedNullColumn> defns = new ArrayList<>(); + defns.add(makeNullCol("unspecified", null)); + defns.add(makeNullCol("nullType", MajorType.newBuilder() + .setMinorType(MinorType.NULL) + .setMode(DataMode.OPTIONAL) + .build())); + defns.add(makeNullCol("nullTypeReq", MajorType.newBuilder() + .setMinorType(MinorType.NULL) + .setMode(DataMode.REQUIRED) Review comment: How null can be required? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
