[patches] [PATCH 2/2] sql: use index uniqueness property from Tarantool

Nikita Pettik korablev at tarantool.org
Wed Feb 28 20:50:58 MSK 2018


As part of Tarantool data dictionary integration into SQL, it is
required to fetch index uniqueness directly from Tarantool's Index
structure (not SQL one). This patch implements exactly this feature.

Closes #3181
---
 src/box/sql/analyze.c   |  2 +-
 src/box/sql/build.c     | 14 ++++++++++++++
 src/box/sql/expr.c      |  2 +-
 src/box/sql/fkey.c      |  2 +-
 src/box/sql/insert.c    |  4 ++--
 src/box/sql/pragma.c    |  2 +-
 src/box/sql/sqliteInt.h |  2 ++
 src/box/sql/where.c     |  6 +++---
 8 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c
index ca8d37c76..824406aa5 100644
--- a/src/box/sql/analyze.c
+++ b/src/box/sql/analyze.c
@@ -965,7 +965,7 @@ analyzeOneTable(Parse * pParse,	/* Parser context */
 			sqlite3VdbeAddOp0(v, OP_Goto);
 			addrNextRow = sqlite3VdbeCurrentAddr(v);
 			if (nColTest == 1 && pIdx->nKeyCol == 1
-			    && IsUniqueIndex(pIdx)) {
+			    && index_is_unique(pIdx)) {
 				/* For a single-column UNIQUE index, once we have found a non-NULL
 				 * row, we know that all the rest will be distinct, so skip
 				 * subsequent distinctness tests.
diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index efc58dad0..feaf1ff6b 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -2863,6 +2863,20 @@ addIndexToTable(Index * pIndex, Table * pTab)
 	}
 }
 
+bool
+index_is_unique(Index *idx)
+{
+	assert(idx != NULL);
+	uint32_t space_id = SQLITE_PAGENO_TO_SPACEID(idx->tnum);
+	uint32_t index_id = SQLITE_PAGENO_TO_INDEXID(idx->tnum);
+	struct space *space = space_by_id(space_id);
+	assert(space != NULL);
+	struct index *tnt_index = space_index(space, index_id);
+	assert(tnt_index != NULL);
+
+	return tnt_index->def->opts.is_unique;
+}
+
 /*
  * Create a new index for an SQL table.  pName1.pName2 is the name of the index
  * and pTblList is the name of the table that is to be indexed.  Both will
diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c
index b69a176cb..7243a8d02 100644
--- a/src/box/sql/expr.c
+++ b/src/box/sql/expr.c
@@ -2534,7 +2534,7 @@ sqlite3FindInIndex(Parse * pParse,	/* Parsing context */
 				if (mustBeUnique) {
 					if (pIdx->nKeyCol > nExpr
 					    || (pIdx->nColumn > nExpr
-					    && !IsUniqueIndex(pIdx))) {
+					    && !index_is_unique(pIdx))) {
 							continue;	/* This index is not unique over the IN RHS columns */
 					}
 				}
diff --git a/src/box/sql/fkey.c b/src/box/sql/fkey.c
index 088d84e27..ac0386934 100644
--- a/src/box/sql/fkey.c
+++ b/src/box/sql/fkey.c
@@ -255,7 +255,7 @@ sqlite3FkLocateIndex(Parse * pParse,	/* Parse context to store any error in */
 	}
 
 	for (pIdx = pParent->pIndex; pIdx; pIdx = pIdx->pNext) {
-		if (pIdx->nKeyCol == nCol && IsUniqueIndex(pIdx)
+		if (pIdx->nKeyCol == nCol && index_is_unique(pIdx)
 		    && pIdx->pPartIdxWhere == 0) {
 			/* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
 			 * of columns. If each indexed column corresponds to a foreign key
diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index e4023e030..7ba8e2179 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -1611,7 +1611,7 @@ sqlite3OpenTableAndIndices(Parse * pParse,	/* Parsing context */
 		    IsPrimaryKeyIndex(pIdx) ||		/* Condition 2 */
 		    sqlite3FkReferences(pTab) ||	/* Condition 3 */
 		    /* Condition 4 */
-		    (IsUniqueIndex(pIdx) && pIdx->onError !=
+		    (index_is_unique(pIdx) && pIdx->onError !=
 		     ON_CONFLICT_ACTION_DEFAULT &&
 		     /* Condition 4.1 */
 		     pIdx->onError != ON_CONFLICT_ACTION_ABORT) ||
@@ -1855,7 +1855,7 @@ xferOptimization(Parse * pParse,	/* Parser context */
 		}
 	}
 	for (pDestIdx = pDest->pIndex; pDestIdx; pDestIdx = pDestIdx->pNext) {
-		if (IsUniqueIndex(pDestIdx)) {
+		if (index_is_unique(pDestIdx)) {
 			destHasUniqueIdx = 1;
 		}
 		for (pSrcIdx = pSrc->pIndex; pSrcIdx; pSrcIdx = pSrcIdx->pNext) {
diff --git a/src/box/sql/pragma.c b/src/box/sql/pragma.c
index 03ae926e0..1a01644d4 100644
--- a/src/box/sql/pragma.c
+++ b/src/box/sql/pragma.c
@@ -493,7 +493,7 @@ sqlite3Pragma(Parse * pParse, Token * pId,	/* First part of [schema.]id field */
 								     "isisi", i,
 								     pIdx->
 								     zName,
-								     IsUniqueIndex
+								     index_is_unique
 								     (pIdx),
 								     azOrigin
 								     [pIdx->
diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h
index b3405cb61..04d211728 100644
--- a/src/box/sql/sqliteInt.h
+++ b/src/box/sql/sqliteInt.h
@@ -3160,6 +3160,8 @@ void sqlite3SrcListAssignCursors(Parse *, SrcList *);
 void sqlite3IdListDelete(sqlite3 *, IdList *);
 void sqlite3SrcListDelete(sqlite3 *, SrcList *);
 Index *sqlite3AllocateIndexObject(sqlite3 *, i16, int, char **);
+bool
+index_is_unique(Index *);
 void sqlite3CreateIndex(Parse *, Token *, SrcList *, ExprList *, int, Token *,
 			Expr *, int, int, u8);
 void sqlite3DropIndex(Parse *, SrcList *, Token *, int);
diff --git a/src/box/sql/where.c b/src/box/sql/where.c
index 2f1c627e5..4cd78b3ca 100644
--- a/src/box/sql/where.c
+++ b/src/box/sql/where.c
@@ -555,7 +555,7 @@ isDistinctRedundant(Parse * pParse,		/* Parsing context */
 	 *      contain a "col=X" term are subject to a NOT NULL constraint.
 	 */
 	for (pIdx = pTab->pIndex; pIdx; pIdx = pIdx->pNext) {
-		if (!IsUniqueIndex(pIdx))
+		if (!index_is_unique(pIdx))
 			continue;
 		for (i = 0; i < pIdx->nKeyCol; i++) {
 			if (0 ==
@@ -3293,7 +3293,7 @@ wherePathSatisfiesOrderBy(WhereInfo * pWInfo,	/* The WHERE clause */
 			} else {
 				nKeyCol = pIndex->nKeyCol;
 				nColumn = pIndex->nColumn;
-				isOrderDistinct = IsUniqueIndex(pIndex);
+				isOrderDistinct = index_is_unique(pIndex);
 			}
 
 			/* Loop through all columns of the index and deal with the ones
@@ -4069,7 +4069,7 @@ whereShortCut(WhereLoopBuilder * pBuilder)
 		for (pIdx = pTab->pIndex; pIdx; pIdx = pIdx->pNext) {
 			int opMask;
 			assert(pLoop->aLTermSpace == pLoop->aLTerm);
-			if (!IsUniqueIndex(pIdx)
+			if (!index_is_unique(pIdx)
 			    || pIdx->pPartIdxWhere != 0
 			    || pIdx->nKeyCol > ArraySize(pLoop->aLTermSpace)
 			    )
-- 
2.15.1




More information about the Tarantool-patches mailing list