Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.connector.expressions;

import org.apache.spark.annotation.Evolving;

import java.io.Serializable;

/**
* Represent an extract function, which extracts and returns the value of a
* specified datetime field from a datetime or interval value expression.
* <p>
* The currently supported fields names following the ISO standard:
* <ol>
* <li> <code>SECOND</code> Since 3.4.0 </li>
* <li> <code>MINUTE</code> Since 3.4.0 </li>
* <li> <code>HOUR</code> Since 3.4.0 </li>
* <li> <code>MONTH</code> Since 3.4.0 </li>
* <li> <code>QUARTER</code> Since 3.4.0 </li>
* <li> <code>YEAR</code> Since 3.4.0 </li>
* <li> <code>DAY_OF_WEEK</code> Since 3.4.0 </li>
* <li> <code>DAY</code> Since 3.4.0 </li>
* <li> <code>DAY_OF_YEAR</code> Since 3.4.0 </li>
* <li> <code>WEEK</code> Since 3.4.0 </li>
* <li> <code>YEAR_OF_WEEK</code> Since 3.4.0 </li>
* </ol>
*
* @since 3.4.0
*/

@Evolving
public class Extract implements Expression, Serializable {

private String field;
private Expression source;

public Extract(String field, Expression source) {
this.field = field;
this.source = source;
}

public String field() { return field; }
public Expression source() { return source; }

@Override
public Expression[] children() { return new Expression[]{ source() }; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@
* <li>Since version: 3.4.0</li>
* </ul>
* </li>
* <li>Name: <code>DATE_ADD</code>
* <ul>
* <li>SQL semantic: <code>DATE_ADD(start_date, num_days)</code></li>
* <li>Since version: 3.4.0</li>
* </ul>
* </li>
* <li>Name: <code>DATE_DIFF</code>
* <ul>
* <li>SQL semantic: <code>DATE_DIFF(end_date, start_date)</code></li>
* <li>Since version: 3.4.0</li>
* </ul>
* </li>
* <li>Name: <code>TRUNC</code>
* <ul>
* <li>SQL semantic: <code>TRUNC(date, format)</code></li>
* <li>Since version: 3.4.0</li>
* </ul>
* </li>
* </ol>
* Note: SQL semantic conforms ANSI standard, so some expressions are not supported when ANSI off,
* including: add, subtract, multiply, divide, remainder, pmod.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.spark.sql.connector.expressions.Cast;
import org.apache.spark.sql.connector.expressions.Expression;
import org.apache.spark.sql.connector.expressions.Extract;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.GeneralScalarExpression;
import org.apache.spark.sql.connector.expressions.Literal;
Expand All @@ -46,6 +47,9 @@ public String build(Expression expr) {
} else if (expr instanceof Cast) {
Cast cast = (Cast) expr;
return visitCast(build(cast.expression()), cast.dataType());
} else if (expr instanceof Extract) {
Extract extract = (Extract) expr;
return visitExtract(extract.field(), build(extract.source()));
} else if (expr instanceof GeneralScalarExpression) {
GeneralScalarExpression e = (GeneralScalarExpression) expr;
String name = e.name();
Expand Down Expand Up @@ -136,6 +140,9 @@ public String build(Expression expr) {
case "UPPER":
case "LOWER":
case "TRANSLATE":
case "DATE_ADD":
case "DATE_DIFF":
case "TRUNC":
return visitSQLFunction(name,
Arrays.stream(e.children()).map(c -> build(c)).toArray(String[]::new));
case "CASE_WHEN": {
Expand Down Expand Up @@ -327,4 +334,8 @@ protected String visitTrim(String direction, String[] inputs) {
return "TRIM(" + direction + " " + inputs[1] + " FROM " + inputs[0] + ")";
}
}

protected String visitExtract(String field, String source) {
return "EXTRACT(" + field + " FROM " + source + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.spark.sql.catalyst.util

import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.connector.expressions.{Cast => V2Cast, Expression => V2Expression, FieldReference, GeneralScalarExpression, LiteralValue, UserDefinedScalarFunc}
import org.apache.spark.sql.connector.expressions.{Cast => V2Cast, Expression => V2Expression, Extract => V2Extract, FieldReference, GeneralScalarExpression, LiteralValue, UserDefinedScalarFunc}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysTrue, And => V2And, Not => V2Not, Or => V2Or, Predicate => V2Predicate}
import org.apache.spark.sql.types.BooleanType
import org.apache.spark.sql.types.{BooleanType, IntegerType}

/**
* The builder to generate V2 expressions from catalyst expressions.
Expand Down Expand Up @@ -344,6 +344,59 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) {
} else {
None
}
case date: DateAdd =>
val childrenExpressions = date.children.flatMap(generateExpression(_))
if (childrenExpressions.length == date.children.length) {
Some(new GeneralScalarExpression("DATE_ADD", childrenExpressions.toArray[V2Expression]))
} else {
None
}
case date: DateDiff =>
val childrenExpressions = date.children.flatMap(generateExpression(_))
if (childrenExpressions.length == date.children.length) {
Some(new GeneralScalarExpression("DATE_DIFF", childrenExpressions.toArray[V2Expression]))
} else {
None
}
case date: TruncDate =>
val childrenExpressions = date.children.flatMap(generateExpression(_))
if (childrenExpressions.length == date.children.length) {
Some(new GeneralScalarExpression("TRUNC", childrenExpressions.toArray[V2Expression]))
} else {
None
}
case Second(child, _) =>
generateExpression(child).map(v => new V2Extract("SECOND", v))
case Minute(child, _) =>
generateExpression(child).map(v => new V2Extract("MINUTE", v))
case Hour(child, _) =>
generateExpression(child).map(v => new V2Extract("HOUR", v))
case Month(child) =>
generateExpression(child).map(v => new V2Extract("MONTH", v))
case Quarter(child) =>
generateExpression(child).map(v => new V2Extract("QUARTER", v))
case Year(child) =>
generateExpression(child).map(v => new V2Extract("YEAR", v))
// DayOfWeek uses Sunday = 1, Monday = 2, ... and ISO standard is Monday = 1, ...,
// so we use the formula ((ISO_standard % 7) + 1) to do translation.
case DayOfWeek(child) =>
generateExpression(child).map(v => new GeneralScalarExpression("+",
Array[V2Expression](new GeneralScalarExpression("%",
Array[V2Expression](new V2Extract("DAY_OF_WEEK", v), LiteralValue(7, IntegerType))),
LiteralValue(1, IntegerType))))
// WeekDay uses Monday = 0, Tuesday = 1, ... and ISO standard is Monday = 1, ...,
// so we use the formula (ISO_standard - 1) to do translation.
case WeekDay(child) =>
generateExpression(child).map(v => new GeneralScalarExpression("-",
Array[V2Expression](new V2Extract("DAY_OF_WEEK", v), LiteralValue(1, IntegerType))))
case DayOfMonth(child) =>
generateExpression(child).map(v => new V2Extract("DAY", v))
case DayOfYear(child) =>
generateExpression(child).map(v => new V2Extract("DAY_OF_YEAR", v))
case WeekOfYear(child) =>
generateExpression(child).map(v => new V2Extract("WEEK", v))
case YearOfWeek(child) =>
generateExpression(child).map(v => new V2Extract("YEAR_OF_WEEK", v))
// TODO supports other expressions
case ApplyFunctionExpression(function, children) =>
val childrenExpressions = children.flatMap(generateExpression(_))
Expand Down
26 changes: 26 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/jdbc/H2Dialect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import java.util.Locale
import java.util.concurrent.ConcurrentHashMap

import scala.collection.JavaConverters._
import scala.util.control.NonFatal

import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.analysis.{NoSuchNamespaceException, NoSuchTableException, TableAlreadyExistsException}
import org.apache.spark.sql.connector.catalog.functions.UnboundFunction
import org.apache.spark.sql.connector.expressions.Expression
import org.apache.spark.sql.connector.expressions.aggregate.{AggregateFunc, GeneralAggregateFunc}
import org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils
import org.apache.spark.sql.types.{BooleanType, ByteType, DataType, DecimalType, ShortType, StringType}
Expand Down Expand Up @@ -123,4 +125,28 @@ private[sql] object H2Dialect extends JdbcDialect {
}
super.classifyException(message, e)
}

override def compileExpression(expr: Expression): Option[String] = {
val jdbcSQLBuilder = new H2JDBCSQLBuilder()
try {
Some(jdbcSQLBuilder.build(expr))
} catch {
case NonFatal(e) =>
logWarning("Error occurs while compiling V2 expression", e)
None
}
}

class H2JDBCSQLBuilder extends JDBCSQLBuilder {

override def visitExtract(field: String, source: String): String = {
val newField = field match {
case "DAY_OF_WEEK" => "ISO_DAY_OF_WEEK"
case "WEEK" => "ISO_WEEK"
case "YEAR_OF_WEEK" => "ISO_WEEK_YEAR"
case _ => field
}
s"EXTRACT($newField FROM $source)"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel
"(1, 'bottle', 11111111111111111111.123)").executeUpdate()
conn.prepareStatement("INSERT INTO \"test\".\"item\" VALUES " +
"(1, 'bottle', 99999999999999999999.123)").executeUpdate()

conn.prepareStatement(
"CREATE TABLE \"test\".\"datetime\" (name TEXT(32), date1 DATE, time1 TIMESTAMP)")
.executeUpdate()
conn.prepareStatement("INSERT INTO \"test\".\"datetime\" VALUES " +
"('amy', '2022-05-19', '2022-05-19 00:00:00')").executeUpdate()
conn.prepareStatement("INSERT INTO \"test\".\"datetime\" VALUES " +
"('alex', '2022-05-18', '2022-05-18 00:00:00')").executeUpdate()
}
H2Dialect.registerFunction("my_avg", IntegralAverage)
H2Dialect.registerFunction("my_strlen", StrLen(CharLength))
Expand Down Expand Up @@ -1026,18 +1034,97 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel
|AND cast(dept as short) > 1
|AND cast(bonus as decimal(20, 2)) > 1200""".stripMargin)
checkFiltersRemoved(df6, ansiMode)
val expectedPlanFragment8 = if (ansiMode) {
val expectedPlanFragment6 = if (ansiMode) {
"PushedFilters: [BONUS IS NOT NULL, DEPT IS NOT NULL, " +
"CAST(BONUS AS string) LIKE '%30%', CAST(DEPT AS byte) > 1, ...,"
} else {
"PushedFilters: [BONUS IS NOT NULL, DEPT IS NOT NULL],"
}
checkPushedInfo(df6, expectedPlanFragment8)
checkPushedInfo(df6, expectedPlanFragment6)
checkAnswer(df6, Seq(Row(2, "david", 10000, 1300, true)))
}
}
}

test("scan with filter push-down with date time functions") {
Seq(false, true).foreach { ansiMode =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiMode.toString,
SQLConf.MAX_METADATA_STRING_LENGTH.key -> "200") {

val df1 = sql("SELECT name FROM h2.test.datetime WHERE " +
"dayofyear(date1) > 100 AND dayofmonth(date1) > 10 ")
checkFiltersRemoved(df1)
val expectedPlanFragment1 =
"PushedFilters: [DATE1 IS NOT NULL, EXTRACT(DAY_OF_YEAR FROM DATE1) > 100, " +
"EXTRACT(DAY FROM DATE1) > 10]"
checkPushedInfo(df1, expectedPlanFragment1)
checkAnswer(df1, Seq(Row("amy"), Row("alex")))

val df2 = sql("SELECT name FROM h2.test.datetime WHERE " +
"year(date1) = 2022 AND quarter(date1) = 2")
checkFiltersRemoved(df2)
val expectedPlanFragment2 =
"[DATE1 IS NOT NULL, EXTRACT(YEAR FROM DATE1) = 2022, " +
"EXTRACT(QUARTER FROM DATE1) = 2]"
checkPushedInfo(df2, expectedPlanFragment2)
checkAnswer(df2, Seq(Row("amy"), Row("alex")))

val df3 = sql("SELECT name FROM h2.test.datetime WHERE " +
"second(time1) = 0 AND month(date1) = 5")
checkFiltersRemoved(df3)
val expectedPlanFragment3 =
"PushedFilters: [TIME1 IS NOT NULL, DATE1 IS NOT NULL, " +
"EXTRACT(SECOND FROM TIME1) = 0, EXTRACT(MONTH FROM DATE1) = 5]"
checkPushedInfo(df3, expectedPlanFragment3)
checkAnswer(df3, Seq(Row("amy"), Row("alex")))

val df4 = sql("SELECT name FROM h2.test.datetime WHERE " +
"hour(time1) = 0 AND minute(time1) = 0")
checkFiltersRemoved(df4)
val expectedPlanFragment4 =
"PushedFilters: [TIME1 IS NOT NULL, EXTRACT(HOUR FROM TIME1) = 0, " +
"EXTRACT(MINUTE FROM TIME1) = 0]"
checkPushedInfo(df4, expectedPlanFragment4)
checkAnswer(df4, Seq(Row("amy"), Row("alex")))

val df5 = sql("SELECT name FROM h2.test.datetime WHERE " +
"extract(WEEk from date1) > 10 AND extract(YEAROFWEEK from date1) = 2022")
checkFiltersRemoved(df5)
val expectedPlanFragment5 =
"PushedFilters: [DATE1 IS NOT NULL, EXTRACT(WEEK FROM DATE1) > 10, " +
"EXTRACT(YEAR_OF_WEEK FROM DATE1) = 2022]"
checkPushedInfo(df5, expectedPlanFragment5)
checkAnswer(df5, Seq(Row("alex"), Row("amy")))

// H2 does not support
val df6 = sql("SELECT name FROM h2.test.datetime WHERE " +
"trunc(date1, 'week') = date'2022-05-16' AND date_add(date1, 1) = date'2022-05-20' " +
"AND datediff(date1, '2022-05-10') > 0")
checkFiltersRemoved(df6, false)
val expectedPlanFragment6 =
"PushedFilters: [DATE1 IS NOT NULL]"
checkPushedInfo(df6, expectedPlanFragment6)
checkAnswer(df6, Seq(Row("amy")))

val df7 = sql("SELECT name FROM h2.test.datetime WHERE " +
"weekday(date1) = 2")
checkFiltersRemoved(df7)
val expectedPlanFragment7 =
"PushedFilters: [DATE1 IS NOT NULL, (EXTRACT(DAY_OF_WEEK FROM DATE1) - 1) = 2]"
checkPushedInfo(df7, expectedPlanFragment7)
checkAnswer(df7, Seq(Row("alex")))

val df8 = sql("SELECT name FROM h2.test.datetime WHERE " +
"dayofweek(date1) = 4")
checkFiltersRemoved(df8)
val expectedPlanFragment8 =
"PushedFilters: [DATE1 IS NOT NULL, ((EXTRACT(DAY_OF_WEEK FROM DATE1) % 7) + 1) = 4]"
checkPushedInfo(df8, expectedPlanFragment8)
checkAnswer(df8, Seq(Row("alex")))
}
}
}

test("scan with filter push-down with UDF") {
JdbcDialects.unregisterDialect(H2Dialect)
try {
Expand Down Expand Up @@ -1116,7 +1203,8 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel
checkAnswer(sql("SHOW TABLES IN h2.test"),
Seq(Row("test", "people", false), Row("test", "empty_table", false),
Row("test", "employee", false), Row("test", "item", false), Row("test", "dept", false),
Row("test", "person", false), Row("test", "view1", false), Row("test", "view2", false)))
Row("test", "person", false), Row("test", "view1", false), Row("test", "view2", false),
Row("test", "datetime", false)))
}

test("SQL API: create table as select") {
Expand Down