SQL constraints are used to specify rules for data in a table.There are many types of constraints but most commonly used and covering the majority of business scenarios : UNIQUE - Ensures that all values in a column are different CHECK - Ensures that the values in a column satisfies a specific condition Introducing new constraints support in SQL function arsenal . addConstraintsSQL [tableSQL , Association [“constrantname1” {“CHECK” , condition }, “constrantname2” {“UNIQUE” , {col1 ,col2 .... }} ] , showErrorsAsTable ]Input - Table SQL - Association containing constraints to apply to table data. CKECK type constraint contains condition that must be met for the data to be valid UNIQUE type constraints contains list of columns that should be unique - showErrorsAsTable (True|False) default False. If True displays output[“errors”] in table format Output - Table SQL containing only valid data - output[“errors”] association containing invalid data corresponding to each constraints.
Load SQL functions
Load SQL functions
Load the last version of SQLOperators package containing SQL function developed so far.
See my previous post : SQL operators as Wolfram Language functions (https://community.wolfram.com/groups/-/m/t/2195893)
See my previous post : SQL operators as Wolfram Language functions (https://community.wolfram.com/groups/-/m/t/2195893)
In[]:=
CloudImport["https://www.wolframcloud.com/env/damcalrom/Published/SQLOperators.wl"];
Get sample data
Get sample data
In[]:=
symbols={"MSFT","TSLA","AMZN"};sampledata=(Flatten/@Tuples[{{#},FinancialData[#,{{2020}}]//Normal}])&/@symbols//Flatten[#,1]&;sampledataSQL=tableSQL[sampledata,{"symbol","time","close"}];
Add some invalid data
Add some invalid data
In[]:=
tbinvalid=sampledataSQL//(*addduplicateson{symbol,time}*)unionSQL[#,tableSQL[ConstantArray[{"MSFT",DateObject[{2020,3,1}],Quantity[200,"$"]},2],{"symbol","time","close"}] ]&//(*addrowwithinvaliddate*)unionSQL[#,tableSQL[{{"MSFT",20201539,Quantity[300,"$"]}},{"symbol","time","close"}] ]&;tbinvalid//showTableSQL
Total Rows:762 Elapsed:1.×
-7
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Define constraints on table
Define constraints on table
In[]:=
tbvalid=tbinvalid// addConstraintsSQL[#,Association["ckTime"{"CHECK",DateObjectQ[time]},(*checkconstrainttoeliminateinvaliddates*) "ckCloseLimit"{"CHECK",QuantityMagnitude[close]>100},(*checkconstrainttoeliminateclosevaluesongivencondition*) "ukSymbolTime"{"UNIQUE",{symbol,time}}(*uniqueconstrainttoeliminateduplicatesoncombinationofcolumns*) ]]&;//AbsoluteTimingtbvalid//showTableSQL
Out[]=
{5.47238,Null}
Total Rows:743 Elapsed:0.
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT | ||
MSFT |
Get invalid rows filtered out by the constraints defined on table
Get invalid rows filtered out by the constraints defined on table
In[]:=
tbvalid["errors"]["ckTime"]//showTableSQLtbvalid["errors"]["ckCloseLimit"]//showTableSQLtbvalid["errors"]["ukSymbolTime"]//showTableSQL
Total Rows:1 Elapsed:5.×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | 20201539 |
Total Rows:16 Elapsed:1.7×
-6
10
Out[]//TableForm=
symbol | time | close |
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA | ||
TSLA |
Total Rows:2 Elapsed:2.7×
-6
10
Out[]//TableForm=
symbol | time | close |
MSFT | ||
MSFT |
Using showErrorsAsTable = True will display invalid rows in table format
Using showErrorsAsTable = True will display invalid rows in table format
In[]:=
tbvalid=tbinvalid// addConstraintsSQL[#, Association["ckTime"{"CHECK",DateObjectQ[time]},(*checkconstrainttoeliminateinvaliddates*) "ckCloseLimit"{"CHECK",QuantityMagnitude[close]>100},(*checkconstrainttoeliminateclosevaluesongivencondition*) "ukSymbolTime"{"UNIQUE",{symbol,time}}(*uniqueconstrainttoeliminateduplicatesoncombinationofcolumns*) ], True ]&;//AbsoluteTimingtbvalid["errors"]//showTableSQL
If you enjoy the project or find it useful , any crypto donations are welcomed @ wallet address : 0xbc75Ca2Fde6D0FE1d2bA9f6b4a39f10Eb151dd86 or via paystring : calin.damian.crypto$paystring.crypto.com