SemyonSinchenko commented on code in PR #58302: URL: https://github.com/apache/spark/pull/58302#discussion_r4288843540
########## graphframes/src/main/scala/org/apache/spark/graphframes/lib/RandomizedContraction.scala: ########## @@ -0,0 +1,289 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.graphframes.lib + +import java.io.IOException +import java.util.UUID + +import scala.collection.mutable.Stack +import scala.util.Random + +import org.apache.hadoop.fs.Path + +import org.apache.spark.graphframes.GraphFrame +import org.apache.spark.graphframes.GraphFrame.{DST, ID, LONG_DST, LONG_ID, LONG_SRC, SRC} +import org.apache.spark.graphframes.Logging +import org.apache.spark.sql.Column +import org.apache.spark.sql.DataFrame +import org.apache.spark.sql.catalyst.FunctionIdentifier +import org.apache.spark.sql.catalyst.expressions.Expression +import org.apache.spark.sql.functions._ +import org.apache.spark.sql.graphframes.expressions.FiniteAXPlusB +import org.apache.spark.storage.StorageLevel + +/** + * Implementation of parallel connected components algorithm using randomized contraction, based + * on Boegeholz, Harald, Michael Brand, and Radu-Alexandru Todor. "In-database connected component + * analysis." 2020 IEEE 36th International Conference on Data Engineering (ICDE). IEEE, 2020. + * + * The algorithm contracts the graph iteratively using random linear functions, until no edges + * remain, then reconstructs the component identifiers. + */ +private[graphframes] object RandomizedContraction extends Logging with Serializable { + private val CHECKPOINT_NAME_PREFIX = "randomized-contraction" + + private def prepare(graph: GraphFrame): GraphFrame = { + val vertices = graph.indexedVertices + .select(col(LONG_ID).as(ID)) + + val edges = graph.indexedEdges + .select(col(LONG_SRC).as(SRC), col(LONG_DST).as(DST)) + val symmetricEdges = edges + .union(edges.select(col(DST).alias(SRC), col(SRC).alias(DST))) + .distinct() + GraphFrame(vertices, symmetricEdges) + + } + + def run( + inputGraph: GraphFrame, + useLabelsAsComponents: Boolean, + intermediateStorageLevel: StorageLevel, + useLocalCheckpoints: Boolean, + checkpointInterval: Int, + isGraphPrepared: Boolean): DataFrame = { + val spark = inputGraph.vertices.sparkSession + val sc = spark.sparkContext + val runId = UUID.randomUUID().toString.takeRight(8) + val logPrefix = s"[CC $runId]" + + val checkpointDir = sc.getCheckpointDir + .map { d => + new Path(d, s"$CHECKPOINT_NAME_PREFIX-$runId").toString + } + .getOrElse { + // Spark-Connect workaround + spark.conf.getOption("spark.checkpoint.dir") match { + case Some(d) => new Path(d, s"$CHECKPOINT_NAME_PREFIX-$runId").toString + case None => + throw new IOException( + "Checkpoint directory is not set. Please set it first using sc.setCheckpointDir()" + + "or by specifying the conf 'spark.checkpoint.dir'.") + } + } + logInfo(s"$logPrefix Using $checkpointDir for storing intermediate tables.") + + val functionRegistry = spark.sessionState.functionRegistry + functionRegistry.registerFunction( Review Comment: This is a workaround that was ~ok for a library, but I'm not sure do we need to keep it as is right now. `functionRegistry` is a user-facing API, while as I remember internal functions in spark are registered in a different way. The same pattern inside: - kcore - sampling convolution - RandomWalk base class -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
