-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-38196][SQL] Refactor framework so as JDBC dialect could compile expression by self way #35494
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
Closed
Closed
[SPARK-38196][SQL] Refactor framework so as JDBC dialect could compile expression by self way #35494
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
c6dc716
[SPARK-37960][SQL] Reactor framework so as compile expression by JDBC…
beliefer fed8c2b
Update code
beliefer c6c9d82
Update code
beliefer 2f44d5d
Update code
beliefer 6666635
Update code
beliefer 1d377b8
Update code
beliefer 1c77ff2
Update code
beliefer aa5974d
Update code
beliefer 451fd2b
Update code
beliefer dc0d7e6
Update code
beliefer dbcfd48
Update code
beliefer 343cb85
Update code
beliefer 46d0bdd
Update code
beliefer 6dfbad2
Update code
beliefer affec97
Update code
beliefer 40af215
Update code
beliefer 006dc97
Update code
beliefer 083b954
Update code
beliefer 9e68fae
Update code
beliefer 96196b0
Update code
beliefer 7fa8efe
Update code
beliefer 349f672
Update code
beliefer 51b0d13
Update code
beliefer 7388fb5
Update code
beliefer 0ea06a4
Update code
beliefer 0ab4669
Update code
beliefer e0f59c1
Update code
beliefer 0254def
Update code
beliefer 7be4ac3
Update code
beliefer d17366f
Update code
beliefer d9392a7
Update code
beliefer 78164a8
Update code
beliefer b78ebd5
Update sql/catalyst/src/main/java/org/apache/spark/sql/connector/expr…
beliefer 800dd5b
Update code
beliefer f227e44
Merge branch 'SPARK-37960_followup' of github.com:beliefer/spark into…
beliefer cf91bbf
Update code
beliefer 3557a6f
Update code
beliefer eb5894c
Update code
beliefer e63bc98
Update code
beliefer 2b9921d
Update code
beliefer dc7a260
Update code
beliefer de78a0b
Update code
beliefer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
...talyst/src/main/java/org/apache/spark/sql/connector/expressions/GeneralSQLExpression.java
This file was deleted.
Oops, something went wrong.
203 changes: 203 additions & 0 deletions
203
...yst/src/main/java/org/apache/spark/sql/connector/expressions/GeneralScalarExpression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * 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 java.util.Arrays; | ||
|
|
||
| import org.apache.spark.annotation.Evolving; | ||
| import org.apache.spark.sql.connector.util.V2ExpressionSQLBuilder; | ||
|
|
||
| // scalastyle:off line.size.limit | ||
| /** | ||
| * The general representation of SQL scalar expressions, which contains the upper-cased | ||
| * expression name and all the children expressions. | ||
| * <p> | ||
| * The currently supported SQL scalar expressions: | ||
| * <ol> | ||
| * <li>Name: <code>IS_NULL</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr IS NULL</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>IS_NOT_NULL</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr IS NOT NULL</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>=</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 = expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>!=</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 != expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code><></code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 <> expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code><=></code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 <=> expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code><</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 < expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code><=</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 <= expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>></code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 > expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>>=</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 >= expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>+</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 + expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>-</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 - expr2</code> or <code>- expr</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>*</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 * expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>/</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 / expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>%</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 % expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>&</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 & expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>|</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 | expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>^</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 ^ expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>AND</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 AND expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>OR</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>expr1 OR expr2</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>NOT</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>NOT expr</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>~</code> | ||
| * <ul> | ||
| * <li>SQL semantic: <code>~ expr</code></li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * <li>Name: <code>CASE_WHEN</code> | ||
| * <ul> | ||
| * <li>SQL semantic: | ||
| * <code>CASE WHEN expr1 THEN expr2 [WHEN expr3 THEN expr4]* [ELSE expr5] END</code> | ||
| * </li> | ||
| * <li>Since version: 3.3.0</li> | ||
| * </ul> | ||
| * </li> | ||
| * </ol> | ||
| * Note: SQL semantic conforms ANSI standard, so some expressions are not supported when ANSI off, | ||
| * including: add, subtract, multiply, divide, remainder, pmod. | ||
| * | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * @since 3.3.0 | ||
| */ | ||
| // scalastyle:on line.size.limit | ||
| @Evolving | ||
| public class GeneralScalarExpression implements Expression, Serializable { | ||
| private String name; | ||
| private Expression[] children; | ||
|
|
||
| public GeneralScalarExpression(String name, Expression[] children) { | ||
| this.name = name; | ||
| this.children = children; | ||
| } | ||
|
|
||
| public String name() { return name; } | ||
| public Expression[] children() { return children; } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| V2ExpressionSQLBuilder builder = new V2ExpressionSQLBuilder(); | ||
| try { | ||
| return builder.build(this); | ||
| } catch (Throwable e) { | ||
| return name + "(" + | ||
| Arrays.stream(children).map(child -> child.toString()).reduce((a,b) -> a + "," + b) + ")"; | ||
| } | ||
| } | ||
| } | ||
151 changes: 151 additions & 0 deletions
151
sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * 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.util; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.spark.sql.connector.expressions.Expression; | ||
| import org.apache.spark.sql.connector.expressions.FieldReference; | ||
| import org.apache.spark.sql.connector.expressions.GeneralScalarExpression; | ||
| import org.apache.spark.sql.connector.expressions.LiteralValue; | ||
|
|
||
| /** | ||
| * The builder to generate SQL from V2 expressions. | ||
| */ | ||
| public class V2ExpressionSQLBuilder { | ||
| public String build(Expression expr) { | ||
| if (expr instanceof LiteralValue) { | ||
| return visitLiteral((LiteralValue) expr); | ||
| } else if (expr instanceof FieldReference) { | ||
| return visitFieldReference((FieldReference) expr); | ||
| } else if (expr instanceof GeneralScalarExpression) { | ||
| GeneralScalarExpression e = (GeneralScalarExpression) expr; | ||
| String name = e.name(); | ||
| switch (name) { | ||
| case "IS_NULL": | ||
| return visitIsNull(build(e.children()[0])); | ||
| case "IS_NOT_NULL": | ||
| return visitIsNotNull(build(e.children()[0])); | ||
| case "=": | ||
| case "!=": | ||
| case "<=>": | ||
| case "<": | ||
| case "<=": | ||
| case ">": | ||
| case ">=": | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return visitBinaryComparison(name, build(e.children()[0]), build(e.children()[1])); | ||
| case "+": | ||
| case "*": | ||
| case "/": | ||
| case "%": | ||
| case "&": | ||
| case "|": | ||
| case "^": | ||
| return visitBinaryArithmetic(name, build(e.children()[0]), build(e.children()[1])); | ||
| case "-": | ||
| if (e.children().length == 1) { | ||
| return visitUnaryArithmetic(name, build(e.children()[0])); | ||
| } else { | ||
| return visitBinaryArithmetic(name, build(e.children()[0]), build(e.children()[1])); | ||
| } | ||
| case "AND": | ||
| return visitAnd(name, build(e.children()[0]), build(e.children()[1])); | ||
| case "OR": | ||
| return visitOr(name, build(e.children()[0]), build(e.children()[1])); | ||
| case "NOT": | ||
| return visitNot(build(e.children()[0])); | ||
| case "~": | ||
| return visitUnaryArithmetic(name, build(e.children()[0])); | ||
| case "CASE_WHEN": | ||
| List<String> children = new ArrayList<>(); | ||
| for (Expression child : e.children()) { | ||
| children.add(build(child)); | ||
| } | ||
| return visitCaseWhen(children.toArray(new String[e.children().length])); | ||
| // TODO supports other expressions | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: | ||
| return visitUnexpectedExpr(expr); | ||
| } | ||
| } else { | ||
| return visitUnexpectedExpr(expr); | ||
| } | ||
| } | ||
|
|
||
| protected String visitLiteral(LiteralValue literalValue) { | ||
| return literalValue.toString(); | ||
| } | ||
|
|
||
| protected String visitFieldReference(FieldReference fieldRef) { | ||
| return fieldRef.toString(); | ||
| } | ||
|
|
||
| protected String visitIsNull(String v) { | ||
| return v + " IS NULL"; | ||
| } | ||
|
|
||
| protected String visitIsNotNull(String v) { | ||
| return v + " IS NOT NULL"; | ||
| } | ||
|
|
||
| protected String visitBinaryComparison(String name, String l, String r) { | ||
| return "(" + l + ") " + name + " (" + r + ")"; | ||
| } | ||
|
|
||
| protected String visitBinaryArithmetic(String name, String l, String r) { | ||
| return "(" + l + ") " + name + " (" + r + ")"; | ||
| } | ||
|
|
||
| protected String visitAnd(String name, String l, String r) { | ||
| return "(" + l + ") " + name + " (" + r + ")"; | ||
| } | ||
|
|
||
| protected String visitOr(String name, String l, String r) { | ||
| return "(" + l + ") " + name + " (" + r + ")"; | ||
| } | ||
|
|
||
| protected String visitNot(String v) { | ||
| return "NOT (" + v + ")"; | ||
| } | ||
|
|
||
| protected String visitUnaryArithmetic(String name, String v) { return name +" (" + v + ")"; } | ||
|
|
||
| protected String visitCaseWhen(String[] children) { | ||
| StringBuilder sb = new StringBuilder("CASE"); | ||
cloud-fan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (int i = 0; i < children.length; i += 2) { | ||
| String c = children[i]; | ||
| int j = i + 1; | ||
| if (j < children.length) { | ||
| String v = children[j]; | ||
| sb.append(" WHEN "); | ||
| sb.append(c); | ||
| sb.append(" THEN "); | ||
| sb.append(v); | ||
| } else { | ||
| sb.append(" ELSE "); | ||
| sb.append(c); | ||
| } | ||
| } | ||
| sb.append(" END"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| protected String visitUnexpectedExpr(Expression expr) throws IllegalArgumentException { | ||
| throw new IllegalArgumentException("Unexpected V2 expression: " + expr); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.