Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -986,4 +986,13 @@ private[v2] trait V2JDBCTest extends SharedSparkSession with DockerIntegrationFu
test("scan with filter push-down with date time functions") {
testDatetime(s"$catalogAndNamespace.${caseConvert("datetime")}")
}

test("xor operator push-down") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you can do explain formatted and check whether string contains "id" # 3, that will add a little bit of robustness.
Another thing we can do is to make unit test, and just invoke compilation of XOR expression and check whether col # constant is result of compilation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

neat, I like it

val df1 = spark.sql(
s"""SELECT * FROM $catalogAndNamespace.${caseConvert("pattern_testing_table")}
|WHERE id ^ 3 = 0""".stripMargin)
val rows1 = df1.collect()
assert(rows1.length === 1)
assert(rows1(0).getInt(0) === 3)
}
}
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)
}
}
}
}