Skip to content

Commit

Permalink
Updated Builtin-UDO samples with new options
Browse files Browse the repository at this point in the history
outputHeader and charFormat
  • Loading branch information
MikeRys committed Oct 17, 2016
1 parent 89512cb commit bca92ab
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
49 changes: 35 additions & 14 deletions Examples/Builtin-UDOs/Builtin-UDOs/6-Extract-withHeaders.usql
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
// Built-in UDOs
//
// Shows 3 ways on how to deal with header/comment rows with built-in Extractors:
// 1. Filter out rows with known "incorrect values" (e.g., header value or comment start)
// 2. Filter out rows that do not parse to the expected data type
// 3. Use the silent option. Only use if you really trust your data.
// Shows 4 ways on how to deal with header/comment rows with built-in Extractors:
// 1. Use the skipFirstNRows option
// 2. Filter out rows with known "incorrect values" (e.g., header value or comment start)
// 3. Filter out rows that do not parse to the expected data type
// 4. Use the silent option. Only use if you really trust your data.
//
// 1. and 2. expect that header rows and comment rows have same number of columns as data rows.
// 3. only skips rows that have invalid number of columns. If correct number but wrong values, it will replace value with null.
// 2. and 3. expect that header rows and comment rows have same number of columns as data rows.
// 4. only skips rows that have invalid number of columns. If correct number but wrong values, it will replace value with null.
//
// In the near future, we should have the ability to skip the first n rows in a file.


@option1_skipheader =
EXTRACT id long,
name string,
street string,
city string,
zip string,
age short?
FROM "/Samples/Data/CsvWithHdr.csv"
USING Extractors.Csv(skipFirstNRows:1);

OUTPUT @option1_skipheader
TO "/temp/opt1.csv"
USING Outputters.Csv();

///

@r1 =
EXTRACT id string,
Expand All @@ -20,7 +37,7 @@
FROM "/Samples/Data/CsvWithHdr.csv"
USING Extractors.Csv();

@option1_knownheader =
@option2_knownheader =
SELECT Int32.Parse(id) AS id,
name,
street,
Expand All @@ -30,11 +47,13 @@
FROM @r1
WHERE id != "Id";

OUTPUT @option1_knownheader
TO "/temp/opt1.csv"
OUTPUT @option2_knownheader
TO "/temp/opt2.csv"
USING Outputters.Csv();

@option2_tryparse =
///

@option3_tryparse =
SELECT Int32.Parse(id) AS id,
name,
street,
Expand All @@ -44,10 +63,12 @@ USING Outputters.Csv();
FROM @r1
WHERE ((Func<string, bool>)(p => { Int32 dummy; return Int32.TryParse(p, out dummy); }))(id);

OUTPUT @option2_tryparse
TO "/temp/opt2.csv"
OUTPUT @option3_tryparse
TO "/temp/opt3.csv"
USING Outputters.Csv();

///

// silent:true will skip incorrect number of rows or replaces invalid invalid casts to nullable types. It will not skip encoding errors, allow invalid casts to not-nullable types, or skip a row where a cast error occurs.
@silent_r =
EXTRACT id long?,
Expand All @@ -60,7 +81,7 @@ USING Outputters.Csv();
USING Extractors.Csv(silent:true);

OUTPUT @silent_r
TO "/temp/opt3.csv"
TO "/temp/opt4.csv"
USING Outputters.Csv();


Expand Down
19 changes: 19 additions & 0 deletions Examples/Builtin-UDOs/Builtin-UDOs/7-Output-withHeaders.usql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@data =
SELECT "Jane" AS [first name], "Doe" AS [name,last], 42 AS [数量]
FROM(
VALUES
(
1
)) AS T(dummy);

OUTPUT @data
TO "/output/builtinUDO/output_header.csv"
USING Outputters.Csv(outputHeader : true);

OUTPUT @data
TO "/output/builtinUDO/output_header_noquotes.csv"
USING Outputters.Csv(outputHeader : true, quoting:false);

OUTPUT @data
TO "/output/builtinUDO/output_header_noquotes_esc.csv"
USING Outputters.Csv(outputHeader : true, quoting : false, escapeCharacter: '#');
25 changes: 25 additions & 0 deletions Examples/Builtin-UDOs/Builtin-UDOs/8-Output-Char.usql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@data =
SELECT 42 AS n_int
, "\xFFFE" AS c_u
, '\xFFFD' AS c_u2
, '数' AS c_ch
, ',' AS c_c
, '4' AS c_int
, '\n' AS c_nl
, (char?) null AS c_null
FROM (VALUES(1)) AS T(dummy);

OUTPUT @data TO "/output/builtinUDO/char_as_int1.csv" USING Outputters.Csv();

OUTPUT @data TO "/output/builtinUDO/char_as_int2.csv" USING Outputters.Csv(charFormat:"uint16");

OUTPUT @data TO "/output/builtinUDO/char_as_string.csv" USING Outputters.Csv(charFormat:"string");

OUTPUT @data TO "/output/builtinUDO/char_as_string_utf16.csv" USING Outputters.Csv(charFormat:"string", encoding:Encoding.Unicode);

OUTPUT @data TO "/output/builtinUDO/char_as_string2.csv" USING Outputters.Csv(charFormat:null);

OUTPUT @data TO "/output/builtinUDO/char_as_int_null.csv" USING Outputters.Csv(nullEscape:"NULL",escapeCharacter:'#');

OUTPUT @data TO "/output/builtinUDO/char_as_string_null.csv" USING Outputters.Csv(charFormat:"string", nullEscape:"NULL",escapeCharacter:'#');

2 changes: 2 additions & 0 deletions Examples/Builtin-UDOs/Builtin-UDOs/9-Extract-Char.usql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Requires outputs of 8-Output-Char.usql

3 changes: 3 additions & 0 deletions Examples/Builtin-UDOs/Builtin-UDOs/Builtin-UDOs.usqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<LocalDatabase>master</LocalDatabase>
<LocalSchema>dbo</LocalSchema>
</Script>
<Script Include="8-Output-Char.usql" />
<Script Include="7-Output-withHeaders.usql" />
<Script Include="9-Extract-Char.usql" />
</ItemGroup>
<Import Project="$(AppData)\Microsoft\DataLake\MsBuild\1.0\Usql.targets" />
<PropertyGroup>
Expand Down

0 comments on commit bca92ab

Please sign in to comment.