[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-19 Thread asfgit
Github user asfgit closed the pull request at:

https://github.com/apache/spark/pull/15467


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



[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-18 Thread sameeragarwal
Github user sameeragarwal commented on a diff in the pull request:

https://github.com/apache/spark/pull/15467#discussion_r96696307
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ColumnarBatchScan.scala 
---
@@ -0,0 +1,133 @@
+/*
+ * 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.sql.execution
+
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.execution.vectorized.{ColumnarBatch, 
ColumnVector}
+import org.apache.spark.sql.types.DataType
+
+
+/**
+ * Helper trait for abstracting scan functionality using
+ * [[org.apache.spark.sql.execution.vectorized.ColumnarBatch]]es.
+ */
+private[sql] trait ColumnarBatchScan extends CodegenSupport {
+
+  val inMemoryTableScan: InMemoryTableScanExec = null
--- End diff --

nit: this is unused right?


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



[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-18 Thread kiszk
Github user kiszk commented on a diff in the pull request:

https://github.com/apache/spark/pull/15467#discussion_r96604287
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ColumnarBatchScan.scala 
---
@@ -0,0 +1,151 @@
+/*
+ * 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.sql.execution
+
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.execution.vectorized.{ColumnarBatch, 
ColumnVector}
+import org.apache.spark.sql.types.DataType
+
+
+/**
+ * Helper trait for abstracting scan functionality using
+ * [[org.apache.spark.sql.execution.vectorized.ColumnarBatch]]es.
+ */
+private[sql] trait ColumnarBatchScan extends CodegenSupport {
+
+  val columnIndexes: Array[Int] = null
+
+  val inMemoryTableScan: InMemoryTableScanExec = null
+
+  override lazy val metrics = Map(
+"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of 
output rows"),
+"scanTime" -> SQLMetrics.createTimingMetric(sparkContext, "scan time"))
+
+  lazy val enableScanStatistics: Boolean =
+sqlContext.getConf("spark.sql.inMemoryTableScanStatistics.enable", 
"false").toBoolean
+
+  /**
+   * Generate [[ColumnVector]] expressions for our parent to consume as 
rows.
+   * This is called once per [[ColumnarBatch]].
+   */
+  private def genCodeColumnVector(
+  ctx: CodegenContext,
+  columnVar: String,
+  ordinal: String,
+  dataType: DataType,
+  nullable: Boolean): ExprCode = {
+val javaType = ctx.javaType(dataType)
+val value = ctx.getValue(columnVar, dataType, ordinal)
+val isNullVar = if (nullable) { ctx.freshName("isNull") } else { 
"false" }
+val valueVar = ctx.freshName("value")
+val str = s"columnVector[$columnVar, $ordinal, 
${dataType.simpleString}]"
+val code = s"${ctx.registerComment(str)}\n" + (if (nullable) {
+  s"""
+boolean $isNullVar = $columnVar.isNullAt($ordinal);
+$javaType $valueVar = $isNullVar ? ${ctx.defaultValue(dataType)} : 
($value);
+  """
+} else {
+  s"$javaType $valueVar = $value;"
+}).trim
+ExprCode(code, isNullVar, valueVar)
+  }
+
+  /**
+   * Produce code to process the input iterator as [[ColumnarBatch]]es.
+   * This produces an [[UnsafeRow]] for each row in each batch.
+   */
+  // TODO: return ColumnarBatch.Rows instead
+  override protected def doProduce(ctx: CodegenContext): String = {
+val input = ctx.freshName("input")
+// PhysicalRDD always just has one input
+ctx.addMutableState("scala.collection.Iterator", input, s"$input = 
inputs[0];")
+
+// metrics
+val numOutputRows = metricTerm(ctx, "numOutputRows")
+val scanTimeMetric = metricTerm(ctx, "scanTime")
+val scanTimeTotalNs = ctx.freshName("scanTime")
+ctx.addMutableState("long", scanTimeTotalNs, s"$scanTimeTotalNs = 0;")
+val incReadBatches = if (!enableScanStatistics) "" else {
+  val readPartitions = ctx.addReferenceObj("readPartitions", 
inMemoryTableScan.readPartitions)
+  val readBatches = ctx.addReferenceObj("readBatches", 
inMemoryTableScan.readBatches)
+  ctx.addMutableState("int", "initializeInMemoryTableScanStatistics",
+s"""
+   |$readPartitions.setValue(0);
+   |$readBatches.setValue(0);
+   |if ($input.hasNext()) { $readPartitions.add(1); }
+   """.stripMargin)
+  s"$readBatches.add(1);"
+}
+
+val columnarBatchClz = 
"org.apache.spark.sql.execution.vectorized.ColumnarBatch"
+val batch = ctx.freshName("batch")
+ctx.addMutableState(columnarBatchClz, batch, s"$batch = null;")
+

[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-18 Thread kiszk
Github user kiszk commented on a diff in the pull request:

https://github.com/apache/spark/pull/15467#discussion_r96604270
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ColumnarBatchScan.scala 
---
@@ -0,0 +1,151 @@
+/*
+ * 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.sql.execution
+
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.execution.vectorized.{ColumnarBatch, 
ColumnVector}
+import org.apache.spark.sql.types.DataType
+
+
+/**
+ * Helper trait for abstracting scan functionality using
+ * [[org.apache.spark.sql.execution.vectorized.ColumnarBatch]]es.
+ */
+private[sql] trait ColumnarBatchScan extends CodegenSupport {
+
+  val columnIndexes: Array[Int] = null
+
+  val inMemoryTableScan: InMemoryTableScanExec = null
+
+  override lazy val metrics = Map(
+"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of 
output rows"),
+"scanTime" -> SQLMetrics.createTimingMetric(sparkContext, "scan time"))
+
+  lazy val enableScanStatistics: Boolean =
+sqlContext.getConf("spark.sql.inMemoryTableScanStatistics.enable", 
"false").toBoolean
+
+  /**
+   * Generate [[ColumnVector]] expressions for our parent to consume as 
rows.
+   * This is called once per [[ColumnarBatch]].
+   */
+  private def genCodeColumnVector(
+  ctx: CodegenContext,
+  columnVar: String,
+  ordinal: String,
+  dataType: DataType,
+  nullable: Boolean): ExprCode = {
+val javaType = ctx.javaType(dataType)
+val value = ctx.getValue(columnVar, dataType, ordinal)
+val isNullVar = if (nullable) { ctx.freshName("isNull") } else { 
"false" }
+val valueVar = ctx.freshName("value")
+val str = s"columnVector[$columnVar, $ordinal, 
${dataType.simpleString}]"
+val code = s"${ctx.registerComment(str)}\n" + (if (nullable) {
+  s"""
+boolean $isNullVar = $columnVar.isNullAt($ordinal);
+$javaType $valueVar = $isNullVar ? ${ctx.defaultValue(dataType)} : 
($value);
+  """
+} else {
+  s"$javaType $valueVar = $value;"
+}).trim
+ExprCode(code, isNullVar, valueVar)
+  }
+
+  /**
+   * Produce code to process the input iterator as [[ColumnarBatch]]es.
+   * This produces an [[UnsafeRow]] for each row in each batch.
+   */
+  // TODO: return ColumnarBatch.Rows instead
+  override protected def doProduce(ctx: CodegenContext): String = {
+val input = ctx.freshName("input")
+// PhysicalRDD always just has one input
+ctx.addMutableState("scala.collection.Iterator", input, s"$input = 
inputs[0];")
+
+// metrics
+val numOutputRows = metricTerm(ctx, "numOutputRows")
+val scanTimeMetric = metricTerm(ctx, "scanTime")
+val scanTimeTotalNs = ctx.freshName("scanTime")
+ctx.addMutableState("long", scanTimeTotalNs, s"$scanTimeTotalNs = 0;")
+val incReadBatches = if (!enableScanStatistics) "" else {
--- End diff --

sure, I did. Even if it does not exists, it works for now.


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



[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-17 Thread sameeragarwal
Github user sameeragarwal commented on a diff in the pull request:

https://github.com/apache/spark/pull/15467#discussion_r96527596
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ColumnarBatchScan.scala 
---
@@ -0,0 +1,151 @@
+/*
+ * 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.sql.execution
+
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.execution.vectorized.{ColumnarBatch, 
ColumnVector}
+import org.apache.spark.sql.types.DataType
+
+
+/**
+ * Helper trait for abstracting scan functionality using
+ * [[org.apache.spark.sql.execution.vectorized.ColumnarBatch]]es.
+ */
+private[sql] trait ColumnarBatchScan extends CodegenSupport {
+
+  val columnIndexes: Array[Int] = null
+
+  val inMemoryTableScan: InMemoryTableScanExec = null
+
+  override lazy val metrics = Map(
+"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of 
output rows"),
+"scanTime" -> SQLMetrics.createTimingMetric(sparkContext, "scan time"))
+
+  lazy val enableScanStatistics: Boolean =
+sqlContext.getConf("spark.sql.inMemoryTableScanStatistics.enable", 
"false").toBoolean
+
+  /**
+   * Generate [[ColumnVector]] expressions for our parent to consume as 
rows.
+   * This is called once per [[ColumnarBatch]].
+   */
+  private def genCodeColumnVector(
+  ctx: CodegenContext,
+  columnVar: String,
+  ordinal: String,
+  dataType: DataType,
+  nullable: Boolean): ExprCode = {
+val javaType = ctx.javaType(dataType)
+val value = ctx.getValue(columnVar, dataType, ordinal)
+val isNullVar = if (nullable) { ctx.freshName("isNull") } else { 
"false" }
+val valueVar = ctx.freshName("value")
+val str = s"columnVector[$columnVar, $ordinal, 
${dataType.simpleString}]"
+val code = s"${ctx.registerComment(str)}\n" + (if (nullable) {
+  s"""
+boolean $isNullVar = $columnVar.isNullAt($ordinal);
+$javaType $valueVar = $isNullVar ? ${ctx.defaultValue(dataType)} : 
($value);
+  """
+} else {
+  s"$javaType $valueVar = $value;"
+}).trim
+ExprCode(code, isNullVar, valueVar)
+  }
+
+  /**
+   * Produce code to process the input iterator as [[ColumnarBatch]]es.
+   * This produces an [[UnsafeRow]] for each row in each batch.
+   */
+  // TODO: return ColumnarBatch.Rows instead
+  override protected def doProduce(ctx: CodegenContext): String = {
+val input = ctx.freshName("input")
+// PhysicalRDD always just has one input
+ctx.addMutableState("scala.collection.Iterator", input, s"$input = 
inputs[0];")
+
+// metrics
+val numOutputRows = metricTerm(ctx, "numOutputRows")
+val scanTimeMetric = metricTerm(ctx, "scanTime")
+val scanTimeTotalNs = ctx.freshName("scanTime")
+ctx.addMutableState("long", scanTimeTotalNs, s"$scanTimeTotalNs = 0;")
+val incReadBatches = if (!enableScanStatistics) "" else {
+  val readPartitions = ctx.addReferenceObj("readPartitions", 
inMemoryTableScan.readPartitions)
+  val readBatches = ctx.addReferenceObj("readBatches", 
inMemoryTableScan.readBatches)
+  ctx.addMutableState("int", "initializeInMemoryTableScanStatistics",
+s"""
+   |$readPartitions.setValue(0);
+   |$readBatches.setValue(0);
+   |if ($input.hasNext()) { $readPartitions.add(1); }
+   """.stripMargin)
+  s"$readBatches.add(1);"
+}
+
+val columnarBatchClz = 
"org.apache.spark.sql.execution.vectorized.ColumnarBatch"
+val batch = ctx.freshName("batch")
+ctx.addMutableState(columnarBatchClz, batch, s"$batch = null;")
  

[GitHub] spark pull request #15467: [SPARK-17912][SQL] Refactor code generation to ge...

2017-01-17 Thread sameeragarwal
Github user sameeragarwal commented on a diff in the pull request:

https://github.com/apache/spark/pull/15467#discussion_r96527468
  
--- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ColumnarBatchScan.scala 
---
@@ -0,0 +1,151 @@
+/*
+ * 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.sql.execution
+
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, 
ExprCode}
+import org.apache.spark.sql.execution.columnar.InMemoryTableScanExec
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.execution.vectorized.{ColumnarBatch, 
ColumnVector}
+import org.apache.spark.sql.types.DataType
+
+
+/**
+ * Helper trait for abstracting scan functionality using
+ * [[org.apache.spark.sql.execution.vectorized.ColumnarBatch]]es.
+ */
+private[sql] trait ColumnarBatchScan extends CodegenSupport {
+
+  val columnIndexes: Array[Int] = null
+
+  val inMemoryTableScan: InMemoryTableScanExec = null
+
+  override lazy val metrics = Map(
+"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of 
output rows"),
+"scanTime" -> SQLMetrics.createTimingMetric(sparkContext, "scan time"))
+
+  lazy val enableScanStatistics: Boolean =
+sqlContext.getConf("spark.sql.inMemoryTableScanStatistics.enable", 
"false").toBoolean
+
+  /**
+   * Generate [[ColumnVector]] expressions for our parent to consume as 
rows.
+   * This is called once per [[ColumnarBatch]].
+   */
+  private def genCodeColumnVector(
+  ctx: CodegenContext,
+  columnVar: String,
+  ordinal: String,
+  dataType: DataType,
+  nullable: Boolean): ExprCode = {
+val javaType = ctx.javaType(dataType)
+val value = ctx.getValue(columnVar, dataType, ordinal)
+val isNullVar = if (nullable) { ctx.freshName("isNull") } else { 
"false" }
+val valueVar = ctx.freshName("value")
+val str = s"columnVector[$columnVar, $ordinal, 
${dataType.simpleString}]"
+val code = s"${ctx.registerComment(str)}\n" + (if (nullable) {
+  s"""
+boolean $isNullVar = $columnVar.isNullAt($ordinal);
+$javaType $valueVar = $isNullVar ? ${ctx.defaultValue(dataType)} : 
($value);
+  """
+} else {
+  s"$javaType $valueVar = $value;"
+}).trim
+ExprCode(code, isNullVar, valueVar)
+  }
+
+  /**
+   * Produce code to process the input iterator as [[ColumnarBatch]]es.
+   * This produces an [[UnsafeRow]] for each row in each batch.
+   */
+  // TODO: return ColumnarBatch.Rows instead
+  override protected def doProduce(ctx: CodegenContext): String = {
+val input = ctx.freshName("input")
+// PhysicalRDD always just has one input
+ctx.addMutableState("scala.collection.Iterator", input, s"$input = 
inputs[0];")
+
+// metrics
+val numOutputRows = metricTerm(ctx, "numOutputRows")
+val scanTimeMetric = metricTerm(ctx, "scanTime")
+val scanTimeTotalNs = ctx.freshName("scanTime")
+ctx.addMutableState("long", scanTimeTotalNs, s"$scanTimeTotalNs = 0;")
+val incReadBatches = if (!enableScanStatistics) "" else {
--- End diff --

Should we leave this out from this refactoring?


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