-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-12446][SQL] Add unit tests for JDBCRDD internal functions #10409
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
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 |
|---|---|---|
|
|
@@ -163,8 +163,37 @@ private[sql] object JDBCRDD extends Logging { | |
| * @return A Catalyst schema corresponding to columns in the given order. | ||
| */ | ||
| private def pruneSchema(schema: StructType, columns: Array[String]): StructType = { | ||
| val fieldMap = Map(schema.fields map { x => x.metadata.getString("name") -> x }: _*) | ||
| new StructType(columns map { name => fieldMap(name) }) | ||
| val fieldMap = Map(schema.fields.map(x => x.metadata.getString("name") -> x): _*) | ||
| new StructType(columns.map(name => fieldMap(name))) | ||
| } | ||
|
|
||
| /** | ||
| * Converts value to SQL expression. | ||
| */ | ||
| private def compileValue(value: Any): Any = value match { | ||
| case stringValue: String => s"'${escapeSql(stringValue)}'" | ||
| case timestampValue: Timestamp => "'" + timestampValue + "'" | ||
|
Member
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. @rxin `s"'$timestampValue'"`` is better here?
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. both works - i wouldn't worry about it.
Member
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. Okay. |
||
| case dateValue: Date => "'" + dateValue + "'" | ||
| case _ => value | ||
| } | ||
|
|
||
| private def escapeSql(value: String): String = | ||
| if (value == null) null else StringUtils.replace(value, "'", "''") | ||
|
|
||
| /** | ||
| * Turns a single Filter into a String representing a SQL expression. | ||
| * Returns null for an unhandled filter. | ||
| */ | ||
| private def compileFilter(f: Filter): String = f match { | ||
| case EqualTo(attr, value) => s"$attr = ${compileValue(value)}" | ||
| case Not(EqualTo(attr, value)) => s"$attr != ${compileValue(value)}" | ||
| case LessThan(attr, value) => s"$attr < ${compileValue(value)}" | ||
| case GreaterThan(attr, value) => s"$attr > ${compileValue(value)}" | ||
| case LessThanOrEqual(attr, value) => s"$attr <= ${compileValue(value)}" | ||
| case GreaterThanOrEqual(attr, value) => s"$attr >= ${compileValue(value)}" | ||
| case IsNull(attr) => s"$attr IS NULL" | ||
| case IsNotNull(attr) => s"$attr IS NOT NULL" | ||
| case _ => null | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -262,40 +291,12 @@ private[sql] class JDBCRDD( | |
| if (sb.length == 0) "1" else sb.substring(1) | ||
| } | ||
|
|
||
| /** | ||
| * Converts value to SQL expression. | ||
| */ | ||
| private def compileValue(value: Any): Any = value match { | ||
| case stringValue: String => s"'${escapeSql(stringValue)}'" | ||
| case timestampValue: Timestamp => "'" + timestampValue + "'" | ||
| case dateValue: Date => "'" + dateValue + "'" | ||
| case _ => value | ||
| } | ||
|
|
||
| private def escapeSql(value: String): String = | ||
| if (value == null) null else StringUtils.replace(value, "'", "''") | ||
|
|
||
| /** | ||
| * Turns a single Filter into a String representing a SQL expression. | ||
| * Returns null for an unhandled filter. | ||
| */ | ||
| private def compileFilter(f: Filter): String = f match { | ||
| case EqualTo(attr, value) => s"$attr = ${compileValue(value)}" | ||
| case Not(EqualTo(attr, value)) => s"$attr != ${compileValue(value)}" | ||
| case LessThan(attr, value) => s"$attr < ${compileValue(value)}" | ||
| case GreaterThan(attr, value) => s"$attr > ${compileValue(value)}" | ||
| case LessThanOrEqual(attr, value) => s"$attr <= ${compileValue(value)}" | ||
| case GreaterThanOrEqual(attr, value) => s"$attr >= ${compileValue(value)}" | ||
| case IsNull(attr) => s"$attr IS NULL" | ||
| case IsNotNull(attr) => s"$attr IS NOT NULL" | ||
| case _ => null | ||
| } | ||
|
|
||
| /** | ||
| * `filters`, but as a WHERE clause suitable for injection into a SQL query. | ||
| */ | ||
| private val filterWhereClause: String = { | ||
| val filterStrings = filters map compileFilter filter (_ != null) | ||
| val filterStrings = filters.map(JDBCRDD.compileFilter).filter(_ != null) | ||
| if (filterStrings.size > 0) { | ||
| val sb = new StringBuilder("WHERE ") | ||
| filterStrings.foreach(x => sb.append(x).append(" AND ")) | ||
|
|
||
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.
did you just move this code block, or was there any changes made?
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 just moved
compileValue,escapeSql, andcompileFilterin this companion object area.