From 223d1b2b59cf5b59254422d66afc1036c4ca5e39 Mon Sep 17 00:00:00 2001 From: Ben Gribaudo Date: Thu, 24 Mar 2022 09:35:43 -0400 Subject: [PATCH 1/5] Updating to handle an empty table being returned --- .../9-TestConnection/Table.GenerateByPage.pqm | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm index a753257..24f6344 100644 --- a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm +++ b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm @@ -1,23 +1,16 @@ -(getNextPage as function) as table => +(getNextPage as function) as table => let listOfPages = List.Generate( () => getNextPage(null), // get the first page of data (lastPage) => lastPage <> null, // stop when the function returns null (lastPage) => getNextPage(lastPage) // pass the previous page to the next function call ), - // concatenate the pages together - tableOfPages = Table.FromList(listOfPages, Splitter.SplitByNothing(), {"Column1"}), - firstRow = tableOfPages{0}? + firstRow = listOfPages{0}? in - // if we didn't get back any pages of data, return an empty table - // otherwise set the table type based on the columns of the first page if (firstRow = null) then Table.FromRows({}) - // check for empty first table - else if (Table.IsEmpty(firstRow[Column1])) then - firstRow[Column1] - else + else Value.ReplaceType( - Table.ExpandTableColumn(tableOfPages, "Column1", Table.ColumnNames(firstRow[Column1])), - Value.Type(firstRow[Column1]) + Table.Combine(listOfPages, Table.ColumnNames(firstRow)), + Value.Type(firstRow) ) From ec12ddf1533c9577924debb8b98dc29ce16317a2 Mon Sep 17 00:00:00 2001 From: Ben Gribaudo Date: Fri, 25 Mar 2022 11:26:45 -0400 Subject: [PATCH 2/5] Updating to not use Table.Combine + allow providing of table type --- .../9-TestConnection/Table.GenerateByPage.pqm | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm index 24f6344..b88b045 100644 --- a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm +++ b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm @@ -1,16 +1,23 @@ -(getNextPage as function) as table => - let +( + getNextPage as function, + optional tableType as type +) as table => + let listOfPages = List.Generate( () => getNextPage(null), // get the first page of data (lastPage) => lastPage <> null, // stop when the function returns null (lastPage) => getNextPage(lastPage) // pass the previous page to the next function call ), - firstRow = listOfPages{0}? + filteredListOfPages = List.Select(listOfPages, each not Table.IsEmpty(_)), + tableOfPages = Table.FromList(filteredListOfPages, Splitter.SplitByNothing(), {"Column1"}), + firstRow = tableOfPages{0}?, + appliedType = if tableType = null then Value.Type(firstRow[Column1]) else tableType, + columns = Record.FieldNames(Type.RecordFields(Type.TableRow(appliedType))) in - if (firstRow = null) then + if tableType = null and firstRow = null then Table.FromRows({}) - else + else Value.ReplaceType( - Table.Combine(listOfPages, Table.ColumnNames(firstRow)), - Value.Type(firstRow) + Table.ExpandTableColumn(tableOfPages, "Column1", columns), + appliedType ) From ee6142919108c52e250ce05a4bb3cad2b1f260f8 Mon Sep 17 00:00:00 2001 From: Ben Gribaudo Date: Fri, 25 Mar 2022 12:20:44 -0400 Subject: [PATCH 3/5] Refactoring --- .../9-TestConnection/Table.GenerateByPage.pqm | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm index b88b045..14a6b72 100644 --- a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm +++ b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm @@ -2,22 +2,22 @@ getNextPage as function, optional tableType as type ) as table => - let +let listOfPages = List.Generate( - () => getNextPage(null), // get the first page of data - (lastPage) => lastPage <> null, // stop when the function returns null - (lastPage) => getNextPage(lastPage) // pass the previous page to the next function call - ), - filteredListOfPages = List.Select(listOfPages, each not Table.IsEmpty(_)), - tableOfPages = Table.FromList(filteredListOfPages, Splitter.SplitByNothing(), {"Column1"}), - firstRow = tableOfPages{0}?, - appliedType = if tableType = null then Value.Type(firstRow[Column1]) else tableType, - columns = Record.FieldNames(Type.RecordFields(Type.TableRow(appliedType))) - in - if tableType = null and firstRow = null then - Table.FromRows({}) - else - Value.ReplaceType( - Table.ExpandTableColumn(tableOfPages, "Column1", columns), - appliedType - ) + () => getNextPage(null), + (lastPage) => lastPage <> null, + (lastPage) => getNextPage(lastPage) + ), + filteredListOfPages = List.Select(listOfPages, each not Table.IsEmpty(_)), + tableOfPages = Table.FromList(filteredListOfPages, Splitter.SplitByNothing(), {"Column1"}), + firstRow = tableOfPages{0}?, + appliedType = + if tableType <> null then tableType + else if firstRow <> null then Value.Type(firstRow[Column1]) + else type table[], + columns = Record.FieldNames(Type.RecordFields(Type.TableRow(appliedType))) +in + Value.ReplaceType( + Table.ExpandTableColumn(tableOfPages, "Column1", columns), + appliedType + ) From 3f1a035fdda3ca7deac2c7c7ce5bccfa2ad5e0ba Mon Sep 17 00:00:00 2001 From: Ben Gribaudo Date: Thu, 14 Apr 2022 10:35:47 -0400 Subject: [PATCH 4/5] Adjusting error propagation Without this change, `try Table.GenerateByPage(each error "bad", type table[A=any])` won't catch the `error "bad"` raised here (which simulates the first data page fetch attempt raising an error). --- samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm index 14a6b72..ce9ef4f 100644 --- a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm +++ b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm @@ -12,7 +12,7 @@ let tableOfPages = Table.FromList(filteredListOfPages, Splitter.SplitByNothing(), {"Column1"}), firstRow = tableOfPages{0}?, appliedType = - if tableType <> null then tableType + if tableType <> null and not (try firstRow)[HasError] then tableType else if firstRow <> null then Value.Type(firstRow[Column1]) else type table[], columns = Record.FieldNames(Type.RecordFields(Type.TableRow(appliedType))) From 10bf7487595af49118b7d3486ac0f76764b67158 Mon Sep 17 00:00:00 2001 From: Ben Gribaudo Date: Mon, 2 May 2022 08:55:52 -0400 Subject: [PATCH 5/5] Reverting last change to avoid triggering a row fetch --- samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm index ce9ef4f..14a6b72 100644 --- a/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm +++ b/samples/TripPin/9-TestConnection/Table.GenerateByPage.pqm @@ -12,7 +12,7 @@ let tableOfPages = Table.FromList(filteredListOfPages, Splitter.SplitByNothing(), {"Column1"}), firstRow = tableOfPages{0}?, appliedType = - if tableType <> null and not (try firstRow)[HasError] then tableType + if tableType <> null then tableType else if firstRow <> null then Value.Type(firstRow[Column1]) else type table[], columns = Record.FieldNames(Type.RecordFields(Type.TableRow(appliedType)))