Facing problem with PX routines in DataSatge 8.1

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
ShashankDon
Participant
Posts: 9
Joined: Wed Mar 14, 2007 3:34 pm

Facing problem with PX routines in DataSatge 8.1

Post by ShashankDon »

Hi,

Recently we moved to DS8.1 from 7.5. I created few simple PX routines with type "External Function", using c++ programs. In one of them I'm passing 2 char* arguments and and this routine is returning char*. This routine is working exactly as expected.

Then I created a new one, where I'm passing 3 char* and 1 int arguments and return type is char*. Return value is concatenation of two strings one of them is usually Null.

This routine is not returning any thing to transformer stage. The c++ programs runs properly. When I add print statements in the program it prints the value to returned properly in Director logs. But nothing is returned in transformer. For concatenated strings, DS7.5 used make them Null if any of them is Null, so I tried with making both parts as not null, still I don't get any thing.

When I changed the return type from char* to char, and no change in c++ code, it returned some numeric values, not sure if they were ascii codes as they had some -ve values also.

Can some one help me with this and suggest what is going wrong here.
Thanks in advance.

Shashank
Shashank
siauchun84
Participant
Posts: 63
Joined: Mon Oct 20, 2008 12:01 am
Location: Malaysia

Post by siauchun84 »

Have you tried to use string instead of return char*? Do you mind to share how you concatinate it?
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

You said that you made both as non-nullable. That means that your new function is identical to your old one - except for the new input parameters. If that does not work, you need to start by removing them.

By changing the return type from char* to char, you are making the pointer of return value to be considered rather than the value it points.

You need to post the code for anyone to look into and respond.
ShashankDon
Participant
Posts: 9
Joined: Wed Mar 14, 2007 3:34 pm

Post by ShashankDon »

siauchun84 wrote:Have you tried to use string instead of return char*? Do you mind to share how you concatinate it?
Hi,

I don't see a "String" return type while defining the routine. Only char* is available to return string.

I used below code to concatenate.
Here Ans, Val and Message are objects of string class.

Ans.assign(Val.c_str());
Ans.append("~");
Ans.append(Message.c_str());

return (Ans.c_str());
Shashank
ShashankDon
Participant
Posts: 9
Joined: Wed Mar 14, 2007 3:34 pm

Post by ShashankDon »

Sainath.Srinivasan wrote:You said that you made both as non-nullable. That means that your new function is identical to your old one - except for the new input parameters. If that does not work, you need to start by removing ...
Hi Sainath,

as far as return type is concerned, there is no difference. Only the input and logic of function are different. I'm putting here the complete c++ programm.

Please let me know if could locate any issue.

The program is to validate the data against a particular datatype. Input is a string. We need to verify if the string is a valid number, date, Text, Year or Flag.

It receives four Input argument ColumnName, ColumnValue, DataType and Length.

It returns the string ColumnValue:'~':Err_Message.
ColumnValue is what is pased to function, if it matches with DataType. Error message is appropriate message.

Code: Select all

#include <stdlib.h> 
#include <stdio.h> 
#include <iostream>
using namespace std;

const char* DateCheck(const char*);

const char* TimeCheck(const char*, unsigned short);

