language-extensions/python/src/PythonDataSet.cpp
//-------------------------------------------------------------------------------------------------
// Name: PythonInputDataSet::AddBooleanColumnToDictionary
//
// Description:
//  Adds a boolean column to the python dictionary that will be the DataFrame.
//

:AddBooleanColumnToDictionary
Description:
Adds a boolean column to the python dictionary that will be the DataFrame.
: AddBooleanColumnToDictionary Description : Adds a boolean column to the python dictionary that will be the DataFrame .
void PythonInputDataSet::AddBooleanColumnToDictionary(
	SQLSMALLINT columnNumber,
	SQLULEN     rowsNumber,
	SQLPOINTER  data,
	SQLINTEGER  *strLen_or_Ind)
{
	LOG("PythonInputDataSet::AddBooleanColumnToDictionary");
	string name = m_columns[columnNumber].get()->Name();
	np::dtype dt = np::dtype::get_builtin<bool>();
	bp::tuple stride = bp::make_tuple(sizeof(bool));
	bp::tuple shape = bp::make_tuple(rowsNumber);
	bp::object own;
	bool hasNulls = false;
	if (m_columns[columnNumber].get()->Nullable() == SQL_NULLABLE)
	{
		hasNulls = HasNulls(rowsNumber, strLen_or_Ind);
	}
	SQLCHAR* dataArray = static_cast<SQLCHAR *>(data);
	if (!hasNulls)
	{
		for (SQLULEN row = 0; row < rowsNumber; ++row)
		{
			dataArray[row] = PythonExtensionUtils::IsBitTrue(dataArray[row]);
		}
		np::ndarray npDataArray = np::from_data(dataArray, dt, shape, stride, own);
		m_dataDict[name] = npDataArray;
	}
	else
	{
		np::ndarray nArray = np::empty(shape, np::dtype(bp::object("O")));
		for (SQLULEN row = 0; row < rowsNumber; ++row)
		{
			if (strLen_or_Ind[row] == SQL_NULL_DATA)
			{
				nArray[row] = bp::object();
			}
			else
			{
				dataArray[row] = PythonExtensionUtils::IsBitTrue(dataArray[row]);
				nArray[row] = bool(dataArray[row]);
			}
		}
		m_dataDict[name] = nArray;
	}
}
void PythonInputDataSet :: AddBooleanColumnToDictionary ( SQLSMALLINT columnNumber , SQLULEN rowsNumber , SQLPOINTER data , SQLINTEGER * strLen_or_Ind ) { LOG ( " " ) ; string name = m_columns [ columnNumber ] . get ( ) -> Name ( ) ; np :: dtype dt = np :: dtype :: get_builtin < bool > ( ) ; bp :: tuple stride = bp :: make_tuple ( sizeof ( bool ) ) ; bp :: tuple shape = bp :: make_tuple ( rowsNumber ) ; bp :: object own ; bool hasNulls = false ; if ( m_columns [ columnNumber ] . get ( ) -> Nullable ( ) == SQL_NULLABLE ) { hasNulls = HasNulls ( rowsNumber , strLen_or_Ind ) ; } SQLCHAR * dataArray = static_cast < SQLCHAR * > ( data ) ; if ( ! hasNulls ) { for ( SQLULEN row = 0 ; row < rowsNumber ; ++ row ) { dataArray [ row ] = PythonExtensionUtils :: IsBitTrue ( dataArray [ row ] ) ; } np :: ndarray npDataArray = np :: from_data ( dataArray , dt , shape , stride , own ) ; m_dataDict [ name ] = npDataArray ; } else { np :: ndarray nArray = np :: empty ( shape , np :: dtype ( bp :: object ( " " ) ) ) ; for ( SQLULEN row = 0 ; row < rowsNumber ; ++ row ) { if ( strLen_or_Ind [ row ] == SQL_NULL_DATA ) { nArray [ row ] = bp :: object ( ) ; } else { dataArray [ row ] = PythonExtensionUtils :: IsBitTrue ( dataArray [ row ] ) ; nArray [ row ] = bool ( dataArray [ row ] ) ; } } m_dataDict [ name ] = nArray ; } }
Name: PythonInputDataSet::AddBooleanColumnToDictionary
Description:
Adds a boolean column to the python dictionary that will be the DataFrame.
Name : PythonInputDataSet :: AddBooleanColumnToDictionary Description : Adds a boolean column to the python dictionary that will be the DataFrame .
// Properties of the numpy array that will be populated in from_data below.
 // dt     - Numpy DataType
 // stride - size of each element of the array, so numpy knows how to separate elements
 // shape  - shape of the array as a tuple of sizes of each dimension (we only have 1 dimension)
 // own    - python object to hold a reference until it is transferred to the namespace
 //
 // Check if there are actually any nulls in this nullable column
 //
 // If there are no NULLs in the input data, then we can use a more memory efficient way
 // of constructing the numpy array.
 // If there ARE NULLs, then we need to create python objects for each value
 // and use None for the NULLs
 //
 // We modify the data array that is passed in by ExtHost in place so that
 // numpy can use that memory location for its data and not need to copy it out.
 //
 // Create a numpy array that points directly to the C++ data array.
 // This DOES NOT copy the data, so numpy will directly point to the data location.
 //
 // Use None for NULLs.
 //
