You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kylin.apache.org by GitBox <gi...@apache.org> on 2020/12/07 16:27:37 UTC

[GitHub] [kylin] hit-lacus commented on a change in pull request #1485: KYLIN-4818 Support Cube Planner Phase One in Kylin 4

hit-lacus commented on a change in pull request #1485:
URL: https://github.com/apache/kylin/pull/1485#discussion_r537643225



##########
File path: kylin-spark-project/kylin-spark-engine/src/main/scala/org/apache/kylin/engine/spark/job/CuboidStatisticsJob.scala
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.kylin.engine.spark.job
+
+
+import org.apache.kylin.engine.spark.metadata.SegmentInfo
+import org.apache.kylin.measure.hllc.HLLCounter
+import org.apache.kylin.shaded.com.google.common.hash.{HashFunction, Hashing}
+import org.apache.spark.sql.{Dataset, Row}
+
+import scala.collection.mutable
+
+/**
+ * Calculate HLLCounter for each cuboid, to serve Cube Planner (to calculate cost and benefit of each cuboid).
+ */
+object CuboidStatisticsJob {
+
+  /**
+   * @param inputDs Part of FlatTable which contains all normal dimensions
+   * @return Cuboid level statistics data
+   */
+  def statistics(inputDs: Dataset[Row], seg: SegmentInfo): Array[(String, AggInfo)] = {
+
+    val rkc = seg.allColumns.count(c => c.rowKey)
+    // maybe we should use sample operation to reduce cost later
+    val res = inputDs.rdd
+      .mapPartitions(new CuboidStatisticsJob(seg.getAllLayout.map(x => x.getId), rkc).statisticsWithinPartition)
+    val l = res.map(a => (a.key, a)).reduceByKey((a, b) => a.merge(b)).collect()
+    //    l.foreach(x => println(x._1 + " >>><<< " + x._2.cuboid.counter.getCountEstimate))
+    l
+  }
+}
+
+class CuboidStatisticsJob(ids: List[Long], rkc: Int) extends Serializable {
+  private val info = mutable.Map[String, AggInfo]()
+  private var allCuboidsBitSet: Array[Array[Integer]] = Array()
+  private val hf: HashFunction = Hashing.murmur3_128
+  private val rowHashCodesLong = new Array[Long](rkc)
+  private var idx = 0
+  private var meter1 = 0L
+  private var meter2 = 0L
+  private var startMills = 0L
+  private var endMills = 0L
+
+
+  def statisticsWithinPartition(rows: Iterator[Row]): Iterator[AggInfo] = {
+    init()
+    println("CuboidStatisticsJob-statisticsWithinPartition1-" + System.currentTimeMillis())
+    rows.foreach(update)
+    printStat()
+    println("CuboidStatisticsJob-statisticsWithinPartition2-" + System.currentTimeMillis())
+    info.valuesIterator
+  }
+
+  def init(): Unit = {
+    println("CuboidStatisticsJob-Init1-" + System.currentTimeMillis())
+    allCuboidsBitSet = getCuboidBitSet(ids, rkc)
+    ids.foreach(i => info.put(i.toString, AggInfo(i.toString)))
+    println("CuboidStatisticsJob-Init2-" + System.currentTimeMillis())
+  }
+
+  def update(r: Row): Unit = {
+    idx += 1
+    if (idx <= 5)
+      println(r)
+    updateCuboid(r)
+  }
+
+  def updateCuboid(r: Row): Unit = {
+    // generate hash for each row key column
+    startMills = System.currentTimeMillis()
+    var idx = 0
+    while (idx < rkc) {
+      val hc = hf.newHasher
+      var colValue = r.get(idx).toString
+      if (colValue == null) colValue = "0"
+      // add column ordinal to the hash value to distinguish between (a,b) and (b,a)
+      rowHashCodesLong(idx) = hc.putUnencodedChars(colValue).hash().padToLong() + idx
+      idx += 1
+    }
+    endMills = System.currentTimeMillis()
+    meter1 += (endMills - startMills)
+
+
+    startMills = System.currentTimeMillis()
+    // use the row key column hash to get a consolidated hash for each cuboid
+    val n = allCuboidsBitSet.length
+    idx = 0
+    while (idx < n) {
+      var value: Long = 0
+      var position = 0
+      while (position < allCuboidsBitSet(idx).length) {
+        value += rowHashCodesLong(allCuboidsBitSet(idx)(position))
+        position += 1
+      }
+      info(ids(idx).toString).cuboid.counter.addHashDirectly(value)

Review comment:
       If we have 10 thousand cuboid to be statistics, we will call Long.toString 10 thousand times and create 10 thousand String objects. Is it the cause of bad performance ?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org