Multiple WHEN condition implementation in Pyspark - tsql

I've my T-SQL code below which I've converted in Pyspark but is giving me error
CASE
WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'OUT' AND time_on_site.timespent_sec < 72000 THEN 1 -- 20 hours
WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'OUT' AND time_on_site.timespent_sec >= 72000 THEN 0
WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'IN' AND time_on_site.timespent_sec <= 28800 THEN 2 -- 8 hours
WHEN time_on_site.eventaction = 'IN' AND time_on_site.next_action = 'IN' AND time_on_site.timespent_sec > 28800 THEN 3
WHEN time_on_site.type_flag = 'TYPE4' THEN 4
ELSE NULL
END AS "type"
Below is my Pyspark script which is throwing an error
from pyspark.sql.functions import when
TOS=TOS.withColumn('type', F.when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec < 72000") , 1).
when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec >= 72000") , 0).
when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec <= 28800") , 2).
when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec > 28800") , 3).
when(col('type_flag')=='TYPE4', 4).otherwise('NULL')
)
Where am I going wrong!?

Nest your 'when' in otherwise(). My braces may not be entirely balanced, so do check them, but the idea is the same.
TOS=TOS.withColumn('type', F.when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec < 72000") , 1).
otherwise( when( (col('eventaction') == 'IN') & (col('next_action') == 'OUT') & ("timespent_sec >= 72000") , 0).
otherwise( when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec <= 28800") , 2).
otherwise( when( (col('eventaction') == 'IN') & (col('next_action') == 'IN') & ("timespent_sec > 28800") , 3).
otherwise( when( col('type_flag')=='TYPE4', 4).otherwise('NULL'))))))

I've correctly implemented it as shown below
TOS=TOS.withColumn('type', F.when( (F.col("eventaction") == 'IN') & (F.col("next_action") == 'OUT') & (F.col("timespent_sec") < 72000) , 1).
when( (F.col("eventaction") == 'IN') & (F.col("next_action") == 'OUT') & (F.col("timespent_sec") >= 72000) , 0).
when( (F.col("eventaction") == 'IN') & (F.col("next_action") == 'IN') & (F.col("timespent_sec") <= 28800) , 2).
when( (F.col("eventaction") == 'IN') & (F.col("next_action") == 'IN') & (F.col("timespent_sec") > 28800) , 3).
when(F.col('type_flag')=='TYPE4', 4).otherwise('NULL'))

Related

