16.20 - RuntimeContract Methods - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Release Date
April 2020
Content Type
Programming Reference
Publication ID
B035-1147-162K
Language
English (United States)

bytesTransferred

public void  bytesTransferred(long  in, long  out)
   throws SQLException
Parameter Name Definition
in The number of bytes transferred into Teradata Database from the foreign server.
out The number of bytes transferred from Teradata Database to the foreign server.

Records the number of bytes transferred between Teradata Database and the foreign server by the table operator. This routine is callable on an AMP vproc only by a table operator.

complete

public void  complete()

Finalize the contract with the Teradata parser.

disableCoGroup

public void  disableCoGroup()

Disables the cogroup functionality for table operators that handle multiple input streams.

Example usage:

public int contract(RuntimeContract contract, ResultSet rsin[], ResultSet rsout[])
throws SQLException

{
   DbsInfo.traceWrite("MITblOpContract");
   contract.disableCoGroup();
      ...
}
If cogroup is disabled, table operators that handle multiple input streams may return different results on systems with different configurations where the number of AMPs differ. To get consistent results on different configurations, cogroup must be enabled.

getAmpHash

public void  getAmpHash(int[][]  amphash)
Parameter Name Definition
amphash amphash[n][0] is the AMP number.

amphash[n][1] will be returned with the value that hashes to the AMP.

n is the size of the amphash array.

Returns values that hash to the specified AMPs. This routine is callable on an AMP or PE vproc.

getBaseInfo

public UDTBaseInfo[]  getBaseInfo(ColumnDefinition[]  colDefs)
   throws SQLException
Parameter Name Definition
colDefs A list of column definitions.

Retrieves metadata information about a UDT or CDT column.

The method returns an array of UDTBaseInfo objects for the ColumnDefinition objects passed in. This method can only be invoked in the contract function.

getContractCtx

public byte[]  getContractCtx()

Provides access to the contract bytes in the AMP. This method is used with setContracCtx(byte []).

byte[] returns the object from setContracCtx(Object) from the Teradata parser to the operator in the AMP.

getContractObject

public java.lang.Object  getContractObject()
   throws java.io.IOException,
          java.lang.ClassNotFoundException

Provides access to the serialized contract object in the AMP. This method is used with setContracCtx(Object).

Object returns the object from setContracCtx(Object) from the Teradata parser to the operator in the AMP.

getContractPhase

public ContractPhase  getContractPhase();
Indicates the phase in the parser from which the contract function is being called. The parser phases are as follows:
  • FNC_CTRCT_GET_ALLCOLS_PHASE = 0

    Indicates that all columns for the remote table should be returned.

  • FNC_CTRCT_VALIDATE_PHASE = 1

    Validates that the given inputs are correct. The contract function can be called multiple times from this phase.

    This phase is currently not used.
  • FNC_CTRCT_COMPLETE_PHASE = 2

    Indicates that this is the last call of the contract function and any foreign server actions that need to be done should be completed.

  • FNC_CTRCT_DDL_PHASE = 3

    Indicates that execution of the CREATE SERVER statement is being completed and the connectivity should be verified.

  • FNC_CTRCT_DEFINE_SERVER_PHASE = 4

    Indicates that a CREATE VIEW or CREATE MACRO statement is being executed and that the custom clause data may not be valid.

This routine is callable on a PE vproc only by a table operator.

getDBSSessionAttrInfo

String getDBSSessionAttrInfo();
Returns information about the current session to a table operator. getDBSSessionAttrInfo returns a string in JSON format containing current session information, such as system variables and session attributes, including the following:
  • Current user name
  • Current role name
  • Transaction mode
  • Collation

For current role name, a maximum of 127 role names are returned. If a user is granted more than 127 roles and the user issues a SET ROLE ALL statement to enable all of the roles, "ALL" is returned for the current role name. For example, the following sample output shows the session information returned for TESTUSER who has more than 127 roles enabled:

{"CurUserName":"TESTUSER","CurRoleName":"ALL","TransactionMode":"BTET","Collation":"ASCII"}

