com.amazon.redshift.core.v3

## Class QueryExecutorImpl

- java.lang.Object
- - com.amazon.redshift.core.QueryExecutorBase
  - - com.amazon.redshift.core.v3.QueryExecutorImpl

All Implemented Interfaces:
  QueryExecutor, TypeTransferModeRegistry

    public class QueryExecutorImpl
    extends QueryExecutorBase

QueryExecutor implementation for the V3 protocol.

### Field Summary

### Fields inherited from class com.amazon.redshift.core.QueryExecutorBase

`datashareEnabled, enableMultiSqlSupport, enableStatementCache, logger, logServerErrorDetail, pgStream, serverProtocolVersion`

### Fields inherited from interface com.amazon.redshift.core.QueryExecutor

`MAX_SAVE_POINTS, QUERY_BOTH_ROWS_AND_STATUS, QUERY_DESCRIBE_ONLY, QUERY_DISALLOW_BATCHING, QUERY_EXECUTE_AS_SIMPLE, QUERY_FORCE_DESCRIBE_PORTAL, QUERY_FORWARD_CURSOR, QUERY_NO_BINARY_TRANSFER, QUERY_NO_METADATA, QUERY_NO_RESULTS, QUERY_ONESHOT, QUERY_READ_ONLY_HINT, QUERY_SUPPRESS_BEGIN`

### Constructor Summary

Constructors
Constructor and Description
`QueryExecutorImpl(RedshiftStream pgStream, String user, String database, int cancelSignalTimeout, Properties info, RedshiftLogger logger)`

### Method Summary

Methods
`void` `cancelCopy(CopyOperationImpl op)` Finishes a copy operation and unlocks connection discarding any exchanged data.
`void` `closeRingBufferThread(RedshiftRowsBlockingQueue<Tuple> queueRows, Thread ringBufferThread)` Close the last active ring buffer thread.
`void` `closeStatementAndPortal()` Close the statement and portal when statement get closed.
`ParameterList` `createFastpathParameters(int count)` Create a new ParameterList implementation suitable for invoking a fastpath function via `QueryExecutor.fastpathCall(int, com.amazon.redshift.core.ParameterList, boolean)`.
`Query` `createSimpleQuery(String sql)` Create an unparameterized Query object suitable for execution by this QueryExecutor.
`void` `doSubprotocolBegin()`
`long` `endCopy(CopyOperationImpl op)` Finishes writing to copy and unlocks connection.
`void` `execute(Query[] queries, ParameterList[] parameterLists, BatchResultHandler batchHandler, int maxRows, int fetchSize, int flags)` Execute several Query, passing results to a provided ResultHandler.
`void` `execute(Query query, ParameterList parameters, ResultHandler handler, int maxRows, int fetchSize, int flags)` Execute a Query, passing results to a provided ResultHandler.
`byte[]` `fastpathCall(int fnid, ParameterList parameters, boolean suppressBegin)` Invoke a backend function via the fastpath interface.
`void` `fetch(ResultCursor cursor, ResultHandler handler, int fetchSize, int initRowCount)` Fetch additional rows from a cursor.
`void` `flushCopy(CopyOperationImpl op)`
`String` `getApplicationName()` Returns application_name connection property.
`boolean` `getIntegerDateTimes()` Returns true if server uses integer instead of double for binary date and time encodings.
`int` `getProtocolVersion()`
`ReplicationProtocol` `getReplicationProtocol()`
`TimeZone` `getTimeZone()` Returns backend timezone in java format.
`boolean` `isRingBufferThreadRunning()` Check for a running ring buffer thread.
`void` `processNotifies()` Prior to attempting to retrieve notifications, we need to pull any recently received notifications off of the network buffers.
`void` `processNotifies(int timeoutMillis)` Prior to attempting to retrieve notifications, we need to pull any recently received notifications off of the network buffers.
`protected void` `processResults(ResultHandler handler, int flags, int fetchSize, boolean subQueries, int maxRows)`
`protected void` `processResults(ResultHandler handler, int flags, int fetchSize, boolean subQueries, int initRowCount, int maxRows)`
`protected void` `processSyncOnClose()`
`void` `readStartupMessages()`
`void` `receiveParameterStatus()`
`protected void` `sendCloseMessage()`
`void` `sendQueryCancel()` Sends a query cancellation for this connection.
`void` `setApplicationName(String applicationName)`
`void` `setBinaryReceiveOids(Set<Integer> oids)` Sets the oids that should be received using binary encoding.
`void` `setBinarySendOids(Set<Integer> oids)` Sets the oids that should be sent using binary encoding.
`void` `setTimeZone(TimeZone timeZone)`
`CopyOperation` `startCopy(String sql, boolean suppressBegin)` Sends given query to BE to start, initialize and lock connection for a CopyOperation.
`boolean` `useBinaryForReceive(int oid)` Returns if given oid should be received in binary format.
`boolean` `useBinaryForSend(int oid)` Returns if given oid should be sent in binary format.
`void` `waitForRingBufferThreadToFinish(boolean calledFromConnectionClose, boolean calledFromResultsetClose, RedshiftRowsBlockingQueue<Tuple> queueRows, Thread ringBufferThread)` Wait for ring buffer thread to finish.
`Query` `wrap(List<NativeQuery> queries)` Wrap given native query into a ready for execution format.
`void` `writeToCopy(CopyOperationImpl op, byte[] data, int off, int siz)` Sends data during a live COPY IN operation.
`void` `writeToCopy(CopyOperationImpl op, ByteStreamWriter from)` Sends data during a live COPY IN operation.

