19 #ifndef NdbQueryBuilderImpl_H
20 #define NdbQueryBuilderImpl_H
23 #define QRY_REQ_ARG_IS_NULL 4800
24 #define QRY_TOO_FEW_KEY_VALUES 4801
25 #define QRY_TOO_MANY_KEY_VALUES 4802
26 #define QRY_OPERAND_HAS_WRONG_TYPE 4803
27 #define QRY_CHAR_OPERAND_TRUNCATED 4804
28 #define QRY_NUM_OPERAND_RANGE 4805
29 #define QRY_MULTIPLE_PARENTS 4806
30 #define QRY_UNKONWN_PARENT 4807
31 #define QRY_UNKNOWN_COLUMN 4808
32 #define QRY_UNRELATED_INDEX 4809
33 #define QRY_WRONG_INDEX_TYPE 4810
34 #define QRY_OPERAND_ALREADY_BOUND 4811
35 #define QRY_DEFINITION_TOO_LARGE 4812
36 #define QRY_SEQUENTIAL_SCAN_SORTED 4813
37 #define QRY_RESULT_ROW_ALREADY_DEFINED 4814
38 #define QRY_HAS_ZERO_OPERATIONS 4815
39 #define QRY_IN_ERROR_STATE 4816
40 #define QRY_ILLEGAL_STATE 4817
41 #define QRY_WRONG_OPERATION_TYPE 4820
42 #define QRY_SCAN_ORDER_ALREADY_SET 4821
43 #define QRY_PARAMETER_HAS_WRONG_TYPE 4822
44 #define QRY_CHAR_PARAMETER_TRUNCATED 4823
45 #define QRY_MULTIPLE_SCAN_SORTED 4824
46 #define QRY_BATCH_SIZE_TOO_SMALL 4825
50 #include <Bitmask.hpp>
51 #include "NdbQueryBuilder.hpp"
52 #include "NdbIndexScanOperation.hpp"
53 #include "ndb_limits.h"
59 class NdbQueryBuilderImpl;
60 class NdbQueryDefImpl;
61 class NdbQueryOperationDefImpl;
63 class NdbParamOperandImpl;
64 class NdbConstOperandImpl;
65 class NdbLinkedOperandImpl;
95 #if defined(TEST_Uint32Buffer)
96 STATIC_CONST(initSize = 1);
98 STATIC_CONST(initSize = 32);
101 explicit Uint32Buffer():
105 m_memoryExhausted(false),
110 if (unlikely(m_array != m_local)) {
118 void releaseExtend() {
119 if (unlikely(m_array != m_local)) {
132 Uint32* alloc(Uint32 count) {
133 Uint32 reqSize = m_size+count;
134 if(unlikely(reqSize >= m_avail)) {
135 if (unlikely(m_memoryExhausted)) {
138 #if defined(TEST_Uint32Buffer)
139 Uint32 newSize = reqSize;
141 Uint32 newSize = reqSize*2;
145 Uint32* newBuf =
new Uint32[newSize];
146 if (likely(newBuf!=NULL)) {
148 memcpy (newBuf, m_array, m_size*
sizeof(Uint32));
149 if (m_array != m_local) {
153 m_avail =
static_cast<Uint32
>(newSize);
156 m_memoryExhausted =
true;
160 Uint32* extend = &m_array[m_size];
161 m_size +=
static_cast<Uint32
>(count);
167 void put(Uint32 idx, Uint32 value) {
168 assert(idx < m_size);
169 m_array[idx] = value;
174 void append(
const Uint32 src) {
176 if (likely(m_size < m_avail)) {
177 m_array[m_size++] = src;
179 Uint32* dst = alloc(1);
180 if (likely(dst!=NULL))
187 void append(
const Uint32Buffer& src) {
188 assert (!src.isMemoryExhausted());
190 Uint32 len = src.getSize();
191 if (likely(len > 0)) {
192 Uint32* dst = alloc(len);
193 if (likely(dst!=NULL)) {
194 memcpy(dst, src.addr(), len*
sizeof(Uint32));
202 void appendBytes(
const void* src, Uint32 len) {
203 if (likely(len > 0)) {
205 static_cast<Uint32
>((len +
sizeof(Uint32)-1 - m_bytesLeft)
207 Uint32* dst = alloc(wordCount);
208 if (likely(dst!=NULL)) {
210 Uint8*
const start =
reinterpret_cast<Uint8*
>(dst) - m_bytesLeft;
211 memcpy(start, src, len);
212 m_bytesLeft = (m_bytesLeft - len) %
sizeof(Uint32);
214 bzero(start + len, m_bytesLeft);
221 void skipRestOfWord()
224 Uint32* addr(Uint32 idx=0) {
225 return (likely(!m_memoryExhausted && m_size>idx)) ?&m_array[idx] :NULL;
227 const Uint32* addr(Uint32 idx=0)
const {
228 return (likely(!m_memoryExhausted && m_size>idx)) ?&m_array[idx] :NULL;
232 Uint32
get(Uint32 idx)
const {
233 assert(idx < m_size);
238 bool isMemoryExhausted()
const {
239 return m_memoryExhausted;
242 Uint32 getSize()
const {
248 Uint32Buffer(Uint32Buffer&);
249 Uint32Buffer& operator=(Uint32Buffer&);
252 Uint32 m_local[initSize];
256 bool m_memoryExhausted;
262 class NdbQueryOptionsImpl
265 friend class NdbQueryOperationDefImpl;
268 explicit NdbQueryOptionsImpl()
272 m_interpretedCode(NULL)
274 NdbQueryOptionsImpl(
const NdbQueryOptionsImpl&);
275 ~NdbQueryOptionsImpl();
280 NdbQueryOperationDefImpl* m_parent;
289 NdbQueryOptionsImpl&operator=(
const NdbQueryOptionsImpl&);
297 class NdbQueryOperationDefImpl
306 NdbQueryOperandImpl* low[MAX_ATTRIBUTES_IN_INDEX];
307 NdbQueryOperandImpl* high[MAX_ATTRIBUTES_IN_INDEX];
308 Uint32 lowKeys, highKeys;
309 bool lowIncl, highIncl;
313 Uint32 getNoOfParentOperations()
const
314 {
return (m_parent) ? 1 : 0; }
316 NdbQueryOperationDefImpl& getParentOperation(Uint32
i)
const
317 { assert(i==0 && m_parent!=NULL);
321 NdbQueryOperationDefImpl* getParentOperation()
const
325 Uint32 getNoOfChildOperations()
const
326 {
return m_children.size(); }
328 NdbQueryOperationDefImpl& getChildOperation(Uint32
i)
const
329 {
return *m_children[
i]; }
334 const char* getName()
const
338 {
return m_options.m_matchType; }
341 {
return m_options.m_scanOrder; }
344 {
return m_options.m_interpretedCode; }
346 Uint32 assignQueryOperationId(Uint32& nodeId)
353 int linkWithParent(NdbQueryOperationDefImpl* parentOp);
363 Uint32 addColumnRef(
const NdbColumnImpl* column,
int& error);
371 int addParamRef(
const NdbParamOperandImpl* param);
373 Uint32 getNoOfParameters()
const
374 {
return m_params.size(); }
376 const NdbParamOperandImpl& getParameter(Uint32 ix)
const
377 {
return *m_params[ix]; }
382 virtual const NdbQueryOperandImpl*
const* getKeyOperands()
const
385 virtual const IndexBound* getBounds()
const
389 virtual bool isScanOperation()
const = 0;
397 virtual int serializeOperation(Uint32Buffer& serializedTree) = 0;
403 return m_spjProjection;
406 virtual int checkPrunable(
const Uint32Buffer& keyInfo,
407 Uint32 shortestBound,
409 Uint32& hashValue)
const {
414 virtual ~NdbQueryOperationDefImpl();
418 const NdbQueryOptionsImpl& options,
424 Uint32 getQueryOperationIx()
const
428 Uint32 getQueryOperationId()
const
441 Bitmask<(NDB_SPJ_MAX_TREE_NODES+31)/32> hasMoreSiblingsMask)
const;
446 Uint32 appendParentList(Uint32Buffer& serializedDef)
const;
449 Uint32 appendChildProjection(Uint32Buffer& serializedDef)
const;
459 bool m_diskInChildProjection;
462 bool isChildOf(
const NdbQueryOperationDefImpl* parentOp)
const;
469 int addChild(NdbQueryOperationDefImpl* child);
472 void removeChild(
const NdbQueryOperationDefImpl*);
476 const char*
const m_ident;
484 const NdbQueryOptionsImpl m_options;
488 NdbQueryOperationDefImpl* m_parent;
499 class NdbQueryScanOperationDefImpl :
500 public NdbQueryOperationDefImpl
503 explicit NdbQueryScanOperationDefImpl (
505 const NdbQueryOptionsImpl& options,
510 virtual bool isScanOperation()
const
514 int serialize(Uint32Buffer& serializedDef,
518 virtual Uint32 appendBoundPattern(Uint32Buffer& serializedDef)
const
521 virtual Uint32 appendPrunePattern(Uint32Buffer& serializedDef)
const
527 class NdbQueryIndexScanOperationDefImpl :
public NdbQueryScanOperationDefImpl
535 virtual int serializeOperation(Uint32Buffer& serializedDef);
538 {
return m_interface; }
543 virtual int checkPrunable(
const Uint32Buffer& keyInfo,
544 Uint32 shortestBound,
546 Uint32& hashValue)
const;
548 virtual const IndexBound* getBounds()
const
553 virtual Uint32 appendBoundPattern(Uint32Buffer& serializedDef)
const;
555 virtual Uint32 appendPrunePattern(Uint32Buffer& serializedDef)
const;
559 explicit NdbQueryIndexScanOperationDefImpl (
563 const NdbQueryOptionsImpl& options,
569 Uint32 appendBoundValue( Uint32Buffer& serializedDef,
571 const NdbQueryOperandImpl* value,
572 int& paramCnt)
const;
583 class NdbQueryDefImpl
595 bool isScanQuery()
const
596 {
return m_operations[0]->isScanOperation(); }
600 Uint32 getNoOfOperations()
const
601 {
return m_operations.size(); }
605 const NdbQueryOperationDefImpl& getQueryOperation(Uint32
index)
const
606 {
return *m_operations[
index]; }
608 const NdbQueryOperationDefImpl* getQueryOperation(
const char* ident)
const;
611 {
return m_interface; }
614 Uint32Buffer& getSerialized()
615 {
return m_serializedDef; }
618 const Uint32Buffer& getSerialized()
const
619 {
return m_serializedDef; }
626 Uint32Buffer m_serializedDef;
630 class NdbQueryBuilderImpl
635 ~NdbQueryBuilderImpl();
636 explicit NdbQueryBuilderImpl();
638 const NdbQueryDefImpl* prepare();
642 void setErrorCode(
int aErrorCode);
645 bool hasError()
const
646 {
return m_hasError; }
664 int takeOwnership(NdbQueryOperandImpl*);
665 int takeOwnership(NdbQueryOperationDefImpl*);
667 bool contains(
const NdbQueryOperationDefImpl*);
685 class NdbQueryOperandImpl
687 friend class NdbQueryBuilderImpl;
703 NdbQueryOperationDefImpl& operation)
704 {
if (m_column && m_column != &column)
706 return QRY_OPERAND_ALREADY_BOUND;
717 friend NdbQueryBuilderImpl::~NdbQueryBuilderImpl();
718 friend NdbQueryDefImpl::~NdbQueryDefImpl();
720 virtual ~NdbQueryOperandImpl(){};
722 NdbQueryOperandImpl(Kind kind)
737 class NdbLinkedOperandImpl :
public NdbQueryOperandImpl
742 const NdbQueryOperationDefImpl& getParentOperation()
const
743 {
return m_parentOperation; }
747 Uint32 getLinkedColumnIx()
const
748 {
return m_parentColumnIx; }
751 {
return *m_parentOperation.getSPJProjection()[m_parentColumnIx]; }
754 {
return m_interface; }
757 NdbQueryOperationDefImpl& operation);
760 NdbLinkedOperandImpl (NdbQueryOperationDefImpl& parent,
762 : NdbQueryOperandImpl(Linked),
764 m_parentOperation(parent),
765 m_parentColumnIx(columnIx)
769 NdbQueryOperationDefImpl& m_parentOperation;
770 const Uint32 m_parentColumnIx;
774 class NdbParamOperandImpl :
public NdbQueryOperandImpl
779 const char* getName()
const
782 Uint32 getParamIx()
const
783 {
return m_paramIx; }
786 {
return m_interface; }
789 NdbQueryOperationDefImpl& operation);
792 NdbParamOperandImpl (
const char*
name, Uint32 paramIx)
793 : NdbQueryOperandImpl(
Param),
800 const char*
const m_name;
801 const Uint32 m_paramIx;
805 class NdbConstOperandImpl :
public NdbQueryOperandImpl
809 Uint32 getSizeInBytes()
const
810 {
return m_converted.len; }
811 const void* getAddr()
const
812 {
return likely(m_converted.buffer==NULL) ? &m_converted.val : m_converted.buffer; }
815 {
return m_interface; }
818 NdbQueryOperationDefImpl& operation);
821 NdbConstOperandImpl ()
822 : NdbQueryOperandImpl(Const),
827 #define UNDEFINED_CONVERSION \
828 { return QRY_OPERAND_HAS_WRONG_TYPE; }
830 virtual int convertUint8() UNDEFINED_CONVERSION;
831 virtual
int convertInt8() UNDEFINED_CONVERSION;
832 virtual
int convertUint16() UNDEFINED_CONVERSION;
833 virtual
int convertInt16() UNDEFINED_CONVERSION;
834 virtual
int convertUint24() UNDEFINED_CONVERSION;
835 virtual
int convertInt24() UNDEFINED_CONVERSION;
836 virtual
int convertUint32() UNDEFINED_CONVERSION;
837 virtual
int convertInt32() UNDEFINED_CONVERSION;
838 virtual
int convertUint64() UNDEFINED_CONVERSION;
839 virtual
int convertInt64() UNDEFINED_CONVERSION;
840 virtual
int convertFloat() UNDEFINED_CONVERSION;
841 virtual
int convertDouble() UNDEFINED_CONVERSION
843 virtual
int convertUDec() UNDEFINED_CONVERSION;
844 virtual
int convertDec() UNDEFINED_CONVERSION;
846 virtual
int convertBit() UNDEFINED_CONVERSION;
847 virtual
int convertChar() UNDEFINED_CONVERSION;
848 virtual
int convertVChar() UNDEFINED_CONVERSION;
849 virtual
int convertLVChar() UNDEFINED_CONVERSION;
850 virtual
int convertBin() UNDEFINED_CONVERSION;
851 virtual
int convertVBin() UNDEFINED_CONVERSION;
852 virtual
int convertLVBin() UNDEFINED_CONVERSION;
854 virtual
int convertDate() UNDEFINED_CONVERSION;
855 virtual
int convertDatetime() UNDEFINED_CONVERSION;
856 virtual
int convertTime() UNDEFINED_CONVERSION;
857 virtual
int convertYear() UNDEFINED_CONVERSION;
858 virtual
int convertTimestamp() UNDEFINED_CONVERSION;
860 virtual
int convert2ColumnType();
865 class ConvertedValue {
867 ConvertedValue() : len(0), buffer(NULL) {};
869 if (buffer)
delete[] ((
char*)buffer);
872 char* getCharBuffer(Uint32
size) {
873 char* dst = val.shortChar;
874 if (unlikely(size >
sizeof(val.shortChar))) {
875 dst =
new char[
size];
882 STATIC_CONST(maxShortChar = 32);
898 char shortChar[maxShortChar];