Class AliasHelper
AliasHelper keeps track of all the created aliases. There is only one alias for the same expression and its negation.
Instances are meant to exist local to a conversion process and especially not safe to be accessed (modified) concurrently by multiple threads.
- Author:
- Karl Eilebrecht
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<de.calamanari.adl.irl.CoreExpression, ExpressionAlias> Lookup from sub-expression to its alias, even if the same sub-expression occurs multiple times in a query this will ensure we have only a single alias. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondetermineAdequateBaseQueryCombination(CoreExpressionSqlHelper expressionHelper, List<ExpressionAlias> aliases) At the begin of every SQL-query there must be a base selection to start with.voiddeterminePrimaryAlias(CoreExpressionSqlHelper expressionHelper, Set<ExpressionAlias> aliasesInWhereClause) This method tests all available aliases if they have to be fulfilled to let the root expression to become true.
In case of success the methodgetPrimaryAlias()will return the identified alias.determineReferenceMatchDataColumnOrAliasRight(MatchCondition condition, boolean qualified) Determines the effective name of the right side of a reference match.Determines the effective right ID column in a reference matchDetermines the effective name of the right side table or alias of a reference match.determineReferenceMatchTableRight(MatchCondition condition) Determines the effective name or alias of the right table in a reference matchvoidDetermines the identifier of the main query if not already present: either name ofgetPrimaryAlias()orgetBaseQueryAliasName()protected StringReturns the name to be used for the base query alias if required.getOrCreateAlias(de.calamanari.adl.irl.CoreExpression expression) Returns an alias for the given expression.Returns all aliases (main aliases and supplementary aliases) ordered by nameReturns the primary alias if it is available.Returns the identifier of the main query in the statementgroupAliasesByTable(MatchConditionFactory conditionFactory, List<ExpressionAlias> aliases) Groups any aliases by table or table-duo (reference matches on two tables)booleanisPrimaryAliasRunnningOnPrimaryTable(CoreExpressionSqlHelper expressionHelper) This method determines if there is a primary alias and this alias runs on the primary table.voidsetPrimaryAlias(ExpressionAlias primaryAlias) Sets the primary aliasvoidsetStartSelectionName(String startQueryAliasName) Sets the identifier of the main query in the statement
-
Field Details
-
aliasMap
Lookup from sub-expression to its alias, even if the same sub-expression occurs multiple times in a query this will ensure we have only a single alias.
-
-
Constructor Details
-
AliasHelper
public AliasHelper()
-
-
Method Details
-
createAliasName
- Returns:
- new alias name, this implementation adds an upcounting number to
SqlFormatConstants.ALIAS_PREFIX, e.g.,sq__001, sq__002...
-
getOrCreateAlias
Returns an alias for the given expression. For the same expression we always return the same alias instance (cached). A new aliases will only be created if there was no alias, yet for the given expression.Important: If the given expression is a negative expression we will return the positive alias, so we can better consolidate aliases. In other words, for a
NegationExpressionwe return the alias for the internal delegate and for any AND ofNegationExpressions (means a NOT IN), we return the alias for the corresponding IN-expression (OR).- Parameters:
expression- the expression an alias is required for- Returns:
- alias
-
getPrimaryAlias
Returns the primary alias if it is available.The primary alias is the table or alias defined in the WITH-section we will start the query with.
- Returns:
- primary alias or null if undefined
- See Also:
-
setPrimaryAlias
Sets the primary aliasThe primary alias is the table or alias defined in the WITH-section we will start the query with.
It must be guaranteed that any possible match of the query is covered by this selection.- Parameters:
primaryAlias-
-
determinePrimaryAlias
public void determinePrimaryAlias(CoreExpressionSqlHelper expressionHelper, Set<ExpressionAlias> aliasesInWhereClause) This method tests all available aliases if they have to be fulfilled to let the root expression to become true.
In case of success the methodgetPrimaryAlias()will return the identified alias.If the primary alias is already set, this method returns immediately (no update).
Primary aliases can serve as a base query because we know that the whole query won't succeed if this particular alias is not fulfilled. If there are multiple candidates, we pick the most complex one (restrict result set early).
- Parameters:
expressionHelper-aliasesInWhereClause- references to alias names in the where clause
-
determineAdequateBaseQueryCombination
public List<ExpressionAlias> determineAdequateBaseQueryCombination(CoreExpressionSqlHelper expressionHelper, List<ExpressionAlias> aliases) At the begin of every SQL-query there must be a base selection to start with. Any ID not included in this selection becomes unreachable because further joins can only further restrict the selection.If we cannot identify a primary table or alias then one option is creating a UNION of all aliases identified for the given query.
However, unnecessary UNIONs are expensive. This method aims to find a sufficient combination of as few as possible aliases that covers the desired entirety of IDs and can serve as a base query.
The approach is prone to combinatoric explosion. Thus, the search for adequate combinations is limited and may not always find the best combination.
This method intentionally excludes any alias withExpressionAlias.requiresAllRowsOfTableQueryInUnion()because I assume that in this case it makes more sense to start with a base table that contains all IDs rather than building a UNION.Important: Should this helper have a
getPrimaryAlias()then this method will always return a list with the primary alias as its only element independent from the aliases list provided as input to this method.- Parameters:
expressionHelper-aliases-- Returns:
- either a list with the
getPrimaryAlias()(if present), or a list list with one or more aliases that combined by OR meet the base query requirements, or empty list if there was no primary alias defined and the combination search was unsuccessful
-
determineStartSelectionName
public void determineStartSelectionName()Determines the identifier of the main query if not already present: either name ofgetPrimaryAlias()orgetBaseQueryAliasName() -
getBaseQueryAliasName
Returns the name to be used for the base query alias if required.Technical detail, sub-classes may decide to choose a different name.
- Returns:
- name of the base query alias, returns
SqlFormatConstants.DEFAULT_BASE_QUERY_ALIASby default
-
getStartSelectionName
Returns the identifier of the main query in the statementThe start selection name is either a table name (simple query case) or a name that stands for a renamed table or one of the alias names defined in the WITH-section of the query.
- Returns:
- alias or table name the query is based on
-
setStartSelectionName
Sets the identifier of the main query in the statementThe start selection name is either a table name (simple query case) or a name that stands for a renamed table or one of the alias names defined in the WITH-section of the query.
- Parameters:
startQueryAliasName-
-
getOrderedAliasList
Returns all aliases (main aliases and supplementary aliases) ordered by name- Returns:
- ordered list of aliases (mutable)
-
groupAliasesByTable
public List<List<ExpressionAlias>> groupAliasesByTable(MatchConditionFactory conditionFactory, List<ExpressionAlias> aliases) Groups any aliases by table or table-duo (reference matches on two tables)Each sub-list represents the aliases for a table or specific table combination.
- Parameters:
conditionFactory-aliases-- Returns:
- list of alias lists
-
determineReferenceMatchTableOrAliasRight
Determines the effective name of the right side table or alias of a reference match.- Parameters:
condition-- Returns:
- right table name or
SqlFormatConstants.SELF_JOIN_ALIASif required
-
determineReferenceMatchDataColumnOrAliasRight
public String determineReferenceMatchDataColumnOrAliasRight(MatchCondition condition, boolean qualified) Determines the effective name of the right side of a reference match.- Parameters:
condition-qualified- columns must be qualified (e.g. in global where and ON-condition)- Returns:
- column qualified by
SqlFormatConstants.SELF_JOIN_ALIASif required
-
determineReferenceMatchIdColumnOrAliasRight
Determines the effective right ID column in a reference match- Parameters:
condition-- Returns:
- column qualified by
SqlFormatConstants.SELF_JOIN_ALIASor the source table
-
determineReferenceMatchTableRight
Determines the effective name or alias of the right table in a reference match- Parameters:
condition-- Returns:
- the table name or "renamed" to
SqlFormatConstants.SELF_JOIN_ALIAS
-
isPrimaryAliasRunnningOnPrimaryTable
This method determines if there is a primary alias and this alias runs on the primary table.An alias runs on the primary table if the primary table is involved in the corresponding SQL.
- Parameters:
expressionHelper-- Returns:
- if there is a primary alias and either the left or the right table (if reference match) of the primary alias is the primary table
- See Also:
-