1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9#include "monetdb_config.h"
10#include "projectionpath.h"
11
12str
13ALGprojectionpath(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
14{
15 int i;
16 bat bid;
17 bat *r = getArgReference_bat(stk, pci, 0);
18 BAT *b, **joins = (BAT**)GDKzalloc(pci->argc * sizeof(BAT*));
19
20 (void) mb;
21 (void) cntxt;
22
23 if(pci->argc <= 1)
24 throw(MAL, "algebra.projectionpath", SQLSTATE(HY001) "INTERNAL ERROR");
25 if ( joins == NULL)
26 throw(MAL, "algebra.projectionpath", SQLSTATE(HY001) MAL_MALLOC_FAIL);
27 for (i = pci->retc; i < pci->argc; i++) {
28 bid = *getArgReference_bat(stk, pci, i);
29 b = BATdescriptor(bid);
30 if (b == NULL || (i + 1 < pci->argc && ATOMtype(b->ttype) != TYPE_oid)) {
31 while (--i >= pci->retc)
32 BBPunfix(joins[i - pci->retc]->batCacheid);
33 GDKfree(joins);
34 throw(MAL, "algebra.projectionpath", "%s", b ? SEMANTIC_TYPE_MISMATCH : INTERNAL_BAT_ACCESS);
35 }
36 joins[i - pci->retc] = b;
37 }
38 joins[pci->argc - pci->retc] = NULL;
39 b = BATprojectchain(joins);
40 for (i = pci->retc; i < pci->argc; i++)
41 BBPunfix(joins[i - pci->retc]->batCacheid);
42 GDKfree(joins);
43 if ( b)
44 BBPkeepref( *r = b->batCacheid);
45 else
46 throw(MAL, "algebra.projectionpath", INTERNAL_OBJ_CREATE);
47 return MAL_SUCCEED;
48}
49