Skip to content
Closed
Show file tree
Hide file tree
Changes from 16 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,41 @@
/*
* 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 java.io.Serializable;

import org.apache.spark.annotation.Evolving;

/**
* The general SQL string corresponding to expression.
*
* @since 3.3.0
*/
@Evolving
public class GeneralSQLExpression implements Expression, Serializable {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also come up with another name instead of GeneralSQLExpression (e.g., PushedExpression)

private String sql;

public GeneralSQLExpression(String sql) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, shouldn't we keep the expression tree here, and let the JDBC source side to translate into SQL string? I think we can't assume that all downstream source implementation can understand SQL expression in string

@beliefer beliefer Jan 30, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The V2 expression holds the V1 expression, which looks strange.
cc @cloud-fan

this.sql = sql;
}

public String sql() { return sql; }

@Override
public String toString() { return sql; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.connector.expressions.aggregate;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* An aggregate function that returns the mean of all the values in a group.
Expand All @@ -27,23 +27,23 @@
*/
@Evolving
public final class Avg implements AggregateFunc {
private final NamedReference column;
private final Expression input;
private final boolean isDistinct;

public Avg(NamedReference column, boolean isDistinct) {
this.column = column;
public Avg(Expression column, boolean isDistinct) {
this.input = column;
this.isDistinct = isDistinct;
}

public NamedReference column() { return column; }
public Expression column() { return input; }
public boolean isDistinct() { return isDistinct; }

@Override
public String toString() {
if (isDistinct) {
return "AVG(DISTINCT " + column.describe() + ")";
return "AVG(DISTINCT " + input.describe() + ")";
} else {
return "AVG(" + column.describe() + ")";
return "AVG(" + input.describe() + ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.connector.expressions.aggregate;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* An aggregate function that returns the number of the specific row in a group.
Expand All @@ -27,23 +27,23 @@
*/
@Evolving
public final class Count implements AggregateFunc {
private final NamedReference column;
private final Expression input;
private final boolean isDistinct;

public Count(NamedReference column, boolean isDistinct) {
this.column = column;
public Count(Expression column, boolean isDistinct) {
this.input = column;
this.isDistinct = isDistinct;
}

public NamedReference column() { return column; }
public Expression column() { return input; }
public boolean isDistinct() { return isDistinct; }

@Override
public String toString() {
if (isDistinct) {
return "COUNT(DISTINCT " + column.describe() + ")";
return "COUNT(DISTINCT " + input.describe() + ")";
} else {
return "COUNT(" + column.describe() + ")";
return "COUNT(" + input.describe() + ")";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.connector.expressions.aggregate;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* An aggregate function that returns the maximum value in a group.
Expand All @@ -27,12 +27,12 @@
*/
@Evolving
public final class Max implements AggregateFunc {
private final NamedReference column;
private final Expression input;

public Max(NamedReference column) { this.column = column; }
public Max(Expression column) { this.input = column; }

public NamedReference column() { return column; }
public Expression column() { return input; }

@Override
public String toString() { return "MAX(" + column.describe() + ")"; }
public String toString() { return "MAX(" + input.describe() + ")"; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.connector.expressions.aggregate;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* An aggregate function that returns the minimum value in a group.
Expand All @@ -27,12 +27,12 @@
*/
@Evolving
public final class Min implements AggregateFunc {
private final NamedReference column;
private final Expression input;

public Min(NamedReference column) { this.column = column; }
public Min(Expression column) { this.input = column; }

public NamedReference column() { return column; }
public Expression column() { return input; }

@Override
public String toString() { return "MIN(" + column.describe() + ")"; }
public String toString() { return "MIN(" + input.describe() + ")"; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.connector.expressions.aggregate;

import org.apache.spark.annotation.Evolving;
import org.apache.spark.sql.connector.expressions.NamedReference;
import org.apache.spark.sql.connector.expressions.Expression;

/**
* An aggregate function that returns the summation of all the values in a group.
Expand All @@ -27,23 +27,23 @@
*/
@Evolving
public final class Sum implements AggregateFunc {
private final NamedReference column;
private final Expression input;
private final boolean isDistinct;

public Sum(NamedReference column, boolean isDistinct) {
this.column = column;
public Sum(Expression column, boolean isDistinct) {
this.input = column;
this.isDistinct = isDistinct;
}

public NamedReference column() { return column; }
public Expression column() { return input; }
public boolean isDistinct() { return isDistinct; }

@Override
public String toString() {
if (isDistinct) {
return "SUM(DISTINCT " + column.describe() + ")";
return "SUM(DISTINCT " + input.describe() + ")";
} else {
return "SUM(" + column.describe() + ")";
return "SUM(" + input.describe() + ")";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.catalyst.util

import org.apache.spark.sql.catalyst.expressions.{Attribute, BinaryOperator, CaseWhen, EqualTo, Expression, IsNotNull, IsNull, Literal, Not}
import org.apache.spark.sql.connector.expressions.LiteralValue

/**
* The builder to generate SQL string from catalyst expressions.
*/
class ExpressionSQLBuilder(e: Expression) {

def build(): Option[String] = generateSQL(e)

private def generateSQL(expr: Expression): Option[String] = expr match {
case Literal(value, dataType) => Some(LiteralValue(value, dataType).toString)
case a: Attribute => Some(quoteIfNeeded(a.name))
case IsNull(col) => generateSQL(col).map(c => s"$c IS NULL")
case IsNotNull(col) => generateSQL(col).map(c => s"$c IS NOT NULL")
case b: BinaryOperator =>
val l = generateSQL(b.left)
val r = generateSQL(b.right)
if (l.isDefined && r.isDefined) {
Some(s"(${l.get}) ${b.sqlOperator} (${r.get})")
} else {
None
}
case Not(EqualTo(left, right)) =>
Comment thread
cloud-fan marked this conversation as resolved.
val l = generateSQL(left)
val r = generateSQL(right)
if (l.isDefined && r.isDefined) {
Some(s"${l.get} != ${r.get}")
} else {
None
}
case Not(child) => generateSQL(child).map(v => s"NOT $v")
Comment thread
cloud-fan marked this conversation as resolved.
Outdated
Comment thread
beliefer marked this conversation as resolved.
Outdated
case CaseWhen(branches, elseValue) =>
val conditionsSQL = branches.map(_._1).flatMap(generateSQL)
val valuesSQL = branches.map(_._2).flatMap(generateSQL)
if (conditionsSQL.length == branches.length && valuesSQL.length == branches.length) {
val branchSQL =
conditionsSQL.zip(valuesSQL).map { case (c, v) => s" WHEN $c THEN $v" }.mkString
val elseSQL = elseValue.flatMap(generateSQL).map(v => s" ELSE $v").getOrElse("")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to return None if we can't generate SQL for the else value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CASE whenClause+ (ELSE elseExpression=expression)? END

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it.

Some(s"CASE$branchSQL$elseSQL END")
} else {
None
}
// TODO supports other expressions
case _ => None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ object AggregatePushDownUtils {

def processMinOrMax(agg: AggregateFunc): Boolean = {
val (column, aggType) = agg match {
case max: Max => (max.column, "max")
case min: Min => (min.column, "min")
case _ =>
throw new IllegalArgumentException(s"Unexpected type of AggregateFunc ${agg.describe}")
case max: Max if max.column().isInstanceOf[NamedReference] =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we use V2ColumnUtils here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

(max.column.asInstanceOf[NamedReference], "max")
case min: Min if min.column().isInstanceOf[NamedReference] =>
(min.column.asInstanceOf[NamedReference], "min")
case _ => return false
}

if (isPartitionCol(column)) {
Expand Down Expand Up @@ -117,10 +118,11 @@ object AggregatePushDownUtils {
if (!processMinOrMax(max)) return None
case min: Min =>
if (!processMinOrMax(min)) return None
case count: Count =>
if (count.column.fieldNames.length != 1 || count.isDistinct) return None
case count: Count if count.column().isInstanceOf[NamedReference] =>
val reference = count.column.asInstanceOf[NamedReference]
if (reference.fieldNames.length != 1 || count.isDistinct) return None
finalSchema =
finalSchema.add(StructField(s"count(" + count.column.fieldNames.head + ")", LongType))
finalSchema.add(StructField(s"count(" + reference.fieldNames.head + ")", LongType))
case _: CountStar =>
finalSchema = finalSchema.add(StructField("count(*)", LongType))
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import org.apache.spark.sql.catalyst.planning.ScanOperation
import org.apache.spark.sql.catalyst.plans.logical.{InsertIntoDir, InsertIntoStatement, LogicalPlan, Project}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.streaming.StreamingRelationV2
import org.apache.spark.sql.catalyst.util.ExpressionSQLBuilder
import org.apache.spark.sql.connector.catalog.SupportsRead
import org.apache.spark.sql.connector.catalog.TableCapability._
import org.apache.spark.sql.connector.expressions.{FieldReference, NullOrdering, SortDirection, SortOrder => SortOrderV2, SortValue}
import org.apache.spark.sql.connector.expressions.{Expression => ExpressionV2, FieldReference, GeneralSQLExpression, NullOrdering, SortDirection, SortOrder => SortOrderV2, SortValue}
import org.apache.spark.sql.connector.expressions.aggregate.{AggregateFunc, Aggregation, Avg, Count, CountStar, GeneralAggregateFunc, Max, Min, Sum}
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.execution.{InSubqueryExec, RowDataSourceScanExec, SparkPlan}
Expand Down Expand Up @@ -705,22 +706,17 @@ object DataSourceStrategy
protected[sql] def translateAggregate(agg: AggregateExpression): Option[AggregateFunc] = {
if (agg.filter.isEmpty) {
agg.aggregateFunction match {
case aggregate.Min(PushableColumnWithoutNestedColumn(name)) =>
Some(new Min(FieldReference.column(name)))
case aggregate.Max(PushableColumnWithoutNestedColumn(name)) =>
Some(new Max(FieldReference.column(name)))
case aggregate.Min(PushableExpression(expr)) => Some(new Min(expr))
case aggregate.Max(PushableExpression(expr)) => Some(new Max(expr))
case count: aggregate.Count if count.children.length == 1 =>
count.children.head match {
// COUNT(any literal) is the same as COUNT(*)
case Literal(_, _) => Some(new CountStar())
case PushableColumnWithoutNestedColumn(name) =>
Some(new Count(FieldReference.column(name), agg.isDistinct))
case PushableExpression(expr) => Some(new Count(expr, agg.isDistinct))
case _ => None
}
case aggregate.Sum(PushableColumnWithoutNestedColumn(name), _) =>
Some(new Sum(FieldReference.column(name), agg.isDistinct))
case aggregate.Average(PushableColumnWithoutNestedColumn(name), _) =>
Some(new Avg(FieldReference.column(name), agg.isDistinct))
case aggregate.Sum(PushableExpression(expr), _) => Some(new Sum(expr, agg.isDistinct))
case aggregate.Average(PushableExpression(expr), _) => Some(new Avg(expr, agg.isDistinct))
case aggregate.VariancePop(PushableColumnWithoutNestedColumn(name), _) =>
Some(new GeneralAggregateFunc(
"VAR_POP", agg.isDistinct, Array(FieldReference.column(name))))
Expand Down Expand Up @@ -860,3 +856,13 @@ object PushableColumnAndNestedColumn extends PushableColumnBase {
object PushableColumnWithoutNestedColumn extends PushableColumnBase {
override val nestedPredicatePushdownEnabled = false
}

/**
* Get the expression of DS V2 to represent catalyst expression that can be pushed down.
*/
object PushableExpression {
Comment thread
cloud-fan marked this conversation as resolved.
def unapply(e: Expression): Option[ExpressionV2] = e match {
case PushableColumnWithoutNestedColumn(name) => Some(FieldReference.column(name))
case _ => new ExpressionSQLBuilder(e).build().map(new GeneralSQLExpression(_))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.apache.spark.sql.catalyst.expressions.JoinedRow
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.util.{quoteIdentifier, CharVarcharUtils, DateTimeUtils}
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.connector.expressions.NamedReference
import org.apache.spark.sql.connector.expressions.aggregate.{Aggregation, Count, CountStar, Max, Min}
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.execution.datasources.{AggregatePushDownUtils, SchemaMergeUtils}
Expand Down Expand Up @@ -487,18 +488,18 @@ object OrcUtils extends Logging {

val aggORCValues: Seq[WritableComparable[_]] =
aggregation.aggregateExpressions.zipWithIndex.map {
case (max: Max, index) =>
val columnName = max.column.fieldNames.head
case (max: Max, index) if max.column.isInstanceOf[NamedReference] =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not caused by this PR, but can we also check the length of field names to be future proof? We can even create a util object to save duplicated code:

object V2Column {
  def unapply(e: org.apache.spark.sql.connector.expressions.Expression): Option[String] = e match {
    case r: NamedReference if r. fieldNames.length == 1 => Some(r.fieldNames.head)
    case _ => None
  }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK at least we can make a util function

object V2ColumnUtils {
  def extractV2Column(expr: V2Expression): Option[String] = ...
}

and here we can write

case (max: Max, index) if V2ColumnUtils.extractV2Column(max.column).isDefined =>
  val columnName = V2ColumnUtils.extractV2Column(max.column).get
  ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

val columnName = max.column.asInstanceOf[NamedReference].fieldNames.head
val statistics = getColumnStatistics(columnName)
val dataType = schemaWithoutGroupBy(index).dataType
getMinMaxFromColumnStatistics(statistics, dataType, isMax = true)
case (min: Min, index) =>
val columnName = min.column.fieldNames.head
case (min: Min, index) if min.column.isInstanceOf[NamedReference] =>
val columnName = min.column.asInstanceOf[NamedReference].fieldNames.head
val statistics = getColumnStatistics(columnName)
val dataType = schemaWithoutGroupBy.apply(index).dataType
getMinMaxFromColumnStatistics(statistics, dataType, isMax = false)
case (count: Count, _) =>
val columnName = count.column.fieldNames.head
case (count: Count, _) if count.column.isInstanceOf[NamedReference] =>
val columnName = count.column.asInstanceOf[NamedReference].fieldNames.head
val isPartitionColumn = partitionSchema.fields.map(_.name).contains(columnName)
// NOTE: Count(columnName) doesn't include null values.
// org.apache.orc.ColumnStatistics.getNumberOfValues() returns number of non-null values
Expand Down
Loading