const char* MHIMandatoryDataQualityCheck(char* ColumnName, char* ColumnValue, char* DataType, int Length) 
{ 
//char* ColumnName="DISCHARGE_DATE";
//char* ColumnValue="20322";
//char* DataType="YEAR";
//int   Length=20;

string ColName (ColumnName);
string DtType (DataType);

unsigned short casenum=0;

string Val (ColumnValue);
string Message ("NoError"), Ans;

if (ColumnName[0] == '\0')
{
cout << "Routine MHINonMandatoryDataQualityCheck not called properly. ColumnName is mandatory." << endl;
//exit(1);
}

if (DataType[0] == '\0')
{
DtType.assign("TEXT");
Length=50;

cout << DataType << endl;
cout << Length << endl;
}


if (DtType.compare("NUM") == 0)
casenum=1;
else
  if (DtType.compare("TEXT") == 0)
  casenum=2; 
  else
    if (DtType.compare("DATE") == 0)
    casenum=3;
    else
      if (DtType.compare("YEAR") == 0)
      casenum=4; 
      else
	 if (DtType.compare("FLAG") == 0)	
	 casenum=5;

switch (casenum)
{
case 1:
	if (ColumnValue[0] == '\0')
	{
	   Val.assign("\0");
	   Message.assign("Mandatory Error - ");
	   Message.append(ColumnName);
	   Message.append(" value is Null/Empty String.");
	}
	else
	{
	   if (Val.length()>Length)
	   {
	      Val.assign("\0");
	      Message.assign("Value Too Big Error - ");	
	      Message.append(ColumnName);
	      Message.append(" value is too big to be stored.");	
	   }
	   else
	   {  
	      if(strlen(ColumnValue) != strspn(ColumnValue,"-.0123456789"))
	      {
		  Val.assign("\0"); 	
		  Message.assign("Invalid Value Error - "); 
		  Message.append(ColumnName);
		  Message.append(" value is invalid.");
	      }
	   }
	}
break;

case 2:
	if (ColumnValue[0] == '\0')
	{
	   Val.assign("\0");
	   Message.assign("Mandatory Error - ");
	   Message.append(ColumnName);
	   Message.append(" value is Null/Empty String.");
	}
	else
	{
	   if (Val.length()>Length)
	   {
	      Val.assign(" ");
	      Message.assign("Value Too Big Error - ");	
	      Message.append(ColumnName);
	      Message.append(" value is too big to be stored.");	
	   }
	   else
	   {  
	      if(strlen(ColumnValue) == strspn(ColumnValue,"`~!@#$%^&*()_-+=[{]}\\|;:,<.>?/',"))
	      {
		  Val.assign("\0"); 	
		  Message.assign("Invalid Value Error - "); 
		  Message.append(ColumnName);
		  Message.append(" value is invalid.");
	      }
	   }
	}	
break;

case 3:
	if (ColumnValue[0] == '\0')
	{
	   Val.assign("\0");
	   Message.assign("Mandatory Error - ");
	   Message.append(ColumnName);
	   Message.append(" value is Null/Empty String.");
	}
	else
	{
	   int i=0,SpaceCount=0,SlashCount=0,ColonCount=0;
	   
	   for(i=0;i<strlen(ColumnValue);i++)
	   {
	      if (ColumnValue[i] == ' ' )
	      SpaceCount++;		      

	      if (ColumnValue[i] == '/' )
	      SlashCount++;		      

	      if (ColumnValue[i] == ':' )
	      ColonCount++;		      
	   }	
	   cout << "Space Count: " << SpaceCount << endl; 
	   cout << "Slash Count: " << SlashCount << endl;
	   cout << "Colon Count: " << ColonCount << endl;
	   cout << "Date1: " << Val.c_str() << endl;

	   if (SlashCount!=2)
	   {
   	     Val.assign("\0"); 	
	     Message.assign("Format Mismatch Error - "); 
	     Message.append(ColumnName);
	     Message.append(" value is invalid or not in desired format.");
	   }
	   else
	   {					
	   switch (SpaceCount)
	   {
	    case 0:
			  cout << "Date2: " << Val.c_str() << endl;
			  Val.assign(DateCheck(ColumnValue));
			  cout << "Date3: " << Val.c_str() << endl;
			  if (Val.compare("Error") == 0)
			  {
 			    Val.assign("\0"); 	
		  	    Message.assign("Format Mismatch Error - "); 
		  	    Message.append(ColumnName);
		  	    Message.append(" value is invalid or not in desired format.");				
			  }	
	    break;

	    case 1:
			if (ColonCount < 1 || ColonCount > 2)
			{
 			  Val.assign("\0"); 	
		  	  Message.assign("Format Mismatch Error - "); 
		  	  Message.append(ColumnName);
		  	  Message.append(" value is invalid or not in desired format.");
			}
			else
			{
			   string Date;
			   string Time;	
			   unsigned short SpacePos;	
                        
			   SpacePos=Val.find(" ");
                        
			   Date.assign(DateCheck(Val.substr(0,SpacePos).c_str())); 	
			   cout << "Date : " << Date.c_str() << endl; 

			   Time.assign(TimeCheck(Val.substr(SpacePos+1).c_str(),ColonCount)); 	
			   cout << "Time : " << Time.c_str() << endl; 

			   if(Date.compare("Error") == 0 || Time.compare("Error") == 0)
			   {
 			     Val.assign("\0"); 	
		  	     Message.assign("Format Mismatch Error - "); 
		  	     Message.append(ColumnName);
		  	     Message.append(" value is invalid or not in desired format.");
			   }	
			   else
			   {	
			     Val.assign(Date.c_str());
			     Val.append(" ");
			     Val.append(Time.c_str());
			   }	
			} 
	    break;

	    default:
			Val.assign("\0"); 	
		  	Message.assign("Format Mismatch Error - "); 
		  	Message.append(ColumnName);
		  	Message.append(" value is invalid or not in desired format.");
	   }
	   }
	}

break;

case 4:
	if (ColumnValue[0] == '\0')
	{
	   Val.assign("\0");
	   Message.assign("Mandatory Error - ");
	   Message.append(ColumnName);
	   Message.append(" value is Null/Empty String.");
	}
	else
	{
	   if(Val.length()!=4 || atoi(Val.c_str())<1000 || atoi(Val.c_str())>9999)
	   {
	      Val.assign("\0");
	      Message.assign("Invalid Value Error - ");  
	      Message.append(ColumnName);
	      Message.append(" value is invalid.");
	   }
	}
break;

case 5:
	if (ColumnValue[0] == '\0')
	{
	   Val.assign("\0");
	   Message.assign("Mandatory Error - ");
	   Message.append(ColumnName);
	   Message.append(" value is Null/Empty String.");
	}
	else	
	{
          if(Val.compare("Y")!=0 && Val.compare("y")!=0 && Val.compare("N")!=0 && Val.compare("n")!=0)
	   {
	      Val.assign("\0");
	      Message.assign("Invalid Value Error - ");  
	      Message.append(ColumnName);
	      Message.append(" value is invalid.");	
	   }
	   else
	   {
	      if(Val.compare("Y")==0 || Val.compare("y")==0)
	        Val.assign("Y");
	      else
	        Val.assign("N");
	   }
	}
break;

default:
cout << "Invalid Data Type" << endl;
cout << DataType << endl;
//exit(0);

}

//cout << Val.data() << endl;
//cout << Message.data() << endl;

Ans.assign(Val.c_str());
Ans.append("~");
Ans.append(Message.c_str());

cout << "Final Date: "<< Ans.c_str() << endl;
return (Ans.c_str());

}

