> -- $Id: //open/dev/farrago/examples/rng/unitsql/testPlugin.ref#26 $
> -- Test usage of RNG plugin
> 
> -- verify that without plugin enabled, custom syntax is unrecognized
> 
> create schema rngtest;
> set schema 'rngtest';
> set path 'rngtest';
> 
> -- should fail
> create rng rng1 external '${FARRAGO_HOME}/testgen/rng1.dat' seed 999;
Error: org.eigenbase.sql.parser.SqlParseException: Encountered "rng" at line 1, column 8.
Was expecting one of:
    "CLUSTERED" ...
    "INDEX" ...
    "LABEL" ...
    "OR" ...
    "ROLE" ...
    "SCHEMA" ...
    "SERVER" ...
    "USER" ...
    "LOCAL" ...
    "FOREIGN" ...
    "TABLE" ...
    "GLOBAL" ...
    "VIEW" ...
    "FUNCTION" ...
    "PROCEDURE" ...
    "SPECIFIC" ...
    "JAR" ...
    "TYPE" ...
    "ORDERING" ... (state=,code=0)
> 
> 
> -- now, enable plugin personality for this session
> alter session implementation set jar sys_boot.sys_boot.rngplugin;
> 
> 
> -- create some random number generators; use seeds to guarantee determinism
> 
> create rng rng1 external '${FARRAGO_HOME}/testgen/rng1.dat' seed 999;
> 
> create rng rng2 external '${FARRAGO_HOME}/testgen/rng2.dat' seed 999;
> 
> create rng rng3 external '${FARRAGO_HOME}/testgen/rng3.dat' seed 777;
> 
> create function rng_next_int(
>     rng_name varchar(512),
>     n int)
> returns int
> language java
> reads sql data
> external name 
> 'sys_boot.sys_boot.rngplugin:net.sf.farrago.rng.FarragoRngUDR.rng_next_int';
> 
> -- test various ways of naming the rng
> 
> values rng_next_int('rng1',10);
+---------+
| EXPR$0  |
+---------+
| 9       |
+---------+
> 
> values rng_next_int('RNG1',10);
+---------+
| EXPR$0  |
+---------+
| 1       |
+---------+
> 
> values rng_next_int('localdb.rngtest."RNG1"',10);
+---------+
| EXPR$0  |
+---------+
| 1       |
+---------+
> 
> values rng_next_int('localdb.rngtest.rng1',10);
+---------+
| EXPR$0  |
+---------+
| 7       |
+---------+
> 
> values rng_next_int('rng1',10);
+---------+
| EXPR$0  |
+---------+
| 2       |
+---------+
> 
> values rng_next_int('rng1',10);
+---------+
| EXPR$0  |
+---------+
| 5       |
+---------+
> 
> -- should fail:  bad schema
> values rng_next_int('sales.rng1',10);
Error: DDL validation error: Reference to unknown random number generator "RNG1" (state=,code=0)
> 
> -- should fail:  no such RNG
> values rng_next_int('rng_nonexistent',10);
Error: DDL validation error: Reference to unknown random number generator "RNG_NONEXISTENT" (state=,code=0)
> 
> 
> -- verify that rng with same initial seed yields same sequence
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 9       |
+---------+
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 1       |
+---------+
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 1       |
+---------+
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 7       |
+---------+
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 2       |
+---------+
> 
> values rng_next_int('rng2',10);
+---------+
| EXPR$0  |
+---------+
| 5       |
+---------+
> 
> 
> -- verify that rng with different initial seed yields different sequence
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 0       |
+---------+
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 6       |
+---------+
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 2       |
+---------+
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 2       |
+---------+
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 5       |
+---------+
> 
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 9       |
+---------+
> 
> 
> -- test fancy syntax
> values next_random_int(ceiling 10 from rng2);
+---------+
| EXPR$0  |
+---------+
| 8       |
+---------+
> 
> values next_random_int(unbounded from rng2);
+------------+
|   EXPR$0   |
+------------+
| 512689831  |
+------------+
> 
> 
> -- test view over rng
> 
> create view random_personality_view as
> values next_random_int(ceiling 10 from rng2);
> 
> create view random_udf_view as
> values rng_next_int('rng2',10);
> 
> select * from random_personality_view;
+---------+
| EXPR$0  |
+---------+
| 1       |
+---------+
> 
> select * from random_udf_view;
+---------+
| EXPR$0  |
+---------+
| 4       |
+---------+
> 
> -- should fail:  dependency
> drop rng rng2 restrict;
Error: Dropping random number generator "RNGTEST"."RNG2" requires CASCADE because other objects still reference it (state=,code=0)
> 
> -- should fail:  SELECT DISTINCT feature is disabled in this personality
> select distinct empno from sales.emps order by empno;
Error: SELECT DISTINCT not supported (state=,code=0)
> 
> -- now, disable plugin personality for this session
> alter session implementation set default;
> 
> -- flush query cache
> call sys_boot.mgmt.flush_code_cache();
> 
> -- verify that DDL personality is wiped out
> -- should fail
> create rng rng4 external '${FARRAGO_HOME}/testgen/rng4.dat' seed 777;
Error: org.eigenbase.sql.parser.SqlParseException: Encountered "rng" at line 1, column 8.
Was expecting one of:
    "CLUSTERED" ...
    "INDEX" ...
    "LABEL" ...
    "OR" ...
    "ROLE" ...
    "SCHEMA" ...
    "SERVER" ...
    "USER" ...
    "LOCAL" ...
    "FOREIGN" ...
    "TABLE" ...
    "GLOBAL" ...
    "VIEW" ...
    "FUNCTION" ...
    "PROCEDURE" ...
    "SPECIFIC" ...
    "JAR" ...
    "TYPE" ...
    "ORDERING" ... (state=,code=0)
