Github user markhamstra commented on a diff in the pull request:

    https://github.com/apache/spark/pull/993#discussion_r14847035
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala
 ---
    @@ -0,0 +1,421 @@
    +/*
    + * 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.catalyst.expressions.codegen
    +
    +import scala.language.existentials
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.sql.catalyst.expressions
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.types._
    +
    +/**
    + * A base class for generators of byte code that performs expression 
evaluation.  Includes helpers
    + * for refering to Catalyst types and building trees that perform 
evaluation of individual
    + * expressions.
    + */
    +abstract class CodeGenerator extends Logging {
    +  import scala.reflect.runtime.{universe => ru}
    +  import scala.reflect.runtime.universe._
    +
    +  import scala.tools.reflect.ToolBox
    +
    +  val toolBox = runtimeMirror(getClass.getClassLoader).mkToolBox()
    +
    +  val rowType = typeOf[Row]
    +  val mutableRowType = typeOf[MutableRow]
    +  val genericRowType = typeOf[GenericRow]
    +  val genericMutableRowType = typeOf[GenericMutableRow]
    +
    +  val projectionType = typeOf[Projection]
    +  val mutableProjectionType = typeOf[MutableProjection]
    +
    +  private val curId = new java.util.concurrent.atomic.AtomicInteger()
    +  private val javaSeperator = "$"
    +
    +  /**
    +   * Returns a term name that is unique within this instance of a 
`CodeGenerator`.
    +   *
    +   * (Since we aren't in a macro context we do not seem to have access to 
the built in `freshName`
    +   * function.)
    +   */
    +  protected def freshName(prefix: String): TermName = {
    +    newTermName(s"$prefix$javaSeperator${curId.getAndIncrement}")
    +  }
    +
    +  /**
    +   * Scala ASTs for evaluating an [[Expression]] given a [[Row]] of input.
    +   *
    +   * @param code The sequence of statements required to evaluate the 
expression.
    +   * @param nullTerm A term that holds a boolean value representing 
whether the expression evaluated
    +   *                 to null.
    +   * @param primitiveTerm A term for a possible primitive value of the 
result of the evaluation. Not
    +   *                      valid if `nullTerm` is set to `false`.
    +   * @param objectTerm An possibly boxed version of the result of 
evaluating this expression.
    +   */
    +  protected case class EvaluatedExpression(
    +      code: Seq[Tree],
    +      nullTerm: TermName,
    +      primitiveTerm: TermName,
    +      objectTerm: TermName)
    +
    +  /**
    +   * Given an expression tree returns the code required to determine both 
if the result is NULL
    +   * as well as the code required to compute the value.
    +   */
    +  def expressionEvaluator(e: Expression): EvaluatedExpression = {
    +    val primitiveTerm = freshName("primitiveTerm")
    +    val nullTerm = freshName("nullTerm")
    +    val objectTerm = freshName("objectTerm")
    +
    +    implicit class Evaluate1(e: Expression) {
    +      def castOrNull(f: TermName => Tree, dataType: DataType): Seq[Tree] = 
{
    +        val eval = expressionEvaluator(e)
    +        eval.code ++
    +          q"""
    +          val $nullTerm = ${eval.nullTerm}
    +          val $primitiveTerm =
    +            if($nullTerm)
    +              ${defaultPrimitive(dataType)}
    +            else
    +              ${f(eval.primitiveTerm)}
    +        """.children
    --- End diff --
    
    Is this really the way we want to format `q"""`?  To my eye, it would be 
much more readable as:
    ```scala
    q"""
      val $nullTerm = ${eval1.nullTerm} || ${eval2.nullTerm}
      val $primitiveTerm: ${termForType(resultType)} =
        if($nullTerm) {
          ${defaultPrimitive(resultType)}
        } else {
          $resultCode.asInstanceOf[${termForType(resultType)}]
        }
     """.children : Seq[Tree]
    ```



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

Reply via email to