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:
1
org.apache.spark.sql.AnalysisException:
2
Detected implicit cartesian product for INNER
3
join between logical plans
4
Project [VisitorId#14]
5
+- LogicalRDD [products#13, visitorId#14], false
6
and
7
Project [id#21, if (isnotnull(name#6)) name#6 else invalid product AS name#25, interest#22]
8
+- Join FullOuter, (id#21 = id#5)
9
:- Project [products#19.id AS id#21, products#19.interest AS interest#22]
10
: +- Generate explode(products#13), [0], false, [products#19]
11
: +- Project [products#13]
12
: +- LogicalRDD [products#13, visitorId#14], false
13
+- LocalRelation [id#5, name#6]
14
Join condition is missing or trivial.
15
Either: use the CROSS JOIN syntax to allow cartesian products between these
16
relations, or: enable implicit cartesian products by setting the configuration
17
variable spark.sql.crossJoin.enabled=true;
Copied!
The error message does tell you the work around:
1
Either: use the CROSS JOIN syntax to allow
2
cartesian products between these
3
relations, or: enable implicit cartesian
4
products by setting the configuration variable
5
​
6
spark.sql.crossJoin.enabled=true;
Copied!
Therefore, the solutions are below:
Add below config in Spark session
1
spark.sql.crossJoin.enabled=true;
Copied!
By for example
1
val spark = SparkSession
2
.builder
3
.appName("JsonApp")
4
.master("local[*]")
5
.config("spark.sql.warehouse.dir", "file:///d:/tmp")
6
.config("spark.sql.crossJoin.enabled", "true")
7
.getOrCreate()
Copied!
or
1
val spark = SparkSession
2
.builder
3
.appName("JsonApp")
4
.master("local[*]")
5
.config("spark.sql.warehouse.dir", "file:///d:/tmp")
6
.getOrCreate()
7
​
8
spark.conf.set("spark.sql.crossJoin.enabled", "true")
Copied!
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:
1
#
2
# Licensed to the Apache Software Foundation (ASF) under one or more
3
# contributor license agreements. See the NOTICE file distributed with
4
# this work for additional information regarding copyright ownership.
5
# The ASF licenses this file to You under the Apache License, Version 2.0
6
# (the "License"); you may not use this file except in compliance with
7
# the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
​
18
# Default system properties included when running spark-submit.
19
# This is useful for setting default environmental settings.
20
​
21
# Example:
22
# spark.master spark://master:7077
23
# spark.eventLog.enabled true
24
# spark.eventLog.dir hdfs://namenode:8021/directory
25
# spark.serializer org.apache.spark.serializer.KryoSerializer
26
# spark.driver.memory 5g
27
# spark.executor.extraJavaOptions -XX:+PrintGCDetails -Dkey=value -Dnumbers="one two three"
28
spark.sql.crossJoin.enabled true
29
​
Copied!
Make sure no # sign at the begin. Then bounce the Spark cluster by:
1
$SPARK_HOME/sbin/stop-all.sh
2
$SPARK_HOME/sbin/start-all.sh
Copied!
If you are running jupyter-notebook, make sure restart jupyter-notebook server process.
Last modified 1yr ago