private RelDataType validateUsingCol(SqlIdentifier id, SqlNode leftOrRight) { if (id.names.size() == 1) { String name = id.names.get(0); final SqlValidatorNamespace namespace = getNamespace(leftOrRight); final RelDataType rowType = namespace.getRowType(); final SqlNameMatcher nameMatcher = catalogReader.nameMatcher(); final RelDataTypeField field = nameMatcher.field(rowType, name); if (field != null) { if (nameMatcher.frequency(rowType.getFieldNames(), name) > 1) { throw newValidationError(id, RESOURCE.columnInUsingNotUnique(id.toString())); } return field.getType(); } } throw newValidationError(id, RESOURCE.columnNotFound(id.toString())); }
/** * Derives the list of column names suitable for NATURAL JOIN. These are the * columns that occur exactly once on each side of the join. * * @param nameMatcher Whether matches are case-sensitive * @param leftRowType Row type of left input to the join * @param rightRowType Row type of right input to the join * @return List of columns that occur once on each side */ public static List<String> deriveNaturalJoinColumnList( SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType) { final List<String> naturalColumnNames = new ArrayList<>(); final List<String> leftNames = leftRowType.getFieldNames(); final List<String> rightNames = rightRowType.getFieldNames(); for (String name : leftNames) { if (nameMatcher.frequency(leftNames, name) == 1 && nameMatcher.frequency(rightNames, name) == 1) { naturalColumnNames.add(name); } } return naturalColumnNames; }
/** * Derives the list of column names suitable for NATURAL JOIN. These are the * columns that occur exactly once on each side of the join. * * @param nameMatcher Whether matches are case-sensitive * @param leftRowType Row type of left input to the join * @param rightRowType Row type of right input to the join * @return List of columns that occur once on each side */ public static List<String> deriveNaturalJoinColumnList( SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType) { final List<String> naturalColumnNames = new ArrayList<>(); final List<String> leftNames = leftRowType.getFieldNames(); final List<String> rightNames = rightRowType.getFieldNames(); for (String name : leftNames) { if (nameMatcher.frequency(leftNames, name) == 1 && nameMatcher.frequency(rightNames, name) == 1) { naturalColumnNames.add(name); } } return naturalColumnNames; }
@Test public void testNameMatcher() { final ImmutableList<String> beatles = ImmutableList.of("john", "paul", "ringo", "rinGo"); final SqlNameMatcher insensitiveMatcher = SqlNameMatchers.withCaseSensitive(false); assertThat(insensitiveMatcher.frequency(beatles, "ringo"), is(2)); assertThat(insensitiveMatcher.frequency(beatles, "rinGo"), is(2)); final SqlNameMatcher sensitiveMatcher = SqlNameMatchers.withCaseSensitive(true); assertThat(sensitiveMatcher.frequency(beatles, "ringo"), is(1)); assertThat(sensitiveMatcher.frequency(beatles, "rinGo"), is(1)); assertThat(sensitiveMatcher.frequency(beatles, "Ringo"), is(0)); } }
@Test public void testNameMatcher() { final ImmutableList<String> beatles = ImmutableList.of("john", "paul", "ringo", "rinGo"); final SqlNameMatcher insensitiveMatcher = SqlNameMatchers.withCaseSensitive(false); assertThat(insensitiveMatcher.frequency(beatles, "ringo"), is(2)); assertThat(insensitiveMatcher.frequency(beatles, "rinGo"), is(2)); final SqlNameMatcher sensitiveMatcher = SqlNameMatchers.withCaseSensitive(true); assertThat(sensitiveMatcher.frequency(beatles, "ringo"), is(1)); assertThat(sensitiveMatcher.frequency(beatles, "rinGo"), is(1)); assertThat(sensitiveMatcher.frequency(beatles, "Ringo"), is(0)); } }
private RelDataType validateUsingCol(SqlIdentifier id, SqlNode leftOrRight) { if (id.names.size() == 1) { String name = id.names.get(0); final SqlValidatorNamespace namespace = getNamespace(leftOrRight); final RelDataType rowType = namespace.getRowType(); final SqlNameMatcher nameMatcher = catalogReader.nameMatcher(); final RelDataTypeField field = nameMatcher.field(rowType, name); if (field != null) { if (nameMatcher.frequency(rowType.getFieldNames(), name) > 1) { throw newValidationError(id, RESOURCE.columnInUsingNotUnique(id.toString())); } return field.getType(); } } throw newValidationError(id, RESOURCE.columnNotFound(id.toString())); }
private RelDataType validateUsingCol(SqlIdentifier id, SqlNode leftOrRight) { if (id.names.size() == 1) { String name = id.names.get(0); final SqlValidatorNamespace namespace = getNamespace(leftOrRight); final RelDataType rowType = namespace.getRowType(); final SqlNameMatcher nameMatcher = catalogReader.nameMatcher(); final RelDataTypeField field = nameMatcher.field(rowType, name); if (field != null) { if (nameMatcher.frequency(rowType.getFieldNames(), name) > 1) { throw newValidationError(id, RESOURCE.columnInUsingNotUnique(id.toString())); } return field.getType(); } } throw newValidationError(id, RESOURCE.columnNotFound(id.toString())); }