Class DataBlockQueryBuilder
- Namespace
- Datafication.Server.Core.Query
- Assembly
- Datafication.Server.Core.dll
Fluent builder for creating DataBlockQueryRequest objects. Provides a type-safe, discoverable API for constructing complex queries.
public class DataBlockQueryBuilder
- Inheritance
-
objectDataBlockQueryBuilder
Examples
var query = new DataBlockQueryBuilder()
.Select("Name", "Age", "Salary")
.Where("Age", QueryOperator.GreaterThan, 25)
.WhereIn("Status", "Active", "Pending")
.OrderBy("Salary", descending: true)
.Skip(0).Take(100)
.Build();
Methods
Aggregate(string, params string[])
Applies an aggregation function to the specified columns.
public DataBlockQueryBuilder Aggregate(string type, params string[] columns)
Parameters
typestringThe aggregation type (min, max, mean, sum, count, stddev, variance, percentile).
columnsstring[]The columns to aggregate. If empty, all numeric columns are used.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Build()
Builds the DataBlockQueryRequest from the configured options.
public DataBlockQueryRequest Build()
Returns
- DataBlockQueryRequest
A new DataBlockQueryRequest instance.
Compute(string, string)
Adds a computed column based on an expression.
public DataBlockQueryBuilder Compute(string resultColumn, string expression)
Parameters
resultColumnstringThe name for the new column.
expressionstringThe expression to evaluate. Use [ColumnName] to reference columns.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Examples
builder.Compute("Total", "[Price] * [Quantity]");
Count()
Counts the rows.
public DataBlockQueryBuilder Count()
Returns
- DataBlockQueryBuilder
The builder for method chaining.
CumulativeSum(string, string?)
Adds a cumulative sum column.
public DataBlockQueryBuilder CumulativeSum(string column, string? resultColumn = null)
Parameters
columnstringThe source column.
resultColumnstringThe name for the result column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
DropDuplicates(string[]?, string)
Removes duplicate rows.
public DataBlockQueryBuilder DropDuplicates(string[]? columns = null, string keep = "first")
Parameters
columnsstring[]The columns to check for duplicates. If empty, all columns are used.
keepstringWhich duplicate to keep: "first", "last", or "none".
Returns
- DataBlockQueryBuilder
The builder for method chaining.
DropNulls(string)
Removes rows containing null values.
public DataBlockQueryBuilder DropNulls(string mode = "any")
Parameters
modestring"any" to drop rows with any nulls, "all" to drop only rows where all values are null.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
FillNulls(string, object?, params string[])
Fills null values using the specified method.
public DataBlockQueryBuilder FillNulls(string method, object? constantValue = null, params string[] columns)
Parameters
methodstringThe fill method: "forward", "backward", "constant", "mean", "median", "mode", "interpolate".
constantValueobjectThe value to use when method is "constant".
columnsstring[]The columns to fill. If empty, applies to all columns.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Format(string)
Sets the output format.
public DataBlockQueryBuilder Format(string format)
Parameters
formatstringThe format: "json", "csv", "html", or a custom sink name.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
FullJoin(string, string)
Performs a full outer join with another DataBlock.
public DataBlockQueryBuilder FullJoin(string otherDataBlockId, string keyColumn)
Parameters
otherDataBlockIdstringThe ID of the DataBlock to join with.
keyColumnstringThe key column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
GroupBy(string, Dictionary<string, string>?, Dictionary<string, string>?)
Groups the data by the specified column with optional aggregations.
public DataBlockQueryBuilder GroupBy(string column, Dictionary<string, string>? aggregations = null, Dictionary<string, string>? resultColumnNames = null)
Parameters
columnstringThe column to group by.
aggregationsDictionary<string, string>Dictionary of column name to aggregation type.
resultColumnNamesDictionary<string, string>Optional custom names for result columns.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
InnerJoin(string, string)
Performs an inner join with another DataBlock.
public DataBlockQueryBuilder InnerJoin(string otherDataBlockId, string keyColumn)
Parameters
otherDataBlockIdstringThe ID of the DataBlock to join with.
keyColumnstringThe key column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Lag(string, int, object?, string?)
Adds a lag column (previous row's value).
public DataBlockQueryBuilder Lag(string column, int offset = 1, object? defaultValue = null, string? resultColumn = null)
Parameters
columnstringThe source column.
offsetintThe number of rows to look back (default: 1).
defaultValueobjectThe value to use when there is no previous row.
resultColumnstringThe name for the result column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Lead(string, int, object?, string?)
Adds a lead column (next row's value).
public DataBlockQueryBuilder Lead(string column, int offset = 1, object? defaultValue = null, string? resultColumn = null)
Parameters
columnstringThe source column.
offsetintThe number of rows to look ahead (default: 1).
defaultValueobjectThe value to use when there is no next row.
resultColumnstringThe name for the result column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
LeftJoin(string, string)
Performs a left join with another DataBlock.
public DataBlockQueryBuilder LeftJoin(string otherDataBlockId, string keyColumn)
Parameters
otherDataBlockIdstringThe ID of the DataBlock to join with.
keyColumnstringThe key column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Max(params string[])
Finds the maximum value in the specified columns.
public DataBlockQueryBuilder Max(params string[] columns)
Parameters
columnsstring[]The columns to find maximum values.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Mean(params string[])
Calculates the mean (average) of the specified columns.
public DataBlockQueryBuilder Mean(params string[] columns)
Parameters
columnsstring[]The columns to average.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Melt(string[], string, string)
Melts (unpivots) the DataBlock from wide to long format.
public DataBlockQueryBuilder Melt(string[] fixedColumns, string meltedColumnName = "variable", string meltedValueName = "value")
Parameters
fixedColumnsstring[]The columns to keep fixed (identifier columns).
meltedColumnNamestringThe name for the column containing original column names.
meltedValueNamestringThe name for the column containing values.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Merge(string, string, string?, string)
Merges (joins) with another DataBlock.
public DataBlockQueryBuilder Merge(string otherDataBlockId, string keyColumn, string? otherKeyColumn = null, string mode = "inner")
Parameters
otherDataBlockIdstringThe ID of the DataBlock to merge with.
keyColumnstringThe key column in the source DataBlock.
otherKeyColumnstringThe key column in the other DataBlock. If null, uses keyColumn.
modestringThe merge mode: "inner", "left", "right", or "full".
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Min(params string[])
Finds the minimum value in the specified columns.
public DataBlockQueryBuilder Min(params string[] columns)
Parameters
columnsstring[]The columns to find minimum values.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
MovingAverage(string, int, string?)
Adds a moving average column.
public DataBlockQueryBuilder MovingAverage(string column, int windowSize, string? resultColumn = null)
Parameters
columnstringThe source column.
windowSizeintThe window size.
resultColumnstringThe name for the result column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
OrderBy(string, bool)
Sorts the results by the specified column.
public DataBlockQueryBuilder OrderBy(string column, bool descending = false)
Parameters
columnstringThe column name to sort by.
descendingboolIf true, sorts in descending order; otherwise ascending.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
OrderByDescending(string)
Sorts the results by the specified column in descending order.
public DataBlockQueryBuilder OrderByDescending(string column)
Parameters
columnstringThe column name to sort by.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Percentile(double, params string[])
Calculates the specified percentile of the specified columns.
public DataBlockQueryBuilder Percentile(double percentile, params string[] columns)
Parameters
percentiledoubleThe percentile value (0-100).
columnsstring[]The columns to calculate the percentile for.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Pivot(string, string, string, string, string)
Pivots the DataBlock from long format to wide format. Values from the pivot column become new columns, with values aggregated from the value column.
public DataBlockQueryBuilder Pivot(string indexColumn, string pivotColumn, string valueColumn, string aggregationType = "sum", string columnNameFormat = "{pivot}_{value}")
Parameters
indexColumnstringThe column to use as row identifier.
pivotColumnstringThe column whose unique values become new column names.
valueColumnstringThe column containing values to aggregate.
aggregationTypestringThe aggregation type: "sum", "mean", "min", "max", "count", "stddev", "variance".
columnNameFormatstringFormat string for generated column names. Use {pivot} and {value} as placeholders.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Examples
// Pivot sales data by region
builder.Pivot("Category", "Region", "Sales", "sum");
// Result: Category | East_Sales | West_Sales | ...
Pivot(string[], string, string, string, string)
Pivots the DataBlock from long format to wide format using multiple index columns. Values from the pivot column become new columns, with values aggregated from the value column.
public DataBlockQueryBuilder Pivot(string[] indexColumns, string pivotColumn, string valueColumn, string aggregationType = "sum", string columnNameFormat = "{pivot}_{value}")
Parameters
indexColumnsstring[]The columns to use as row identifiers.
pivotColumnstringThe column whose unique values become new column names.
valueColumnstringThe column containing values to aggregate.
aggregationTypestringThe aggregation type: "sum", "mean", "min", "max", "count", "stddev", "variance".
columnNameFormatstringFormat string for generated column names. Use {pivot} and {value} as placeholders.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Examples
// Pivot with multiple index columns
builder.Pivot(new[] { "Year", "Category" }, "Region", "Sales", "sum");
RightJoin(string, string)
Performs a right join with another DataBlock.
public DataBlockQueryBuilder RightJoin(string otherDataBlockId, string keyColumn)
Parameters
otherDataBlockIdstringThe ID of the DataBlock to join with.
keyColumnstringThe key column.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
RowNumber(string, string?, string[]?)
Adds a row number column.
public DataBlockQueryBuilder RowNumber(string orderByColumn, string? resultColumn = null, string[]? partitionByColumns = null)
Parameters
orderByColumnstringThe column to order by.
resultColumnstringThe name for the result column.
partitionByColumnsstring[]The columns to partition by.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Sample(int, int?)
Randomly samples the specified number of rows. When specified, Skip and Take are ignored.
public DataBlockQueryBuilder Sample(int count, int? seed = null)
Parameters
countintThe number of rows to sample.
seedint?Optional seed for reproducible sampling.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Select(params string[])
Specifies which columns to include in the result.
public DataBlockQueryBuilder Select(params string[] columns)
Parameters
columnsstring[]The column names to include.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Skip(int)
Skips the specified number of rows.
public DataBlockQueryBuilder Skip(int count)
Parameters
countintThe number of rows to skip.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
StandardDeviation(params string[])
Calculates the standard deviation of the specified columns.
public DataBlockQueryBuilder StandardDeviation(params string[] columns)
Parameters
columnsstring[]The columns to calculate standard deviation for.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Sum(params string[])
Calculates the sum of the specified columns.
public DataBlockQueryBuilder Sum(params string[] columns)
Parameters
columnsstring[]The columns to sum.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Take(int)
Takes the specified number of rows.
public DataBlockQueryBuilder Take(int count)
Parameters
countintThe maximum number of rows to return.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Transpose(string?)
Transposes the DataBlock (swaps rows and columns).
public DataBlockQueryBuilder Transpose(string? headerColumnName = null)
Parameters
headerColumnNamestringThe column to use as header row.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Variance(params string[])
Calculates the variance of the specified columns.
public DataBlockQueryBuilder Variance(params string[] columns)
Parameters
columnsstring[]The columns to calculate variance for.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Where(string, QueryOperator, object?)
Adds a filter condition using the specified operator. Multiple Where calls are combined with AND logic.
public DataBlockQueryBuilder Where(string column, QueryOperator op, object? value)
Parameters
columnstringThe column name to filter on.
opQueryOperatorThe comparison operator.
valueobjectThe value to compare against.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereContains(string, string)
Adds a contains filter condition for string columns.
public DataBlockQueryBuilder WhereContains(string column, string value)
Parameters
columnstringThe column name to filter on.
valuestringThe substring to search for.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereEndsWith(string, string)
Adds an ends-with filter condition for string columns.
public DataBlockQueryBuilder WhereEndsWith(string column, string value)
Parameters
columnstringThe column name to filter on.
valuestringThe suffix to match.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereEquals(string, object?)
Adds an equality filter condition.
public DataBlockQueryBuilder WhereEquals(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to match.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereGreaterThan(string, object?)
Adds a greater-than filter condition.
public DataBlockQueryBuilder WhereGreaterThan(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to compare against.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereGreaterThanOrEqual(string, object?)
Adds a greater-than-or-equal filter condition.
public DataBlockQueryBuilder WhereGreaterThanOrEqual(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to compare against.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereIn(string, params object[])
Filters rows where the column value is in the specified collection.
public DataBlockQueryBuilder WhereIn(string column, params object[] values)
Parameters
columnstringThe column name to filter on.
valuesobject[]The values to include.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereLessThan(string, object?)
Adds a less-than filter condition.
public DataBlockQueryBuilder WhereLessThan(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to compare against.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereLessThanOrEqual(string, object?)
Adds a less-than-or-equal filter condition.
public DataBlockQueryBuilder WhereLessThanOrEqual(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to compare against.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereNot(string, object?)
Filters rows where the column value is not equal to the specified value.
public DataBlockQueryBuilder WhereNot(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to exclude.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereNotEquals(string, object?)
Adds a not-equals filter condition.
public DataBlockQueryBuilder WhereNotEquals(string column, object? value)
Parameters
columnstringThe column name to filter on.
valueobjectThe value to exclude.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
WhereStartsWith(string, string)
Adds a starts-with filter condition for string columns.
public DataBlockQueryBuilder WhereStartsWith(string column, string value)
Parameters
columnstringThe column name to filter on.
valuestringThe prefix to match.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Window(string, string, int?, string?, string?, string[]?)
Applies a window function to create a new column.
public DataBlockQueryBuilder Window(string column, string function, int? windowSize = null, string? resultColumn = null, string? orderByColumn = null, string[]? partitionByColumns = null)
Parameters
columnstringThe source column.
functionstringThe window function type.
windowSizeint?The window size for moving functions.
resultColumnstringThe name for the result column.
orderByColumnstringThe column to order by within the window.
partitionByColumnsstring[]The columns to partition by.
Returns
- DataBlockQueryBuilder
The builder for method chaining.
Operators
implicit operator DataBlockQueryRequest(DataBlockQueryBuilder)
Implicitly converts the builder to a DataBlockQueryRequest.
public static implicit operator DataBlockQueryRequest(DataBlockQueryBuilder builder)
Parameters
builderDataBlockQueryBuilderThe builder to convert.