### Methods inherited from class com.amazon.redshift.core.QueryExecutorBase

`abort, addNotification, addWarning, borrowCallableQuery, borrowQuery, borrowQueryByKey, borrowReturningQuery, close, createQuery, createQueryByKey, createQueryKey, getAutoSave, getBackendPID, getDatabase, getEncoding, getEscapeSyntaxCallMode, getHostSpec, getNetworkTimeout, getNotifications, getParameterStatus, getParameterStatuses, getPreferQueryMode, getServerProtocolVersion, getServerVersion, getServerVersionNum, getStandardConformingStrings, getTransactionState, getUser, getWarnings, hasNotifications, isClosed, isColumnSanitiserDisabled, isDatashareEnabled, isFlushCacheOnDeallocate, isMultiSqlSupport, isRaiseExceptionOnSilentRollback, isReWriteBatchedInsertsEnabled, onParameterStatus, releaseQuery, setAutoSave, setBackendKeyData, setDatashareEnabled, setEncoding, setFlushCacheOnDeallocate, setNetworkTimeout, setRaiseExceptionOnSilentRollback, setServerProtocolVersion, setServerVersion, setServerVersionNum, setStandardConformingStrings, setTransactionState, willHealOnRetry, willHealViaReparse`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### QueryExecutorImpl

    public QueryExecutorImpl(RedshiftStream pgStream,
                     String user,
                     String database,
                     int cancelSignalTimeout,
                     Properties info,
                     RedshiftLogger logger)
                      throws SQLException,
                             IOException

Throws:
  `SQLException`
  `IOException`

### Method Detail

#### getProtocolVersion

    public int getProtocolVersion()

Returns:
  the version of the implementation

#### createSimpleQuery

    public Query createSimpleQuery(String sql)
                            throws SQLException

Description copied from interface: `QueryExecutor`
Create an unparameterized Query object suitable for execution by this QueryExecutor. The provided query string is not parsed for parameter placeholders ('?' characters), and the `Query.createParameterList()` of the returned object will always return an empty ParameterList.

Parameters:
  `sql` - the SQL for the query to create
Returns:
  a new Query object
Throws:
  `SQLException` - if something goes wrong

#### wrap

    public Query wrap(List<NativeQuery> queries)

Description copied from interface: `QueryExecutor`
Wrap given native query into a ready for execution format.

Parameters:
  `queries` - list of queries in native to database syntax
Returns:
  query object ready for execution by this query executor

