[tarantool-patches] [PATCH v1] sql: change of PRAGMA INDEX_INFO syntax

Ivan Ilyin vanyail at yandex.ru
Tue Jun 12 12:32:20 MSK 2018


This change removes 'pragma index_xinfo' syntax. 'pragma index_info'
now works as 'pragma index_xinfo' and also displays type of columns in
index.

Fixes #3194
---
 src/box/sql/parse.y              |  2 +-
 src/box/sql/pragma.c             | 65 +++++++++++++-----------------------
 src/box/sql/pragma.h             | 12 +++----
 test/sql-tap/index-info.test.lua | 71 +++++++++++++++++++++++++++-------------
 4 files changed, 75 insertions(+), 75 deletions(-)

diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index 2e5f349..1c6f8b7 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -1278,7 +1278,7 @@ cmd ::= PRAGMA nm(X) EQ minus_num(Y).        {
 cmd ::= PRAGMA nm(X) LP minus_num(Y) RP.     {
     sqlite3Pragma(pParse,&X,&Y,0,1);
 }
-cmd ::= PRAGMA nm(X) EQ nm(Z) DOT nm(Y).    {
+cmd ::= PRAGMA nm(X) LP nm(Z) DOT nm(Y) RP.    {
     sqlite3Pragma(pParse,&X,&Y,&Z,0);
 }
 cmd ::= PRAGMA .                            {
diff --git a/src/box/sql/pragma.c b/src/box/sql/pragma.c
index 9dab5a7..ec0dff3 100644
--- a/src/box/sql/pragma.c
+++ b/src/box/sql/pragma.c
@@ -447,54 +447,32 @@ sqlite3Pragma(Parse * pParse, Token * pId,	/* First part of [schema.]id field */
 				if (pIdx) {
 					int i;
 					int mx;
-					if (pPragma->iArg) {
-						/* PRAGMA index_xinfo (newer
-						 * version with more rows and
-						 * columns)
-						 */
-						pParse->nMem = 6;
-					} else {
-						/* PRAGMA index_info (legacy
-						 * version)
-						 */
-						pParse->nMem = 3;
-					}
+					pParse->nMem = 7;
 					mx = index_column_count(pIdx);
 					assert(pParse->nMem <=
 					       pPragma->nPragCName);
 					for (i = 0; i < mx; i++) {
 						i16 cnum = pIdx->aiColumn[i];
 						assert(pIdx->pTable);
-						sqlite3VdbeMultiLoad(v, 1,
-								     "iis", i,
-								     cnum,
-								     cnum <
-								     0 ? 0 :
-								     pIdx->
-								     pTable->
-								     def->
-								     fields[cnum].
-								     name);
-						if (pPragma->iArg) {
-							const char *c_n;
-							uint32_t id;
-							struct coll *coll =
-								sql_index_collation(pIdx, i, &id);
-							if (coll != NULL)
-								c_n = coll_by_id(id)->name;
-							else
-								c_n = "BINARY";
-							enum sort_order sort_order;
-							sort_order = sql_index_column_sort_order(pIdx,
-												 i);
-							sqlite3VdbeMultiLoad(v,
-									     4,
-									     "isi",
-									     sort_order,
-									     c_n,
-									     i <
-									     mx);
-						}
+						const char *c_n;
+						uint32_t id;
+						struct coll *coll =
+							sql_index_collation(pIdx, i, &id);
+						if (coll != NULL)
+							c_n = coll_by_id(id)->name;
+						else
+							c_n = "BINARY";
+						enum sort_order sort_order;
+						sort_order = sql_index_column_sort_order(pIdx,
+											 i);
+						enum field_type type = pIdx->pTable->
+											def->fields[cnum].type;
+						sqlite3VdbeMultiLoad(v, 1, "iisisis", i,
+											cnum, cnum < 0 ? 0 :
+											pIdx->pTable->def->
+											fields[cnum].name,
+											sort_order, c_n, i < mx,
+											field_type_strs[type]);
 						sqlite3VdbeAddOp2(v,
 								  OP_ResultRow,
 								  1,
@@ -503,7 +481,8 @@ sqlite3Pragma(Parse * pParse, Token * pId,	/* First part of [schema.]id field */
 				}
 			}
 			break;
-		}
+	}
+
 	case PragTyp_INDEX_LIST:{
 			if (zRight) {
 				Index *pIdx;
diff --git a/src/box/sql/pragma.h b/src/box/sql/pragma.h
index f966018..25a2d8a 100644
--- a/src/box/sql/pragma.h
+++ b/src/box/sql/pragma.h
@@ -100,6 +100,8 @@ typedef struct PragmaName {
 	u8 nPragCName;		/* Num of col names. 0 means use pragma name */
 	u32 iArg;		/* Extra argument */
 } PragmaName;
+/* The order of pragmas in this array is important: it has */
+/* to be sorted. For more info see pragma_locate function. */
 static const PragmaName aPragmaName[] = {
 	{ /* zName:     */ "busy_timeout",
 	 /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
@@ -177,20 +179,14 @@ static const PragmaName aPragmaName[] = {
 	 /* ePragTyp:  */ PragTyp_INDEX_INFO,
 	 /* ePragFlg:  */
 	 PragFlg_NeedSchema | PragFlg_Result1 | PragFlg_SchemaOpt,
-	 /* ColNames:  */ 10, 3,
-	 /* iArg:      */ 0},
+	 /* ColNames:  */ 10, 7,
+	 /* iArg:      */ 1},
 	{ /* zName:     */ "index_list",
 	 /* ePragTyp:  */ PragTyp_INDEX_LIST,
 	 /* ePragFlg:  */
 	 PragFlg_NeedSchema | PragFlg_Result1 | PragFlg_SchemaOpt,
 	 /* ColNames:  */ 19, 5,
 	 /* iArg:      */ 0},
-	{ /* zName:     */ "index_xinfo",
-	 /* ePragTyp:  */ PragTyp_INDEX_INFO,
-	 /* ePragFlg:  */
-	 PragFlg_NeedSchema | PragFlg_Result1 | PragFlg_SchemaOpt,
-	 /* ColNames:  */ 13, 6,
-	 /* iArg:      */ 1},
 #endif
 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_PARSER_TRACE)
 	{ /* zName:     */ "parser_trace",
diff --git a/test/sql-tap/index-info.test.lua b/test/sql-tap/index-info.test.lua
index d3bb70e..471b308 100755
--- a/test/sql-tap/index-info.test.lua
+++ b/test/sql-tap/index-info.test.lua
@@ -1,50 +1,75 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(4)
+test:plan(8)
 
 test:execsql([[
-	CREATE TABLE t1(a INT PRIMARY KEY, b INT UNIQUE, c INT);
-	INSERT INTO t1 VALUES (1, 1, 1), (2, 2, 2);
-	INSERT INTO t1 VALUES (3, 3, 3), (4, 4, 4);
+	CREATE TABLE t1(a INT PRIMARY KEY, b INT UNIQUE, c INT, d STRING);
+	INSERT INTO t1 VALUES (1, 1, 1, 'abcd'), (2, 2, 2, 'abcde');
+	INSERT INTO t1 VALUES (3, 3, 3, 'abcdef'), (4, 4, 4, 'abcdefg');
 	CREATE INDEX t1ix1 ON t1(a);
 	CREATE INDEX t1ix2 ON t1(a, b);
 	CREATE INDEX t1ix3 ON t1(a, b, c);
+	CREATE INDEX t1ix4 ON t1(b);
+	CREATE INDEX t1ix5 ON t1(b, c);
+	CREATE INDEX t1ix6 ON t1(d);
 ]])
 
-test:do_execsql_test(
+test:do_catchsql_test(
 	"index-info-1.1",
-	"PRAGMA index_info = t1.t1ix1;",
+	"PRAGMA index_xinfo (t1.t1ix1);",
 	{
-	-- <index-info-1.1>
-	0, 0, 'A'
-	-- <index-info-1.1>
+	1,"no such pragma: INDEX_XINFO"
 	})
 
-test:do_execsql_test(
+test:do_catchsql_test(
 	"index-info-1.2",
-	"PRAGMA index_info = t1.t1ix2;",
+	"PRAGMA index_info = t1.t1ix1;",
 	{
-	-- <index-info-1.1>
-	0, 0, 'A', 1, 1, 'B',
-	-- <index-info-1.1>
+	1,"near \".\": syntax error"
 	})
 
 test:do_execsql_test(
 	"index-info-1.3",
-	"PRAGMA index_info = t1.t1ix3;",
+	"PRAGMA index_info (t1.t1ix1);",
 	{
-	-- <index-info-1.1>
-	0, 0, 'A', 1, 1, 'B', 2, 2, 'C'
-	-- <index-info-1.1>
+	0, 0, 'A', 0, 'BINARY', 1, 'integer',
 	})
 
 test:do_execsql_test(
-	"index-info-1.1",
-	"PRAGMA index_xinfo = t1.t1ix1;",
+	"index-info-1.4",
+	"PRAGMA index_info (t1.t1ix2);",
+	{
+	0, 0, 'A', 0, 'BINARY', 1, 'integer', 1, 1, 'B', 0, 'BINARY', 1, 'integer',
+	})
+
+
+test:do_execsql_test(
+	"index-info-1.5",
+	"PRAGMA index_info (t1.t1ix3);",
+	{
+	0, 0, 'A', 0, 'BINARY', 1, 'integer', 1, 1, 'B', 0, 'BINARY', 1, 'integer', 2,
+	2, 'C', 0, 'BINARY', 1, 'integer',
+	})
+
+test:do_execsql_test(
+	"index-info-1.6",
+	"PRAGMA index_info (t1.t1ix4);",
+	{
+	0, 1, 'B', 0, 'BINARY', 1, 'integer',
+	})
+
+test:do_execsql_test(
+	"index-info-1.7",
+	"PRAGMA index_info (t1.t1ix5);",
+	{
+	0, 1, 'B', 0, 'BINARY', 1, 'integer', 1, 2, 'C', 0, 'BINARY', 1, 'integer',
+	})
+
+test:do_execsql_test(
+	"index-info-1.8",
+	"PRAGMA index_info (t1.t1ix6);",
 	{
-	-- <index-info-1.1>
-	0, 0, 'A', 0, 'BINARY', 1,
-	-- <index-info-1.1>
+	0, 3, 'D', 0, 'BINARY', 1, 'scalar',
 	})
 
 test:finish_test()
-- 
2.7.4





More information about the Tarantool-patches mailing list