Skip to content

Commit a3cfef1

Browse files
committed
Update code
1 parent 0e135cd commit a3cfef1

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

sql/core/src/main/scala/org/apache/spark/sql/catalyst/util/V2ExpressionBuilder.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,14 @@ class V2ExpressionBuilder(e: Expression, isPredicate: Boolean = false) {
377377
generateExpression(child).map(v => new V2Extract("QUARTER", v))
378378
case Year(child) =>
379379
generateExpression(child).map(v => new V2Extract("YEAR", v))
380-
// DayOfWeek uses 1 = Sunday, 2 = Monday, ... and ISO standard is Monday=1, ...,
380+
// DayOfWeek uses Sunday = 1, Monday = 2, ... and ISO standard is Monday = 1, ...,
381381
// so we use the formula ((ISO_standard % 7) + 1) to do translation.
382382
case DayOfWeek(child) =>
383383
generateExpression(child).map(v => new GeneralScalarExpression("+",
384384
Array[V2Expression](new GeneralScalarExpression("%",
385385
Array[V2Expression](new V2Extract("DAY_OF_WEEK", v), LiteralValue(7, IntegerType))),
386386
LiteralValue(1, IntegerType))))
387-
// WeekDay uses 0 = Monday, 1 = Tuesday, ... and ISO standard is Monday=1, ...,
387+
// WeekDay uses Monday = 0, Tuesday = 1, ... and ISO standard is Monday = 1, ...,
388388
// so we use the formula (ISO_standard - 1) to do translation.
389389
case WeekDay(child) =>
390390
generateExpression(child).map(v => new GeneralScalarExpression("-",

sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCV2Suite.scala

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,77 +1042,85 @@ class JDBCV2Suite extends QueryTest with SharedSparkSession with ExplainSuiteHel
10421042
}
10431043
checkPushedInfo(df6, expectedPlanFragment6)
10441044
checkAnswer(df6, Seq(Row(2, "david", 10000, 1300, true)))
1045+
}
1046+
}
1047+
}
10451048

