Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.jdbc.v2
import java.sql.{Connection, SQLFeatureNotSupportedException}

import org.apache.spark.{SparkConf, SparkSQLFeatureNotSupportedException}
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog
import org.apache.spark.sql.jdbc.MySQLDatabaseOnDocker
import org.apache.spark.sql.types._
Expand Down Expand Up @@ -313,6 +313,15 @@ class MySQLIntegrationSuite extends DockerJDBCIntegrationV2Suite with V2JDBCTest
assert(rows10(0).getString(0) === "amy")
assert(rows10(1).getString(0) === "alex")
}

test("do not push down casts to double") {
val df = sql(
s"SELECT name FROM $catalogName.employee " +
"WHERE CAST(salary AS DOUBLE) > 10000.5")

checkFilterPushed(df, pushed = false)
checkAnswer(df, Seq(Row("alex"), Row("jen")))
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ private case class MySQLDialect() extends JdbcDialect with SQLConfHelper with No
} else {
super.visitAggregateFunction(funcName, isDistinct, inputs)
}

override def visitCast(expr: String, exprDataType: DataType, dataType: DataType): String = {
dataType match {
case DoubleType =>
// The common JDBC mapping is DOUBLE PRECISION, which is not a portable CAST target for
// MySQL-compatible databases. In particular, MariaDB rejects it with a syntax error.
throw new UnsupportedOperationException("Cannot cast to double type")
case _ => super.visitCast(expr, exprDataType, dataType)
}
}
}

override def compileExpression(expr: Expression): Option[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.catalyst.plans.logical.ShowCreateTable
import org.apache.spark.sql.catalyst.util.{CaseInsensitiveMap, CharVarcharUtils, DateTimeTestUtils}
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.connector.expressions.{Expression => V2Expression, FieldReference, GeneralScalarExpression, LiteralValue}
import org.apache.spark.sql.connector.expressions.{Cast => V2Cast, Expression => V2Expression, FieldReference, GeneralScalarExpression, LiteralValue}
import org.apache.spark.sql.connector.expressions.filter.{AlwaysFalse, AlwaysTrue, Predicate}
import org.apache.spark.sql.execution.{DataSourceScanExec, ExtendedMode, ProjectExec}
import org.apache.spark.sql.execution.command.{ExplainCommand, ShowCreateTableCommand}
Expand Down Expand Up @@ -1454,6 +1454,13 @@ class JDBCSuite extends SharedSparkSession {
assert(mySqlDialect.getJDBCType(FloatType).map(_.databaseTypeDefinition).get == "FLOAT")
}

test("MySQL blocks casts to double") {
val dialect = MySQLDialect()
val cast = new V2Cast(FieldReference("value"), IntegerType, DoubleType)

assert(dialect.compileExpression(cast).isEmpty)
}

test("PostgresDialect type mapping") {
val Postgres = JdbcDialects.get("jdbc:postgresql://127.0.0.1/db")
val md = new MetadataBuilder().putLong("scale", 0).putBoolean("isTimestampNTZ", false)
Expand Down