-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-50792][SQL] Format binary data as a binary literal in JDBC. #49452
base: master
Are you sure you want to change the base?
Changes from 1 commit
ca63d5a
7fbb786
ce6bc59
13bc3ab
2d86ab9
d3bb055
09022ab
8ddfcc2
84ea312
86a1cda
b8c9708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: Xiaoguang Sun <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,17 +62,29 @@ private case class OracleDialect() extends JdbcDialect with SQLConfHelper with N | |
super.visitAggregateFunction(funcName, isDistinct, inputs) | ||
} | ||
|
||
private def generateBlobEquals(lhs: Expression, rhs: Expression): String = { | ||
s"DBMS_LOB.COMPARE(${inputToSQL(lhs)}, ${inputToSQL(rhs)}) = 0" | ||
private def compareBlob(lhs: Expression, operator: String, rhs: Expression): String = { | ||
val l = inputToSQL(lhs) | ||
val r = inputToSQL(rhs) | ||
val op = if (operator == "<=>") "=" else operator | ||
val compare = s"DBMS_LOB.COMPARE($l, $r) $op 0" | ||
if (operator == "<=>") { | ||
s"(($l IS NOT NULL AND $r IS NOT NULL AND $compare) OR ($l IS NULL AND $r IS NULL))" | ||
} else { | ||
compare | ||
} | ||
} | ||
|
||
override def build(expr: Expression): String = expr match { | ||
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. You just need override 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 can only rewrite comparison when one of the arguments is BLOB. For other cases, we have to use existing implementation. But unfortunately, the signature of visitBinaryComparison is accepting everything in string which loss the type information to understand if one of the arguments is binary type. protected String visitBinaryComparison(String name, String l, String r) {
if (name.equals("<=>")) {
return "((" + l + " IS NOT NULL AND " + r + " IS NOT NULL AND " + l + " = " + r + ") " +
"OR (" + l + " IS NULL AND " + r + " IS NULL))";
}
return l + " " + name + " " + r;
} 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. All tests passed, but downloading report failed. We can rerun the whole test again to clear all the checks, but it takes quite some time to finish. |
||
case e: GeneralScalarExpression if e.name == "=" => | ||
(e.children()(0), e.children()(1)) match { | ||
case (lhs: Literal[_], rhs: Expression) if lhs.dataType == BinaryType => | ||
generateBlobEquals(rhs, lhs) | ||
case (lhs: Expression, rhs: Literal[_]) if rhs.dataType == BinaryType => | ||
generateBlobEquals(rhs, lhs) | ||
case e: GeneralScalarExpression => | ||
e.name() match { | ||
case "=" | "<>" | "<=>" | "<" | "<=" | ">" | ">=" => | ||
(e.children()(0), e.children()(1)) match { | ||
case (lhs: Literal[_], rhs: Expression) if lhs.dataType == BinaryType => | ||
compareBlob(lhs, e.name, rhs) | ||
case (lhs: Expression, rhs: Literal[_]) if rhs.dataType == BinaryType => | ||
compareBlob(lhs, e.name, rhs) | ||
case _ => super.build(expr) | ||
} | ||
case _ => super.build(expr) | ||
} | ||
case _ => super.build(expr) | ||
|
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.
Please add the similar test case into
JDBCV2Suite
.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.
Please prepare data at
tablePreparation
.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.
Sorry, I'm not quite familiar with the test infrastructure. In case I make mistakes, let me confirm this question.
To mixin the tablePreparation and dataPreparation from trait defined in V2JDBCTest.scala, we need to update all the integration tests and call the these functions defined in trait.
And duplicate the extra call to multiple integration tests is OK, am I right?
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.
Hm, Just realized I have to use Spark SQL to create table and use the types defined in Spark SQL. If I prepare table and data in tablePreparation and dataPreparation, that will have to be database specific. The code will definitely have to be duplicated for connectors of all the databases.
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.
Yes. If we update the basic class
JdbcDialect
, we should test all the built-in integration tests.tablePreparation
used to customize the DDL, I'm afraid Spark SQL can covers all the built-in integration tests. But you could do your best effort, let's see the result and make the decision.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.
Wait, just realized each dialect embeds a builder which can override the implementation. Let me have a try.
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.
Oracle support is ready for review, PTAL. Thanks.
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.
You can override
visitBinaryComparison
inOracleSQLBuilder
.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.
There are couple threads discussing this topic. Let me copy the comment in case it's missed.
We can only rewrite comparison when one of the arguments is BLOB. For other cases, we have to use existing implementation. But unfortunately, the signature of visitBinaryComparison is accepting everything in string which loss the type information to understand if one of the arguments is binary type.
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.
All tests passed, but downloading report failed. We can rerun the whole test again to clear all the checks, but it takes quite some time to finish.
https://github.com/sunxiaoguang/spark/actions/runs/12744731853