Search in sources :

Example 1 with FunctionWork

use of org.apache.hadoop.hive.ql.plan.FunctionWork in project hive by apache.

the class MacroSemanticAnalyzer method analyzeDropMacro.

@SuppressWarnings("unchecked")
private void analyzeDropMacro(ASTNode ast) throws SemanticException {
    String functionName = ast.getChild(0).getText();
    boolean ifExists = (ast.getFirstChildWithType(TOK_IFEXISTS) != null);
    // we want to signal an error if the function doesn't exist and we're
    // configured not to ignore this
    boolean throwException = !ifExists && !HiveConf.getBoolVar(conf, ConfVars.DROPIGNORESNONEXISTENT);
    // Temp macros are not allowed to have qualified names.
    if (FunctionUtils.isQualifiedFunctionName(functionName)) {
        throw new SemanticException("Temporary macro name cannot be a qualified name.");
    }
    if (throwException && FunctionRegistry.getFunctionInfo(functionName) == null) {
        throw new SemanticException(ErrorMsg.INVALID_FUNCTION.getMsg(functionName));
    }
    DropMacroDesc desc = new DropMacroDesc(functionName);
    rootTasks.add(TaskFactory.get(new FunctionWork(desc), conf));
    addEntities();
}
Also used : DropMacroDesc(org.apache.hadoop.hive.ql.plan.DropMacroDesc) FunctionWork(org.apache.hadoop.hive.ql.plan.FunctionWork)

Example 2 with FunctionWork

use of org.apache.hadoop.hive.ql.plan.FunctionWork in project hive by apache.

the class FunctionSemanticAnalyzer method analyzeCreateFunction.

private void analyzeCreateFunction(ASTNode ast) throws SemanticException {
    // ^(TOK_CREATEFUNCTION identifier StringLiteral ({isTempFunction}? => TOK_TEMPORARY))
    String functionName = ast.getChild(0).getText().toLowerCase();
    boolean isTemporaryFunction = (ast.getFirstChildWithType(HiveParser.TOK_TEMPORARY) != null);
    String className = unescapeSQLString(ast.getChild(1).getText());
    // Temp functions are not allowed to have qualified names.
    if (isTemporaryFunction && FunctionUtils.isQualifiedFunctionName(functionName)) {
        throw new SemanticException("Temporary function cannot be created with a qualified name.");
    }
    // find any referenced resources
    List<ResourceUri> resources = getResourceList(ast);
    CreateFunctionDesc desc = new CreateFunctionDesc(functionName, isTemporaryFunction, className, resources);
    rootTasks.add(TaskFactory.get(new FunctionWork(desc), conf));
    addEntities(functionName, isTemporaryFunction, resources);
}
Also used : ResourceUri(org.apache.hadoop.hive.metastore.api.ResourceUri) CreateFunctionDesc(org.apache.hadoop.hive.ql.plan.CreateFunctionDesc) FunctionWork(org.apache.hadoop.hive.ql.plan.FunctionWork)

Example 3 with FunctionWork

use of org.apache.hadoop.hive.ql.plan.FunctionWork in project hive by apache.

the class FunctionSemanticAnalyzer method analyzeDropFunction.

private void analyzeDropFunction(ASTNode ast) throws SemanticException {
    // ^(TOK_DROPFUNCTION identifier ifExists? $temp?)
    String functionName = ast.getChild(0).getText();
    boolean ifExists = (ast.getFirstChildWithType(HiveParser.TOK_IFEXISTS) != null);
    // we want to signal an error if the function doesn't exist and we're
    // configured not to ignore this
    boolean throwException = !ifExists && !HiveConf.getBoolVar(conf, ConfVars.DROPIGNORESNONEXISTENT);
    FunctionInfo info = FunctionRegistry.getFunctionInfo(functionName);
    if (info == null) {
        if (throwException) {
            throw new SemanticException(ErrorMsg.INVALID_FUNCTION.getMsg(functionName));
        } else {
            // Fail silently
            return;
        }
    } else if (info.isBuiltIn()) {
        throw new SemanticException(ErrorMsg.DROP_NATIVE_FUNCTION.getMsg(functionName));
    }
    boolean isTemporaryFunction = (ast.getFirstChildWithType(HiveParser.TOK_TEMPORARY) != null);
    DropFunctionDesc desc = new DropFunctionDesc(functionName, isTemporaryFunction);
    rootTasks.add(TaskFactory.get(new FunctionWork(desc), conf));
    addEntities(functionName, isTemporaryFunction, null);
}
Also used : DropFunctionDesc(org.apache.hadoop.hive.ql.plan.DropFunctionDesc) FunctionInfo(org.apache.hadoop.hive.ql.exec.FunctionInfo) FunctionWork(org.apache.hadoop.hive.ql.plan.FunctionWork)

