zachjsh commented on code in PR #13627: URL: https://github.com/apache/druid/pull/13627#discussion_r1066585225
########## server/src/main/java/org/apache/druid/catalog/model/table/BaseInputSourceDefn.java: ########## @@ -0,0 +1,278 @@ +/* + * 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 com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.druid.catalog.model.ColumnSpec; +import org.apache.druid.catalog.model.Columns; +import org.apache.druid.catalog.model.TableDefnRegistry; +import org.apache.druid.data.input.InputFormat; +import org.apache.druid.data.input.InputSource; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.utils.CollectionUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Base class for input source definitions. + * + * @see {@link FormattedInputSourceDefn} for the base class for (most) input formats + * which take an input format. + */ +public abstract class BaseInputSourceDefn implements InputSourceDefn +{ + /** + * The "from-scratch" table function for this input source. The parameters + * are those defined by the subclass, and the apply simply turns around and + * asks the input source definition to do the conversion. + */ + public class AdHocTableFunction extends BaseTableFunction + { + public AdHocTableFunction(List<ParameterDefn> parameters) + { + super(parameters); + } + + @Override + public ExternalTableSpec apply( + final String fnName, + final Map<String, Object> args, + final List<ColumnSpec> columns, + final ObjectMapper jsonMapper + ) + { + requireSchema(fnName, columns); + return convertArgsToTable(args, columns, jsonMapper); + } + } + + /** + * The "partial" table function that starts with a catalog external table spec, then + * uses SQL function arguments to "complete" (i.e. fill in) the missing properties to + * produce a complete table which is then converted to an external table which Calcite + * can use. + * <p> + * The set of parameters depends on the input source and on whether or not the catalog + * spec provides a format. + */ + public class PartialTableFunction extends BaseTableFunction + { + private final ResolvedExternalTable table; + + public PartialTableFunction(final ResolvedExternalTable table, List<ParameterDefn> params) + { + super(params); + this.table = table; + } + + @Override + public ExternalTableSpec apply( + final String fnName, + final Map<String, Object> args, + final List<ColumnSpec> columns, + final ObjectMapper jsonMapper + ) + { + if (CollectionUtils.isNullOrEmpty(table.resolvedTable().spec().columns())) { + requireSchema(fnName, columns); + } + return convertCompletedTable(table, args, columns); + } + } + + /** + * The one and only from-scratch table function for this input source. The + * function is defined a bind time, not construction time, since it typically + * needs visibility to the set of available input formats. + */ + private AdHocTableFunction adHocTableFn; + + /** + * Overridden by each subclass to return the input source class to be + * used for JSON conversions. + */ + protected abstract Class<? extends InputSource> inputSourceClass(); + + @Override + public void bind(TableDefnRegistry registry) + { + this.adHocTableFn = defineAdHocTableFunction(); + } + + @Override + public void validate(ResolvedExternalTable table) + { + convertTableToSource(table); + } + + /** + * Overridden by each subclass to define the parameters needed by each + * input source. + */ + protected abstract AdHocTableFunction defineAdHocTableFunction(); + + @Override + public TableFunction adHocTableFn() + { + return adHocTableFn; + } + + /** + * Define a table "from scratch" using SQL function arguments. + */ + protected ExternalTableSpec convertArgsToTable( + final Map<String, Object> args, + final List<ColumnSpec> columns, + final ObjectMapper jsonMapper + ) + { + return new ExternalTableSpec( + convertArgsToSource(args, jsonMapper), + convertArgsToFormat(args, columns, jsonMapper), + Columns.convertSignature(columns) + ); + } + + /** + * Convert the input source using arguments to a "from scratch" table function. + */ + protected InputSource convertArgsToSource(Map<String, Object> args, ObjectMapper jsonMapper) + { + final Map<String, Object> jsonMap = new HashMap<>(); + auditInputSource(jsonMap); + convertArgsToSourceMap(jsonMap, args); + return convertSource(jsonMap, jsonMapper); + } + + /** + * Convert SQL arguments to the corresponding "generic JSON" form in the given map. + * The map will then be adjusted and converted to the actual input source. + */ + protected abstract void convertArgsToSourceMap(Map<String, Object> jsonMap, Map<String, Object> args); + + /** + * Convert SQL arguments, and the column schema, to an input format, if required. + */ + protected InputFormat convertArgsToFormat(Map<String, Object> args, List<ColumnSpec> columns, ObjectMapper jsonMapper) + { + return null; + } + + /** + * Complete a partial table using the table function arguments and columns provided. + * The arguments match the set of parameters used for the function. The columns are + * provided if the SQL included an {@code EXTENDS} clause: the implementation should decide + * if columns are required (or allowed) depending on whether the partial spec already + * defines columns. + * + * @param table the partial table spec, with input source and format parsed into a + * generic Java map + * @param args the argument values provided in the SQL table function call. The arguments + * use the Java types defined in the parameter definitions. + * @param columns the set of columns (if any) from the SQL {@code EXTEND} clause + * + * @return an external table spec which Calcite can consume + */ + protected abstract ExternalTableSpec convertCompletedTable( Review Comment: This this be moved into the `PartialTableFunction` class, since it seems it should only be used in that case? -- 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]
