-
Notifications
You must be signed in to change notification settings - Fork 29k
[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 all 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,69 @@ | ||
| /* | ||
| * 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.
Show resolved
Hide 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)") | ||
| 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 | ||
| if (elseValue.isDefined) { | ||
| elseValue.flatMap(generateSQL).map(v => s"CASE$branchSQL ELSE $v END") | ||
| } else { | ||
| Some(s"CASE$branchSQL END") | ||
| } | ||
| } else { | ||
| None | ||
| } | ||
| // TODO supports other expressions | ||
| case _ => None | ||
| } | ||
| } | ||
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)