-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-37960][SQL] A new framework to represent catalyst expressions in DS v2 APIs #35248
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
Changes from 14 commits
eec8477
25a6fc4
156db0b
4fc9012
a773fdf
b1a910b
bf457de
a43d4af
7522a33
30d0450
661c6ec
40aaa01
00fc887
2be30e2
ffeb8f3
7534ea1
dfcccaa
9f48dde
5ce74f3
424d3a5
7a645f5
c4487c2
646e427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||
| * <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 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also come up with another name instead of |
||
| private String sql; | ||
|
|
||
| public GeneralSQLExpression(String sql) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The V2 expression holds the V1 expression, which looks strange. |
||
| this.sql = sql; | ||
| } | ||
|
|
||
| public String sql() { return sql; } | ||
|
|
||
| @Override | ||
| public String toString() { return sql; } | ||
| } | ||
| 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. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| */ | ||||||
| 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}") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it safer to do |
||||||
| } else { | ||||||
| None | ||||||
| } | ||||||
| case Not(EqualTo(left, right)) => | ||||||
|
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("") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
|---|---|---|
|
|
@@ -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] => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
|
@@ -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 _ => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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} | ||
|
|
@@ -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)) => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To simplify the code, can we make
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)))) | ||
|
|
@@ -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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to update the comment now
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
| */ | ||
| object PushableExpression { | ||
|
cloud-fan marked this conversation as resolved.
|
||
| def unapply(e: Expression): Option[GeneralSQLExpression] = { | ||
| new ExpressionSQLBuilder(e).build().map(new GeneralSQLExpression(_)) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it.