Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 04:11 AM
pfa
 
Posts: n/a
Default using nextval in external udf

I have a udf which returns a table. I was hoping to access the NEXTVAL
from a sequence to affect the logic flow and consequently the return
result. I'm using a LANGUAGE C type function.

Here's the fetch snippet

case SQLUDF_TF_FETCH:
/* fetch next row */
{
char * nextid = myrecids++;
char * ptr;
--pScratArea->recids_len;
if (pScratArea->recids_len < 1)
{
/* SQLUDF_STATE is part of SQLUDF_TRAIL_ARGS_ALL */
strcpy(outRecid, "");
strcpy(SQLUDF_STATE, "02000");
break;
}
// look for AM and terminate nextid
for (ptr = nextid; *ptr != '\376'; ++ptr) --pScratArea->recids_len;
*(ptr) = '\0';
myrecids = ptr + 1;

// copy current null terminated ptr to outRecid (return arg)
strcpy(outRecid, nextid);
}

*recidNullInd = 0;
/* next row of data */
pScratArea->file_pos++;
break;


What I'm hoping to do is use the result of a NEXTVAL call to cross
check against a counter in my scratch pad such that multiple process
can be feeding off this function table each row from the function table
would only be processed once.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 04:11 AM
Knut Stolze
 
Posts: n/a
Default Re: using nextval in external udf

pfa wrote:

> I have a udf which returns a table. I was hoping to access the NEXTVAL
> from a sequence to affect the logic flow and consequently the return
> result. I'm using a LANGUAGE C type function.
>
> Here's the fetch snippet
>
> case SQLUDF_TF_FETCH:
> /* fetch next row */
> {
> char * nextid = myrecids++;
> char * ptr;
> --pScratArea->recids_len;
> if (pScratArea->recids_len < 1)
> {
> /* SQLUDF_STATE is part of SQLUDF_TRAIL_ARGS_ALL */
> strcpy(outRecid, "");
> strcpy(SQLUDF_STATE, "02000");
> break;
> }
> // look for AM and terminate nextid
> for (ptr = nextid; *ptr != '\376'; ++ptr) --pScratArea->recids_len;
> *(ptr) = '\0';
> myrecids = ptr + 1;
>
> // copy current null terminated ptr to outRecid (return arg)
> strcpy(outRecid, nextid);
> }
>
> *recidNullInd = 0;
> /* next row of data */
> pScratArea->file_pos++;
> break;
>
>
> What I'm hoping to do is use the result of a NEXTVAL call to cross
> check against a counter in my scratch pad such that multiple process
> can be feeding off this function table each row from the function table
> would only be processed once.


I still don't quite understand your scenario, but you could use embedded SQL
and simply query the sequence that way. You have to register the UDF with
READS SQL DATA, however.

--
Knut Stolze
Information Integration Development
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 04:11 AM
pfa
 
Posts: n/a
Default Re: using nextval in external udf

Hmmm ok, can't be done via CLI? i.e. are there any handles available?

Knut Stolze wrote:
> pfa wrote:
>
> > I have a udf which returns a table. I was hoping to access the NEXTVAL
> > from a sequence to affect the logic flow and consequently the return
> > result. I'm using a LANGUAGE C type function.
> >
> > Here's the fetch snippet
> >
> > case SQLUDF_TF_FETCH:
> > /* fetch next row */
> > {
> > char * nextid = myrecids++;
> > char * ptr;
> > --pScratArea->recids_len;
> > if (pScratArea->recids_len < 1)
> > {
> > /* SQLUDF_STATE is part of SQLUDF_TRAIL_ARGS_ALL */
> > strcpy(outRecid, "");
> > strcpy(SQLUDF_STATE, "02000");
> > break;
> > }
> > // look for AM and terminate nextid
> > for (ptr = nextid; *ptr != '\376'; ++ptr) --pScratArea->recids_len;
> > *(ptr) = '\0';
> > myrecids = ptr + 1;
> >
> > // copy current null terminated ptr to outRecid (return arg)
> > strcpy(outRecid, nextid);
> > }
> >
> > *recidNullInd = 0;
> > /* next row of data */
> > pScratArea->file_pos++;
> > break;
> >
> >
> > What I'm hoping to do is use the result of a NEXTVAL call to cross
> > check against a counter in my scratch pad such that multiple process
> > can be feeding off this function table each row from the function table
> > would only be processed once.

