Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 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,52 @@
/*
* 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.
* <p>
* The currently supported SQL expressions:

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.

I think we can omit the internal details in the classdoc, as the users don't care. They just take the SQL string anyway.

This is different from GeneralAggregateFunction, which user needs to know all the supported functions and match them.

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.

* <ol>
* <li><pre>Literal</pre> Since 3.3.0</li>
* <li><pre>Attribute</pre> Since 3.3.0</li>
* <li><pre>IsNull</pre> Since 3.3.0</li>
* <li><pre>IsNotNull</pre> Since 3.3.0</li>
* <li><pre>BinaryOperator</pre> Since 3.3.0</li>
* <li><pre>Not</pre> Since 3.3.0</li>
* <li><pre>CaseWhen</pre> Since 3.3.0</li>
* </ol>
*
* @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,65 @@
/*
* 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 build expression and generate sql string.

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.

Suggested change
* The builder to build expression and generate sql string.
* 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}")

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.

is it safer to do (${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 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.{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 @@ -707,20 +708,30 @@ object DataSourceStrategy
agg.aggregateFunction match {
case aggregate.Min(PushableColumnWithoutNestedColumn(name)) =>
Some(new Min(FieldReference.column(name)))
case aggregate.Min(PushableExpression(expr)) =>

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.

To simplify the code, can we make PushableExpression handle field reference as well? e.g. it returns FieldReference if possible, or return GeneralSQLExpression. Then here we can just use whatever v2 expression returned by PushableExpression.

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

Some(new Min(expr))
case aggregate.Max(PushableColumnWithoutNestedColumn(name)) =>
Some(new Max(FieldReference.column(name)))
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.Sum(PushableExpression(expr), _) =>
Some(new Sum(expr, agg.isDistinct))
case aggregate.Average(PushableColumnWithoutNestedColumn(name), _) =>
Some(new Avg(FieldReference.column(name), 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 +871,12 @@ object PushableColumnAndNestedColumn extends PushableColumnBase {
object PushableColumnWithoutNestedColumn extends PushableColumnBase {
override val nestedPredicatePushdownEnabled = false
}

/**
* Get the SQL string of an expression that can be pushed down.

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.

need to update the comment now

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

*/
object PushableExpression {
Comment thread
cloud-fan marked this conversation as resolved.
def unapply(e: Expression): Option[GeneralSQLExpression] = {
new ExpressionSQLBuilder(e).build().map(new GeneralSQLExpression(_))
}
}
Loading