For users who specify SET ROLE ALL but have 127 roles or less, "ALL" is returned along with each of the role names. For example, TESTUSER2 has 3 roles: Role1, Role2, and Role3.

{"CurUserName":"TESTUSER2","CurRoleName":["ALL","Role1","Role2","Role3"],"TransactionMode":"BTET","Collation":"ASCII"}

getExternalQuery

public String  getExternalQuery(
   ColumnDefinition[]  colDefs,
   ServerType          serverType,
   ExtOpSetType[]      extOpSetTypes,
   int[]               interfaceVersions)
      throws SQLException
Parameter Name Definition
colDefs A list of column definitions that may occur in a WHERE clause by the foreign server.
serverType ServerType is defined as follows:
public enum ServerType {
   ANSISQL(0),
   HADOOP(1);
}
extOpSetTypes A set of valid operators supported on the foreign server.
ExOpSetType is defined as follows:
public enum ExtOpSetType {
   Eq_ET(0),
   Ne_ET(1),
   Gt_ET(2),
   Le_ET(3),
   Lt_ET(4),
   And_ET(5),
   Or_ET(6),
   Not_ET(7),
   Between_ET(8),
   In_ET(9),
   NotIn_ET(10),
   Ge_ET(11),
   Like_ET(12),
   LastOp_ET(13);
}
interfaceVersions
  • The caller passes in the desired interface version as the argument.
  • The routine returns the actual interface version that is currently supported.

Generates the text query string for the foreign server and returns the interface version that is currently supported. The method returns a string which contains the external query.

This routine is callable on a PE vproc only by a table operator.

Example: Calling getExternalQuery

ServerType sType = ServerType.ANSISQL;
ExtOpSetType extOpTypes[] = new ExtOpSetType[3];
extOpTypes[0] = ExtOpSetType.Eq_ET;
extOpTypes[1] = ExtOpSetType.And_ET;
extOpTypes[2] = ExtOpSetType.Or_ET;

int[] versions = new int[2];
versions[0] = 1;    // The caller passes in the desired interface version.

String extQuery = contract.getExternalQuery(colDefs, sType, extOpTypes, versions);

After calling getExternalQuery, versions[1] will contain the actual interface version that is currently supported on the system.

getHashAmp

public int  getHashAmp(Object[]  data)
Parameter Name Definition
data An array representing table columns.

Accepts data and determines the AMP which would be responsible for that key. The method returns an integer representing the number of the AMP that would be responsible for the key. This routine is callable on a PE vproc only by a table operator.

getInnerContractCtx

public byte[]  getInnerContractCtx()
   throws SQLException

Gets the contract definition of a nested inner table operator for the outer table operator to use. NULL is returned if the inner contract function does not exist. This routine is callable on a PE vproc only by a table operator.

getInputInfo

public InputInfo  getInputInfo()

Provides access to the InputInfo container to get metadata about the execution of the table operator.

getUniqTblOpID

public int  getUniqTblOpID()

Returns the unique identifier associated with a table operator.

