-
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 16 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,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 { | ||
| 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,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)) => | ||
|
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") | ||
|
cloud-fan marked this conversation as resolved.
Outdated
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("") | ||
|
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 |
|---|---|---|
|
|
@@ -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} | ||
|
|
@@ -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] => | ||
|
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. 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:
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. OK at least we can make a util function and here we can write
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 |
||
| 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 | ||
|
|
||
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 would also come up with another name instead of
GeneralSQLExpression(e.g.,PushedExpression)