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

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


    ArrayList<String> columnNames = new ArrayList<String>();
    Map<String, ExprNodeDesc> colExprMap = new HashMap<String, ExprNodeDesc>();
    for (int i = 0; i < col_list.size(); i++) {
      // Replace NULL with CAST(NULL AS STRING)
      if (col_list.get(i) instanceof ExprNodeNullDesc) {
        col_list.set(i, new ExprNodeConstantDesc(
            TypeInfoFactory.stringTypeInfo, null));
      }
      String outputCol = getColumnInternalName(i);
      colExprMap.put(outputCol, col_list.get(i));
      columnNames.add(outputCol);
View Full Code Here


  private ExprNodeDesc genSamplePredicate(TableSample ts,
      List<String> bucketCols, boolean useBucketCols, String alias,
      RowResolver rwsch, QBMetaData qbm, ExprNodeDesc planExpr)
      throws SemanticException {

    ExprNodeDesc numeratorExpr = new ExprNodeConstantDesc(
        TypeInfoFactory.intTypeInfo, Integer.valueOf(ts.getNumerator() - 1));

    ExprNodeDesc denominatorExpr = new ExprNodeConstantDesc(
        TypeInfoFactory.intTypeInfo, Integer.valueOf(ts.getDenominator()));

    ExprNodeDesc intMaxExpr = new ExprNodeConstantDesc(
        TypeInfoFactory.intTypeInfo, Integer.valueOf(Integer.MAX_VALUE));

    ArrayList<ExprNodeDesc> args = new ArrayList<ExprNodeDesc>();
    if (planExpr != null) {
      args.add(planExpr);
View Full Code Here

            TableSample tsSample = new TableSample(1, freq);
            tsSample.setInputPruning(false);
            qb.getParseInfo().setTabSample(alias, tsSample);
            LOG.info("Need sample filter");
            ExprNodeDesc randFunc = TypeCheckProcFactory.DefaultExprProcessor
                .getFuncExprNodeDesc("rand", new ExprNodeConstantDesc(Integer
                .valueOf(460476415)));
            ExprNodeDesc samplePred = genSamplePredicate(tsSample, null, false,
                alias, rwsch, qb.getMetaData(), randFunc);
            tableOp = OperatorFactory.getAndMakeChild(new FilterDesc(
                samplePred, true),
View Full Code Here

    ExprNodeDesc lhs = new ExprNodeGenericFuncDesc(
        TypeInfoFactory.doubleTypeInfo, FunctionRegistry.getFunctionInfo(
        Constants.DOUBLE_TYPE_NAME).getGenericUDF(), children1);

    ArrayList<ExprNodeDesc> children2 = new ArrayList<ExprNodeDesc>();
    children2.add(new ExprNodeConstantDesc(TypeInfoFactory.longTypeInfo, Long
        .valueOf(100)));
    ExprNodeDesc rhs = new ExprNodeGenericFuncDesc(
        TypeInfoFactory.doubleTypeInfo, FunctionRegistry.getFunctionInfo(
        Constants.DOUBLE_TYPE_NAME).getGenericUDF(), children2);
View Full Code Here

          // Literal tinyint.
          v = Byte.valueOf(expr.getText().substring(
                0, expr.getText().length() - 1));
        } else if (expr.getText().endsWith("BD")) {
          // Literal decimal
          return new ExprNodeConstantDesc(TypeInfoFactory.decimalTypeInfo,
                expr.getText().substring(0, expr.getText().length() - 2));
        } else {
          v = Double.valueOf(expr.getText());
          v = Long.valueOf(expr.getText());
          v = Integer.valueOf(expr.getText());
        }
      } catch (NumberFormatException e) {
        // do nothing here, we will throw an exception in the following block
      }
      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

      // Get the string value and convert to a Date value.
      try {
        String dateString = BaseSemanticAnalyzer.stripQuotes(expr.getText());
        Date date = Date.valueOf(dateString);
        return new ExprNodeConstantDesc(TypeInfoFactory.dateTypeInfo, date);
      } catch (IllegalArgumentException err) {
        throw new SemanticException("Unable to convert date literal string to date value.", err);
      }
    }
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));
          }
        }

        // getGenericUDF() actually clones the UDF. Just call it once and reuse.
        GenericUDF genericUDF = fi.getGenericUDF();

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

        // Handle type casts that may contain type parameters
        if (isFunction) {
          ASTNode funcNameNode = (ASTNode)expr.getChild(0);
          switch (funcNameNode.getType()) {
            case HiveParser.TOK_VARCHAR:
              // Add type params
              VarcharTypeParams varcharTypeParams = new VarcharTypeParams();
              varcharTypeParams.length = Integer.valueOf((funcNameNode.getChild(0).getText()));
              if (genericUDF != null) {
                ((SettableUDF)genericUDF).setParams(varcharTypeParams);
              }
              break;
            default:
              // Do nothing
              break;
          }
        }

        // 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() && (genericUDF != null)) {
          if (FunctionRegistry.isStateful(genericUDF)) {
            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 (genericUDF 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)) {
            Object originalValue =  ((ExprNodeConstantDesc) children.get(constIdx)).getValue();
            String constValue = originalValue.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)) {
                triedDouble = true;
                value = new Double(constValue);
              } else if (columnType.equalsIgnoreCase(serdeConstants.STRING_TYPE_NAME)) {
                // Don't scramble the const type information if comparing to a string column,
                // It's not useful to do so; as of now, there is also a hack in
                // SemanticAnalyzer#genTablePlan that causes every column to look like a string
                // a string down here, so number type information is always lost otherwise.
                boolean isNumber = (originalValue instanceof Number);
                triedDouble = !isNumber;
                value = isNumber ? (Number)originalValue : 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 ||
                  (genericUDF 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(genericUDF, children);
View Full Code Here

   */
  static private ExprNodeDesc removeNonPartCols(ExprNodeDesc expr, List<String> partCols) {
    if (expr instanceof ExprNodeColumnDesc
        && !partCols.contains(((ExprNodeColumnDesc) expr).getColumn())) {
      // Column doesn't appear to be a partition column for the table.
      return new ExprNodeConstantDesc(expr.getTypeInfo(), null);
    }
    if (expr instanceof ExprNodeGenericFuncDesc) {
      List<ExprNodeDesc> children = expr.getChildren();
      for (int i = 0; i < children.size(); ++i) {
        children.set(i, removeNonPartCols(children.get(i), partCols));
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.