I got error on 'filter' but not on 'for'..WHY??(error: the compiler is unable to type-check this expression in reasonable time')

I'm a newbie to swift programming.
I'm trying to get string with the only letters("a-z", "A-Z"), numbers("0-9"), and "-", "_", "*".
when I first tried this code
var new_id = "abc123##%%.."
new_id.filter { ($0 >= "a" && $0 <= "z") || ($0 >= "0" && $0 <= "9") || $0 == "-" || $0 == "_" || $0 == "." }
I got this error
error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
I searched out this error and found out I should break up this code.
However, when I try the below code, actually it works..!
for i in 0..<new_id.count {
let ch: Character = new_id[new_id.index(new_id.startIndex, offsetBy: i)]
if ( (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9") || ch == "-" || ch == "_" || ch == "." ) {
new_new_id.append(ch)
}
}
Then, what's the difference between these two codes? I thought the above 'filter' function works same with the below code with 'for'
statement.
let newString = "Hello123..H##^^%%"
let StringData = newString.filter({$0.isLetter})
let NumericData = newString.filter({$0.isNumber})
let caseData = newString.filter({$0 == "_" || $0 == "-" || $0 == "."})
let ChekedData = newString.filter({$0.isNumber || $0.isLetter || $0 == "_" || $0 == "-" || $0 == "."})
print(StringData)
print(NumericData)
print(caseData)
print(ChekedData) ```

If statement in a Loop

I want my code to check if my 'e' components are in range of +,- 0.72 to +,- 0.73 to store the magnitude of Delta_theta and M in a vector for Plot. But I don't really know what is wrong in the last if statement. I just tried to store the amount of M that satisfies this condition in another matrix 'G' and plot it against delta theta, but it doesn't work either.
With kind regards.
I am sure the second and all other statements are correct, I just need help for the last one, thanks
Problem is Here
if e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077 || e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077;
G=M;
end
clear all
clc
%% Constants
B=1.2;
H=0.35;
R=2.93;
b=1;
h=0.24;
d_c=0.06;
D_s=0.03;
d_s=0.4;
r_s=0.015;
N_s=2;
E_s=200e9;
f_y=500e6;
E_c=43.6e9;
v_c=0.24;
f_c=35.5e6;
F=1.9;
A_s=pi*r_s^2;
e=0.3*h;
%% Operator
Idx_1 = 0;
for delta_theta =-40*10^-3:0.05*10^-3:40*10^-3
alpha_e=0;
alpha_p=0;
beta_e=0;
beta_p=0;
r_b=0;
r_s=0;
r_e=0;
Idx_1 = Idx_1 + 1;
Idx_2 = 0;
delta_thetavalue(Idx_1)=delta_theta;
A=r_s-(f_y*d_s/(E_s*delta_theta));
for delta_l=-10*10^-2:0.05*10^-2:10*10^-2
Idx_2 = Idx_2 + 1;
delta_lvalue(Idx_2)=delta_l;
r_0=R-(delta_l/delta_theta);
r_1=R-((1/delta_theta)*(delta_l+(F*f_c*d_c)/E_c));
if delta_theta >=0
r_op=R+(h/2);
r_cl=R-(h/2);
if r_0 >= r_op && r_1<=r_cl
alpha_e=1;
r_b=r_op;
r_e=r_cl;
elseif r_0 >=r_op && r_cl<r_1 && r_1<r_op
alpha_e=1;
alpha_p=1;
r_b=r_op;
r_e=r_1;
elseif r_0 >=r_op && r_1>=r_op
alpha_p=1;
r_b=r_op;
r_e=r_op;
elseif r_s<r_0 && r_0<r_op && r_1<=r_cl
alpha_e=1;
r_b=r_0;
r_e=r_cl;
elseif r_s<r_0 && r_0<r_op && r_1>r_cl
alpha_e=1;
alpha_p=1;
r_b=r_0;
r_e=r_1;
elseif A<r_0 && r_cl<r_0 && r_0<=r_s && r_1<=r_cl
alpha_e=1;
beta_e=1;
r_b=r_0;
r_e=r_cl;
elseif A<r_0 && r_cl<r_0 && r_0<=r_s && r_1>r_cl
alpha_e=1;
alpha_p=1;
beta_e=1;
r_b=r_0;
r_e=r_1;
elseif A>=r_0 && r_0>r_cl && r_1<=r_cl
alpha_e=1;
beta_p=1;
r_b=r_0;
r_e=r_cl;
elseif A>=r_0 && r_0>r_cl && r_1>r_cl
alpha_e=1;
alpha_p=1;
beta_p=1;
r_b=r_0;
r_e=r_1;
elseif r_0 <= r_cl
r_b=r_cl;
r_e=r_cl;
end
else
r_op=R-(h/2);
r_cl=R+(h/2);
if r_0 <= r_op && r_1 >= r_cl
alpha_e=1;
r_b=r_op;
r_e=r_cl;
elseif r_0 <=r_op && r_1>r_op && r_cl>r_1
alpha_e=1;
alpha_p=1;
r_b=r_op;
r_e=r_1;
elseif r_0 <=r_op && r_1<=r_op
alpha_p=1;
r_b=r_op;
r_e=r_op;
elseif r_s>r_0 && r_0>r_op && r_1>=r_cl
alpha_e=1;
r_b=r_0;
r_e=r_cl;
elseif r_s>r_0 && r_0>r_op && r_1<r_cl
alpha_e=1;
alpha_p=1;
r_b=r_0;
r_e=r_1;
elseif A>r_0 && r_cl>r_0 && r_0>=r_s && r_1>=r_cl
alpha_e=1;
beta_e=1;
r_b=r_0;
r_e=r_cl;
elseif A>r_0 && r_cl>r_0 && r_0>=r_s && r_1<r_cl
alpha_e=1;
alpha_p=1;
beta_e=1;
r_b=r_0;
r_e=r_1;
elseif A<=r_0 && r_0<r_cl && r_1>r_cl
alpha_e=1;
beta_p=1;
r_b=r_0;
r_e=r_cl;
elseif A<=r_0 && r_0<r_cl && r_1<=r_cl
alpha_e=1;
alpha_p=1;
beta_p=1;
r_b=r_0;
r_e=r_1;
elseif r_0 >= r_cl
r_b=r_cl;
r_e=r_cl;
end
end
M(Idx_1,Idx_2)=alpha_e*(E_c*b/d_c)*((delta_l/2)*((r_b-R)^2-(r_e-R)^2)+(delta_theta/3)*((r_b-R)^3-(r_e-R)^3))+alpha_p*(-F*f_c*b*abs(r_cl-r_e)*(((r_cl+r_e)/2)-R))+beta_e*E_s*A_s*((delta_l/d_s)+(delta_theta/d_s)*(r_s-R))*(r_s-R)+beta_p*f_y*A_s*(r_s-R);
N(Idx_1,Idx_2)=alpha_e*(E_c*b/d_c)*(delta_l*(r_e-r_b)+(delta_theta/2)*((r_e-R)^2-(r_b-R)^2))+alpha_p*(-F*f_c*b*abs(r_cl-r_e))+beta_e*E_s*A_s*((delta_l/d_s)+(delta_theta/d_s)*(r_s-R))+beta_p*f_y*A_s;
e=M./N;
end
if e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077 || e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077;
G=M;
end
end
plot(delta_thetavalue,G)
We cannot check how your function e works but have you tried wrapping your conditions in parenthesis so it understand that it need to check them in pair?
if (e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.077) || (e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.077);
also it could be easier to debug if you evaluate it once
evaluation = e(Idx_1,Idx_2); // you can print or anything to check the value before the if expression
if (evaluation >=0.072 && evaluation < 0.077) || (evaluation <=-0.072 && evaluation > -0.077);
Since you specifically ask for help for the last if statement, I can tell you there is nothing wrong with it. It appears your "e" function might have some bugs. Here is a dummy code snippet for you to play with the eVal which is a placeholder for your e(arg1, arg2) function. You should narrow this down and make sure where the error is by using the built in debugger.
clc;
eVal = 0.076;
G = zeros(3);
M = eye(3);
if eVal >= 0.072 && eVal < 0.077 || eVal <= -0.072 && eVal > -0.077;
G = M;
end
disp(G); % Should return eye(3) if the statement is true
I solved this problem like this :
t=0;
for Idx_1=1:1601 ;
for Idx_2=1:401;
if (e(Idx_1,Idx_2) >=0.072 && e(Idx_1,Idx_2) < 0.073) || (e(Idx_1,Idx_2) <=-0.072 && e(Idx_1,Idx_2) > -0.073);
t=t+1;
dt(t)=delta_thetavalue(Idx_1);
mf(t)=M(Idx_1,Idx_2);
end
end
end
plot(dt,mf)

Formatting of new line indentation of if conditions in Eclipse CDT

In Eclipse (4.4) CDT (8.5) on windows (7) the indentation of new lines within if and else if conditions does not make sense to me. Where in eclipse is this set?
I have tried looking in Window > Preferences > C/C++ > Code Style > Formatter but could not see any relevant setting.
An example where I hit the return key as I type the conditions or select the lines and press Ctrl+I ("Fix Indentation"). Note that the first four lines all have the same indentation despite differing levels of nesting. The following fifth line indents two more levels than the preceding line, as does the seventh line both with no apparent relation to the level of nesting.
else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >=2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
e = EXIT_FAILURE;
}
I would prefer something along the lines of the following:
else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >=2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
e = EXIT_FAILURE;
}
Is it possible to change this?
Also if someone could comment with an explanation of the current indentation/nesting formatting it would be appreciated!
EDIT: Realised this may be similar to Eclipse JDT Code Formatter: Indent parameter in IF-Statement
Is it possible to change this?
Yes. With the default indentation strategy (K&R built-in) I get the same as you with Ctrl+I, but if I do Ctrl+Shift+F (format code) I get the following.
} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write)
&& (qcn >= (CMD_Number_Bytes_max + 1)))) {
If you want to get column alignment, rather than simple indentation on continued lines, create your own formatting profile and change the following:
Indentation tab -> Tab policy to either Spaces only or Mixed
With the default of Tabs only the indents will be to the next tab stop
Line Wrapping tab -> Expressions/Binary Expressions set Indentation policy to Indent on column
This is what you end up with when you format (but not when you simply correct indentation):
} else if (((wp == SMB_Protocol_Write_Byte) && (qcn >= 1)
&& (rp != SMB_Protocol_Process_Call)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Write_Word) && (qcn >= 2)
&& (rp != SMB_Protocol_Block_WR))
|| ((wp == SMB_Protocol_Block_Write) && (qcn
>= (CMD_Number_Bytes_max + 1)))) {
Here are screenshots showing the settings:
and

How to use '||' in if-else statement in iphone appllication?

if((([txtFldO1.text length] == 0 ) || ([txtFldO2.text length] == 0 ) ||
([txtFldO3.text length] == 0 ) || ([txtFldO4.text length] == 0 ) ||
([txtFldO5.text length] == 0 )) && (([txtFldR1.text length]== 0) ||
([txtFldR2.text length]== 0) || ([txtFldR3.text length]== 0) || ([txtFldR4.text length]== 0) || ([txtFldR5.text length]== 0)))
{
//alert
//return
}
This statement does not work. It does access all of the propeties. Could anyone tell me why?
I need to access one txtFldO and one txtFldR.
To access properties, try SELF
self.txtFld00.text.length
It should be like this.
if([txtFldO1.text length] == 0 || [txtFldO2.text length] == 0 || [txtFldO3.text length] == 0 || [txtFldO4.text length] == 0){
// Code here
}
You don't need to put parentheses before condition. Just put one parentheses like above example.
Hope this help.

Comparison operators not functioning as expected

I have a set of checks to perform certain tasks.
// tempDouble is a (double), hour is an int
if (tempDouble > 60.0 && (hour >= 6 || hour <= 17)) { //CLEAR
NSLog(#"CLEAR");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLEAR
NSLog(#"NIGHT_CLEAR");
}
else if (tempDouble <= 60.0 && (hour >= 6 || hour <= 17)) { //CLOUDY
NSLog(#"CLOUDY");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLOUDY
NSLog(#"NIGHT_CLOUDY");
}
When I have a temp of 76.3 and an hour of 2, for example, I'd expect it to jump to NIGHT_CLEAR, but it actually goes to CLEAR. Did I set up my comparisons wrongly?
Thanks in advance for this simple question!
(hour >= 6 || hour <= 17)
is always true. All real numbers are either greater than or equal to 6 or less than or equal to 17 (some are both). I think you want:
(hour >= 6 && hour <= 17)
The same also applies to CLOUDY.
Some of your ||'s might be better off being &&'s.
Perhaps what you want is...
if (tempDouble > 60.0 && (hour >= 6 && hour <= 17)) { //CLEAR
NSLog(#"CLEAR");
}
else if (tempDouble > 60.0 && (hour < 6 && hour > 17)) { //NIGHT_CLEAR
NSLog(#"NIGHT_CLEAR");
}
else if (tempDouble <= 60.0 && (hour >= 6 || hour <= 17)) { //CLOUDY
NSLog(#"CLOUDY");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLOUDY
NSLog(#"NIGHT_CLOUDY");
}