Skip to content

Commit ba28cee

Browse files
committed
Index support for Declared LTT
1 parent e61eca0 commit ba28cee

25 files changed

Lines changed: 996 additions & 188 deletions

doc/sql.extensions/README.declared_local_temporary_tables.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Declared Local Temporary Tables are declared in the same declaration section as
2020
DECLARE [LOCAL] TEMPORARY TABLE <table_name>
2121
(
2222
<column_definition> [, ...]
23-
);
23+
)
24+
[[UNIQUE] [ASC | DESC] INDEX <index_name> (<column_name> [, ...])]...;
2425
```
2526

2627
There is no `ON COMMIT` clause.
@@ -35,7 +36,8 @@ as
3536
declare local temporary table t (
3637
id integer not null,
3738
val varchar(20)
38-
);
39+
)
40+
index idx_t_id (id);
3941
begin
4042
insert into t(id, val) values (1, 'a');
4143
insert into t(id, val) values (2, 'b');
@@ -59,7 +61,8 @@ create procedure p_count_values returns (n integer)
5961
as
6062
declare local temporary table t (
6163
id integer not null
62-
);
64+
)
65+
unique index uq_t_id (id);
6366
begin
6467
insert into t(id) values (1);
6568
insert into t(id) values (2);
@@ -214,6 +217,17 @@ delete from t where ...;
214217

215218
They can be used in subqueries, joins and cursor loops like other record sources, subject to the restrictions below.
216219

220+
Indexes can be declared inline as part of the table declaration:
221+
222+
```sql
223+
declare local temporary table t (
224+
id integer not null,
225+
val varchar(20)
226+
)
227+
index idx_t_id (id)
228+
descending index idx_t_val_desc (val);
229+
```
230+
217231
## Restrictions
218232

219233
Declared Local Temporary Tables intentionally support a small table definition surface.
@@ -237,7 +251,7 @@ Constraint restrictions:
237251
Other restrictions:
238252

239253
- A single PSQL statement, procedure, function or trigger may declare at most 1024 local temporary tables.
240-
- Indexes are not supported.
254+
- Expression-based indexes and partial indexes are not supported.
241255
- Triggers on declared local temporary tables are not supported.
242256
- Explicit privileges are not supported.
243257
- `ALTER TABLE`, `DROP TABLE`, `CREATE INDEX`, `ALTER INDEX` and `DROP INDEX` are not valid for declared local

src/dsql/DdlNodes.epp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7076,17 +7076,17 @@ DdlNode* RelationNode::dsqlPass(DsqlCompilerScratch* dsqlScratch)
70767076
case Clause::TYPE_ALTER_PUBLICATION:
70777077
break;
70787078

7079-
case Clause::TYPE_ADD_PACKAGED_TABLE_INDEX:
7079+
case Clause::TYPE_ADD_INLINE_TABLE_INDEX:
70807080
{
7081-
auto addPackagedTableIndexClause = static_cast<AddPackagedTableIndexClause*>(clause.getObject());
7081+
auto addInlineTableIndexClause = static_cast<AddInlineTableIndexClause*>(clause.getObject());
70827082

7083-
if (!addPackagedTableIndexClause->indexNode->relation)
7083+
if (!addInlineTableIndexClause->indexNode->relation)
70847084
{
7085-
addPackagedTableIndexClause->indexNode->relation = FB_NEW_POOL(dsqlScratch->getPool())
7085+
addInlineTableIndexClause->indexNode->relation = FB_NEW_POOL(dsqlScratch->getPool())
70867086
RelationSourceNode(dsqlScratch->getPool(), name);
70877087
}
70887088

7089-
addPackagedTableIndexClause->indexNode->dsqlPass(dsqlScratch);
7089+
addInlineTableIndexClause->indexNode->dsqlPass(dsqlScratch);
70907090
break;
70917091
}
70927092

@@ -7335,7 +7335,7 @@ void RelationNode::validateLttClauses(const Array<NestConst<Clause>>& clauses)
73357335
"Only NOT NULL constraints without names are supported on LOCAL TEMPORARY TABLEs");
73367336
break;
73377337

7338-
case Clause::TYPE_ADD_PACKAGED_TABLE_INDEX:
7338+
case Clause::TYPE_ADD_INLINE_TABLE_INDEX:
73397339
break;
73407340

73417341
default:
@@ -9864,14 +9864,14 @@ void CreateRelationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScrat
98649864
static_cast<AddConstraintClause*>(i->getObject()), constraints);
98659865
break;
98669866

9867-
case Clause::TYPE_ADD_PACKAGED_TABLE_INDEX:
9867+
case Clause::TYPE_ADD_INLINE_TABLE_INDEX:
98689868
{
9869-
auto addPackagedTableIndexClause = static_cast<AddPackagedTableIndexClause*>(i->getObject());
9870-
const auto indexNode = addPackagedTableIndexClause->indexNode;
9869+
auto addInlineTableIndexClause = static_cast<AddInlineTableIndexClause*>(i->getObject());
9870+
const auto indexNode = addInlineTableIndexClause->indexNode;
98719871

98729872
if (!indexNode->relation)
98739873
{
9874-
addPackagedTableIndexClause->indexNode->relation = FB_NEW_POOL(dsqlScratch->getPool())
9874+
addInlineTableIndexClause->indexNode->relation = FB_NEW_POOL(dsqlScratch->getPool())
98759875
RelationSourceNode(dsqlScratch->getPool(), name);
98769876
}
98779877

@@ -9893,7 +9893,7 @@ void CreateRelationNode::execute(thread_db* tdbb, DsqlCompilerScratch* dsqlScrat
98939893
}
98949894

98959895
indexList.push(CreateIndexNode::store(tdbb, indexList.getPool(), transaction,
9896-
addPackagedTableIndexClause->indexNode->name, definition));
9896+
addInlineTableIndexClause->indexNode->name, definition));
98979897

98989898
break;
98999899
}

src/dsql/DdlNodes.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ class RelationNode : public DdlNode
14591459
TYPE_DROP_CONSTRAINT,
14601460
TYPE_ALTER_SQL_SECURITY,
14611461
TYPE_ALTER_PUBLICATION,
1462-
TYPE_ADD_PACKAGED_TABLE_INDEX
1462+
TYPE_ADD_INLINE_TABLE_INDEX
14631463
};
14641464

14651465
explicit Clause(MemoryPool& p, Type aType) noexcept
@@ -1487,10 +1487,10 @@ class RelationNode : public DdlNode
14871487
unsigned deleteAction;
14881488
};
14891489

1490-
struct AddPackagedTableIndexClause : public Clause
1490+
struct AddInlineTableIndexClause : public Clause
14911491
{
1492-
explicit AddPackagedTableIndexClause(MemoryPool& p, CreateIndexNode* aIndexNode)
1493-
: Clause(p, TYPE_ADD_PACKAGED_TABLE_INDEX),
1492+
explicit AddInlineTableIndexClause(MemoryPool& p, CreateIndexNode* aIndexNode)
1493+
: Clause(p, TYPE_ADD_INLINE_TABLE_INDEX),
14941494
indexNode(aIndexNode)
14951495
{
14961496
}

0 commit comments

Comments
 (0)