#### execute

    public void execute(Query query,
               ParameterList parameters,
               ResultHandler handler,
               int maxRows,
               int fetchSize,
               int flags)
                 throws SQLException

Description copied from interface: `QueryExecutor`
Execute a Query, passing results to a provided ResultHandler.

Parameters:
  `query` - the query to execute; must be a query returned from calling `QueryExecutor.wrap(List)` on this QueryExecutor object.
  `parameters` - the parameters for the query. Must be non-`null` if the query takes parameters. Must be a parameter object returned by `Query.createParameterList()`.
  `handler` - a ResultHandler responsible for handling results generated by this query
  `maxRows` - the maximum number of rows to retrieve
  `fetchSize` - if QUERY_FORWARD_CURSOR is set, the preferred number of rows to retrieve before suspending
  `flags` - a combination of QUERY_* flags indicating how to handle the query.
Throws:
  `SQLException` - if query execution fails

#### execute

    public void execute(Query[] queries,
               ParameterList[] parameterLists,
               BatchResultHandler batchHandler,
               int maxRows,
               int fetchSize,
               int flags)
                 throws SQLException

Description copied from interface: `QueryExecutor`
Execute several Query, passing results to a provided ResultHandler.

Parameters:
  `queries` - the queries to execute; each must be a query returned from calling `QueryExecutor.wrap(List)` on this QueryExecutor object.
  `parameterLists` - the parameter lists for the queries. The parameter lists correspond 1:1 to the queries passed in the `queries` array. Each must be non- `null` if the corresponding query takes parameters, and must be a parameter object returned by `Query.createParameterList()` created by the corresponding query.
  `batchHandler` - a ResultHandler responsible for handling results generated by this query
  `maxRows` - the maximum number of rows to retrieve
  `fetchSize` - if QUERY_FORWARD_CURSOR is set, the preferred number of rows to retrieve before suspending
  `flags` - a combination of QUERY_* flags indicating how to handle the query.
Throws:
  `SQLException` - if query execution fails

#### fastpathCall

    public byte[] fastpathCall(int fnid,
                      ParameterList parameters,
                      boolean suppressBegin)
                        throws SQLException

Description copied from interface: `QueryExecutor`
Invoke a backend function via the fastpath interface.

Parameters:
  `fnid` - the OID of the backend function to invoke
  `parameters` - a ParameterList returned from `QueryExecutor.createFastpathParameters(int)` containing the parameters to pass to the backend function
  `suppressBegin` - if begin should be suppressed
Returns:
  the binary-format result of the fastpath call, or `null` if a void result was returned
Throws:
  `SQLException` - if an error occurs while executing the fastpath call

#### doSubprotocolBegin

    public void doSubprotocolBegin()
                            throws SQLException

Throws:
  `SQLException`

#### createFastpathParameters

    public ParameterList createFastpathParameters(int count)

Description copied from interface: `QueryExecutor`
Create a new ParameterList implementation suitable for invoking a fastpath function via `QueryExecutor.fastpathCall(int, com.amazon.redshift.core.ParameterList, boolean)`.

Parameters:
  `count` - the number of parameters the fastpath call will take
Returns:
  a ParameterList suitable for passing to `QueryExecutor.fastpathCall(int, com.amazon.redshift.core.ParameterList, boolean)`.

#### processNotifies

    public void processNotifies()
                         throws SQLException

Description copied from interface: `QueryExecutor`
Prior to attempting to retrieve notifications, we need to pull any recently received notifications off of the network buffers. The notification retrieval in ProtocolConnection cannot do this as it is prone to deadlock, so the higher level caller must be responsible which requires exposing this method.

Throws:
  `SQLException` - if and error occurs while fetching notifications

#### processNotifies

    public void processNotifies(int timeoutMillis)
                         throws SQLException

Description copied from interface: `QueryExecutor`
Prior to attempting to retrieve notifications, we need to pull any recently received notifications off of the network buffers. The notification retrieval in ProtocolConnection cannot do this as it is prone to deadlock, so the higher level caller must be responsible which requires exposing this method. This variant supports blocking for the given time in millis.

