Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11557#discussion_r57271566
  
    --- Diff: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/ng/ExpressionParserSuite.scala
 ---
    @@ -0,0 +1,494 @@
    +/*
    + * 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.parser.ng
    +
    +import java.sql.{Date, Timestamp}
    +
    +import org.apache.spark.sql.catalyst.TableIdentifier
    +import org.apache.spark.sql.catalyst.analysis.{UnresolvedAttribute, _}
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.plans.PlanTest
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.CalendarInterval
    +
    +/**
    + * Test basic expression parsing. If a type of expression is supported it 
should be tested here.
    + *
    + * Please note that some of the expressions test don't have to be sound 
expressions, only their
    + * structure needs to be valid. Unsound expressions should be caught by 
the Analyzer or
    + * CheckAnalysis classes.
    + */
    +class ExpressionParserSuite extends PlanTest {
    +  import CatalystSqlParser._
    +  import org.apache.spark.sql.catalyst.dsl.expressions._
    +  import org.apache.spark.sql.catalyst.dsl.plans._
    +
    +  def assertEqual(sqlCommand: String, e: Expression): Unit = {
    +    compareExpressions(parseExpression(sqlCommand), e)
    +  }
    +
    +  def intercept(sqlCommand: String, messages: String*): Unit = {
    +    val e = intercept[ParseException](parseExpression(sqlCommand))
    +    messages.foreach { message =>
    +      assert(e.message.contains(message))
    +    }
    +  }
    +
    +  test("star expressions") {
    +    // Global Star
    +    assertEqual("*", UnresolvedStar(None))
    +
    +    // Targeted Star
    +    assertEqual("a.b.*", UnresolvedStar(Option(Seq("a", "b"))))
    +  }
    +
    +  // NamedExpression (Alias/Multialias)
    +  test("named expressions") {
    +    // No Alias
    +    val r0 = 'a
    +    assertEqual("a", r0)
    +
    +    // Single Alias.
    +    val r1 = 'a as "b"
    +    assertEqual("a as b", r1)
    +    assertEqual("a b", r1)
    +
    +    // Multi-Alias
    +    assertEqual("a as (b, c)", MultiAlias('a, Seq("b", "c")))
    +    assertEqual("a() (b, c)", MultiAlias('a.function(), Seq("b", "c")))
    +
    +    // Numeric literals without a space between the literal qualifier and 
the alias, should not be
    +    // interpreted as such. An unresolved reference should be returned 
instead.
    +    // TODO add the JIRA-ticket number.
    +    assertEqual("1SL", Symbol("1SL"))
    +
    +    // Aliased star is allowed.
    +    assertEqual("a.* b", UnresolvedStar(Option(Seq("a"))) as 'b)
    +  }
    +
    +  test("binary logical expressions") {
    +    // And
    +    assertEqual("a and b", 'a && 'b)
    +
    +    // Or
    +    assertEqual("a or b", 'a || 'b)
    +
    +    // Combination And/Or check precedence
    +    assertEqual("a and b or c and d", ('a && 'b) || ('c && 'd))
    +    assertEqual("a or b or c and d", 'a || 'b || ('c && 'd))
    +
    +    // Multiple AND/OR get converted into a balanced tree
    +    assertEqual("a or b or c or d or e or f", (('a || 'b) || 'c) || (('d 
|| 'e) || 'f))
    +    assertEqual("a and b and c and d and e and f", (('a && 'b) && 'c) && 
(('d && 'e) && 'f))
    +  }
    +
    +  test("long binary logical expressions") {
    +    def testVeryBinaryExpression(op: String, clazz: Class[_]): Unit = {
    +      val sql = (1 to 1000).map(x => s"$x == $x").mkString(op)
    +      val e = parseExpression(sql)
    +      assert(e.collect { case _: EqualTo => true }.size === 1000)
    +      assert(e.collect { case x if clazz.isInstance(x) => true }.size === 
999)
    +    }
    +    testVeryBinaryExpression(" AND ", classOf[And])
    +    testVeryBinaryExpression(" OR ", classOf[Or])
    +  }
    +
    +  test("not expressions") {
    +    assertEqual("not a", !'a)
    +    assertEqual("!a", !'a)
    +    assertEqual("not true > true", Not(GreaterThan(true, true)))
    +  }
    +
    +  test("exists expression") {
    +    intercept("exists (select 1 from b where b.x = a.x)", "EXISTS clauses 
are not supported")
    +  }
    +
    +  test("comparison expressions") {
    +    assertEqual("a = b", 'a === 'b)
    +    assertEqual("a == b", 'a === 'b)
    +    assertEqual("a <=> b", 'a <=> 'b)
    +    assertEqual("a <> b", 'a =!= 'b)
    +    assertEqual("a != b", 'a =!= 'b)
    +    assertEqual("a < b", 'a < 'b)
    +    assertEqual("a <= b", 'a <= 'b)
    +    assertEqual("a > b", 'a > 'b)
    +    assertEqual("a >= b", 'a >= 'b)
    +  }
    +
    +  test("between expressions") {
    +    assertEqual("a between b and c", 'a >= 'b && 'a <= 'c)
    +    assertEqual("a not between b and c", !('a >= 'b && 'a <= 'c))
    +  }
    +
    +  test("in expressions") {
    +    assertEqual("a in (b, c, d)", 'a in ('b, 'c, 'd))
    +    assertEqual("a not in (b, c, d)", !('a in ('b, 'c, 'd)))
    +  }
    +
    +  test("in sub-query") {
    +    intercept("a in (select b from c)", "IN with a Sub-query is currently 
not supported")
    +  }
    +
    +  test("like expressions") {
    +    assertEqual("a like 'pattern%'", 'a like "pattern%")
    +    assertEqual("a not like 'pattern%'", !('a like "pattern%"))
    +    assertEqual("a rlike 'pattern%'", 'a rlike "pattern%")
    +    assertEqual("a not rlike 'pattern%'", !('a rlike "pattern%"))
    +    assertEqual("a regexp 'pattern%'", 'a rlike "pattern%")
    +    assertEqual("a not regexp 'pattern%'", !('a rlike "pattern%"))
    +  }
    +
    +  test("is null expressions") {
    +    assertEqual("a is null", 'a.isNull)
    +    assertEqual("a is not null", 'a.isNotNull)
    +  }
    +
    +  test("binary arithmetic expressions") {
    +    // Simple operations
    +    assertEqual("a * b", 'a * 'b)
    +    assertEqual("a / b", 'a / 'b)
    +    assertEqual("a DIV b", ('a / 'b).cast(LongType))
    +    assertEqual("a % b", 'a % 'b)
    +    assertEqual("a + b", 'a + 'b)
    +    assertEqual("a - b", '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 / 'h).cast(LongType) / 'i 
* 'k)))))
    +  }
    +
    +  test("unary arithmetic expressions") {
    +    assertEqual("+a", 'a)
    +    assertEqual("-a", -'a)
    +    assertEqual("~a", ~'a)
    +    assertEqual("-+~~a", -(~(~'a)))
    +  }
    +
    +  test("cast expressions") {
    +    // Note that DataType parsing is tested elsewhere.
    +    assertEqual("cast(a as int)", 'a.cast(IntegerType))
    +    assertEqual("cast(a as timestamp)", 'a.cast(TimestampType))
    +    assertEqual("cast(a as array<int>)", 'a.cast(ArrayType(IntegerType)))
    +    assertEqual("cast(cast(a as int) as long)", 
'a.cast(IntegerType).cast(LongType))
    +  }
    +
    +  test("function expressions") {
    +    assertEqual("foo()", 'foo.function())
    +    assertEqual("foo.bar()", Symbol("foo.bar").function())
    +    assertEqual("foo(*)", 'foo.function(1))
    +    assertEqual("foo(a, b)", 'foo.function('a, 'b))
    +    assertEqual("foo(all a, b)", 'foo.function('a, 'b))
    +    assertEqual("foo(distinct a, b)", 'foo.distinctFunction('a, 'b))
    +    assertEqual("grouping(distinct a, b)", 'grouping.distinctFunction('a, 
'b))
    +    assertEqual("`select`(all a, b)", 'select.function('a, 'b))
    +  }
    +
    +  test("window function expressions") {
    +    val func = 'foo.function(1)
    +    def windowed(
    +        partitioning: Seq[Expression] = Seq.empty,
    +        ordering: Seq[SortOrder] = Seq.empty,
    +        frame: WindowFrame = UnspecifiedFrame): Expression = {
    +      WindowExpression(func, WindowSpecDefinition(partitioning, ordering, 
frame))
    +    }
    +
    +    // Basic window testing.
    +    assertEqual("foo(*) over w1", UnresolvedWindowExpression(func, 
WindowSpecReference("w1")))
    +    assertEqual("foo(*) over ()", windowed())
    +    assertEqual("foo(*) over (partition by a, b)", windowed(Seq('a, 'b)))
    +    assertEqual("foo(*) over (order by a desc, b asc)", 
windowed(Seq.empty, Seq('a.desc, 'b.asc )))
    +    assertEqual("foo(*) over (partition by a, b order by c)", 
windowed(Seq('a, 'b), Seq('c.asc)))
    +
    +    // 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)))
    +    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)))
    +
    +    // Range/Row
    +    val frameTypes = Seq(("rows", RowFrame), ("range", RangeFrame))
    +    val boundaries = Seq(
    +      ("10 preceding", ValuePreceding(10), CurrentRow),
    +      ("3 + 1 following", ValueFollowing(4), CurrentRow), // Will fail 
during analysis
    +      ("unbounded preceding", UnboundedPreceding, CurrentRow),
    +      ("unbounded following", UnboundedFollowing, CurrentRow), // Will 
fail during analysis
    +      ("between unbounded preceding and current row", UnboundedPreceding, 
CurrentRow),
    +      ("between unbounded preceding and unbounded following",
    +        UnboundedPreceding, UnboundedFollowing),
    +      ("between 10 preceding and current row", ValuePreceding(10), 
CurrentRow),
    +      ("between current row and 5 following", CurrentRow, 
ValueFollowing(5)),
    +      ("between 10 preceding and 5 following", ValuePreceding(10), 
ValueFollowing(5))
    +    )
    +    frameTypes.foreach {
    +      case (frameTypeSql, frameType) =>
    +        boundaries.foreach {
    +          case (boundarySql, begin, end) =>
    +            val query = s"foo(*) over (partition by a order by b 
$frameTypeSql $boundarySql)"
    +            val expr = windowed(Seq('a), Seq('b.asc), 
SpecifiedWindowFrame(frameType, begin, end))
    +            assertEqual(query, expr)
    +        }
    +    }
    +
    +    // We cannot use non integer constants.
    +    intercept("foo(*) over (partition by a order by b rows 10.0 
preceding)",
    +      "Frame bound value must be a constant integer.")
    +
    +    // We cannot use an arbitrary expression.
    +    intercept("foo(*) over (partition by a order by b rows exp(b) 
preceding)",
    +      "Frame bound value must be a constant integer.")
    +
    +    // We cannot have a frame without an order by clause.
    +    intercept("foo(*) over (partition by a rows 10 preceding)", 
"mismatched input 'rows'")
    +  }
    +
    +  test("row constructor") {
    +    // Note that '(a)' will be interpreted as a nested expression.
    +    assertEqual("(a, b)", CreateStruct(Seq('a, 'b)))
    --- End diff --
    
    is it standard? Previously we only `struct(a, b)` as row constructor


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to