Shasu
Shasu

Reputation: 502

spark join causing column id ambiguity error

I have following dataframes:

accumulated_results_df
 |-- company_id: string (nullable = true)
 |-- max_dd: string (nullable = true)
 |-- min_dd: string (nullable = true)
 |-- count: string (nullable = true)
 |-- mean: string (nullable = true)

computed_df
 |-- company_id: string (nullable = true)
 |-- min_dd: date (nullable = true)
 |-- max_dd: date (nullable = true)
 |-- mean: double (nullable = true)
 |-- count: long (nullable = false)

Trying to do a join using spark-sql as below

 val resultDf = accumulated_results_df.as("a").join(computed_df.as("c"), 
                             ( $"a.company_id" === $"c.company_id" ) && ( $"c.min_dd" > $"a.max_dd" ), "left")

Giving error as :

org.apache.spark.sql.AnalysisException: Reference 'company_id' is ambiguous, could be: a.company_id, c.company_id.;

What am i doing wrong here and How to fix this ?

Upvotes: 2

Views: 9453

Answers (2)

Alex Ortner
Alex Ortner

Reputation: 1238

Should work using the col function to reference correctly the alias dataframes and columns

val resultDf = (accumulated_results_df.as("a")
       .join(
           computed_df.as("c"),
           (col("a.company_id") === col("c.company_id")) && (col("c.min_dd") > col("a.max_dd")), 
           "left"
        )

Upvotes: 2

Shasu
Shasu

Reputation: 502

I have fixed it something like below.

val resultDf = accumulated_results_df.join(computed_df.withColumnRenamed("company_id", "right_company_id").as("c"), 
                             (  accumulated_results_df("company_id" ) === $"c.right_company_id" && ( $"c.min_dd" > accumulated_results_df("max_dd") ) )
                        , "left")

Upvotes: 1

Related Questions