Github user clockfly commented on a diff in the pull request: https://github.com/apache/spark/pull/14176#discussion_r76339716 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/HashAggregateExec.scala --- @@ -459,52 +475,91 @@ case class HashAggregateExec( } /** - * Using the vectorized hash map in HashAggregate is currently supported for all primitive - * data types during partial aggregation. However, we currently only enable the hash map for a - * subset of cases that've been verified to show performance improvements on our benchmarks - * subject to an internal conf that sets an upper limit on the maximum length of the aggregate - * key/value schema. - * + * A required check for any fast hash map implementation (basically the common requirements + * for row-based and vectorized). + * Currently fast hash map is supported for primitive data types during partial aggregation. * This list of supported use-cases should be expanded over time. */ - private def enableVectorizedHashMap(ctx: CodegenContext): Boolean = { - val schemaLength = (groupingKeySchema ++ bufferSchema).length + private def checkIfFastHashMapSupported(ctx: CodegenContext): Boolean = { val isSupported = (groupingKeySchema ++ bufferSchema).forall(f => ctx.isPrimitiveType(f.dataType) || f.dataType.isInstanceOf[DecimalType] || f.dataType.isInstanceOf[StringType]) && bufferSchema.nonEmpty && modes.forall(mode => mode == Partial || mode == PartialMerge) - // We do not support byte array based decimal type for aggregate values as - // ColumnVector.putDecimal for high-precision decimals doesn't currently support in-place + // For vectorized hash map, We do not support byte array based decimal type for aggregate values + // as ColumnVector.putDecimal for high-precision decimals doesn't currently support in-place // updates. Due to this, appending the byte array in the vectorized hash map can turn out to be // quite inefficient and can potentially OOM the executor. + // For row-based hash map, while decimal update is supported in UnsafeRow, we will just act + // conservative here, due to lack of testing and benchmarking. val isNotByteArrayDecimalType = bufferSchema.map(_.dataType).filter(_.isInstanceOf[DecimalType]) .forall(!DecimalType.isByteArrayDecimalType(_)) - isSupported && isNotByteArrayDecimalType && - schemaLength <= sqlContext.conf.vectorizedAggregateMapMaxColumns + isSupported && isNotByteArrayDecimalType + } + + private def enableTwoLevelHashMap(ctx: CodegenContext) = { + if (!checkIfFastHashMapSupported(ctx)) { + if (modes.forall(mode => mode == Partial || mode == PartialMerge) && !Utils.isTesting) { + logInfo("spark.sql.codegen.aggregate.map.twolevel.enable is set to true, but" + + " current version of codegened fast hashmap does not support this aggregate.") + } + } else { + isFastHashMapEnabled = true + + // This is for testing/benchmarking only. + // We enforce to first level to be a vectorized hashmap, instead of the default row-based one. + sqlContext.getConf("spark.sql.codegen.aggregate.map.vectorized.enable", null) match { + case "true" => isVectorizedHashMapEnabled = true + case null | "" | "false" => None } + } } private def doProduceWithKeys(ctx: CodegenContext): String = { val initAgg = ctx.freshName("initAgg") ctx.addMutableState("boolean", initAgg, s"$initAgg = false;") - isVectorizedHashMapEnabled = enableVectorizedHashMap(ctx) - vectorizedHashMapTerm = ctx.freshName("vectorizedHashMap") - val vectorizedHashMapClassName = ctx.freshName("VectorizedHashMap") - val vectorizedHashMapGenerator = new VectorizedHashMapGenerator(ctx, aggregateExpressions, - vectorizedHashMapClassName, groupingKeySchema, bufferSchema) + if (sqlContext.conf.enableTwoLevelAggMap) { + enableTwoLevelHashMap(ctx) + } else { + sqlContext.getConf("spark.sql.codegen.aggregate.map.vectorized.enable", null) match { + case "true" => logWarning("Two level hashmap is disabled but vectorized hashmap is " + + "enabled.") + case null | "" | "false" => None + } + } --- End diff -- Maybe you want to wrap line 521-529 in a function like `checkAndEnableTwoLevelHashMap`, and do a call like ``` isFastHashMapEnabled = checkAndEnableTwoLevelHashMap(ctx) ``` Current impl of `enableTwoLevelHashMap` has a side effect of updating boolean value `isFastHashMapEnabled`. User is not clear of the side effect if he doesn't inspect the code of `enableTwoLevelHashMap`
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org