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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.XMLOptimizerTraceTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.io.File;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import junit.framework.Test;
import org.apache.derby.iapi.services.info.JVMInfo;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.SupportFilesSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* <p>
* Test xml-based optimizer tracing, introduced by DERBY-6211.
* </p>
*/
public class XMLOptimizerTraceTest extends GeneratedColumnsHelper
{
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTANTS
//
///////////////////////////////////////////////////////////////////////////////////
private static final String TRACE_FILE_NAME = "xott.xml";
private static final String SAVED_TRACE_NAME = "xmlOptimizer.trace";
private static final String FILE_EXISTS = "XIE0S";
///////////////////////////////////////////////////////////////////////////////////
//
// STATE
//
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance.
*/
public XMLOptimizerTraceTest(String name)
{
super(name);
}
///////////////////////////////////////////////////////////////////////////////////
//
// JUnit BEHAVIOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Construct top level suite in this JUnit test
*/
public static Test suite()
{
String[] testFiles = new String[] { "functionTests/tests/lang/" + SAVED_TRACE_NAME };
BaseTestSuite suite = new BaseTestSuite("XMLOptimizerTraceTest");
suite.addTest( TestConfiguration.defaultSuite( XMLOptimizerTraceTest.class ) );
return new SupportFilesSetup( TestConfiguration.singleUseDatabaseDecorator( suite ), testFiles );
}
protected void setUp()
throws Exception
{
super.setUp();
Connection conn = getConnection();
if ( !routineExists( conn, "INTEGERLIST" ) )
{
goodStatement
(
conn,
"create function integerList()\n" +
"returns table( a int, b int, c int, d int )\n" +
"language java parameter style derby_jdbc_result_set no sql\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.lang.RestrictedVTITest.integerList'\n"
);
}
if ( !routineExists( conn, "GETRESULTSETMETADATA" ) )
{
goodStatement
(
conn,
"create function getResultSetMetaData( query varchar( 32672 ) )\n" +
"returns table\n" +
"(\n" +
" getCatalogName varchar( 32672 ),\n" +
" getColumnClassName varchar( 32672 ),\n" +
" getColumnDisplaySize int,\n" +
" getColumnLabel varchar( 32672 ),\n" +
" getColumnName varchar( 32672 ),\n" +
" getColumnType int,\n" +
" getColumnTypeName varchar( 32672 ),\n" +
" getPrecision int,\n" +
" getScale int,\n" +
" getSchemaName varchar( 32672 ),\n" +
" getTableName varchar( 32672 ),\n" +
" isAutoIncrement boolean,\n" +
" isCaseSensitive boolean,\n" +
" isCurrency boolean,\n" +
" isDefinitelyWritable boolean,\n" +
" isNullable int,\n" +
" isReadOnly boolean,\n" +
" isSearchable boolean,\n" +
" isSigned boolean,\n" +
" isWritable boolean\n" +
")\n" +
"language java parameter style derby_jdbc_result_set reads sql data\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.lang.RSMDWrapper.getResultSetMetaData'\n"
);
}
if ( !tableExists( conn, "T" ) )
{
goodStatement
(
conn,
"create table t( a int, b varchar( 100 ) )"
);
goodStatement
(
conn,
"create index t_a on t( a )"
);
}
if ( !tableExists( conn, "S" ) )
{
goodStatement
(
conn,
"create table s( a int, b varchar( 100 ) )"
);
goodStatement
(
conn,
"create index s_a on s( a )"
);
}
if ( !tableExists( conn, "R" ) )
{
goodStatement
(
conn,
"create table r( a int, b varchar( 100 ) )"
);
goodStatement
(
conn,
"create index r_a on r( a )"
);
}
if ( !tableExists( conn, "T1" ) )
{
goodStatement
(
conn,
"create table t1( c1 int, c2 int, c3 int )"
);
}
if ( !tableExists( conn, "T2" ) )
{
goodStatement
(
conn,
"create table t2( c1 int, c2 int, c3 int )"
);
}
if ( !tableExists( conn, "T3" ) )
{
goodStatement
(
conn,
"create table t3( c1 int, c2 int, c3 int )"
);
}
}
///////////////////////////////////////////////////////////////////////////////////
//
// TESTS
//
///////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Test the planCost table function.
* </p>
*/
public void test_01_planCost() throws Exception
{
Connection conn = getConnection();
File traceFile = SupportFilesSetup.getReadWrite( TRACE_FILE_NAME );
SupportFilesSetup.deleteFile( traceFile );
// turn on xml-based optimizer tracing and run some queries
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'xml' )"
);
// 2-table query
goodStatement
(
conn,
"select s.a from t, s where t.a = s.a"
);
// 3-table query
goodStatement
(
conn,
"select s.a from t, s, r where t.a = s.a and s.a = r.a"
);
// query involving a table function
goodStatement
(
conn,
"select s.a from s, table( integerList() ) i where s.a = i.a"
);
// turn off optimizer tracing and dump the xml trace to a file
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', false, '" + traceFile.getPath() + "' )"
);
// install the planCost table function and view
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracingViews', true, '" + traceFile.getPath() + "' )"
);
// verify the full signature of the planCost table function
assertResults
(
conn,
"select getColumnName, getColumnTypeName, getPrecision from table( getResultSetMetaData( 'select * from planCost where 1=2' ) ) g",
new String[][]
{
{ "TEXT", "VARCHAR", "32672" },
{ "STMTID", "INTEGER", "10" },
{ "QBID", "INTEGER", "10" },
{ "COMPLETE", "BOOLEAN", "1" },
{ "SUMMARY", "VARCHAR", "32672" },
{ "TYPE", "VARCHAR", "50" },
{ "ESTIMATEDCOST", "DOUBLE", "15" },
{ "ESTIMATEDROWCOUNT", "BIGINT", "19" },
},
false
);
// verify some contents of the xml output which we hope will remain stable
// across test platforms
assertResults
(
conn,
"select distinct stmtID, summary from planCost where complete order by stmtID, summary",
new String[][]
{
{ "1", "( \"APP\".\"S_A\" # \"APP\".\"T_A\" )" },
{ "1", "( \"APP\".\"T_A\" # \"APP\".\"S_A\" )" },
{ "2", "( ( \"APP\".\"R_A\" # \"APP\".\"S_A\" ) * \"APP\".\"T_A\" )" },
{ "2", "( ( \"APP\".\"R_A\" # \"APP\".\"T_A\" ) * \"APP\".\"S_A\" )" },
{ "2", "( ( \"APP\".\"S_A\" # \"APP\".\"R_A\" ) * \"APP\".\"T_A\" )" },
{ "2", "( ( \"APP\".\"S_A\" # \"APP\".\"T_A\" ) * \"APP\".\"R_A\" )" },
{ "2", "( ( \"APP\".\"T_A\" # \"APP\".\"R_A\" ) * \"APP\".\"S_A\" )" },
{ "2", "( ( \"APP\".\"T_A\" # \"APP\".\"S_A\" ) * \"APP\".\"R_A\" )" },
{ "3", "( \"APP\".\"INTEGERLIST\"() # \"APP\".\"S_A\" )" },
{ "3", "( \"APP\".\"S_A\" # \"APP\".\"INTEGERLIST\"() )" },
},
false
);
// uninstall the planCost table function and view
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracingViews', false )"
);
// use planCost to examine an outer join
vetOuterJoin( conn );
// verify that you can't overwrite an existing file with xml output (DERBY-6635)
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'xml' )"
);
expectExecutionError
(
conn,
FILE_EXISTS,
"call syscs_util.syscs_register_tool( 'optimizerTracing', false, '" + traceFile.getPath() + "' )"
);
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', false )"
);
}
/**
* <p>
* Some general tests for XmlVTI.
* </p>
*/
public void test_02_xmlVTI() throws Exception
{
Connection conn = getConnection();
File traceFile = SupportFilesSetup.getReadOnly( SAVED_TRACE_NAME );
URL traceURL = SupportFilesSetup.getReadOnlyURL( SAVED_TRACE_NAME );
String[][] resultsParentAndChild = new String[][]
{
{ "1", "R_A", "HASH", "20.1395", "6" },
{ "1", "R_A", "NESTEDLOOP", "20.039500000000004", "6" },
{ "1", "S_A", "HASH", "20.1395", "6" },
{ "1", "S_A", "NESTEDLOOP", "20.039500000000004", "6" },
{ "1", "T_A", "HASH", "20.1395", "6" },
{ "1", "T_A", "NESTEDLOOP", "20.039500000000004", "6" },
};
String[][] resultsChildOnly = new String[][]
{
{ "R_A", "HASH", "20.1395", "6" },
{ "R_A", "NESTEDLOOP", "20.039500000000004", "6" },
{ "S_A", "HASH", "20.1395", "6" },
{ "S_A", "NESTEDLOOP", "20.039500000000004", "6" },
{ "T_A", "HASH", "20.1395", "6" },
{ "T_A", "NESTEDLOOP", "20.039500000000004", "6" },
};
// create the type and factory function needed by the XmlVTI
goodStatement
(
conn,
"create type ArrayList external name 'java.util.ArrayList' language java"
);
goodStatement
(
conn,
"create function asList( cell varchar( 32672 ) ... ) returns ArrayList\n" +
"language java parameter style derby no sql\n" +
"external name 'org.apache.derby.vti.XmlVTI.asList'\n"
);
// create an XmlVTI which reads from a file and incorporates parent tags
goodStatement
(
conn,
"create function decorationWithParentInfo\n" +
"(\n" +
" fileName varchar( 32672 ),\n" +
" rowTag varchar( 32672 ),\n" +
" parentTags ArrayList,\n" +
" childTags ArrayList\n" +
")\n" +
"returns table\n" +
"(\n" +
" qbID int,\n" +
" conglomerateName varchar( 36 ),\n" +
" joinStrategy varchar( 20 ),\n" +
" estimatedCost double,\n" +
" estimatedRowCount int\n" +
")\n" +
"language java parameter style derby_jdbc_result_set no sql\n" +
"external name 'org.apache.derby.vti.XmlVTI.xmlVTI'\n"
);
goodStatement
(
conn,
"create view decorationWithParentInfo as\n" +
"select * from table\n" +
"(\n" +
" decorationWithParentInfo\n" +
" (\n" +
" '" + traceFile.getPath() + "',\n" +
" 'decoration',\n" +
" asList( 'qbID' ),\n" +
" asList( 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount' )\n" +
" )\n" +
") v\n"
);
// create an XmlVTI which reads from a file and only used child tags
goodStatement
(
conn,
"create function decorationChildOnly\n" +
"(\n" +
" fileName varchar( 32672 ),\n" +
" rowTag varchar( 32672 ),\n" +
" childTags varchar( 32672 )...\n" +
")\n" +
"returns table\n" +
"(\n" +
" conglomerateName varchar( 36 ),\n" +
" joinStrategy varchar( 20 ),\n" +
" estimatedCost double,\n" +
" estimatedRowCount int\n" +
")\n" +
"language java parameter style derby_jdbc_result_set no sql\n" +
"external name 'org.apache.derby.vti.XmlVTI.xmlVTI'\n"
);
goodStatement
(
conn,
"create view decorationChildOnly as\n" +
"select * from table\n" +
"(\n" +
" decorationChildOnly\n" +
" (\n" +
" '" + traceFile.getPath() + "',\n" +
" 'decoration',\n" +
" 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount'\n" +
" )\n" +
") v\n"
);
// create an XmlVTI which reads from an url file and uses parent tags
goodStatement
(
conn,
"create function decorationURLParentInfo\n" +
"(\n" +
" urlString varchar( 32672 ),\n" +
" rowTag varchar( 32672 ),\n" +
" parentTags ArrayList,\n" +
" childTags ArrayList\n" +
")\n" +
"returns table\n" +
"(\n" +
" qbID int,\n" +
" conglomerateName varchar( 36 ),\n" +
" joinStrategy varchar( 20 ),\n" +
" estimatedCost double,\n" +
" estimatedRowCount int\n" +
")\n" +
"language java parameter style derby_jdbc_result_set no sql\n" +
"external name 'org.apache.derby.vti.XmlVTI.xmlVTIFromURL'\n"
);
goodStatement
(
conn,
"create view decorationURLParentInfo as\n" +
"select * from table\n" +
"(\n" +
" decorationURLParentInfo\n" +
" (\n" +
" '" + traceURL.toString() + "',\n" +
" 'decoration',\n" +
" asList( 'qbID' ),\n" +
" asList( 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount' )\n" +
" )\n" +
") v\n"
);
// create an XmlVTI which reads from an url file and uses only child tags
goodStatement
(
conn,
"create function decorationURLChildOnly\n" +
"(\n" +
" urlString varchar( 32672 ),\n" +
" rowTag varchar( 32672 ),\n" +
" childTags varchar( 32672 )...\n" +
")\n" +
"returns table\n" +
"(\n" +
" conglomerateName varchar( 36 ),\n" +
" joinStrategy varchar( 20 ),\n" +
" estimatedCost double,\n" +
" estimatedRowCount int\n" +
")\n" +
"language java parameter style derby_jdbc_result_set no sql\n" +
"external name 'org.apache.derby.vti.XmlVTI.xmlVTIFromURL'\n"
);
goodStatement
(
conn,
"create view decorationURLChildOnly as\n" +
"select * from table\n" +
"(\n" +
" decorationURLChildOnly\n" +
" (\n" +
" '" + traceURL.toString() + "',\n" +
" 'decoration',\n" +
" 'decConglomerateName', 'decJoinStrategy', 'ceEstimatedCost', 'ceEstimatedRowCount'\n" +
" )\n" +
") v\n"
);
// verify that the XmlVTIs work
assertResults
(
conn,
"select distinct qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n" +
"from decorationWithParentInfo\n" +
"where conglomerateName like '%_A' and estimatedCost is not null\n" +
"order by qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
resultsParentAndChild,
false
);
assertResults
(
conn,
"select distinct conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n" +
"from decorationChildOnly\n" +
"where conglomerateName like '%_A' and estimatedCost is not null\n" +
"order by conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
resultsChildOnly,
false
);
assertResults
(
conn,
"select distinct qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n" +
"from decorationURLParentInfo\n" +
"where conglomerateName like '%_A' and estimatedCost is not null\n" +
"order by qbID, conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
resultsParentAndChild,
false
);
assertResults
(
conn,
"select distinct conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n" +
"from decorationURLChildOnly\n" +
"where conglomerateName like '%_A' and estimatedCost is not null\n" +
"order by conglomerateName, joinStrategy, estimatedCost, estimatedRowCount\n",
resultsChildOnly,
false
);
// clean up after ourselves
goodStatement( conn, "drop view decorationURLChildOnly" );
goodStatement( conn, "drop function decorationURLChildOnly" );
goodStatement( conn, "drop view decorationURLParentInfo" );
goodStatement( conn, "drop function decorationURLParentInfo" );
goodStatement( conn, "drop view decorationChildOnly" );
goodStatement( conn, "drop function decorationChildOnly" );
goodStatement( conn, "drop view decorationWithParentInfo" );
goodStatement( conn, "drop function decorationWithParentInfo" );
goodStatement( conn, "drop function asList" );
goodStatement( conn, "drop type ArrayList restrict" );
}
/**
* <p>
* Test xml optimizer tracing of outer joins.
* </p>
*/
private void vetOuterJoin( Connection conn ) throws Exception
{
File traceFile = SupportFilesSetup.getReadWrite( TRACE_FILE_NAME );
SupportFilesSetup.deleteFile( traceFile );
// turn on xml-based optimizer tracing
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', true, 'xml' )"
);
// run an outer join
goodStatement
(
conn,
"select * from t3, (t1 left outer join t2 on t1.c1 = t2.c1) where t3.c1 = t1.c1"
);
// turn off optimizer tracing
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracing', false, '" + traceFile.getPath() + "' )"
);
// load the trace viewer
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracingViews', true, '" + traceFile.getPath() + "' )"
);
// verify the plan shapes which were considered
PreparedStatement ps = chattyPrepare
(
conn,
"select distinct summary from planCost\n" +
"where complete and qbID = 1\n" +
"order by summary\n"
);
ResultSet rs = ps.executeQuery();
rs.next();
String summary1 = rs.getString( 1 ).trim();
rs.next();
String summary2 = rs.getString( 1 ).trim();
assertTrue( summary1.startsWith( "( \"APP\"." ) );
assertTrue( summary1.endsWith( " * ProjectRestrictNode )" ) );
assertTrue( summary2.startsWith( "( ProjectRestrictNode # \"APP\"." ) );
rs.close();
ps.close();
// unload the trace viewer
goodStatement
(
conn,
"call syscs_util.syscs_register_tool( 'optimizerTracingViews', false )"
);
}
///////////////////////////////////////////////////////////////////////////////////
//
// MINIONS
//
///////////////////////////////////////////////////////////////////////////////////
/** Return true if the SQL routine exists */
private boolean routineExists( Connection conn, String functionName ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, "select count (*) from sys.sysaliases where alias = ?" );
ps.setString( 1, functionName );
ResultSet rs = ps.executeQuery();
rs.next();
boolean retval = rs.getInt( 1 ) > 0 ? true : false;
rs.close();
ps.close();
return retval;
}
/** Return true if the table exists */
private boolean tableExists( Connection conn, String tableName ) throws Exception
{
PreparedStatement ps = chattyPrepare( conn, "select count (*) from sys.systables where tablename = ?" );
ps.setString( 1, tableName );
ResultSet rs = ps.executeQuery();
rs.next();
boolean retval = rs.getInt( 1 ) > 0 ? true : false;
rs.close();
ps.close();
return retval;
}
}
|