Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-8945][SQL] Add add and subtract expressions for IntervalType #7398

Closed
wants to merge 8 commits into from

Conversation

viirya
Copy link
Member

@viirya viirya commented Jul 14, 2015

JIRA: https://issues.apache.org/jira/browse/SPARK-8945

Add add and subtract expressions for IntervalType.

@SparkQA
Copy link

SparkQA commented Jul 14, 2015

Test build #37240 has finished for PR 7398 at commit d3e9d0e.

  • This patch fails Scala style tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -87,6 +88,10 @@ abstract class BinaryArithmetic extends BinaryOperator {
def decimalMethod: String =
sys.error("BinaryArithmetics must override either decimalMethod or genCode")

/** Name of the function for this expression on a [[Interval]] type. */
def intervalMethod: String =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems excessive, since it won't work for multiply or division.

@rxin
Copy link
Contributor

rxin commented Jul 15, 2015

Let's revisit this patch once #7348 is merged.

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37301 has finished for PR 7398 at commit acfe1ab.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@viirya
Copy link
Member Author

viirya commented Jul 15, 2015

A failure about modified error message. I will update soon.

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37316 has finished for PR 7398 at commit 83ec129.

  • This patch fails Scala style tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@viirya
Copy link
Member Author

viirya commented Jul 15, 2015

There are some conflicts since #7348 is merged. I will update later.

@@ -32,6 +32,14 @@ object TypeUtils {
}
}

def checkForNumericAndIntervalExpr(t: DataType, caller: String): TypeCheckResult = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now #7348 is in, just create a TypeCollection that contains all the numeric types as well as interval type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I will update again.

…ract

Conflicts:
	sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
	sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala
@@ -86,6 +86,31 @@ public Interval(int months, long microseconds) {
this.microseconds = microseconds;
}

public Interval doOp(Interval that, String op) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think you want this -- it is super slow. Just remove this function, and define codegen in Add / Subtract, rather than relying on what BinaryArithmetic provides.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I wanted to avoid defining codegen in two operations. Update later together.

…ract

Conflicts:
	sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ExpressionTypeCheckingSuite.scala

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = dataType match {
case dt: DecimalType =>
defineCodeGen(ctx, ev, (eval1, eval2) => s"$eval1.$decimalMethod($eval2)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just inline $decimalMethod here; don't define the function anymore.

@rxin
Copy link
Contributor

rxin commented Jul 15, 2015

LGTM other than that.

@rxin
Copy link
Contributor

rxin commented Jul 15, 2015

LGTM

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37330 has finished for PR 7398 at commit 13a2fc5.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • trait ImplicitCastInputTypes extends ExpectsInputTypes
    • abstract class BinaryOperator extends BinaryExpression with ExpectsInputTypes
    • case class UnaryMinus(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class UnaryPositive(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Abs(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class BitwiseNot(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Factorial(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Hex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Unhex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Md5(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Sha1(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Crc32(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Not(child: Expression)
    • case class And(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • case class Or(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • trait StringRegexExpression extends ImplicitCastInputTypes
    • trait String2StringExpression extends ImplicitCastInputTypes
    • trait StringComparison extends ImplicitCastInputTypes
    • case class StringSpace(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class StringLength(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Ascii(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Base64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class UnBase64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37337 has finished for PR 7398 at commit dbe3906.

  • This patch passes all tests.
  • This patch does not merge cleanly.
  • This patch adds the following public classes (experimental):
    • trait ImplicitCastInputTypes extends ExpectsInputTypes
    • abstract class BinaryOperator extends BinaryExpression with ExpectsInputTypes
    • case class UnaryMinus(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class UnaryPositive(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Abs(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class BitwiseNot(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Factorial(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Hex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Unhex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Md5(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Sha1(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Crc32(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Not(child: Expression)
    • case class And(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • case class Or(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • trait StringRegexExpression extends ImplicitCastInputTypes
    • trait String2StringExpression extends ImplicitCastInputTypes
    • trait StringComparison extends ImplicitCastInputTypes
    • case class StringSpace(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class StringLength(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Ascii(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Base64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class UnBase64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37342 has finished for PR 7398 at commit 5abae28.

  • This patch fails PySpark unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • class LDAModel(JavaModelWrapper):
    • class LDA(object):
    • case class Round(child: Expression, scale: Expression)

@viirya
Copy link
Member Author

viirya commented Jul 15, 2015

Looks like an unrelated failure.

@SparkQA
Copy link

SparkQA commented Jul 15, 2015

Test build #37339 has finished for PR 7398 at commit 6f5b72e.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • class LDAModel(JavaModelWrapper):
    • class LDA(object):
    • trait ImplicitCastInputTypes extends ExpectsInputTypes
    • abstract class BinaryOperator extends BinaryExpression with ExpectsInputTypes
    • case class UnaryMinus(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class UnaryPositive(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Abs(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class BitwiseNot(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class Factorial(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Hex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Unhex(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Round(child: Expression, scale: Expression)
    • case class Md5(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Sha1(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Crc32(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Not(child: Expression)
    • case class And(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • case class Or(left: Expression, right: Expression) extends BinaryOperator with Predicate
    • trait StringRegexExpression extends ImplicitCastInputTypes
    • trait String2StringExpression extends ImplicitCastInputTypes
    • trait StringComparison extends ImplicitCastInputTypes
    • case class StringSpace(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class StringLength(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Ascii(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class Base64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes
    • case class UnBase64(child: Expression) extends UnaryExpression with ImplicitCastInputTypes

val NumericAndInterval = TypeCollection(
ByteType, ShortType, IntegerType, LongType,
FloatType, DoubleType, DecimalType,
IntervalType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we simplify it to TypeCollection(NumericType, IntervalType)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because TypeCollection also specifies precedence for types, I am not sure if it works when we use NumericType here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we should use NumericType here.
Think about 2 + "2", before this PR, we will first cast "2" to 2.0(double is the default for numeric type), and then the result should be double 4.0. After this PR, we will first cast "2" to 2(byte is the default for your type collection), and thus make the result of type int.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Another problem that might be minor is that the casting error message will become argument 1 is expected to be of type (numeric or interval), instead of ...type (tinyint or smallint or int or bigint or float or double or decimal or interval....

Is it still informative? Because supposed that NumericType should be internal? Will users know what type is numeric meaning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw there are already use cases for directly showing numeric in error message. So I updated this too.

@cloud-fan
Copy link
Contributor

lgtm

…ract

Conflicts:
	sql/catalyst/src/main/scala/org/apache/spark/sql/types/AbstractDataType.scala
@SparkQA
Copy link

SparkQA commented Jul 16, 2015

Test build #37485 has finished for PR 7398 at commit acd1f1e.

  • This patch fails PySpark unit tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
    • class RFormula(override val uid: String)
    • case class Length(child: Expression) extends UnaryExpression with ExpectsInputTypes
    • case class FormatNumber(x: Expression, d: Expression)

@viirya
Copy link
Member Author

viirya commented Jul 16, 2015

unrelated failure.

@viirya
Copy link
Member Author

viirya commented Jul 16, 2015

please retest this.

@rxin
Copy link
Contributor

rxin commented Jul 17, 2015

Jenkins, retest this please.

@SparkQA
Copy link

SparkQA commented Jul 17, 2015

Test build #37608 has finished for PR 7398 at commit acd1f1e.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@rxin
Copy link
Contributor

rxin commented Jul 17, 2015

Thanks - I merged it and resolved the conflict.

@asfgit asfgit closed this in eba6a1a Jul 17, 2015
@viirya viirya deleted the interval_add_subtract branch December 27, 2023 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants