Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0b293db
[SPARK-29774][SQL] Date and Timestamp type +/- null should be null as…
yaooqinn Nov 6, 2019
f726297
Merge branch 'master' into SPARK-29774
yaooqinn Nov 27, 2019
e7225a3
regen golden file
yaooqinn Nov 27, 2019
b925517
null - dates
yaooqinn Nov 27, 2019
cd49411
Merge branch 'master' into SPARK-29774
yaooqinn Dec 2, 2019
57b13e9
support +/-
yaooqinn Dec 2, 2019
eab6a83
support ×/÷
yaooqinn Dec 2, 2019
e8b75ba
import
yaooqinn Dec 2, 2019
02b3738
childResolved required
yaooqinn Dec 2, 2019
e89d806
regen golden file
yaooqinn Dec 2, 2019
0694e07
update comments
yaooqinn Dec 2, 2019
0f5618b
fix tests
yaooqinn Dec 3, 2019
efab3ec
fix tests
yaooqinn Dec 3, 2019
1c27be1
refine case match pattern
yaooqinn Dec 3, 2019
5df6980
fix ut
yaooqinn Dec 3, 2019
9817d2d
hack assert Equal
yaooqinn Dec 3, 2019
9808b9c
regen g f
yaooqinn Dec 3, 2019
b190612
AnalysisTest
yaooqinn Dec 3, 2019
e544137
regen g f
yaooqinn Dec 3, 2019
83705fd
fix test
yaooqinn Dec 4, 2019
846802d
date add/sub only work for int/smallint/tinyint
yaooqinn Dec 4, 2019
4af7edb
regen g f
yaooqinn Dec 4, 2019
a67be30
refine
yaooqinn Dec 4, 2019
9a1affd
type coercion for subtract timestamp
yaooqinn Dec 4, 2019
ae70022
add and reorgnize tests in datetime.sql
yaooqinn Dec 4, 2019
571225b
DateExpressionsSuite
yaooqinn Dec 4, 2019
6052e5a
fix py
yaooqinn Dec 4, 2019
928fd86
fix py
yaooqinn Dec 4, 2019
254d2d2
Revert "fix py"
yaooqinn Dec 5, 2019
5dd632c
fix py
yaooqinn Dec 5, 2019
c84d46e
rm unresolved binary arithmetic
yaooqinn Dec 5, 2019
a44948e
import
yaooqinn Dec 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix ut
  • Loading branch information