When multiple table operators are present in a single query, as in a map reduce operation such as SELECT * FROM operator (on (select * from operator), you can use the unique table identifier to distinguish between each of the table operators.

setActivityCount

public void  setActivityCount(int  stream, long  rowsexported)
   throws SQLException
Parameter Name Definition
stream Specifies which stream to write to.
rowsexported The value to be written to ActivityCount.

Sets the number of rows exported. This routine is callable on an AMP vproc only by a table operator

setContractCtx

public void  setContractCtx(byte[]ctx)
   throws java.sql.SQLException
Parameter Name Definition
ctx The data to be passed to the AMPs.

Defines a byte [] to be passed from the Teradata parser to the AMPs.

setContractCtx

public void  setContractCtx(java.lang.Object  Obj)
   throws java.io.IOException
Parameter Name Definition
Obj The contract object.

Serializes and sets the contract object. Class object must support writeObject().

setDisplayLength

public void  setDisplayLength(char  direction,
                             ColumnDefinition[]  colDefs)
   throws SQLException
Parameter Name Definition
char direction IN parameter

Specify an input value of 'R' for export and 'W' for import.

ColumnDefinition[] colDefs IN/OUT parameter

The column definitions for which the display lengths will be reset.

Resets the lengths in column definitions for VARCHAR data types. This routine can be invoked for both import and export operations.

The routine is callable on a PE vproc only by a table operator.

setExplainText

public void  setExplainText(String[]  texts);
Parameter Name Definition
texts An array containing the EXPLAIN text strings.

Sets the EXPLAIN text when the table operator has the hexplain custom clause set.

Hexplain has the following values for the type of EXPLAIN to be completed:
  • 1 = simple
  • 2 = verbose
  • 3 = DBQL

This routine accepts multiple self-contained EXPLAIN text strings as input in order to handle a multi-row EXPLAIN plan from a foreign server. The routine provides the EXPLAIN plan to the parser which will display the multiple lines of the EXPLAIN plan.

This routine is callable on a PE vproc only by a table operator.

setFormat

public void  setFormat(
   int  stream,
   InputInfo.StreamDir  dir,
   java.util.Map<StreamFormat.FormatAttribute,java.lang.Object>  formatattributes)
Parameter Name Definition
stream IN parameter

Indicates the stream on which the format will be applied. Currently the only valid value is 0.

dir IN parameter

The direction of the stream (input or output).

formatattributes IN parameter
Map of attribute values to apply. Valid attributes are as follows:
  • "RECFMT"
  • "TZTYPE"
  • "CHARSETFMT"
  • "REPUNSPTCHR"

"CHARSETFMT" and "REPUNSPTCHR" apply only to import table operators.

Sets attributes of the format of the input and output streams. This allows the contract function to specify the format of the data types to the parser.

Usage Notes
  • This routine is valid only when called within the contract function of a table operator.
  • For "RECFMT" the default value is INDICFMT1, where the format is IndicData with row separator sentinels. All field-level formats impact the entire record.
  • If data being imported from a foreign server contains characters unsupported by Teradata Database, you must use setFormat() and explicitly set "CHARSETFMT" and "REPUNSPTCHR" attributes.

Format Attribute Values

Parameter Name Definition
"RECFMT" Defines the record format:
  • INDICFMT1 = 1

    IndicData with row separator sentinels.

  • INDICBUFFMT1 = 2

    IndicData with NO row or partition separator sentinels.

"TZTYPE" Used as an indicator to Teradata Database to receive from or send TIME/TIMESTAMP data to the table operator in a different format.
  • RAW = 0 as stored on the Teradata Database file system
  • UTC = 1 as UTC
"CHARSETFMT"
  • EVLDBC

    Signals that neither data conversion nor detection is needed.

  • EVLUTF16CHARSET

    Signals that the external data to be imported into Teradata Database are in UTF16 encoding.

  • EVLUTF8CHARSET

    Signals that the external data to be imported into Teradata Database are in UTF8 encoding.

"REPUNSPTCHR" A boolean value that specifies what to do when an unsupported unicode character is detected in the external data to be imported into Teradata Database.
  • true

    Replaces the unsupported character with U+FFFD.

  • false

    Return an error when an unsupported character is detected. This is the default behavior.

Importing and Exporting TIME/TIMESTAMP Data

You can map the Teradata Database TIME and TIMESTAMP data types to the Hadoop STRING or the Oracle TIMESTAMP data type when importing or exporting data to these foreign servers.

The table operator can use setFormat() to set the tztype attribute as an indicator to Teradata Database to receive from or send TIMESTAMP data to the table operator in a native but adjusted format.

The tztype attribute is set as follows for the import and export operators:
  • For Hadoop, the attribute is set to UTC.
  • For Oracle, the attribute is set to UTC.

If the transform is off, the data will be transferred in Raw form which is the default for table operators and is consistent with standard UDFs.

tztype is a member of the structure FNC_FmtConfig_t defined in fnctypes.h as follows:
typedef struct
{
   int Stream_Fmt_en recordfmt; //enum - indicdata, fastload binary, delimited
   bool inlinelob; //inline or deferred
   bool UDTTransformsOff; //true or false
   bool PDTTransformsOff; //true or false
   bool ArrayTransformsOff; //true or false
   char auxinfo[128]; //For delimited text can contain the record separator, delimiter
                         //specification and the field enclosure characters
   double inperc; //recommended percentage of buffer devoted to input rows
   bool inputnames; //send input column names to step
   bool outputnames; //send output column names to step
   TZType_en tztype; //enum - Raw or UTC
   int charsetfmt; // charset format of data to be imported into TD through QG
   bool replUnsprtedUniChar; /* true - replace unsupported unicode character
                                encountered with U+FFFD when data is imported
                                into TD through QG
                                false - error out when unsupported unicode
                                char encountered */
} FNC_FmtConfig_t;
TZType_en is defined as follows:
typedef enum
{
   Raw = 0, /* as stored on TD File system */
   UTC = 1, /* as UTC */
} TZType_en;

For export, setInputInfo() is called during the contract phase in the resolver and will use the tztype attribute to add the desired cast to the input TIME or TIMESTAMP column types.

Teradata Database converts the TIME and TIMESTAMP data to the session local time before casting to the character type, so when a TIME or TIMESTAMP column is being mapped to charfix/charvar as when mapping to the Hadoop STRING type, the data will transmit in session local time zone and no explicit casts are needed.

For import, when getting the input buffer from the table operator, TIME or TIMESTAMP data have to be converted to Raw form. There is no conversion needed for the import of Hadoop Strings to Teradata Database TIME or TIMESTAMP data types since it follows the normal conversion path from character to TIME/TIMESTAMP in Teradata Database.

Teradata does not recommend importing or exporting TIME/TIMESTAMP data for a Teradata Database system with timedatewzcontrol flag 57 = 0. For such systems, the TIME/TIMESTAMP data is stored in OS local time. The System/Session time zone is not set and Teradata Database does not apply any conversions on TIME/TIMESTAMP data when reading or writing from disk. Therefore, exporting such data reliably in the format desired by the foreign server is a problem and Teradata recommends that the Teradata-to-Hadoop connector feature not be used for such systems.

setHashBy

public void  setHashBy(int  streamno,
                      String[]  colNames)
   throws SQLException
Parameter Name Definition
streamno The input stream number.
colNames A pointer to the HASH BY metadata.

Allows the contract function writer to set the HASH BY specification when developing table operators.

This routine can only run if called from the contract function. It is callable on a PE vproc.

The routine will produce an error if the stream number is invalid or the HASH BY metadata was already set.

setInputInfo

public void  setInputInfo(int  streamNo,
                         ColumnDefinition[]  colDefs)
   throws SQLException
Parameter Name Definition
streamno The input stream number.
colDefs A list of column definitions.

Sets casting statements on the input columns so that the data types are cast as indicated by the caller.

The routine is callable on a PE vproc only by a table operator.

setOrderBy

public void  setOrderBy(int  streamno,
                       String[]  colNames)
   throws SQLException
Parameter Name Definition
streamno The input stream number.
colNames A pointer to the LOCAL ORDER BY metadata.

Allows the contract function writer to set the ordering specification when developing table operators.

This routine can only run if called from the contract function. It is callable on a PE vproc.

The routine will produce an error if the stream number is invalid or the LOCAL ORDER BY metadata was already set.

setOutputInfo

public void  setOutputInfo(int  streamno,
   ColumnDefinition[]  info)
   throws java.sql.SQLException
Parameter Name Definition
streamno Indicates the stream to which the column definitions will be applied. Currently the only valid value is 0.
info The defined columns.

Defines the output information for the stream.

Inherited Methods

The following methods are inherited from class java.lang.Object:
  • equals
  • getClass
  • hashCode
  • notify
  • notifyAll
  • toString
  • wait, wait, wait