Generate SQL SELECT from Pipeline Actions
Updated: Oct 5, 2026
This example shows how to push the work of a DataPipeline Foundations pipeline into the database. Pipeline actions such as AggregateGroupFieldsAction, RenameFieldsAction and SortFieldsAction normally configure readers that aggregate, rename and sort records in memory. Each of them can also render the same work as a SQL SELECT with generateSqlSelect(), and because the result is a Select that doubles as a query source, the actions chain into nested sub-selects.
Java Code Listing
package com.northconcepts.datapipeline.foundations.examples.pipeline;
import com.northconcepts.datapipeline.foundations.pipeline.action.aggregate.AggregateGroupFieldsAction;
import com.northconcepts.datapipeline.foundations.pipeline.action.aggregate.AggregateGroupFieldsAction.GroupByField;
import com.northconcepts.datapipeline.foundations.pipeline.action.aggregate.AggregateGroupFieldsAction.OperatorType;
import com.northconcepts.datapipeline.foundations.pipeline.action.transform.RenameFieldsAction;
import com.northconcepts.datapipeline.foundations.pipeline.action.transform.SortFieldsAction;
import com.northconcepts.datapipeline.jdbc.sql.select.Select;
import com.northconcepts.datapipeline.jdbc.sql.select.TableQuerySource;
public class GenerateSqlSelectFromPipelineActions {
public static void main(String[] args) {
AggregateGroupFieldsAction aggregate = new AggregateGroupFieldsAction()
.add(new GroupByField("Rating", "Rating", OperatorType.GROUPBY, true))
.add(new GroupByField("Account", "Accounts", OperatorType.COUNT, true))
.add(new GroupByField("Balance", "TotalBalance", OperatorType.SUM, true));
RenameFieldsAction rename = new RenameFieldsAction()
.add("Rating", "CreditRating")
.add("Accounts", "AccountCount")
.add("TotalBalance", "TotalBalance");
SortFieldsAction sort = new SortFieldsAction()
.add("TotalBalance", false);
Select select = aggregate.generateSqlSelect(new TableQuerySource("credit_balance"));
select = rename.generateSqlSelect(select.setNestedAlias("totals"));
select = sort.generateSqlSelect(select.setNestedAlias("renamed"));
System.out.println(select.setPretty(true).getSqlFragment());
}
}
Code Walkthrough
- An
AggregateGroupFieldsActionis configured with threeGroupByFieldentries, each naming a source field, a target field, anOperatorTypeand whether nulls are excluded: group byRating, countAccountasAccountsand sumBalanceasTotalBalance. - A
RenameFieldsActionrenamesRatingtoCreditRatingandAccountstoAccountCount, and mapsTotalBalanceto itself. A rename action projects only the fields in its mapping, so that self-mapping is what carriesTotalBalancethrough to the outer query. - A
SortFieldsActionsorts byTotalBalance; thefalseargument means descending. aggregate.generateSqlSelect(new TableQuerySource("credit_balance"))produces aSelectover thecredit_balancetable with the grouped column, the two aggregate expressions and aGROUP BYclause.setNestedAlias("totals")names that query for use as a derived table (MySQL requires an alias on every derived table), andrename.generateSqlSelect(...)wraps it in a sub-select projecting the renamed columns.setNestedAlias("renamed")andsort.generateSqlSelect(...)wrap it once more to add theORDER BYclause.select.setPretty(true).getSqlFragment()renders the statement with indentation and prints it.
Console Output
SELECT *
FROM (
SELECT Rating as CreditRating, Accounts as AccountCount, TotalBalance
FROM (
SELECT Rating, count(Account) as Accounts, sum(Balance) as TotalBalance
FROM credit_balance
GROUP BY Rating
) totals
) renamed
ORDER BY TotalBalance DESC
