Skip to content

Commit

Permalink
Normalize symbol names
Browse files Browse the repository at this point in the history
Replace any sequence of non-alphanumeric with underscore.
  • Loading branch information
martint committed Mar 25, 2024
1 parent 7e84eea commit d24ed81
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.sql.planner;

import com.google.common.base.CharMatcher;
import com.google.common.primitives.Ints;
import io.trino.spi.type.Type;
import io.trino.sql.analyzer.Field;
Expand All @@ -30,6 +31,11 @@

public class SymbolAllocator
{
public static final CharMatcher EXCLUDED_CHARACTERS = CharMatcher.inRange('a', 'z')
.or(CharMatcher.inRange('0', '9'))
.negate()
.precomputed();

private final Map<String, Symbol> symbols;
private int nextId;

Expand All @@ -54,8 +60,7 @@ public Symbol newSymbol(String nameHint, Type type)
requireNonNull(nameHint, "nameHint is null");
requireNonNull(type, "type is null");

// TODO: workaround for the fact that QualifiedName lowercases parts
nameHint = nameHint.toLowerCase(ENGLISH);
nameHint = EXCLUDED_CHARACTERS.trimAndCollapseFrom(nameHint.toLowerCase(ENGLISH), '_');

// don't strip the tail if the only _ is the first character
int index = nameHint.lastIndexOf("_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ public void testPushProjection()
Map.Entry::getValue,
e -> column(e.getValue(), types.get(e.getKey()))));

String name = newNames.get(dereference);
String name1 = newNames.get(identity);
ruleTester.assertThat(createRule(ruleTester))
.withSession(MOCK_SESSION)
.on(p -> {
Expand Down Expand Up @@ -195,15 +193,7 @@ public void testPushProjection()
Optional.of(ImmutableList.copyOf(expectedColumns.values())))::equals,
TupleDomain.all(),
expectedColumns.entrySet().stream()
.collect(toImmutableMap(Map.Entry::getKey, e -> e.getValue()::equals)),
Optional.of(PlanNodeStatsEstimate.builder()
.setOutputRowCount(42)
.addSymbolStatistics(new Symbol(UNKNOWN, name1), SymbolStatsEstimate.builder()
.setDistinctValuesCount(33)
.setNullsFraction(0)
.build())
.addSymbolStatistics(new Symbol(UNKNOWN, name), SymbolStatsEstimate.unknown())
.build())::equals)));
.collect(toImmutableMap(Map.Entry::getKey, e -> e.getValue()::equals)))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,7 @@ public void testProjectionPushdownExplain()
assertExplain(
"EXPLAIN SELECT root.f2 FROM " + tableName,
"TableScan\\[table = (.*)]",
"root#f2 := root#f2:bigint:REGULAR");
"(.*) := (.*):bigint:REGULAR");

Session sessionWithoutPushdown = Session.builder(getSession())
.setCatalogSessionProperty(getSession().getCatalog().orElseThrow(), "projection_pushdown_enabled", "false")
Expand All @@ -2178,12 +2178,12 @@ public void testProjectionPushdownNonPrimitiveTypeExplain()
assertExplain(
"EXPLAIN SELECT id, _row.child, _array[1].child, _map[1] FROM " + tableName,
"ScanProject\\[table = (.*)]",
"expr(.*) := system\\.builtin\\.\\$operator\\$subscript\\(_array_.*, bigint '1'\\).0",
"expr(.*) := system\\.builtin\\.\\$operator\\$subscript\\(.*, bigint '1'\\).0",
"id(.*) := id:bigint:REGULAR",
// _array:array\\(row\\(child bigint\\)\\) is a symbol name, not a dereference expression.
"_array(.*) := _array:array\\(row\\(child bigint\\)\\):REGULAR",
"_map(.*) := _map:map\\(bigint, bigint\\):REGULAR",
"_row#child := _row#child:bigint:REGULAR");
"(.*) := _array:array\\(row\\(child bigint\\)\\):REGULAR",
"(.*) := _map:map\\(bigint, bigint\\):REGULAR",
"(.*) := _row#child:bigint:REGULAR");
}

@Test
Expand Down

0 comments on commit d24ed81

Please sign in to comment.