>
> I still don't quite understand your scenario, but you could use embedded SQL
> and simply query the sequence that way. You have to register the UDF with
> READS SQL DATA, however.
>
> --
> Knut Stolze
> Information Integration Development
> IBM Germany / University of Jena


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 04:11 AM
pfa
 
Posts: n/a
Default Re: using nextval in external udf

Perhaps there's an alternative way to achieve my end result. I'll try
to explain better:

We have a program which "programmatically" builds a selection of keys
to be processed at a later date by multiple processes run in parallel.
As this list is "potentially" too big to be used in a WHERE key IN
(...) I figured a function table which extracts each key and returns it
as a row would work better:

e.g. SELECT key u, data_column t from udftable('listfile') u, table t
WHERE u.key = t.key

(btw 'listfile' is currently a file on the OS which the udftable reads
in and scans through looking for delimiters end returning a null
terminated char * as the key, keeping the position in the list for the
next fetch)

Each process run in parallel feeds of the above SELECT (now this is a
work in progress project so may not be the best way to go...we are not
DB2 experts) and processes the data_column contents in some way. So the
catch is that each row in "table t" must only be processed once so a
got the idea of using a NEXTVAL sequence from DB2 so that each process
when issuing a FETCH would get a unique key u, data_column t result
because the udftable's function would skip keys in the list until the
NEXTVAL position has been reached.

The only other way I could see to do this was having a separate process
(MQ series perhaps? never used it) which has this cursor and the other
processes would fetch from it ensuring each row is only processed once.

Splitting the list up is not an option as the number of processes
started is up to the customer and they can start more after the others
are already running.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 04:11 AM
Serge Rielau
 
Posts: n/a
Default Re: using nextval in external udf

pfa wrote:
> Perhaps there's an alternative way to achieve my end result. I'll try
> to explain better:
>
> We have a program which "programmatically" builds a selection of keys
> to be processed at a later date by multiple processes run in parallel.
> As this list is "potentially" too big to be used in a WHERE key IN
> (...) I figured a function table which extracts each key and returns it
> as a row would work better:
>
> e.g. SELECT key u, data_column t from udftable('listfile') u, table t
> WHERE u.key = t.key
>
> (btw 'listfile' is currently a file on the OS which the udftable reads
> in and scans through looking for delimiters end returning a null
> terminated char * as the key, keeping the position in the list for the
> next fetch)
>
> Each process run in parallel feeds of the above SELECT (now this is a
> work in progress project so may not be the best way to go...we are not
> DB2 experts) and processes the data_column contents in some way. So the
> catch is that each row in "table t" must only be processed once so a
> got the idea of using a NEXTVAL sequence from DB2 so that each process
> when issuing a FETCH would get a unique key u, data_column t result
> because the udftable's function would skip keys in the list until the
> NEXTVAL position has been reached.
>
> The only other way I could see to do this was having a separate process
> (MQ series perhaps? never used it) which has this cursor and the other
> processes would fetch from it ensuring each row is only processed once.
>
> Splitting the list up is not an option as the number of processes
> started is up to the customer and they can start more after the others
> are already running.
>