Parameters:
  `timeoutMillis` - when > 0, block for this time when =0, block forever when < 0, don't block
Throws:
  `SQLException` - if and error occurs while fetching notifications

#### startCopy

    public CopyOperation startCopy(String sql,
                          boolean suppressBegin)
                            throws SQLException

Sends given query to BE to start, initialize and lock connection for a CopyOperation.

Parameters:
  `sql` - COPY FROM STDIN / COPY TO STDOUT statement
  `suppressBegin` - if begin should be suppressed
Returns:
  CopyIn or CopyOut operation object
Throws:
  `SQLException` - on failure

#### cancelCopy

    public void cancelCopy(CopyOperationImpl op)
                    throws SQLException

Finishes a copy operation and unlocks connection discarding any exchanged data.

Parameters:
  `op` - the copy operation presumably currently holding lock on this connection
Throws:
  `SQLException` - on any additional failure

#### endCopy

    public long endCopy(CopyOperationImpl op)
                 throws SQLException

Finishes writing to copy and unlocks connection.

Parameters:
  `op` - the copy operation presumably currently holding lock on this connection
Returns:
  number of rows updated for server versions 8.2 or newer
Throws:
  `SQLException` - on failure

#### writeToCopy

    public void writeToCopy(CopyOperationImpl op,
                   byte[] data,
                   int off,
                   int siz)
                     throws SQLException

Sends data during a live COPY IN operation. Only unlocks the connection if server suddenly returns CommandComplete, which should not happen

Parameters:
  `op` - the CopyIn operation presumably currently holding lock on this connection
  `data` - bytes to send
  `off` - index of first byte to send (usually 0)
  `siz` - number of bytes to send (usually data.length)
Throws:
  `SQLException` - on failure

#### writeToCopy

    public void writeToCopy(CopyOperationImpl op,
                   ByteStreamWriter from)
                     throws SQLException

Sends data during a live COPY IN operation. Only unlocks the connection if server suddenly returns CommandComplete, which should not happen

Parameters:
  `op` - the CopyIn operation presumably currently holding lock on this connection
  `from` - the source of bytes, e.g. a ByteBufferByteStreamWriter
Throws:
  `SQLException` - on failure

#### flushCopy

    public void flushCopy(CopyOperationImpl op)
                   throws SQLException

Throws:
  `SQLException`

#### closeStatementAndPortal

    public void closeStatementAndPortal()

Description copied from interface: `QueryExecutor`
Close the statement and portal when statement get closed.

Specified by:
  `closeStatementAndPortal` in interface `QueryExecutor`
Overrides:
  `closeStatementAndPortal` in class `QueryExecutorBase`

#### isRingBufferThreadRunning

    public boolean isRingBufferThreadRunning()

Check for a running ring buffer thread.

Specified by:
  `isRingBufferThreadRunning` in interface `QueryExecutor`
Overrides:
  `isRingBufferThreadRunning` in class `QueryExecutorBase`
Returns:
  returns true if Ring buffer thread is running, otherwise false.

#### closeRingBufferThread

    public void closeRingBufferThread(RedshiftRowsBlockingQueue<Tuple> queueRows,
                             Thread ringBufferThread)

Close the last active ring buffer thread.

Specified by:
  `closeRingBufferThread` in interface `QueryExecutor`
Overrides:
  `closeRingBufferThread` in class `QueryExecutorBase`
Parameters:
  `queueRows` - the blocking queue rows
  `ringBufferThread` - the thread fetching rows in the blocking queue.

#### sendQueryCancel

    public void sendQueryCancel()
                         throws SQLException

Description copied from interface: `QueryExecutor`
Sends a query cancellation for this connection.

Specified by:
  `sendQueryCancel` in interface `QueryExecutor`
Overrides:
  `sendQueryCancel` in class `QueryExecutorBase`
Throws:
  `SQLException` - if something goes wrong.

#### processSyncOnClose

    protected void processSyncOnClose()
                               throws IOException,
                                      SQLException