yaooqinn committed Dec 3, 2019
commit 5df69809ce9e62a8daf38c11e57c78552888bf50
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.expressions.objects._
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.{Rule, _}
import org.apache.spark.sql.catalyst.rules._
import org.apache.spark.sql.catalyst.trees.TreeNodeRef
import org.apache.spark.sql.catalyst.util.toPrettySQL
import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogPlugin, CatalogV2Util, Identifier, LookupCatalog, Table, TableCatalog, TableChange, V1Table}
Expand Down Expand Up @@ -285,7 +285,7 @@ class Analyzer(
case (CalendarIntervalType, CalendarIntervalType) => Subtract(l, r)
case (_, CalendarIntervalType) => Cast(TimeSub(l, r), l.dataType)
case (TimestampType, _) => SubtractTimestamps(l, r)
case (_, TimestampType) => SubtractTimestamps(Cast(l, TimestampType), r)
case (_, TimestampType) => SubtractTimestamps(l, r)
case (_, DateType) => if (conf.usePostgreSQLDialect) {
DateDiff(l, r)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ package object dsl {
def unary_! : Predicate = Not(expr)
def unary_~ : Expression = BitwiseNot(expr)

def + (other: Expression): Expression = UnresolvedAdd(expr, other)
def - (other: Expression): Expression = UnresolvedSubtract(expr, other)
def * (other: Expression): Expression = UnresolvedMultiply(expr, other)
def / (other: Expression): Expression = UnresolvedDivide(expr, other)
def + (other: Expression): Expression = Add(expr, other)
def - (other: Expression): Expression = Subtract(expr, other)
def * (other: Expression): Expression = Multiply(expr, other)
def / (other: Expression): Expression = Divide(expr, other)
def div (other: Expression): Expression = IntegralDivide(expr, other)
def % (other: Expression): Expression = Remainder(expr, other)
def & (other: Expression): Expression = BitwiseAnd(expr, other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
)

override lazy val mergeExpressions = Seq(
/* sum = */ Add(sum.left, sum.right),
/* count = */ Add(count.left, count.right)
/* sum = */ sum.left + sum.right,
/* count = */ count.left + count.right
)

// If all input are nulls, count will be 0 and we will get null after the division.
Expand All @@ -83,14 +83,14 @@ case class Average(child: Expression) extends DeclarativeAggregate with Implicit
case CalendarIntervalType =>
DivideInterval(sum.cast(resultType), count.cast(DoubleType))
case _ =>
Divide(sum.cast(resultType), count.cast(resultType))
sum.cast(resultType) / count.cast(resultType)
}

override lazy val updateExpressions: Seq[Expression] = Seq(
/* sum = */
Add(
sum,
coalesce(child.cast(sumDataType), Literal.default(sumDataType))),
/* count = */ If(child.isNull, count, Add(count, 1L))
/* count = */ If(child.isNull, count, count + 1L)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ case class Sum(child: Expression) extends DeclarativeAggregate with ImplicitCast
if (child.nullable) {
Seq(
/* sum = */
coalesce(Add(coalesce(sum, zero), child.cast(sumDataType)), sum)
coalesce(coalesce(sum, zero) + child.cast(sumDataType), sum)
)
} else {
Seq(
/* sum = */
Add(coalesce(sum, zero), child.cast(sumDataType))
coalesce(sum, zero) + child.cast(sumDataType)
)
}
}

override lazy val mergeExpressions: Seq[Expression] = {
Seq(
/* sum = */
coalesce(Add(coalesce(sum.left, zero), sum.right), sum.left)
coalesce(coalesce(sum.left, zero) + sum.right, sum.left)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,22 @@ class ExpressionParserSuite extends AnalysisTest {

test("binary arithmetic expressions") {
// Simple operations
assertEqual("a * b", 'a * 'b)
assertEqual("a / b", 'a / 'b)
assertEqual("a * b", UnresolvedMultiply('a, 'b))
Copy link
Contributor

Choose a reason for hiding this comment

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

we can update the assertEqual method to resolve UnresolvedAdd etc. manually.

assertEqual("a / b", UnresolvedDivide('a, 'b))
assertEqual("a DIV b", 'a div 'b)
assertEqual("a % b", 'a % 'b)
assertEqual("a + b", 'a + 'b)
assertEqual("a - b", 'a - 'b)
assertEqual("a + b", UnresolvedAdd('a, 'b))
assertEqual("a - b", UnresolvedSubtract('a, 'b))
assertEqual("a & b", 'a & 'b)
assertEqual("a ^ b", 'a ^ 'b)
assertEqual("a | b", 'a | 'b)

// Check precedences
assertEqual(
"a * t | b ^ c & d - e + f % g DIV h / i * k",
'a * 't | ('b ^ ('c & ('d - 'e + (('f % 'g div 'h) / 'i * 'k)))))
UnresolvedMultiply('a, 't) |
('b ^ ('c & UnresolvedAdd(UnresolvedSubtract('d, 'e),
UnresolvedMultiply(UnresolvedDivide(('f % 'g) div 'h, 'i), 'k)))))
}

test("unary arithmetic expressions") {
Expand Down Expand Up @@ -257,8 +259,9 @@ class ExpressionParserSuite extends AnalysisTest {
private def lv(s: Symbol) = UnresolvedNamedLambdaVariable(Seq(s.name))

test("lambda functions") {
assertEqual("x -> x + 1", LambdaFunction(lv('x) + 1, Seq(lv('x))))
assertEqual("(x, y) -> x + y", LambdaFunction(lv('x) + lv('y), Seq(lv('x), lv('y))))
assertEqual("x -> x + 1", LambdaFunction(UnresolvedAdd(lv('x), 1), Seq(lv('x))))
assertEqual("(x, y) -> x + y",
LambdaFunction(UnresolvedAdd(lv('x), lv('y)), Seq(lv('x), lv('y))))
}

test("window function expressions") {
Expand All @@ -284,12 +287,14 @@ class ExpressionParserSuite extends AnalysisTest {
// Test use of expressions in window functions.
assertEqual(
"sum(product + 1) over (partition by ((product) + (1)) order by 2)",
WindowExpression('sum.function('product + 1),
WindowSpecDefinition(Seq('product + 1), Seq(Literal(2).asc), UnspecifiedFrame)))
WindowExpression('sum.function(UnresolvedAdd('product, 1)),
WindowSpecDefinition(
Seq(UnresolvedAdd('product, 1)), Seq(Literal(2).asc), UnspecifiedFrame)))
assertEqual(
"sum(product + 1) over (partition by ((product / 2) + 1) order by 2)",
WindowExpression('sum.function('product + 1),
WindowSpecDefinition(Seq('product / 2 + 1), Seq(Literal(2).asc), UnspecifiedFrame)))
WindowExpression('sum.function(UnresolvedAdd('product, 1)),
WindowSpecDefinition(Seq(UnresolvedAdd(UnresolvedDivide('product, 2), 1)),
Seq(Literal(2).asc), UnspecifiedFrame)))
}

test("range/rows window function expressions") {
Expand Down Expand Up @@ -390,13 +395,14 @@ class ExpressionParserSuite extends AnalysisTest {
assertEqual("case when a = 1 then b when a = 2 then c else d end",
CaseWhen(Seq(('a === 1, 'b.expr), ('a === 2, 'c.expr)), 'd))
assertEqual("case when (1) + case when a > b then c else d end then f else g end",
CaseWhen(Seq((Literal(1) + CaseWhen(Seq(('a > 'b, 'c.expr)), 'd.expr), 'f.expr)), 'g))
CaseWhen(
Seq((UnresolvedAdd(Literal(1), CaseWhen(Seq(('a > 'b, 'c.expr)), 'd.expr)), 'f.expr)), 'g))
}

test("dereference") {
assertEqual("a.b", UnresolvedAttribute("a.b"))
assertEqual("`select`.b", UnresolvedAttribute("select.b"))
assertEqual("(a + b).b", ('a + 'b).getField("b")) // This will fail analysis.
assertEqual("(a + b).b", UnresolvedAdd('a, 'b).getField("b")) // This will fail analysis.
assertEqual(
"struct(a, b).b",
namedStruct(NamePlaceholder, 'a, NamePlaceholder, 'b).getField("b"))
Expand All @@ -418,13 +424,13 @@ class ExpressionParserSuite extends AnalysisTest {

test("subscript") {
assertEqual("a[b]", 'a.getItem('b))
assertEqual("a[1 + 1]", 'a.getItem(Literal(1) + 1))
assertEqual("a[1 + 1]", 'a.getItem(UnresolvedAdd(Literal(1), 1)))
assertEqual("`c`.a[b]", UnresolvedAttribute("c.a").getItem('b))
}

test("parenthesis") {
assertEqual("(a)", 'a)
assertEqual("r * (a + b)", 'r * ('a + 'b))
assertEqual("r * (a + b)", UnresolvedMultiply('r, UnresolvedAdd('a, 'b)))
}

test("type constructors") {
Expand Down Expand Up @@ -749,8 +755,9 @@ class ExpressionParserSuite extends AnalysisTest {
}

test("composed expressions") {
assertEqual("1 + r.r As q", (Literal(1) + UnresolvedAttribute("r.r")).as("q"))
assertEqual("1 - f('o', o(bar))", Literal(1) - 'f.function("o", 'o.function('bar)))
assertEqual("1 + r.r As q", UnresolvedAdd(Literal(1), UnresolvedAttribute("r.r")).as("q"))
assertEqual("1 - f('o', o(bar))",
UnresolvedSubtract(Literal(1), 'f.function("o", 'o.function('bar))))
intercept("1 - f('o', o(bar)) hello * world", "mismatched input '*'")
}

Expand Down