Package org.apache.hadoop.hive.ql.plan

Examples of org.apache.hadoop.hive.ql.plan.exprNodeConstantDesc


        bool = Boolean.FALSE;
        break;
      default:
        assert false;
      }
      return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo, bool);
    }
View Full Code Here


        // "." : FIELD Expression
        assert (children.size() == 2);
        // Only allow constant field name for now
        assert (children.get(1) instanceof ExprNodeConstantDesc);
        ExprNodeDesc object = children.get(0);
        ExprNodeConstantDesc fieldName = (ExprNodeConstantDesc) children.get(1);
        assert (fieldName.getValue() instanceof String);

        // Calculate result TypeInfo
        String fieldNameString = (String) fieldName.getValue();
        TypeInfo objectTypeInfo = object.getTypeInfo();

        // Allow accessing a field of list element structs directly from a list
        boolean isList = (object.getTypeInfo().getCategory() == ObjectInspector.Category.LIST);
        if (isList) {
          objectTypeInfo = ((ListTypeInfo) objectTypeInfo)
              .getListElementTypeInfo();
        }
        if (objectTypeInfo.getCategory() != Category.STRUCT) {
          throw new SemanticException(ErrorMsg.INVALID_DOT.getMsg(expr));
        }
        TypeInfo t = ((StructTypeInfo) objectTypeInfo)
            .getStructFieldTypeInfo(fieldNameString);
        if (isList) {
          t = TypeInfoFactory.getListTypeInfo(t);
        }

        desc = new ExprNodeFieldDesc(t, children.get(0), fieldNameString,
            isList);

      } else if (funcText.equals("[")) {
        // "[]" : LSQUARE/INDEX Expression
        assert (children.size() == 2);

        // Check whether this is a list or a map
        TypeInfo myt = children.get(0).getTypeInfo();

        if (myt.getCategory() == Category.LIST) {
          // Only allow integer index for now
          if (!(children.get(1) instanceof ExprNodeConstantDesc)
              || !(((ExprNodeConstantDesc) children.get(1)).getTypeInfo()
              .equals(TypeInfoFactory.intTypeInfo))) {
            throw new SemanticException(SemanticAnalyzer.generateErrorMessage(
                  expr,
                  ErrorMsg.INVALID_ARRAYINDEX_CONSTANT.getMsg()));
          }

          // Calculate TypeInfo
          TypeInfo t = ((ListTypeInfo) myt).getListElementTypeInfo();
          desc = new ExprNodeGenericFuncDesc(t, FunctionRegistry
              .getGenericUDFForIndex(), children);
        } else if (myt.getCategory() == Category.MAP) {
          // Only allow constant map key for now
          if (!(children.get(1) instanceof ExprNodeConstantDesc)) {
            throw new SemanticException(SemanticAnalyzer.generateErrorMessage(
                  expr,
                  ErrorMsg.INVALID_MAPINDEX_CONSTANT.getMsg()));
          }
          if (!(((ExprNodeConstantDesc) children.get(1)).getTypeInfo()
              .equals(((MapTypeInfo) myt).getMapKeyTypeInfo()))) {
            throw new SemanticException(ErrorMsg.INVALID_MAPINDEX_TYPE
                .getMsg(expr));
          }
          // Calculate TypeInfo
          TypeInfo t = ((MapTypeInfo) myt).getMapValueTypeInfo();
          desc = new ExprNodeGenericFuncDesc(t, FunctionRegistry
              .getGenericUDFForIndex(), children);
        } else {
          throw new SemanticException(ErrorMsg.NON_COLLECTION_TYPE.getMsg(expr,
              myt.getTypeName()));
        }
      } else {
        // other operators or functions
        FunctionInfo fi = FunctionRegistry.getFunctionInfo(funcText);

        if (fi == null) {
          if (isFunction) {
            throw new SemanticException(ErrorMsg.INVALID_FUNCTION
                .getMsg((ASTNode) expr.getChild(0)));
          } else {
            throw new SemanticException(ErrorMsg.INVALID_FUNCTION.getMsg(expr));
          }
        }

        if (!fi.isNative()) {
          ctx.getUnparseTranslator().addIdentifierTranslation(
              (ASTNode) expr.getChild(0));
        }

        // Detect UDTF's in nested SELECT, GROUP BY, etc as they aren't
        // supported
        if (fi.getGenericUDTF() != null) {
          throw new SemanticException(ErrorMsg.UDTF_INVALID_LOCATION.getMsg());
        }
        // UDAF in filter condition, group-by caluse, param of funtion, etc.
        if (fi.getGenericUDAFResolver() != null) {
          if (isFunction) {
            throw new SemanticException(ErrorMsg.UDAF_INVALID_LOCATION.
                getMsg((ASTNode) expr.getChild(0)));
          } else {
            throw new SemanticException(ErrorMsg.UDAF_INVALID_LOCATION.getMsg(expr));
          }
        }
        if (!ctx.getAllowStatefulFunctions() && (fi.getGenericUDF() != null)) {
          if (FunctionRegistry.isStateful(fi.getGenericUDF())) {
            throw new SemanticException(
              ErrorMsg.UDF_STATEFUL_INVALID_LOCATION.getMsg());
          }
        }

        // Try to infer the type of the constant only if there are two
        // nodes, one of them is column and the other is numeric const
        if (fi.getGenericUDF() instanceof GenericUDFBaseCompare
            && children.size() == 2
            && ((children.get(0) instanceof ExprNodeConstantDesc
                && children.get(1) instanceof ExprNodeColumnDesc)
                || (children.get(0) instanceof ExprNodeColumnDesc
                    && children.get(1) instanceof ExprNodeConstantDesc))) {
          int constIdx =
              children.get(0) instanceof ExprNodeConstantDesc ? 0 : 1;

          Set<String> inferTypes = new HashSet<String>(Arrays.asList(
              serdeConstants.TINYINT_TYPE_NAME.toLowerCase(),
              serdeConstants.SMALLINT_TYPE_NAME.toLowerCase(),
              serdeConstants.INT_TYPE_NAME.toLowerCase(),
              serdeConstants.BIGINT_TYPE_NAME.toLowerCase(),
              serdeConstants.FLOAT_TYPE_NAME.toLowerCase(),
              serdeConstants.DOUBLE_TYPE_NAME.toLowerCase(),
              serdeConstants.STRING_TYPE_NAME.toLowerCase()
              ));

          String constType = children.get(constIdx).getTypeString().toLowerCase();
          String columnType = children.get(1 - constIdx).getTypeString().toLowerCase();

          if (inferTypes.contains(constType) && inferTypes.contains(columnType)
              && !columnType.equalsIgnoreCase(constType)) {
            String constValue =
                ((ExprNodeConstantDesc) children.get(constIdx)).getValue().toString();
            boolean triedDouble = false;

            Number value = null;
            try {
              if (columnType.equalsIgnoreCase(serdeConstants.TINYINT_TYPE_NAME)) {
                value = new Byte(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.SMALLINT_TYPE_NAME)) {
                value = new Short(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.INT_TYPE_NAME)) {
                value = new Integer(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.BIGINT_TYPE_NAME)) {
                value = new Long(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.FLOAT_TYPE_NAME)) {
                value = new Float(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.DOUBLE_TYPE_NAME)
                  || (columnType.equalsIgnoreCase(serdeConstants.STRING_TYPE_NAME)
                     && !constType.equalsIgnoreCase(serdeConstants.BIGINT_TYPE_NAME))) {
                // no smart inference for queries like "str_col = bigint_const"
                triedDouble = true;
                value = new Double(constValue);
              }
            } catch (NumberFormatException nfe) {
              // this exception suggests the precise type inference did not succeed
              // we'll try again to convert it to double
              // however, if we already tried this, or the column is NUMBER type and
              // the operator is EQUAL, return false due to the type mismatch
              if (triedDouble ||
                  (fi.getGenericUDF() instanceof GenericUDFOPEqual
                  && !columnType.equals(serdeConstants.STRING_TYPE_NAME))) {
                return new ExprNodeConstantDesc(false);
              }

              try {
                value = new Double(constValue);
              } catch (NumberFormatException ex) {
                return new ExprNodeConstantDesc(false);
              }
            }

            if (value != null) {
              children.set(constIdx, new ExprNodeConstantDesc(value));
            }
          }
        }

        desc = ExprNodeGenericFuncDesc.newInstance(fi.getGenericUDF(), children);
View Full Code Here

    if (ifAgree == null) {
      return new NodeInfoWrapper(WalkState.DIVIDED, results,
          getOutExpr(fd, nodeOutputs));
    } else if (ifAgree.booleanValue() == true) {
      return new NodeInfoWrapper(WalkState.TRUE, null,
          new ExprNodeConstantDesc(fd.getTypeInfo(), Boolean.TRUE));
    } else {
      return new NodeInfoWrapper(WalkState.FALSE, null,
          new ExprNodeConstantDesc(fd.getTypeInfo(), Boolean.FALSE));

    }
  }
View Full Code Here

      if (FunctionRegistry.isOpNot(fd)) {
        assert (nodeOutputs.length == 1);
        NodeInfoWrapper wrapper = (NodeInfoWrapper) nodeOutputs[0];
        if (wrapper.state == WalkState.TRUE) {
          ExprNodeConstantDesc falseDesc = new ExprNodeConstantDesc(
              wrapper.outExpr.getTypeInfo(), Boolean.FALSE);
          return new NodeInfoWrapper(WalkState.FALSE, null, falseDesc);
        } else if (wrapper.state == WalkState.FALSE) {
          ExprNodeConstantDesc trueDesc = new ExprNodeConstantDesc(
              wrapper.outExpr.getTypeInfo(), Boolean.TRUE);
          return new NodeInfoWrapper(WalkState.TRUE, null, trueDesc);
        } else if (wrapper.state == WalkState.DIVIDED) {
          Boolean[] results = new Boolean[ctx.getPartList().size()];
          for (int i = 0; i < ctx.getPartList().size(); i++) {
            results[i] = opNot(wrapper.ResultVector[i]);
          }
          return new NodeInfoWrapper(WalkState.DIVIDED, results,
              getOutExpr(fd, nodeOutputs));
        } else {
          return new NodeInfoWrapper(wrapper.state, null,
              getOutExpr(fd, nodeOutputs));
        }
      } else if (FunctionRegistry.isOpAnd(fd)) {
        assert (nodeOutputs.length == 2);
        NodeInfoWrapper c1 = (NodeInfoWrapper)nodeOutputs[0];
        NodeInfoWrapper c2 = (NodeInfoWrapper)nodeOutputs[1];

        if (c1.state == WalkState.FALSE) {
          return c1;
        } else if (c2.state == WalkState.FALSE) {
          return c2;
        } else if (c1.state == WalkState.TRUE) {
          return c2;
        } else if (c2.state == WalkState.TRUE) {
          return c1;
        } else if (c1.state == WalkState.UNKNOWN || c2.state == WalkState.UNKNOWN) {
          return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
        } else if (c1.state == WalkState.DIVIDED && c2.state == WalkState.DIVIDED) {
          Boolean[] results = new Boolean[ctx.getPartList().size()];
          for (int i = 0; i < ctx.getPartList().size(); i++) {
            results[i] = opAnd(c1.ResultVector[i], c2.ResultVector[i]);
          }
          return getResultWrapFromResults(results, fd, nodeOutputs);
        }
        return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
      } else if (FunctionRegistry.isOpOr(fd)) {
        assert (nodeOutputs.length == 2);
        NodeInfoWrapper c1 = (NodeInfoWrapper)nodeOutputs[0];
        NodeInfoWrapper c2 = (NodeInfoWrapper)nodeOutputs[1];

        if (c1.state == WalkState.TRUE) {
          return c1;
        } else if (c2.state == WalkState.TRUE) {
          return c2;
        } else if (c1.state == WalkState.FALSE) {
          return c2;
        } else if (c2.state == WalkState.FALSE) {
          return c1;
        } else if (c1.state == WalkState.UNKNOWN || c2.state == WalkState.UNKNOWN) {
          return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
        } else if (c1.state == WalkState.DIVIDED && c2.state == WalkState.DIVIDED) {
          Boolean[] results = new Boolean[ctx.getPartList().size()];
          for (int i = 0; i < ctx.getPartList().size(); i++) {
            results[i] = opOr(c1.ResultVector[i], c2.ResultVector[i]);
          }
          return getResultWrapFromResults(results, fd, nodeOutputs);
        }
        return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
      } else if (!FunctionRegistry.isDeterministic(fd.getGenericUDF())) {
        // If it's a non-deterministic UDF, set unknown to true
        return new NodeInfoWrapper(WalkState.UNKNOWN, null,
            getOutExpr(fd, nodeOutputs));
      } else {
        // If any child is unknown, set unknown to true
        boolean has_part_col = false;
        for (Object child : nodeOutputs) {
          NodeInfoWrapper wrapper = (NodeInfoWrapper) child;
          if (wrapper.state == WalkState.UNKNOWN) {
            return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
          } else if (wrapper.state == WalkState.PART_COL) {
            has_part_col = true;
          }
        }

        if (has_part_col) {
          //  we need to evaluate result for every pruned partition
          if (fd.getTypeInfo().equals(TypeInfoFactory.booleanTypeInfo)) {
            // if the return type of the GenericUDF is boolean and all partitions agree on
            // a result, we update the state of the node to be TRUE of FALSE
            Boolean[] results = new Boolean[ctx.getPartList().size()];
            for (int i = 0; i < ctx.getPartList().size(); i++) {
              results[i] = (Boolean) evalExprWithPart(fd, ctx.getPartList().get(i));
            }
            return getResultWrapFromResults(results, fd, nodeOutputs);
          }

          // the case that return type of the GenericUDF is not boolean, and if not all partition
          // agree on result, we make the node UNKNOWN. If they all agree, we replace the node
          // to be a CONSTANT node with value to be the agreed result.
          Object[] results = new Object[ctx.getPartList().size()];
          for (int i = 0; i < ctx.getPartList().size(); i++) {
            results[i] = evalExprWithPart(fd, ctx.getPartList().get(i));
          }
          Object result = ifResultsAgree(results);
          if (result == null) {
            // if the result is not boolean and not all partition agree on the
            // result, we don't remove the condition. Potentially, it can miss
            // the case like "where ds % 3 == 1 or ds % 3 == 2"
            // TODO: handle this case by making result vector to handle all
            // constant values.
            return new NodeInfoWrapper(WalkState.UNKNOWN, null, getOutExpr(fd, nodeOutputs));
          }
          return new NodeInfoWrapper(WalkState.CONSTANT, null,
              new ExprNodeConstantDesc(fd.getTypeInfo(), result));
        }

        return new NodeInfoWrapper(WalkState.CONSTANT, null, getOutExpr(fd, nodeOutputs));
      }
    }