const char* DateCheck(const char* Date)
{
   string Ans;
   string Val(Date);
 	
			if (strlen(Date)<6 || strlen(Date)>10)
			{
 			  Ans.assign("Error");
			}
			else
			{
			  unsigned short F_Slash_Pos=0,L_Slash_Pos=0;
			  unsigned short Monthlen[]={31,29,31,30,31,30,31,31,30,31,30,31};
			  string Month,MonthNew;
			  string Day,DayNew;
			  string Year,YearNew;
	
			  F_Slash_Pos=Val.find("/");
			  L_Slash_Pos=Val.rfind("/");

			  Month=Val.substr(0,F_Slash_Pos);
			  Day=Val.substr(F_Slash_Pos+1,L_Slash_Pos-F_Slash_Pos-1);
			  Year=Val.substr(L_Slash_Pos+1);

			  if ( atoi(Year.c_str()) <= 0 || atoi(Year.c_str()) > 9999 || atoi(Month.c_str()) <= 0 || atoi(Month.c_str()) > 12 || atoi(Day.c_str()) <= 0 || atoi(Day.c_str()) > Monthlen[atoi(Month.c_str())])
			  { 
 			    Ans.assign("Error");
			  }
			  else
			  { 
			    if (Month.length() == 1)
			    {	
                            MonthNew.assign("0");  
                            MonthNew.append(Month.c_str());
			    }
			    else
			    {	
				MonthNew=Month;	
			    }
	
			    if (Day.length() == 1)
			    {
                            DayNew.assign("0");  
                            DayNew.append(Day.c_str());
			    }	
			    else
			    {	
				DayNew=Day;	
			    }

			    if (Year.length() == 1)
			    {
                            YearNew.assign("000");  
                            YearNew.append(Year.c_str());
			    }	
			    else	
 			      if (Year.length() == 2)
			      {
                              YearNew.assign("00");  
                              YearNew.append(Year.c_str());
			      }	
			      else	
 			        if (Year.length() == 3)
				 {
                                YearNew.assign("0");  
                                YearNew.append(Year.c_str());
				 }
			        else
			        {	
				    YearNew=Year;	
			        }
			    
			    Ans.assign(MonthNew.c_str());
			    Ans.append("/");			
			    Ans.append(DayNew.c_str());			
			    Ans.append("/");			
			    Ans.append(YearNew.c_str());		
cout << "Function Date: "<< Ans.c_str() << endl;	
			  }		
			} 

return (Ans.c_str());

}


