0

I'm trying to get an if else statement working in the form of when() and otherwise() function. I tried thinking of many ways but my problem is the when() statement needs a column. Is there a way to get the following code working in when() and otherwise() format?

get_data = spark.sql("SELECT STRING({}) AS {} FROM {} WHERE Mobile='{}'".format(dynamic_tag_mapping_column_name, match[0], dynamic_tag_mapping_table_name, mobile_numbers[mob])).collect()[0][0]
if get_data!='0':
     textList.append(campaign_segment_text.withColumn('CampaignSMSText', func.expr("regexp_replace(CampaignSMSText, '{}', {})".format(match[0], get_data)))
else:
     textList.append(campaign_segment_text.withColumn('CampaignSMSText', func.expr("regexp_replace(CampaignSMSText, '{}', {})".format(match[0], dynamic_default_value)))

What I'm trying to achieve is if I get the get_data>0 value from database it will take the get_data value but if get_data=0, it will assign a default value that is set. I tried setting the String value to Int that I'm getting from the DB but that won't compare because get_data isn't a column. Is there anyway to get this working?

1 Answer 1

1

Try putting case when inside F.expr?

get_data = spark.sql("SELECT {} AS {} FROM {} WHERE Mobile='{}'".format(dynamic_tag_mapping_column_name, match[0], dynamic_tag_mapping_table_name, mobile_numbers[mob])).collect()[0][0]

textList.append(
    campaign_segment_text.withColumn(
        'CampaignSMSText',
        func.expr(
            "regexp_replace(CampaignSMSText, '{}', case when {} != 0 then {} else {} end)".format(
                match[0], get_data, get_data, dynamic_default_value
            )
        )
    )
)
Sign up to request clarification or add additional context in comments.

7 Comments

@VarunNagrare I found a missing close bracket in your code (and also my code), could you please try again?
Okay I'll check it
Yeah it worked! Thanks man. Your answer helped me a second time. :D
I know UDF's are slow. I just wanted to practice since I don't know how UDF's work.
yes you can use udf. the udf will take a string, pattern, and pattern to replace as input arguments, and return a replaced string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.