Example 4 with FunctionWork

use of org.apache.hadoop.hive.ql.plan.FunctionWork in project hive by apache.

the class MacroSemanticAnalyzer method analyzeCreateMacro.

@SuppressWarnings("unchecked")
private void analyzeCreateMacro(ASTNode ast) throws SemanticException {
    String functionName = ast.getChild(0).getText();
    // Temp macros are not allowed to have qualified names.
    if (FunctionUtils.isQualifiedFunctionName(functionName)) {
        throw new SemanticException("Temporary macro cannot be created with a qualified name.");
    }
    List<FieldSchema> arguments = BaseSemanticAnalyzer.getColumns((ASTNode) ast.getChild(1), true);
    boolean isNoArgumentMacro = arguments.size() == 0;
    RowResolver rowResolver = new RowResolver();
    ArrayList<String> macroColNames = new ArrayList<String>(arguments.size());
    ArrayList<TypeInfo> macroColTypes = new ArrayList<TypeInfo>(arguments.size());
    final Set<String> actualColumnNames = new HashSet<String>();
    if (!isNoArgumentMacro) {
        /*
       * Walk down expression to see which arguments are actually used.
       */
        Node expression = (Node) ast.getChild(2);
        PreOrderWalker walker = new PreOrderWalker(new Dispatcher() {

            @Override
            public Object dispatch(Node nd, Stack<Node> stack, Object... nodeOutputs) throws SemanticException {
                if (nd instanceof ASTNode) {
                    ASTNode node = (ASTNode) nd;
                    if (node.getType() == HiveParser.TOK_TABLE_OR_COL) {
                        actualColumnNames.add(node.getChild(0).getText());
                    }
                }
                return null;
            }
        });
        walker.startWalking(Collections.singletonList(expression), null);
    }
    for (FieldSchema argument : arguments) {
        TypeInfo colType = TypeInfoUtils.getTypeInfoFromTypeString(argument.getType());
        rowResolver.put("", argument.getName(), new ColumnInfo(argument.getName(), colType, "", false));
        macroColNames.add(argument.getName());
        macroColTypes.add(colType);
    }
    Set<String> expectedColumnNames = new LinkedHashSet<String>(macroColNames);
    if (!expectedColumnNames.equals(actualColumnNames)) {
        throw new SemanticException("Expected columns " + expectedColumnNames + " but found " + actualColumnNames);
    }
    if (expectedColumnNames.size() != macroColNames.size()) {
        throw new SemanticException("At least one parameter name was used more than once " + macroColNames);
    }
    SemanticAnalyzer sa = HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CBO_ENABLED) ? new CalcitePlanner(queryState) : new SemanticAnalyzer(queryState);
    ;
    ExprNodeDesc body;
    if (isNoArgumentMacro) {
        body = sa.genExprNodeDesc((ASTNode) ast.getChild(1), rowResolver);
    } else {
        body = sa.genExprNodeDesc((ASTNode) ast.getChild(2), rowResolver);
    }
    CreateMacroDesc desc = new CreateMacroDesc(functionName, macroColNames, macroColTypes, body);
    rootTasks.add(TaskFactory.get(new FunctionWork(desc), conf));
    addEntities();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CreateMacroDesc(org.apache.hadoop.hive.ql.plan.CreateMacroDesc) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) Node(org.apache.hadoop.hive.ql.lib.Node) ArrayList(java.util.ArrayList) ColumnInfo(org.apache.hadoop.hive.ql.exec.ColumnInfo) Dispatcher(org.apache.hadoop.hive.ql.lib.Dispatcher) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FunctionWork(org.apache.hadoop.hive.ql.plan.FunctionWork) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) PreOrderWalker(org.apache.hadoop.hive.ql.lib.PreOrderWalker)

Aggregations

FunctionWork (org.apache.hadoop.hive.ql.plan.FunctionWork)4 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)1 ResourceUri (org.apache.hadoop.hive.metastore.api.ResourceUri)1 ColumnInfo (org.apache.hadoop.hive.ql.exec.ColumnInfo)1 FunctionInfo (org.apache.hadoop.hive.ql.exec.FunctionInfo)1 Dispatcher (org.apache.hadoop.hive.ql.lib.Dispatcher)1 Node (org.apache.hadoop.hive.ql.lib.Node)1 PreOrderWalker (org.apache.hadoop.hive.ql.lib.PreOrderWalker)1 CreateFunctionDesc (org.apache.hadoop.hive.ql.plan.CreateFunctionDesc)1 CreateMacroDesc (org.apache.hadoop.hive.ql.plan.CreateMacroDesc)1 DropFunctionDesc (org.apache.hadoop.hive.ql.plan.DropFunctionDesc)1 DropMacroDesc (org.apache.hadoop.hive.ql.plan.DropMacroDesc)1 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)1 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)1