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-38899][SQL]DS V2 supports push down datetime functions #36663

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
*/

@Evolving
public class Extract implements Expression, Serializable {

private String field;
private Expression source;

chenzhx marked this conversation as resolved.
Show resolved Hide resolved
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 1 = Sunday, 2 = Monday, ... and ISO standard is Monday=1, ...,
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
// so we use the formula ((ISO_standard % 7) + 1) to do translation.
case DayOfWeek(child) =>
generateExpression(child).map(v => new GeneralScalarExpression("+",
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
Array[V2Expression](new GeneralScalarExpression("%",
Array[V2Expression](new V2Extract("DAY_OF_WEEK", v), LiteralValue(7, IntegerType))),
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
LiteralValue(1, IntegerType))))
// WeekDay uses 0 = Monday, 1 = Tuesday, ... and ISO standard is Monday=1, ...,
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
// so we use the formula (ISO_standard - 1) to do translation.
case WeekDay(child) =>
generateExpression(child).map(v => new GeneralScalarExpression("-",
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
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))
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
// TODO supports other expressions
case ApplyFunctionExpression(function, children) =>
val childrenExpressions = children.flatMap(generateExpression(_))
Expand Down
25 changes: 25 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,27 @@ 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 = {
field match {
case "DAY_OF_WEEK" => s"EXTRACT(ISO_DAY_OF_WEEK FROM $source)"
case "WEEK" => s"EXTRACT(ISO_WEEK FROM $source)"
case "YEAR_OF_WEEK" => s"EXTRACT(ISO_WEEK_YEAR FROM $source)"
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
case _ => super.visitExtract(field, 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,14 +1034,85 @@ 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)))

val df7 = sql("SELECT name FROM h2.test.datetime WHERE " +
"dayofyear(date1) > 100 AND dayofmonth(date1) > 10 ")
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
checkFiltersRemoved(df7)
val expectedPlanFragment7 =
"PushedFilters: [DATE1 IS NOT NULL, EXTRACT(DAY_OF_YEAR FROM DATE1) > 100, " +
"EXTRACT(DAY FROM DATE1) > 10]"
checkPushedInfo(df7, expectedPlanFragment7)
checkAnswer(df7, Seq(Row("amy"), Row("alex")))

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

val df9 = sql("SELECT name FROM h2.test.datetime WHERE " +
"second(time1) = 0 AND month(date1) = 5")
checkFiltersRemoved(df9)
val expectedPlanFragment9 =
"PushedFilters: [TIME1 IS NOT NULL, DATE1 IS NOT NULL, EXTRACT(SECOND FROM TIME1) = 0, " +
"EXTRACT(MONTH FROM DATE1) ..."
chenzhx marked this conversation as resolved.
Show resolved Hide resolved
checkPushedInfo(df9, expectedPlanFragment9)
checkAnswer(df9, Seq(Row("amy"), Row("alex")))

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

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

// H2 does not support
val df12 = 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(df12, false)
val expectedPlanFragment12 =
"PushedFilters: [DATE1 IS NOT NULL]"
checkPushedInfo(df12, expectedPlanFragment12)
checkAnswer(df12, Seq(Row("amy")))

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

val df14 = sql("SELECT name FROM h2.test.datetime WHERE " +
"dayofweek(date1) = 4")
checkFiltersRemoved(df14)
val expectedPlanFragment14 =
"PushedFilters: [DATE1 IS NOT NULL, ((EXTRACT(DAY_OF_WEEK FROM DATE1) % 7) + 1) = 4]"
checkPushedInfo(df14, expectedPlanFragment14)
checkAnswer(df14, Seq(Row("alex")))
}
}
}
Expand Down Expand Up @@ -1116,7 +1195,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