const char* TimeCheck(const char* Time, unsigned short ColonCount)
{
   string Ans;
   string Val(Time);
 	
			if (strlen(Time)<3 || strlen(Time)>8)
			{
 			  Ans.assign("Error");
			}
			else
			{
			  unsigned short F_Colon_Pos=0,L_Colon_Pos=0;

			  string Hour,HourNew;
			  string Min,MinNew;
			  string Sec,SecNew;
	
			  F_Colon_Pos=Val.find(":");

   		         Hour=Val.substr(0,F_Colon_Pos);

			  if(ColonCount == 1)
			  {
			    Min=Val.substr(F_Colon_Pos+1);

			    if((atoi(Hour.c_str())<=0 && Hour.compare("0")!=0 && Hour.compare("00")!=0) || atoi(Hour.c_str())>24)
			    {
				Ans.assign("Error");
			    }			
			    else
			    {
				if((atoi(Min.c_str())<=0 && Min.compare("0")!=0 && Min.compare("00")!=0) || atoi(Min.c_str())>59)
				{
				   Ans.assign("Error");
				}
				else
				{
				  if(Hour.length() == 1)
				  {
				     HourNew.assign("0");	
				     HourNew.append(Hour.c_str());
				  }
				  else
				  {
				     HourNew=Hour;
				  }

				  if(Min.length() == 1)
				  {
				     MinNew.assign("0");	
				     MinNew.append(Min.c_str());
				  }	 
				  else
				  {
				     MinNew=Min;
				  }
			
				  Ans.assign(HourNew.c_str());
				  Ans.append(":");
				  Ans.append(MinNew.c_str());	
				  Ans.append(":00");
				}		
			    }			
			  }	
			  else
			  {		
			    L_Colon_Pos=Val.rfind(":");

			    Min=Val.substr(F_Colon_Pos+1,L_Colon_Pos-F_Colon_Pos-1);
			    Sec=Val.substr(L_Colon_Pos+1);	

			    if((atoi(Hour.c_str())<=0 && Hour.compare("0")!=0 && Hour.compare("00")!=0) || atoi(Hour.c_str())>24)
			    {
				Ans.assign("Error");
			    }			
			    else
			    {
				if((atoi(Min.c_str())<=0 && Min.compare("0")!=0 && Min.compare("00")!=0) || atoi(Min.c_str())>59)
				{
				   Ans.assign("Error");
				}
				else
				{
				   if((atoi(Sec.c_str())<=0 && Sec.compare("0")!=0 && Sec.compare("00")!=0) || atoi(Sec.c_str())>59)
				   {
				      Ans.assign("Error");
				   }
				   else
				   {
  				      if(Hour.length() == 1)
				      {
				         HourNew.assign("0");	
				         HourNew.append(Hour.c_str());
				      }
 				      else
				      {
				         HourNew=Hour;
				      }

				      if(Min.length() == 1)
				      {
				         MinNew.assign("0");	
				         MinNew.append(Min.c_str());
				      }	 
 				      else
				      {
				         MinNew=Min;
				      }

				      if(Sec.length() == 1)
				      {
				         SecNew.assign("0");	
				         SecNew.append(Sec.c_str());
				      }
 				      else
				      {
				         SecNew=Sec;
				      }
			
				      Ans.assign(HourNew.c_str());
				      Ans.append(":");
				      Ans.append(MinNew.c_str());
				      Ans.append(":");
				      Ans.append(SecNew.c_str());
				   }		
				}
			    }	
			  }	


			} 

return (Ans.c_str());

}
Shashank
Post Reply