Issue from running Cartesian Join Query

What is Cartesian Join query?

A Cartesian Join SQL query is also known as cross join SQL query.

A SQL join query that does no have join condition or does not have sufficient join conditions is a Cartesian join query, the result of it is called Cartesian product.

To avoid Cartesian product, a SQL query that joins N tables must have N-1 join conditions.

A Cartesian join query may be needed for reason depending on your design on your application. However, when you run Cartesian join query in Spark SQL, it is likely you may run into below error:

org.apache.spark.sql.AnalysisException: 
Detected implicit cartesian product for INNER 
join between logical plans
Project [VisitorId#14]
+- LogicalRDD [products#13, visitorId#14], false
and
Project [id#21, if (isnotnull(name#6)) name#6 else invalid product AS name#25, interest#22]
+- Join FullOuter, (id#21 = id#5)
   :- Project [products#19.id AS id#21, products#19.interest AS interest#22]
   :  +- Generate explode(products#13), [0], false, [products#19]
   :     +- Project [products#13]
   :        +- LogicalRDD [products#13, visitorId#14], false
   +- LocalRelation [id#5, name#6]
Join condition is missing or trivial.
Either: use the CROSS JOIN syntax to allow cartesian products between these
relations, or: enable implicit cartesian products by setting the configuration
variable spark.sql.crossJoin.enabled=true;

The error message does tell you the work around:

Either: use the CROSS JOIN syntax to allow 
cartesian products between these
relations, or: enable implicit cartesian 
products by setting the configuration variable 

spark.sql.crossJoin.enabled=true;

Therefore, the solutions are below:

Add below config in Spark session

spark.sql.crossJoin.enabled=true;

By for example

val spark = SparkSession
    .builder
    .appName("JsonApp")
    .master("local[*]")
    .config("spark.sql.warehouse.dir", "file:///d:/tmp")
    .config("spark.sql.crossJoin.enabled", "true")
    .getOrCreate()

or

val spark = SparkSession
    .builder
    .appName("JsonApp")
    .master("local[*]")
    .config("spark.sql.warehouse.dir", "file:///d:/tmp")
    .getOrCreate()

spark.conf.set("spark.sql.crossJoin.enabled", "true")

The above solution requires you to add spark.sql.crossJoin.enabled=true

Into each of your Spark driver application code.

Set the default in Spark configuration to be effective to all

in $SPARK_HOME/conf directory, there is a file called:

spark-defaults.conf.template

Copy or rename this file to spark-defaults.conf

Then edit spark-defaults.conf, such as by vi, add a line at the end:

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Default system properties included when running spark-submit.
# This is useful for setting default environmental settings.

# Example:
# spark.master                     spark://master:7077
# spark.eventLog.enabled           true
# spark.eventLog.dir               hdfs://namenode:8021/directory
# spark.serializer                 org.apache.spark.serializer.KryoSerializer
# spark.driver.memory              5g
# spark.executor.extraJavaOptions  -XX:+PrintGCDetails -Dkey=value -Dnumbers="one two three"
spark.sql.crossJoin.enabled        true

Make sure no # sign at the begin. Then bounce the Spark cluster by:

$SPARK_HOME/sbin/stop-all.sh
$SPARK_HOME/sbin/start-all.sh

If you are running jupyter-notebook, make sure restart jupyter-notebook server process.

Last updated