1046-
val df7 = sql("SELECT name FROM h2.test.datetime WHERE " +
1049+
test("scan with filter push-down with date time functions") {
1050+
Seq(false, true).foreach { ansiMode =>
1051+
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiMode.toString,
1052+
SQLConf.MAX_METADATA_STRING_LENGTH.key -> "200") {
1053+
1054+
val df1 = sql("SELECT name FROM h2.test.datetime WHERE " +
10471055
"dayofyear(date1) > 100 AND dayofmonth(date1) > 10 ")
1048-
checkFiltersRemoved(df7)
1049-
val expectedPlanFragment7 =
1056+
checkFiltersRemoved(df1)
1057+
val expectedPlanFragment1 =
10501058
"PushedFilters: [DATE1 IS NOT NULL, EXTRACT(DAY_OF_YEAR FROM DATE1) > 100, " +
1051-
"EXTRACT(DAY FROM DATE1) > 10]"
1052-
checkPushedInfo(df7, expectedPlanFragment7)
1053-
checkAnswer(df7, Seq(Row("amy"), Row("alex")))
1059+
"EXTRACT(DAY FROM DATE1) > 10]"
1060+
checkPushedInfo(df1, expectedPlanFragment1)
1061+
checkAnswer(df1, Seq(Row("amy"), Row("alex")))
10541062

1055-
val df8 = sql("SELECT name FROM h2.test.datetime WHERE " +
1063+
val df2 = sql("SELECT name FROM h2.test.datetime WHERE " +
10561064
"year(date1) = 2022 AND quarter(date1) = 2")
1057-
checkFiltersRemoved(df8)
1058-
val expectedPlanFragment8 =
1065+
checkFiltersRemoved(df2)
1066+
val expectedPlanFragment2 =
10591067
"[DATE1 IS NOT NULL, EXTRACT(YEAR FROM DATE1) = 2022, " +
1060-
"EXTRACT(QUARTER FROM DATE1) = 2]"
1061-
checkPushedInfo(df8, expectedPlanFragment8)
1062-
checkAnswer(df8, Seq(Row("amy"), Row("alex")))
1068+
"EXTRACT(QUARTER FROM DATE1) = 2]"
1069+
checkPushedInfo(df2, expectedPlanFragment2)
1070+
checkAnswer(df2, Seq(Row("amy"), Row("alex")))
10631071

1064-
val df9 = sql("SELECT name FROM h2.test.datetime WHERE " +
1072+
val df3 = sql("SELECT name FROM h2.test.datetime WHERE " +
10651073
"second(time1) = 0 AND month(date1) = 5")
1066-
checkFiltersRemoved(df9)
1067-
val expectedPlanFragment9 =
1068-
"PushedFilters: [TIME1 IS NOT NULL, DATE1 IS NOT NULL, EXTRACT(SECOND FROM TIME1) = 0, " +
1069-
"EXTRACT(MONTH FROM DATE1) ..."
1070-
checkPushedInfo(df9, expectedPlanFragment9)
1071-
checkAnswer(df9, Seq(Row("amy"), Row("alex")))
1072-
1073-
val df10 = sql("SELECT name FROM h2.test.datetime WHERE " +
1074+
checkFiltersRemoved(df3)
1075+
val expectedPlanFragment3 =
1076+
"PushedFilters: [TIME1 IS NOT NULL, DATE1 IS NOT NULL, " +
1077+
"EXTRACT(SECOND FROM TIME1) = 0, EXTRACT(MONTH FROM DATE1) = 5]"
1078+
checkPushedInfo(df3, expectedPlanFragment3)
1079+
checkAnswer(df3, Seq(Row("amy"), Row("alex")))
1080+
1081+
val df4 = sql("SELECT name FROM h2.test.datetime WHERE " +
10741082
"hour(time1) = 0 AND minute(time1) = 0")
1075-
checkFiltersRemoved(df10)
1076-
val expectedPlanFragment10 =
1083+
checkFiltersRemoved(df4)
1084+
val expectedPlanFragment4 =
10771085
"PushedFilters: [TIME1 IS NOT NULL, EXTRACT(HOUR FROM TIME1) = 0, " +
10781086
"EXTRACT(MINUTE FROM TIME1) = 0]"
1079-
checkPushedInfo(df10, expectedPlanFragment10)
1080-
checkAnswer(df10, Seq(Row("amy"), Row("alex")))
1087+
checkPushedInfo(df4, expectedPlanFragment4)
1088+
checkAnswer(df4, Seq(Row("amy"), Row("alex")))
10811089

1082-
val df11 = sql("SELECT name FROM h2.test.datetime WHERE " +
1090+
val df5 = sql("SELECT name FROM h2.test.datetime WHERE " +
10831091
"extract(WEEk from date1) > 10 AND extract(YEAROFWEEK from date1) = 2022")
1084-
checkFiltersRemoved(df11)
1085-
val expectedPlanFragment11 =
1092+
checkFiltersRemoved(df5)
1093+
val expectedPlanFragment5 =
10861094
"PushedFilters: [DATE1 IS NOT NULL, EXTRACT(WEEK FROM DATE1) > 10, " +
10871095
"EXTRACT(YEAR_OF_WEEK FROM DATE1) = 2022]"
1088-
checkPushedInfo(df11, expectedPlanFragment11)
1089-
checkAnswer(df11, Seq(Row("alex"), Row("amy")))
1096+
checkPushedInfo(df5, expectedPlanFragment5)
1097+
checkAnswer(df5, Seq(Row("alex"), Row("amy")))
10901098

10911099
// H2 does not support
1092-
val df12 = sql("SELECT name FROM h2.test.datetime WHERE " +
1100+
val df6 = sql("SELECT name FROM h2.test.datetime WHERE " +
10931101
"trunc(date1, 'week') = date'2022-05-16' AND date_add(date1, 1) = date'2022-05-20' " +
10941102
"AND datediff(date1, '2022-05-10') > 0")
1095-
checkFiltersRemoved(df12, false)
1096-
val expectedPlanFragment12 =
1103+
checkFiltersRemoved(df6, false)
1104+
val expectedPlanFragment6 =
10971105
"PushedFilters: [DATE1 IS NOT NULL]"
1098-
checkPushedInfo(df12, expectedPlanFragment12)
1099-
checkAnswer(df12, Seq(Row("amy")))
1106+
checkPushedInfo(df6, expectedPlanFragment6)
1107+
checkAnswer(df6, Seq(Row("amy")))
11001108

1101-
val df13 = sql("SELECT name FROM h2.test.datetime WHERE " +
1109+
val df7 = sql("SELECT name FROM h2.test.datetime WHERE " +
11021110
"weekday(date1) = 2")
1103-
checkFiltersRemoved(df13)
1104-
val expectedPlanFragment13 =
1111+
checkFiltersRemoved(df7)
1112+
val expectedPlanFragment7 =
11051113
"PushedFilters: [DATE1 IS NOT NULL, (EXTRACT(DAY_OF_WEEK FROM DATE1) - 1) = 2]"
1106-
checkPushedInfo(df13, expectedPlanFragment13)
1107-
checkAnswer(df13, Seq(Row("alex")))
1114+
checkPushedInfo(df7, expectedPlanFragment7)
1115+
checkAnswer(df7, Seq(Row("alex")))
11081116

1109-
val df14 = sql("SELECT name FROM h2.test.datetime WHERE " +
1117+
val df8 = sql("SELECT name FROM h2.test.datetime WHERE " +
11101118
"dayofweek(date1) = 4")
1111-
checkFiltersRemoved(df14)
1112-
val expectedPlanFragment14 =
1119+
checkFiltersRemoved(df8)
1120+
val expectedPlanFragment8 =
11131121
"PushedFilters: [DATE1 IS NOT NULL, ((EXTRACT(DAY_OF_WEEK FROM DATE1) % 7) + 1) = 4]"
1114-
checkPushedInfo(df14, expectedPlanFragment14)
1115-
checkAnswer(df14, Seq(Row("alex")))
1122+
checkPushedInfo(df8, expectedPlanFragment8)
1123+
checkAnswer(df8, Seq(Row("alex")))
11161124
}
11171125
}
11181126
}

0 commit comments

Comments
 (0)