> 
> -- verify that we can still access plugin functionality via UDF
> values rng_next_int('rng3',10);
+---------+
| EXPR$0  |
+---------+
| 0       |
+---------+
> 
> -- sorry, view based on personality will no longer work  :(
> select * from random_personality_view;
Error: org.eigenbase.sql.parser.SqlParseException: Encountered "CEILING 10" at line 2, column 34.
Was expecting one of:
    "ALL" ...
    "CURSOR" ...
    "DISTINCT" ...
    "EXISTS" ...
    "NOT" ...
    "ROW" ...
    "(" ...
    "+" ...
    "-" ...
    <UNSIGNED_INTEGER_LITERAL> ...
    <DECIMAL_NUMERIC_LITERAL> ...
    <APPROX_NUMERIC_LITERAL> ...
    <BINARY_STRING_LITERAL> ...
    <PREFIXED_STRING_LITERAL> ...
    <QUOTED_STRING> ...
    <UNICODE_STRING_LITERAL> ...
    "TRUE" ...
    "FALSE" ...
    "UNKNOWN" ...
    "NULL" ...
    <LBRACE_D> ...
    <LBRACE_T> ...
    <LBRACE_TS> ...
    "DATE" ...
    "TIME" ...
    "TIMESTAMP" ...
    "INTERVAL" ...
    "?" ...
    "CAST" ...
    "EXTRACT" ...
    "POSITION" ...
    "CONVERT" ...
    "TRANSLATE" ...
    "OVERLAY" ...
    "FLOOR" ...
    "CEIL" ...
    "CEILING" ...
    "CEILING" "(" ...
    "SUBSTRING" ...
    "TRIM" ...
    <LBRACE_FN> ...
    "MULTISET" ...
    "SPECIFIC" ...
    <IDENTIFIER> ...
    <QUOTED_IDENTIFIER> ...
    <UNICODE_QUOTED_IDENTIFIER> ...
    "ABS" ...
    "AVG" ...
    "CARDINALITY" ...
    "CHAR_LENGTH" ...
    "CHARACTER_LENGTH" ...
    "COALESCE" ...
    "COLLECT" ...
    "CUME_DIST" ...
    "COUNT" ...
    "CURRENT_DATE" ...
    "CURRENT_TIME" ...
    "CURRENT_TIMESTAMP" ...
    "DENSE_RANK" ...
    "ELEMENT" ...
    "EXP" ...
    "FIRST_VALUE" ...
    "FUSION" ...
    "LAST_VALUE" ...
    "LN" ...
    "LOCALTIME" ...
    "LOCALTIMESTAMP" ...
    "LOWER" ...
    "MAX" ...
    "MIN" ...
    "MOD" ...
    "NULLIF" ...
    "OCTET_LENGTH" ...
    "PERCENT_RANK" ...
    "POWER" ...
    "RANK" ...
    "ROW_NUMBER" ...
    "SQRT" ...
    "SUM" ...
    "UPPER" ...
    "CURRENT_CATALOG" ...
    "CURRENT_DEFAULT_TRANSFORM_GROUP" ...
    "CURRENT_PATH" ...
    "CURRENT_ROLE" ...
    "CURRENT_SCHEMA" ...
    "CURRENT_USER" ...
    "SESSION_USER" ...
    "SYSTEM_USER" ...
    "USER" ...
    "NEW" ...
    "CASE" ...
    "*" ...
    ")" ... (state=,code=0)
> 
> -- but view based on UDF will
> select * from random_udf_view;
+---------+
| EXPR$0  |
+---------+
| 5       |
+---------+
> 
> -- verify that DROP CASCADE works correctly even without DDL personality
> -- TODO:  use Java filesystem access to verify creation/deletion of .dat file
> drop schema rngtest cascade;
> drop schema sys_boot.old_stuff cascade;
> 
> -- verify that SELECT DISTINCT is working again
> select distinct empno from sales.emps order by empno;
+--------+
| EMPNO  |
+--------+
| 100    |
| 110    |
| 120    |
+--------+
> 
> -- NOTE jvs 4-Mar-2009:  This doesn't really belong here, but this
> -- test is currently the only place where we restore a clean catalog,
> -- so it's convenient for testing out the procedure for switching to Unicode
> 
> create schema typecheck;
> 
> create view typecheck.v as
> select "characterSetName","collationName","ordinal"
> from sys_fem."SQL2003"."AbstractColumn"
> where "name" like 'ASC%DESC';
> 
> select * from typecheck.v;
+-------------------+---------------------------+----------+
| characterSetName  |       collationName       | ordinal  |
+-------------------+---------------------------+----------+
| ISO-8859-1        | ISO-8859-1$en_US$primary  | 9        |
+-------------------+---------------------------+----------+
> 
> -- should fail because tables still exist
> call sys_boot.mgmt.change_default_character_set_to_unicode();
Error: Cannot change default character set to Unicode because local tables
have already been defined (state=,code=0)
> 
> drop schema sales cascade;
> 
> -- should succeed now since we dropped all the tables
> call sys_boot.mgmt.change_default_character_set_to_unicode();
> 
> create view typecheck.v2 as select 'blah' as asc_or_desc from (values(0));
> 
> -- existing column should have switched from ISO-8859-1 to UNICODE,
> -- and new column should also be UNICODE; note that collation
> -- is currently incorrect for new column
> select * from typecheck.v;
+-------------------+---------------------------+----------+
| characterSetName  |       collationName       | ordinal  |
+-------------------+---------------------------+----------+
| UTF-16LE          | UTF-16LE$en_US            | 9        |
| UTF-16LE          | ISO-8859-1$en_US$primary  | 0        |
+-------------------+---------------------------+----------+
> 
> -- last thing we do is to prepare for a restore of pre-upgrade catalog contents
> -- NOTE:  this will shut down the system, so don't add any commands
> -- after it
> alter system replace catalog;
> 
> !quit
