public static boolean isNullConstant(ExprNodeDesc value) { if ((value instanceof ExprNodeConstantDesc) && ((ExprNodeConstantDesc) value).getValue() == null) { return true; } return false; }
private boolean isNullConst(ExprNodeDesc exprNodeDesc) { //null constant could be typed so we need to check the value return exprNodeDesc instanceof ExprNodeConstantDesc && ((ExprNodeConstantDesc) exprNodeDesc).getValue() == null; }
public static boolean isNullConstant(ExprNodeDesc value) { if ((value instanceof ExprNodeConstantDesc) && ((ExprNodeConstantDesc) value).getValue() == null) { return true; } return false; }
private static boolean isDefaultPartition(ExprNodeDesc origin, String defaultPartitionName) { if (origin instanceof ExprNodeConstantDesc && ((ExprNodeConstantDesc)origin).getValue() != null && ((ExprNodeConstantDesc)origin).getValue().equals(defaultPartitionName)) { return true; } else { return false; } }
private boolean canConvertIntoCoalesce(GenericUDF genericUDF, ArrayList<ExprNodeDesc> children) { if (genericUDF instanceof GenericUDFWhen && children.size() == 3 && children.get(1) instanceof ExprNodeConstantDesc && children.get(2) instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc constThen = (ExprNodeConstantDesc) children.get(1); ExprNodeConstantDesc constElse = (ExprNodeConstantDesc) children.get(2); Object thenVal = constThen.getValue(); Object elseVal = constElse.getValue(); if (thenVal instanceof Boolean && elseVal instanceof Boolean) { return true; } } return false; }
private boolean canConvertIntoNvl(GenericUDF genericUDF, ArrayList<ExprNodeDesc> children) { if (genericUDF instanceof GenericUDFWhen && children.size() == 3 && children.get(1) instanceof ExprNodeConstantDesc && children.get(2) instanceof ExprNodeConstantDesc) { ExprNodeConstantDesc constThen = (ExprNodeConstantDesc) children.get(1); ExprNodeConstantDesc constElse = (ExprNodeConstantDesc) children.get(2); Object thenVal = constThen.getValue(); Object elseVal = constElse.getValue(); if (thenVal instanceof Boolean && elseVal instanceof Boolean) { return true; } } return false; }
private Object getScalarValue(ExprNodeConstantDesc constDesc) throws HiveException { String typeString = constDesc.getTypeString(); if (typeString.equalsIgnoreCase("String")) { return ((String) constDesc.getValue()).getBytes(StandardCharsets.UTF_8); } else if (charTypePattern.matcher(typeString).matches()) { return ((HiveChar) constDesc.getValue()).getStrippedValue().getBytes(StandardCharsets.UTF_8); } else if (varcharTypePattern.matcher(typeString).matches()) { return ((HiveVarchar) constDesc.getValue()).getValue().getBytes(StandardCharsets.UTF_8); } else if (typeString.equalsIgnoreCase("boolean")) { if (constDesc.getValue().equals(Boolean.TRUE)) { return 1; } else { return 0; } } else if (decimalTypePattern.matcher(typeString).matches()) { return constDesc.getValue(); } else { return constDesc.getValue(); } }
@Override public boolean isSame(Object o) { if (!(o instanceof ExprNodeConstantDesc)) { return false; } ExprNodeConstantDesc dest = (ExprNodeConstantDesc) o; if (!typeInfo.equals(dest.getTypeInfo())) { return false; } if (value == null) { if (dest.getValue() != null) { return false; } } else if (!value.equals(dest.getValue())) { return false; } return true; }
private long evaluateBetweenExpr(Statistics stats, ExprNodeDesc pred, long currNumRows, AnnotateStatsProcCtx aspCtx, List<String> neededCols, Operator<?> op) throws SemanticException { final ExprNodeGenericFuncDesc fd = (ExprNodeGenericFuncDesc) pred; final boolean invert = Boolean.TRUE.equals( ((ExprNodeConstantDesc) fd.getChildren().get(0)).getValue()); // boolean invert (not) final ExprNodeDesc comparisonExpression = fd.getChildren().get(1); // expression final ExprNodeDesc leftExpression = fd.getChildren().get(2); // left expression final ExprNodeDesc rightExpression = fd.getChildren().get(3); // right expression // Short circuit and return the current number of rows if this is a // synthetic predicate with dynamic values if (leftExpression instanceof ExprNodeDynamicValueDesc) { return currNumRows; } ExprNodeDesc newExpression = rewriteBetweenToIn(comparisonExpression, leftExpression, rightExpression, invert); return evaluateExpression(stats, newExpression, aspCtx, neededCols, op, currNumRows); }
private VectorExpression getCastToLongExpression(List<ExprNodeDesc> childExpr, PrimitiveCategory integerPrimitiveCategory) throws HiveException { ExprNodeDesc child = childExpr.get(0); String inputType = childExpr.get(0).getTypeString(); if (child instanceof ExprNodeConstantDesc) { // Return a constant vector expression Object constantValue = ((ExprNodeConstantDesc) child).getValue(); Long longValue = castConstantToLong(constantValue, child.getTypeInfo(), integerPrimitiveCategory); return getConstantVectorExpression(longValue, TypeInfoFactory.longTypeInfo, VectorExpressionDescriptor.Mode.PROJECTION); } // Float family, timestamp are handled via descriptor based lookup, int family needs // special handling. if (isIntFamily(inputType)) { // integer and boolean types require no conversion, so use a no-op return getIdentityExpression(childExpr); } return null; }
private VectorExpression getCastToBoolean(List<ExprNodeDesc> childExpr) throws HiveException { ExprNodeDesc child = childExpr.get(0); TypeInfo inputTypeInfo = child.getTypeInfo(); String inputType = inputTypeInfo.toString(); if (child instanceof ExprNodeConstantDesc) { if (null == ((ExprNodeConstantDesc)child).getValue()) { return getConstantVectorExpression(null, TypeInfoFactory.booleanTypeInfo, VectorExpressionDescriptor.Mode.PROJECTION); } // Don't do constant folding here. Wait until the optimizer is changed to do it. // Family of related JIRAs: HIVE-7421, HIVE-7422, and HIVE-7424. return null; } // Long and double are handled using descriptors, string needs to be specially handled. if (isStringFamily(inputType)) { return createVectorExpression(CastStringToBoolean.class, childExpr, VectorExpressionDescriptor.Mode.PROJECTION, TypeInfoFactory.booleanTypeInfo); } return null; }
public void prepareConstant() { final Object writableValue; if (constExpr != null) { PrimitiveCategory pc = ((PrimitiveTypeInfo) constExpr.getTypeInfo()) .getPrimitiveCategory(); // Convert from Java to Writable writableValue = PrimitiveObjectInspectorFactory .getPrimitiveJavaObjectInspector(pc).getPrimitiveWritableObject( constExpr.getValue()); } else { writableValue = null; } constObjVal = new GenericUDF.DeferredJavaObject(writableValue); }
private static ExprNodeDesc interpretNodeAs(PrimitiveTypeInfo colTypeInfo, ExprNodeDesc constChild) { if (constChild instanceof ExprNodeConstantDesc) { // Try to narrow type of constant Object constVal = ((ExprNodeConstantDesc) constChild).getValue(); if (constVal == null) { // adjust type of null return new ExprNodeConstantDesc(colTypeInfo, null); } Object newConst = interpretConstantAsPrimitive(colTypeInfo, constVal, constChild.getTypeInfo()); if (newConst == null) { return null; } if(newConst == constVal) { return constChild; } else { return new ExprNodeConstantDesc(adjustType(colTypeInfo, newConst), newConst); } } return constChild; }
@Test public void testUDF() throws Exception { udf = new GenericUDFMacro(name, bodyDesc, colNames, colTypes); udf.initialize(inspectors); Object actual = udf.evaluate(arguments); Assert.assertEquals(bodyDesc.getValue(), ((IntWritable)actual).get()); Assert.assertTrue(udf.isDeterministic()); Assert.assertFalse(udf.isStateful()); Assert.assertEquals(name, udf.getMacroName()); Assert.assertEquals(bodyDesc, udf.getBody()); Assert.assertEquals(colNames, udf.getColNames()); Assert.assertEquals(colTypes, udf.getColTypes()); Assert.assertEquals(name + "(x, y)", udf.getDisplayString(new String[] { "x", "y"})); } @Test
@Override protected ExprNodeColumnDesc processQualifiedColRef(TypeCheckCtx ctx, ASTNode expr, Object... nodeOutputs) throws SemanticException { String tableAlias = BaseSemanticAnalyzer.unescapeIdentifier(expr.getChild(0).getChild(0) .getText()); // NOTE: tableAlias must be a valid non-ambiguous table alias, // because we've checked that in TOK_TABLE_OR_COL's process method. ColumnInfo colInfo = getColInfo((JoinTypeCheckCtx) ctx, tableAlias, ((ExprNodeConstantDesc) nodeOutputs[1]).getValue().toString(), expr); if (colInfo == null) { ctx.setError(ErrorMsg.INVALID_COLUMN.getMsg(expr.getChild(1)), expr); return null; } return new ExprNodeColumnDesc(colInfo.getType(), colInfo.getInternalName(), tableAlias, colInfo.getIsVirtualCol()); }
@Override protected ExprNodeColumnDesc processQualifiedColRef(TypeCheckCtx ctx, ASTNode expr, Object... nodeOutputs) throws SemanticException { String tableAlias = BaseSemanticAnalyzer.unescapeIdentifier(expr.getChild(0).getChild(0) .getText()); // NOTE: tableAlias must be a valid non-ambiguous table alias, // because we've checked that in TOK_TABLE_OR_COL's process method. ColumnInfo colInfo = getColInfo((JoinTypeCheckCtx) ctx, tableAlias, ((ExprNodeConstantDesc) nodeOutputs[1]).getValue().toString(), expr); if (colInfo == null) { ctx.setError(ErrorMsg.INVALID_COLUMN.getMsg(expr.getChild(1)), expr); return null; } return new ExprNodeColumnDesc(colInfo.getType(), colInfo.getInternalName(), tableAlias, colInfo.getIsVirtualCol()); }