Skip to content

Commit

Permalink
Prevent appending of alias for CAST expression that keep their name
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-weber committed Apr 3, 2024
1 parent 54f0b57 commit 461559d
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.polypheny.db.algebra.AlgNode;
import org.polypheny.db.algebra.type.AlgDataTypeField;
import org.polypheny.db.rex.RexCall;
Expand All @@ -38,9 +41,21 @@
import org.polypheny.db.rex.RexSubQuery;
import org.polypheny.db.rex.RexTableIndexRef;
import org.polypheny.db.rex.RexVisitor;
import org.polypheny.db.type.PolyType;

public class PolyAlgUtils {

private static final Pattern CAST_PATTERN;


static {
StringJoiner joiner = new StringJoiner( "|", "CAST\\(([^)]+)\\):(", ")" );
for ( PolyType type : PolyType.values() ) {
joiner.add( type.getName() );
}
CAST_PATTERN = Pattern.compile( joiner.toString() ); // matches group "my_field" in "CAST(my_field):INTEGER"
}


public static String getFieldNameFromIndex( AlgNode alg, int idx ) {
for ( AlgNode input : alg.getInputs() ) {
Expand All @@ -55,7 +70,7 @@ public static String getFieldNameFromIndex( AlgNode alg, int idx ) {


public static String appendAlias( String exp, String alias ) {
if ( alias == null || alias.equals( exp ) ) {
if ( alias == null || alias.equals( exp ) || isCastWithSameName( exp, alias ) ) {
return exp;
}
return exp + " AS " + alias;
Expand All @@ -64,7 +79,7 @@ public static String appendAlias( String exp, String alias ) {

/**
* Each element in exps is compared with the corresponding element in aliases.
* If they differ, the alias is appended to the element, separated by the keyword {@code AS}.
* If they differ (and not just by a CAST expression), the alias is appended to the element, separated by the keyword {@code AS}.
* For example {@code AVG(age) AS average}.
*
* @param exps List of strings to be assigned an alias
Expand All @@ -81,6 +96,12 @@ public static List<String> appendAliases( List<String> exps, List<String> aliase
}


private static boolean isCastWithSameName( String exp, String alias ) {
Matcher m = CAST_PATTERN.matcher( exp );
return m.find() && m.group( 1 ).equals( alias );
}


/**
* Joins the values for a multivalued attribute into a single string.
* If values contains more than one element, the returned string is surrounded with brackets to represent a list.
Expand All @@ -95,7 +116,7 @@ public static String joinMultiValued( List<String> values, boolean omitBrackets
}


public static List<String> getAuxProjections( AlgNode child, Set<String> fieldNames) {
public static List<String> getAuxProjections( AlgNode child, Set<String> fieldNames ) {
List<String> projections = new ArrayList<>();
for ( AlgDataTypeField field : child.getTupleType().getFields() ) {
String name = field.getName();
Expand Down Expand Up @@ -184,7 +205,8 @@ public String visitDynamicParam( RexDynamicParam dynamicParam ) {

@Override
public String visitRangeRef( RexRangeRef rangeRef ) {
return "rangeRef: " + rangeRef;
// Regular RexNode trees do not contain this construct
return rangeRef.toString();
}


Expand Down

0 comments on commit 461559d

Please sign in to comment.