Reputation: 3181
I'm trying to implement the same idea of Natural Join in Database but on Two dimensional Arrays, so What I'm trying to do is if I have
A={[a,b],[a',b']} and B={[b,c], [b',c],[b,c']}
The result of NaturalJoin(A,B) should be:
Result = {[a,b,c], [a,b,c'],[a',b',c]}
So after I find the shared column B and compare it in both arrays, how can I combine the rows? Can you please give me some hints on how to create the joinedTableau as I don't know the number of rows from the beginning of the join, how can I create it dynamically?
This is my pseudo code:
int[][] array1;
int[][] array2;
int shared = prtiallyEqualColumn(array1,array2);
for(int i = 0; i <array1.length; i++)
{
for(int j = 0 ; j < array2.length; j++)
{
if(array1[i][shared] == array2[j][shared])
{
for(int s = 0 ; s < shared; s++)
{
joinedTableau[joinedCountRow][s] = array1[i][s];
}
for(int y=shared+1; y<array2.length;y++)
{
joinedTableau[joinedCountRow][y] = array2[j][y];
}
}
}
}
Upvotes: 0
Views: 664
Reputation: 524
This yields the right answer. is it the most efficient? I posted the results of my timing. definitely exponential growth in times
count | avg time |
---|---|
10 | 1.931190E-05 |
100 | 4.734993E-04 |
1000 | 2.540604E-02 |
10000 | 1.400114E+00 |
100000 | 9.096992E+01 |
#!python3
from time import time
def natural_join( S, T):
theJoin = []
for i in T:
for j in S:
if i[0] == j[1]:
theJoin.append((i[1], j[0], j[1]))
break
return theJoin
for n in range(1, 6):
A = []
B = []
for i in range(10 ** n):
A.append(('x' + str(i), 'y' + str(i)))
for i in range(10 ** n):
B.append(('y' + str(i), 'z' + str(i)))
start = time()
joined = natural_join(A ,B)
end = time()
print('{:d}, {:E}'.format(10 ** n, (end-start)/n))
Upvotes: 0
Reputation: 19168
I don't know what you've done in the code as you have hidden several implementations from the code presented here in the question. I am giving you the algo :-
Each column value of array1 must be compared with each row value of array2 to produce a natural join,only in case if they are equal, else not.
a1 = array1.countRow();
a2 = array1.countColumn();
b1 = array2.countRow();
b2 = array2.countColumn();
i = j = 1;
while(i<=a1)
while(j<=b1)
if(array1[i][a2]==array2[j][1]) // I've made array-indices start from 1
// perform natural-join operation between array1[i]+array2[j]-common_element
// Similarly iterate the array for next passes.
If there is some mistake or something which is unclear to you,please notify me. Good luck for your code.
Upvotes: 1