zachjsh commented on code in PR #13165: URL: https://github.com/apache/druid/pull/13165#discussion_r1015664614
########## server/src/main/java/org/apache/druid/catalog/model/table/InputFormats.java: ########## @@ -0,0 +1,301 @@ +/* + * 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.druid.catalog.model.table; + +import org.apache.curator.shaded.com.google.common.collect.ImmutableList; +import org.apache.druid.catalog.model.CatalogUtils; +import org.apache.druid.catalog.model.ModelProperties.BooleanPropertyDefn; +import org.apache.druid.catalog.model.ModelProperties.IntPropertyDefn; +import org.apache.druid.catalog.model.ModelProperties.PropertyDefn; +import org.apache.druid.catalog.model.ModelProperties.SimplePropertyDefn; +import org.apache.druid.catalog.model.ModelProperties.StringPropertyDefn; +import org.apache.druid.catalog.model.ResolvedTable; +import org.apache.druid.data.input.InputFormat; +import org.apache.druid.data.input.impl.CsvInputFormat; +import org.apache.druid.data.input.impl.DelimitedInputFormat; +import org.apache.druid.data.input.impl.JsonInputFormat; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.ISE; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Definition of the input formats which converts from property + * lists in table specs to subclasses of {@link InputFormat}. + */ +public class InputFormats +{ + public interface InputFormatDefn + { + String name(); + String typeTag(); + List<PropertyDefn<?>> properties(); + void validate(ResolvedTable table); + InputFormat convert(ResolvedTable table); + } + + public abstract static class BaseFormatDefn implements InputFormatDefn + { + private final String name; + private final String typeTag; + private final List<PropertyDefn<?>> properties; + + public BaseFormatDefn( + final String name, + final String typeTag, + final List<PropertyDefn<?>> properties + ) + { + this.name = name; + this.typeTag = typeTag; + this.properties = properties; + } + + @Override + public String name() + { + return name; + } + + @Override + public String typeTag() + { + return typeTag; + } + + @Override + public List<PropertyDefn<?>> properties() + { + return properties; + } + + @Override + public void validate(ResolvedTable table) + { + convert(table); + } + } + + /** + * Definition of a flat text (CSV and delimited text) input format. + * <p> + * Note that not all the fields in + * {@link org.apache.druid.data.input.impl.FlatTextInputFormat + * FlatTextInputFormat} appear here: + * <ul> + * <li>{@code findColumnsFromHeader} - not yet supported in MSQ.</li> + * <li>{@code hasHeaderRow} - Always set to false since we don't bother to read + * it. {@code skipHeaderRows} is used to specify the number of header + * rows to skip.</li> + * </ul> + */ + public abstract static class FlatTextFormatDefn extends BaseFormatDefn + { + public static final String LIST_DELIMITER_PROPERTY = "listDelimiter"; + public static final String SKIP_ROWS_PROPERTY = "skipRows"; + + public FlatTextFormatDefn( + final String name, + final String typeTag, + final List<PropertyDefn<?>> properties + ) + { + super( + name, + typeTag, + CatalogUtils.concatLists( + Arrays.asList( + new StringPropertyDefn(LIST_DELIMITER_PROPERTY), + new IntPropertyDefn(SKIP_ROWS_PROPERTY) + ), + properties + ) + ); + } + + protected Map<String, Object> gatherFields(ResolvedTable table) + { + Map<String, Object> jsonMap = new HashMap<>(); + jsonMap.put(InputFormat.TYPE_PROPERTY, CsvInputFormat.TYPE_KEY); + jsonMap.put("listDelimiter", table.property(LIST_DELIMITER_PROPERTY)); + // hasHeaderRow is required, even though we don't infer headers. + jsonMap.put("hasHeaderRow", false); + jsonMap.put("findColumnsFromHeader", false); + // Column list is required. Infer from schema. Review Comment: Didnt realize that MSQ does not support inferring columns from headers. When it does, maybe should revisit this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
