Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -160,6 +160,16 @@ private[sql] object AnyTimestampType extends AbstractDataType with Serializable
override private[sql] def simpleString = "(timestamp or timestamp without time zone)"
}

private[sql] object AnyTimestampNanoType extends AbstractDataType with Serializable {
override private[sql] def defaultConcreteType: DataType = TimestampNTZNanosType()

override private[sql] def acceptsType(other: DataType): Boolean =
other.isInstanceOf[TimestampLTZNanosType] || other.isInstanceOf[TimestampNTZNanosType]

override private[sql] def simpleString =
"(timestamp_ltz(p) or timestamp_ntz(p) with p in [7, 9])"
}

private[sql] abstract class DatetimeType extends AtomicType

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
package org.apache.spark.sql.catalyst.analysis

import org.apache.spark.sql.catalyst.expressions.{Cast, Expression, GetDateField}
import org.apache.spark.sql.types.{AnyTimestampTypeExpression, DateType}
import org.apache.spark.sql.types.{AnyTimestampNanoTypeExpression, AnyTimestampTypeExpression, DateType}

/**
* ANSI type coercion helper that matches against [[GetDateField]] expressions in order to type
* coerce children to [[DateType]], if necessary.
*/
object AnsiGetDateFieldOperationsTypeCoercion {
def apply(expression: Expression): Expression = expression match {
case g: GetDateField if AnyTimestampTypeExpression.unapply(g.child) =>
case g: GetDateField
if AnyTimestampTypeExpression.unapply(g.child) ||
AnyTimestampNanoTypeExpression.unapply(g.child) =>
g.withNewChildren(Seq(Cast(g.child, DateType)))

case other => other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,15 +544,21 @@ trait GetDateField extends UnaryExpression with ImplicitCastInputTypes {
}
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the day of year of the date/timestamp.",
examples = """
Examples:
> SELECT _FUNC_('2016-04-09');
100
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2016-04-09 12:00:00.123456789');
100
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.contains.tab
case class DayOfYear(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getDayInYear
override val funcName = "getDayInYear"
Expand Down Expand Up @@ -848,15 +854,21 @@ case class UnixMicros(child: Expression) extends TimestampToLongBase {
copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the year component of the date/timestamp.",
examples = """
Examples:
> SELECT _FUNC_('2016-07-30');
2016
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2016-07-30 12:00:00.123456789');
2016
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.contains.tab
case class Year(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getYear
override val funcName = "getYear"
Expand All @@ -871,132 +883,174 @@ case class YearOfWeek(child: Expression) extends GetDateField {
copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the quarter of the year for date, in the range 1 to 4.",
examples = """
Examples:
> SELECT _FUNC_('2016-08-31');
3
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2016-08-31 12:00:00.123456789');
3
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.contains.tab
case class Quarter(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getQuarter
override val funcName = "getQuarter"
override protected def withNewChildInternal(newChild: Expression): Quarter =
copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the month component of the date/timestamp.",
examples = """
Examples:
> SELECT _FUNC_('2016-07-30');
7
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2016-07-30 12:00:00.123456789');
7
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.contains.tab
case class Month(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getMonth
override val funcName = "getMonth"
override protected def withNewChildInternal(newChild: Expression): Month = copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the day of month of the date/timestamp.",
examples = """
Examples:
> SELECT _FUNC_('2009-07-30');
30
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2009-07-30 12:00:00.123456789');
30
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.contains.tab
case class DayOfMonth(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getDayOfMonth
override val funcName = "getDayOfMonth"
override protected def withNewChildInternal(newChild: Expression): DayOfMonth =
copy(child = newChild)
}

// scalastyle:off line.size.limit
// scalastyle:off line.size.limit line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the day of the week for date/timestamp (1 = Sunday, 2 = Monday, ..., 7 = Saturday).",
examples = """
Examples:
> SELECT _FUNC_('2009-07-30');
5
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2009-07-30 12:00:00.123456789');
5
""",
group = "datetime_funcs",
since = "2.3.0")
// scalastyle:on line.size.limit
// scalastyle:on line.size.limit line.contains.tab
case class DayOfWeek(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getDayOfWeek
override val funcName = "getDayOfWeek"
override protected def withNewChildInternal(newChild: Expression): DayOfWeek =
copy(child = newChild)
}

// scalastyle:off line.size.limit
// scalastyle:off line.size.limit line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the day of the week for date/timestamp (0 = Monday, 1 = Tuesday, ..., 6 = Sunday).",
examples = """
Examples:
> SELECT _FUNC_('2009-07-30');
3
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2009-07-30 12:00:00.123456789');
3
""",
group = "datetime_funcs",
since = "2.4.0")
// scalastyle:on line.size.limit
// scalastyle:on line.size.limit line.contains.tab
case class WeekDay(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getWeekDay
override val funcName = "getWeekDay"
override protected def withNewChildInternal(newChild: Expression): WeekDay =
copy(child = newChild)
}

// scalastyle:off line.size.limit
// scalastyle:off line.size.limit line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the week of the year of the given date. A week is considered to start on a Monday and week 1 is the first week with >3 days.",
examples = """
Examples:
> SELECT _FUNC_('2008-02-20');
8
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2008-02-20 12:00:00.123456789');
8
""",
group = "datetime_funcs",
since = "1.5.0")
// scalastyle:on line.size.limit
// scalastyle:on line.size.limit line.contains.tab
case class WeekOfYear(child: Expression) extends GetDateField {
override val func = DateTimeUtils.getWeekOfYear
override val funcName = "getWeekOfYear"
override protected def withNewChildInternal(newChild: Expression): WeekOfYear =
copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the three-letter abbreviated month name from the given date.",
examples = """
Examples:
> SELECT _FUNC_('2008-02-20');
Feb
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2008-02-20 12:00:00.123456789');
Feb
""",
group = "datetime_funcs",
since = "4.0.0")
// scalastyle:on line.contains.tab
case class MonthName(child: Expression) extends GetDateField with DefaultStringProducingExpression {
override val func = DateTimeUtils.getMonthName
override val funcName = "getMonthName"
override protected def withNewChildInternal(newChild: Expression): MonthName =
copy(child = newChild)
}

// scalastyle:off line.contains.tab
@ExpressionDescription(
usage = "_FUNC_(date) - Returns the three-letter abbreviated day name from the given date.",
examples = """
Examples:
> SELECT _FUNC_(DATE('2008-02-20'));
Wed
> SET spark.sql.timestampNanosTypes.enabled=true;
spark.sql.timestampNanosTypes.enabled true
> SELECT _FUNC_(TIMESTAMP_NTZ '2008-02-20 12:00:00.123456789');
Wed
""",
group = "datetime_funcs",
since = "4.0.0")
// scalastyle:on line.contains.tab
case class DayName(child: Expression) extends GetDateField with DefaultStringProducingExpression {
override val func = DateTimeUtils.getDayName
override val funcName = "getDayName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ private[sql] object AnyTimestampTypeExpression {
e.dataType.isInstanceOf[TimestampType] || e.dataType.isInstanceOf[TimestampNTZType]
}

private[sql] object AnyTimestampNanoTypeExpression {
def unapply(e: Expression): Boolean =
e.dataType.isInstanceOf[TimestampLTZNanosType] || e.dataType.isInstanceOf[TimestampNTZNanosType]
}

private[sql] object DecimalExpression {
def unapply(e: Expression): Option[(Int, Int)] = e.dataType match {
case t: DecimalType => Some((t.precision, t.scale))
Expand Down
Loading