The solution of the table-fucntion is a no go. Check out the NO PARALLEL
option - which happens to be mandatory.
You will not achieve the parallelizing (sp?) effect you plan to achieve.
If DB2 (hypothetically) were to support "partitioned" (which you want)
or "replicated" (which you don't want) table functions. The likely way
to achieve your goal would lie in an extension of the DBINFO structure
with the partition number. So you could partition the file.

Now, the problem you are facing in not new by any means. It often
appears during data cleansing in a warehouse.
In these cases scripts ar eused to fire the same query from each data
node (using the DB2NODE export variable to connect) and pass the db
partition number as an argument to the table function.
It's not as pretty as having teh optimizer do the job, but it works
quite well.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 04:12 AM
Knut Stolze
 
Posts: n/a
Default Re: using nextval in external udf

pfa wrote:

> Hmmm ok, can't be done via CLI? i.e. are there any handles available?


You can use CLI. There is a description in the Application Development
Guide that explains how to obtain the (default) connection handle inside
the UDF for the current connection.

--
Knut Stolze
Information Integration Development
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 04:12 AM
Knut Stolze
 
Posts: n/a
Default Re: using nextval in external udf

pfa wrote:

> Perhaps there's an alternative way to achieve my end result. I'll try
> to explain better:
>
> We have a program which "programmatically" builds a selection of keys
> to be processed at a later date by multiple processes run in parallel.
> As this list is "potentially" too big to be used in a WHERE key IN
> (...) I figured a function table which extracts each key and returns it
> as a row would work better:
>
> e.g. SELECT key u, data_column t from udftable('listfile') u, table t
> WHERE u.key = t.key
>
> (btw 'listfile' is currently a file on the OS which the udftable reads
> in and scans through looking for delimiters end returning a null
> terminated char * as the key, keeping the position in the list for the
> next fetch)
>
> Each process run in parallel feeds of the above SELECT (now this is a
> work in progress project so may not be the best way to go...we are not
> DB2 experts) and processes the data_column contents in some way. So the
> catch is that each row in "table t" must only be processed once so a
> got the idea of using a NEXTVAL sequence from DB2 so that each process
> when issuing a FETCH would get a unique key u, data_column t result
> because the udftable's function would skip keys in the list until the
> NEXTVAL position has been reached.
>
> The only other way I could see to do this was having a separate process
> (MQ series perhaps? never used it) which has this cursor and the other
> processes would fetch from it ensuring each row is only processed once.
>
> Splitting the list up is not an option as the number of processes
> started is up to the customer and they can start more after the others
> are already running.


My first idea would have been to use a temp table. You populate the table
once and then the different processes could use sequences to coordinate
which process operates on which rows. However, that really depends on the
functionality of the table function and copying its results into a temp
table might not be preferred and Serge's suggestion the way to go.

--
Knut Stolze
Information Integration Development
IBM Germany / University of Jena
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-27-2008, 04:12 AM
Phil Sherman
 
Posts: n/a
Default Re: using nextval in external udf

Did you consider having the process that creates the keys generate
separate files for each of the "worker" processes? It would mean you'd
need a separate procedure to start each of the multiple tasks, or use a
parameter which will be [part of] the file name.

Extending this to a database should be possible by setting up a control
table with an identifier for each of the multiple tasks. A predicate
indicating the task id will separate out the rows for processing.

Either of these should work with "a large number of rows to process" and
have all of the tasks complete in around the same amount of time. If you
truely need to have the tasks process on a "do the next input key"
sequence then the following MIGHT be a way to do this:

Create your keys table each day with a column containing a numeric key
starting with 1. You'll need a unique index on the numeric key column.
After creating the keys table, create a control table having an
autogenerated identity column starting with 1. This table needs no
columns other than the identity column and should not be indexed. It
must have row level locking. Each task inserts a row into the control
table then retrieves the identity key for the inserted row. This key is
used to do a single row retrieval from the keys table. This keys table
retrieval should include an "OPTIMIZE FOR 1 ROWS" clause. Commits must
be taken at appropriate intervals to avoid lock escalation on the
control table.


Phil Sherman








pfa wrote:
> Perhaps there's an alternative way to achieve my end result. I'll try
> to explain better:
>
> We have a program which "programmatically" builds a selection of keys
> to be processed at a later date by multiple processes run in parallel.
> As this list is "potentially" too big to be used in a WHERE key IN
> (...) I figured a function table which extracts each key and returns it
> as a row would work better:
>
> e.g. SELECT key u, data_column t from udftable('listfile') u, table t
> WHERE u.key = t.key
>
> (btw 'listfile' is currently a file on the OS which the udftable reads
> in and scans through looking for delimiters end returning a null
> terminated char * as the key, keeping the position in the list for the
> next fetch)
>
> Each process run in parallel feeds of the above SELECT (now this is a
> work in progress project so may not be the best way to go...we are not
> DB2 experts) and processes the data_column contents in some way. So the
> catch is that each row in "table t" must only be processed once so a
> got the idea of using a NEXTVAL sequence from DB2 so that each process
> when issuing a FETCH would get a unique key u, data_column t result
> because the udftable's function would skip keys in the list until the
> NEXTVAL position has been reached.
>
> The only other way I could see to do this was having a separate process
> (MQ series perhaps? never used it) which has this cursor and the other
> processes would fetch from it ensuring each row is only processed once.
>
> Splitting the list up is not an option as the number of processes
> started is up to the customer and they can start more after the others
> are already running.
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:56 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709