Skip to content
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-49695][SQL] Postgres fix xor push-down #48144

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import java.util
import java.util.Locale

import scala.util.Using
import scala.util.control.NonFatal

import org.apache.spark.internal.LogKeys.COLUMN_NAME
import org.apache.spark.internal.MDC
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.SQLConfHelper
import org.apache.spark.sql.catalyst.analysis.{IndexAlreadyExistsException, NonEmptyNamespaceException, NoSuchIndexException}
import org.apache.spark.sql.connector.catalog.Identifier
import org.apache.spark.sql.connector.expressions.NamedReference
import org.apache.spark.sql.connector.expressions.{Expression, GeneralScalarExpression, NamedReference}
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.execution.datasources.jdbc.{JDBCOptions, JdbcUtils}
import org.apache.spark.sql.execution.datasources.v2.TableSampleInfo
Expand Down Expand Up @@ -379,4 +380,26 @@ private case class PostgresDialect()
case _ =>
}
}

override def compileExpression(expr: Expression): Option[String] = {
val builder = new PostgresSQLBuilder()
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps its better to only override visitBinaryArithmetics?

We have similar problem with some of the functions and we use dialectFunctionName to translate from Spark to local dialect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 here, let's override last possible method in chain of execution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Some(builder.build(expr))
} catch {
case NonFatal(e) =>
logWarning("Error occurs while compiling V2 expression", e)
None
}
}

private class PostgresSQLBuilder extends JDBCSQLBuilder {
override def build(expr: Expression): String = {
expr match {
// Postgres uses '#' for xor, rather then '^'.
case e: GeneralScalarExpression if e.name() == "^" =>
visitBinaryArithmetic("#", inputToSQL(e.children().head), inputToSQL(e.children()(1)))
case _ => super.build(expr)
}
}
}
}