===================================================================================
Flex Table Load and Query Feedback Program Release Notes.pdf
HP Vertica Analytic Database 7.0.0
Copyright 2006-2013 Vertica, an HP Company, and its licensors. All rights reserved.
====================================================================================

Input raw Flex Table data, and be amazed as you query its keys and values!
This feature includes support for nested data, helpers for retrieving keys, and helpers for creating VIEWs and materialized columns.

See examples/queries.sql, examples/queries.out, and examples/mountains.json for the below examples in action.

To start playing with Flexible Tables:


1. Make a file with this JSON data (already done in examples/mountains.json):

{ "name": "Everest", "type":"mountain", "height": 29029, "hike_safety": 34.1 }
{ "name": "Mt St Helens", "type": "volcano", "hike_safety": 15.4 }


2. Load the data and start querying:
    CREATE FLEX TABLE mountains();
    COPY mountains FROM '/path/to/file' PARSER fjsonparser();
    SELECT name, type, height FROM mountains;

 name         | type     | height
--------------+----------+--------
 Everest      | mountain | 29029
 Mt St Helens | volcano  |
(2 rows)



STOP: This ends the Basic Tutorial. Continue to learn more!



3. Try this query to see whats really in there:
    SELECT * FROM mountains;

Inspect the contents of the map in human-speak:
    SELECT mapToString(__raw__) FROM mountains;

Now use this function to find the keys in the data:
    SELECT compute_flextable_keys('mountains');

Observe the resulting output:
    SELECT * FROM mountains_keys;

 key_name    | frequency | data_type_guess
-------------+-----------+-----------------
 hike_safety | 2         | varchar(20)
 name        | 2         | varchar(24)
 type        | 2         | varchar(20)
 height      | 1         | varchar(20)
(4 rows)


4. Build a view and see a more tool-friendly SELECT *:
    SELECT build_flextable_view('mountains');
    SELECT * FROM mountains_view;

Note that the data types are from mountains_keys:
    SELECT column_name,data_type FROM view_columns WHERE table_name = 'mountains_view';

Lets override a data_type_guess with our knowledge:
    UPDATE mountains_keys SET data_type_guess = 'float' WHERE key_name = 'hike_safety'; COMMIT;
    SELECT build_flextable_view('mountains');

Notice that hike_safety is now cast to float:
    SELECT column_name,data_type FROM view_columns WHERE table_name = 'mountains_view';


5. What if we know some columns are consistent and important, meriting full Vertica performance:
    CREATE FLEX TABLE mountains_hybrid(
        name varchar(41) default name::varchar(41),
        hike_safety float default hike_safety::float);
    COPY mountains_hybrid FROM '/path/to/file' PARSER fjsonparser();

Compare the output to the mountains table earlier:
    SELECT * FROM mountains_hybrid;

Lets see the changes in mountains_hybrid_keys and build the view all together this time:
    SELECT compute_flextable_keys_and_build_view('mountains_hybrid');
    SELECT * from mountains_hybrid_keys;

 key_name    | frequency | data_type_guess
-------------+-----------+-----------------
 hike_safety | 2         | float
 name        | 2         | varchar(41)
 type        | 2         | varchar(20)
 height      | 1         | varchar(20)
(4 rows)


6. Now lets materialize the 3 most frequently occurring keys found in mountains_hybrid_keys to real columns:
    SELECT materialize_flextable_columns('mountains_hybrid',3);

Observe the results. Note 2 of 3 are already materialized:
    SELECT * FROM materialize_flextable_columns_results;

Now type also appears as a column on the table:
    \d mountains_hybrid

Data types are derived from mountains_hybrid_keys and can be adjusted with UPDATES as we saw before:
    UPDATE mountains_hybrid_keys SET data_type_guess = 'int' WHERE key_name = 'height'; COMMIT;
    SELECT materialize_flextable_columns('mountains_hybrid',4);

Now check the results. Only 1 column is new this time and the table shows it. Read our best practices to understand some cases when column materialization is especially recommended.

