Skip to content

[SPARK-16199][SQL] Add a method to list the referenced columns in data source Filter #13901

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

Closed
wants to merge 1 commit into from
Closed
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
71 changes: 56 additions & 15 deletions sql/core/src/main/scala/org/apache/spark/sql/sources/filters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,28 @@ package org.apache.spark.sql.sources
*
* @since 1.3.0
*/
abstract class Filter
abstract class Filter {
/**
* List of columns that are referenced by this filter.
* @since 2.1.0
*/
def references: Array[String]

protected def findReferences(value: Any): Array[String] = value match {
case f: Filter => f.references
case _ => Array.empty
}
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a value
* equal to `value`.
*
* @since 1.3.0
*/
case class EqualTo(attribute: String, value: Any) extends Filter
case class EqualTo(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* Performs equality comparison, similar to [[EqualTo]]. However, this differs from [[EqualTo]]
Expand All @@ -43,39 +56,49 @@ case class EqualTo(attribute: String, value: Any) extends Filter
*
* @since 1.5.0
*/
case class EqualNullSafe(attribute: String, value: Any) extends Filter
case class EqualNullSafe(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a value
* greater than `value`.
*
* @since 1.3.0
*/
case class GreaterThan(attribute: String, value: Any) extends Filter
case class GreaterThan(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a value
* greater than or equal to `value`.
*
* @since 1.3.0
*/
case class GreaterThanOrEqual(attribute: String, value: Any) extends Filter
case class GreaterThanOrEqual(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a value
* less than `value`.
*
* @since 1.3.0
*/
case class LessThan(attribute: String, value: Any) extends Filter
case class LessThan(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a value
* less than or equal to `value`.
*
* @since 1.3.0
*/
case class LessThanOrEqual(attribute: String, value: Any) extends Filter
case class LessThanOrEqual(attribute: String, value: Any) extends Filter {
override def references: Array[String] = Array(attribute) ++ findReferences(value)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to one of the values in the array.
Expand All @@ -99,63 +122,81 @@ case class In(attribute: String, values: Array[Any]) extends Filter {
override def toString: String = {
s"In($attribute, [${values.mkString(",")}]"
}

override def references: Array[String] = Array(attribute) ++ values.flatMap(findReferences)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to null.
*
* @since 1.3.0
*/
case class IsNull(attribute: String) extends Filter
case class IsNull(attribute: String) extends Filter {
override def references: Array[String] = Array(attribute)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to a non-null value.
*
* @since 1.3.0
*/
case class IsNotNull(attribute: String) extends Filter
case class IsNotNull(attribute: String) extends Filter {
override def references: Array[String] = Array(attribute)
}

/**
* A filter that evaluates to `true` iff both `left` or `right` evaluate to `true`.
*
* @since 1.3.0
*/
case class And(left: Filter, right: Filter) extends Filter
case class And(left: Filter, right: Filter) extends Filter {
override def references: Array[String] = left.references ++ right.references
}

/**
* A filter that evaluates to `true` iff at least one of `left` or `right` evaluates to `true`.
*
* @since 1.3.0
*/
case class Or(left: Filter, right: Filter) extends Filter
case class Or(left: Filter, right: Filter) extends Filter {
override def references: Array[String] = left.references ++ right.references
}

/**
* A filter that evaluates to `true` iff `child` is evaluated to `false`.
*
* @since 1.3.0
*/
case class Not(child: Filter) extends Filter
case class Not(child: Filter) extends Filter {
override def references: Array[String] = child.references
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to
* a string that starts with `value`.
*
* @since 1.3.1
*/
case class StringStartsWith(attribute: String, value: String) extends Filter
case class StringStartsWith(attribute: String, value: String) extends Filter {
override def references: Array[String] = Array(attribute)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to
* a string that starts with `value`.
*
* @since 1.3.1
*/
case class StringEndsWith(attribute: String, value: String) extends Filter
case class StringEndsWith(attribute: String, value: String) extends Filter {
override def references: Array[String] = Array(attribute)
}

/**
* A filter that evaluates to `true` iff the attribute evaluates to
* a string that contains the string `value`.
*
* @since 1.3.1
*/
case class StringContains(attribute: String, value: String) extends Filter
case class StringContains(attribute: String, value: String) extends Filter {
override def references: Array[String] = Array(attribute)
}