Throws:
  `IOException`
  `SQLException`

#### processResults

    protected void processResults(ResultHandler handler,
                      int flags,
                      int fetchSize,
                      boolean subQueries,
                      int maxRows)
                           throws IOException

Throws:
  `IOException`

#### processResults

    protected void processResults(ResultHandler handler,
                      int flags,
                      int fetchSize,
                      boolean subQueries,
                      int initRowCount,
                      int maxRows)
                           throws IOException

Throws:
  `IOException`

#### fetch

    public void fetch(ResultCursor cursor,
             ResultHandler handler,
             int fetchSize,
             int initRowCount)
               throws SQLException

Description copied from interface: `QueryExecutor`
Fetch additional rows from a cursor.

Parameters:
  `cursor` - the cursor to fetch from
  `handler` - the handler to feed results to
  `fetchSize` - the preferred number of rows to retrieve before suspending
  `initRowCount` - the number of rows already fetched
Throws:
  `SQLException` - if query execution fails

#### sendCloseMessage

    protected void sendCloseMessage()
                             throws IOException

Specified by:
  `sendCloseMessage` in class `QueryExecutorBase`
Throws:
  `IOException`

#### readStartupMessages

    public void readStartupMessages()
                             throws IOException,
                                    SQLException

Throws:
  `IOException`
  `SQLException`

#### receiveParameterStatus

    public void receiveParameterStatus()
                                throws IOException,
                                       SQLException

Throws:
  `IOException`
  `SQLException`

#### setTimeZone

    public void setTimeZone(TimeZone timeZone)

#### getTimeZone

    public TimeZone getTimeZone()

Description copied from interface: `QueryExecutor`
Returns backend timezone in java format.

Returns:
  backend timezone in java format.

#### setApplicationName

    public void setApplicationName(String applicationName)

#### getApplicationName

    public String getApplicationName()

Description copied from interface: `QueryExecutor`
Returns application_name connection property.

Returns:
  application_name connection property

#### getReplicationProtocol

    public ReplicationProtocol getReplicationProtocol()

Returns:
  the ReplicationProtocol instance for this connection.

#### useBinaryForReceive

    public boolean useBinaryForReceive(int oid)

Description copied from interface: `TypeTransferModeRegistry`
Returns if given oid should be received in binary format.

Parameters:
  `oid` - type oid
Returns:
  true if given oid should be received in binary format

#### setBinaryReceiveOids

    public void setBinaryReceiveOids(Set<Integer> oids)

Description copied from interface: `QueryExecutor`
Sets the oids that should be received using binary encoding.

Parameters:
  `oids` - The oids to request with binary encoding.

#### useBinaryForSend

    public boolean useBinaryForSend(int oid)

Description copied from interface: `TypeTransferModeRegistry`
Returns if given oid should be sent in binary format.

Parameters:
  `oid` - type oid
Returns:
  true if given oid should be sent in binary format

#### setBinarySendOids

    public void setBinarySendOids(Set<Integer> oids)

Description copied from interface: `QueryExecutor`
Sets the oids that should be sent using binary encoding.

Parameters:
  `oids` - The oids to send with binary encoding.

#### getIntegerDateTimes

    public boolean getIntegerDateTimes()

Description copied from interface: `QueryExecutor`
Returns true if server uses integer instead of double for binary date and time encodings.

Returns:
  the server integer_datetime setting.

#### waitForRingBufferThreadToFinish

    public void waitForRingBufferThreadToFinish(boolean calledFromConnectionClose,
                                       boolean calledFromResultsetClose,
                                       RedshiftRowsBlockingQueue<Tuple> queueRows,
                                       Thread ringBufferThread)

Wait for ring buffer thread to finish.

Parameters:
  `calledFromConnectionClose` - true, if it called from connection.close(), false otherwise.
  `calledFromResultsetClose` - true, if it called from resultset.close(), false otherwise.
  `queueRows` - the blocking queue rows
  `ringBufferThread` - the thread manage the blocking queue