View Full Code Here

      }
      if (v == null) {
        throw new SemanticException(ErrorMsg.INVALID_NUMERICAL_CONSTANT
            .getMsg(expr));
      }
      return new ExprNodeConstantDesc(v);
    }
View Full Code Here

        // HiveParser.identifier | HiveParse.KW_IF | HiveParse.KW_LEFT |
        // HiveParse.KW_RIGHT
        str = BaseSemanticAnalyzer.unescapeIdentifier(expr.getText());
        break;
      }
      return new ExprNodeConstantDesc(TypeInfoFactory.stringTypeInfo, str);
    }
View Full Code Here

        bool = Boolean.FALSE;
        break;
      default:
        assert false;
      }
      return new ExprNodeConstantDesc(TypeInfoFactory.booleanTypeInfo, bool);
    }
View Full Code Here

        // "." : FIELD Expression
        assert (children.size() == 2);
        // Only allow constant field name for now
        assert (children.get(1) instanceof ExprNodeConstantDesc);
        ExprNodeDesc object = children.get(0);
        ExprNodeConstantDesc fieldName = (ExprNodeConstantDesc) children.get(1);
        assert (fieldName.getValue() instanceof String);

        // Calculate result TypeInfo
        String fieldNameString = (String) fieldName.getValue();
        TypeInfo objectTypeInfo = object.getTypeInfo();

        // Allow accessing a field of list element structs directly from a list
        boolean isList = (object.getTypeInfo().getCategory() == ObjectInspector.Category.LIST);
        if (isList) {
View Full Code Here

      ExprProcCtx epc = (ExprProcCtx) procCtx;
      if (cd.getTabAlias().equalsIgnoreCase(epc.getTabAlias())
          && cd.getIsPartitionColOrVirtualCol()) {
        newcd = cd.clone();
      } else {
        newcd = new ExprNodeConstantDesc(cd.getTypeInfo(), null);
        epc.setHasNonPartCols(true);
      }

      return newcd;
    }
View Full Code Here

          }
        }
      }

      if (unknown) {
        newfd = new ExprNodeConstantDesc(fd.getTypeInfo(), null);
      } else {
        // Create the list of children
        ArrayList<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>();
        for (Object child : nodeOutputs) {
          children.add((ExprNodeDesc) child);
View Full Code Here

TOP

Related Classes of org.apache.hadoop.hive.ql.plan.exprNodeConstantDesc

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.