Search in sources :

Example 1 with TableParamDef

use of org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.TableParamDef in project drill by apache.

the class FormatPluginOptionsDescriptor method presentParams.

/**
   * @return a readable String of the parameters and their names
   */
String presentParams() {
    StringBuilder sb = new StringBuilder("(");
    List<TableParamDef> params = params();
    for (int i = 0; i < params.size(); i++) {
        TableParamDef paramDef = params.get(i);
        if (i != 0) {
            sb.append(", ");
        }
        sb.append(paramDef.name).append(": ").append(paramDef.type.getSimpleName());
    }
    sb.append(")");
    return sb.toString();
}
Also used : TableParamDef(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.TableParamDef)

Example 2 with TableParamDef

use of org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.TableParamDef in project drill by apache.

the class FormatPluginOptionsDescriptor method createConfigForTable.

/**
   * creates an instance of the FormatPluginConfig based on the passed parameters
   * @param t the signature and the parameters passed to the table function
   * @return the corresponding config
   */
FormatPluginConfig createConfigForTable(TableInstance t) {
    // Per the constructor, the first param is always "type"
    TableParamDef typeParamDef = t.sig.params.get(0);
    Object typeParam = t.params.get(0);
    if (!typeParamDef.name.equals("type") || typeParamDef.type != String.class || !(typeParam instanceof String) || !typeName.equalsIgnoreCase((String) typeParam)) {
        // if we reach here, there's a bug as all signatures generated start with a type parameter
        throw UserException.parseError().message("This function signature is not supported: %s\n" + "expecting %s", t.presentParams(), this.presentParams()).addContext("table", t.sig.name).build(logger);
    }
    FormatPluginConfig config;
    try {
        config = pluginConfigClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw UserException.parseError(e).message("configuration for format of type %s can not be created (class: %s)", this.typeName, pluginConfigClass.getName()).addContext("table", t.sig.name).build(logger);
    }
    for (int i = 1; i < t.params.size(); i++) {
        Object param = t.params.get(i);
        if (param == null) {
            // when null is passed, we leave the default defined in the config class
            continue;
        }
        if (param instanceof String) {
            // normalize Java literals, ex: \t, \n, \r
            param = StringEscapeUtils.unescapeJava((String) param);
        }
        TableParamDef paramDef = t.sig.params.get(i);
        TableParamDef expectedParamDef = this.functionParamsByName.get(paramDef.name);
        if (expectedParamDef == null || expectedParamDef.type != paramDef.type) {
            throw UserException.parseError().message("The parameters provided are not applicable to the type specified:\n" + "provided: %s\nexpected: %s", t.presentParams(), this.presentParams()).addContext("table", t.sig.name).build(logger);
        }
        try {
            Field field = pluginConfigClass.getField(paramDef.name);
            field.setAccessible(true);
            if (field.getType() == char.class && param instanceof String) {
                String stringParam = (String) param;
                if (stringParam.length() != 1) {
                    throw UserException.parseError().message("Expected single character but was String: %s", stringParam).addContext("table", t.sig.name).addContext("parameter", paramDef.name).build(logger);
                }
                param = stringParam.charAt(0);
            }
            field.set(config, param);
        } catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
            throw UserException.parseError(e).message("can not set value %s to parameter %s: %s", param, paramDef.name, paramDef.type).addContext("table", t.sig.name).addContext("parameter", paramDef.name).build(logger);
        }
    }
    return config;
}
Also used : Field(java.lang.reflect.Field) TableParamDef(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.TableParamDef) FormatPluginConfig(org.apache.drill.common.logical.FormatPluginConfig)

Aggregations

TableParamDef (org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.TableParamDef)2 Field (java.lang.reflect.Field)1 FormatPluginConfig (org.apache.drill.common.logical.FormatPluginConfig)1