Skip to content

Commit

Permalink
display column names that contain a dot correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudum committed Sep 11, 2015
1 parent bc5a6dd commit d4006c4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changes for Crate
Unreleased
==========

- Display column names that contain a dot correctly in information schema
and result column headers

2015/09/08 0.51.2
=================

Expand Down
9 changes: 0 additions & 9 deletions core/src/main/java/io/crate/core/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ public class StringUtils {
public static final Splitter PATH_SPLITTER = Splitter.on('.');
public static final Joiner PATH_JOINER = Joiner.on('.');

public static String dottedToSqlPath(String dottedPath) {
Iterable<String> splitted = PATH_SPLITTER.split(dottedPath);
Iterator<String> iter = splitted.iterator();
StringBuilder builder = new StringBuilder(iter.next());
while (iter.hasNext()) {
builder.append("['").append(iter.next()).append("']");
}
return builder.toString();
}

/**
* Return the common ancestors of a list of fields.<br>
Expand Down
8 changes: 0 additions & 8 deletions core/src/test/java/io/crate/core/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@

public class StringUtilsTest extends CrateUnitTest {

@Test
public void testDottedToSQLPath() {
assertEquals("a['b']", StringUtils.dottedToSqlPath("a.b"));
assertEquals("a", StringUtils.dottedToSqlPath("a"));
assertEquals("a['']", StringUtils.dottedToSqlPath("a."));
assertEquals("a['b']['c']", StringUtils.dottedToSqlPath("a.b.c"));
}

@Test
public void testCommonAncestors() throws Exception {
assertEquals(ImmutableSet.of("a"), StringUtils.commonAncestors(Arrays.asList("a", "a.b")));
Expand Down
8 changes: 7 additions & 1 deletion sql/src/main/java/io/crate/metadata/ColumnIdent.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,13 @@ public String outputName() {
}

public String sqlFqn() {
return StringUtils.dottedToSqlPath(fqn());
StringBuilder sb = new StringBuilder(name);
for (String s : path) {
sb.append("['");
sb.append(s);
sb.append("']");
}
return sb.toString();
}

public List<String> path() {
Expand Down
3 changes: 1 addition & 2 deletions sql/src/main/java/io/crate/metadata/PartitionInfos.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.crate.Constants;
import io.crate.analyze.TableParameterInfo;
import io.crate.core.NumberOfReplicas;
import io.crate.core.StringUtils;
import io.crate.metadata.doc.PartitionedByMappingExtractor;
import io.crate.types.DataType;
import io.crate.types.DataTypes;
Expand Down Expand Up @@ -99,7 +98,7 @@ private static Map<String, Object> buildValuesMap(PartitionName partitionName, M
Map<String, Object> valuesMap = new HashMap<>();
Iterable<Tuple<ColumnIdent, DataType>> partitionColumnInfoIterable = PartitionedByMappingExtractor.extractPartitionedByColumns(mappingMetaData.sourceAsMap());
for (Tuple<ColumnIdent, DataType> columnInfo : partitionColumnInfoIterable) {
String columnName = StringUtils.dottedToSqlPath(columnInfo.v1().fqn());
String columnName = columnInfo.v1().sqlFqn();
// produce string type values as string, not bytesref
Object value = BytesRefs.toString(partitionName.values().get(i));
if (!columnInfo.v2().equals(DataTypes.STRING)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package io.crate.operation.reference.doc.lucene;


import io.crate.core.StringUtils;
import io.crate.operation.reference.doc.ColumnReferenceExpression;

public abstract class ColumnReferenceCollectorExpression<ReturnType> extends
Expand All @@ -40,7 +39,7 @@ public String columnName() {

@Override
public String toString() {
return columnName().contains(".") ? StringUtils.dottedToSqlPath(columnName()) : columnName();
return columnName;
}

@Override
Expand Down
49 changes: 49 additions & 0 deletions sql/src/test/java/io/crate/metadata/ColumnIdentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Crate under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership. Crate licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial
* agreement.
*/

package io.crate.metadata;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class ColumnIdentTest {

@Test
public void testSqlFqn() throws Exception {
ColumnIdent ident = new ColumnIdent("foo", Arrays.asList("x", "y", "z"));
assertThat(ident.sqlFqn(), is("foo['x']['y']['z']"));

ident = new ColumnIdent("a");
assertThat(ident.sqlFqn(), is("a"));

ident = new ColumnIdent("a", Collections.singletonList(""));
assertThat(ident.sqlFqn(), is("a['']"));

ident = new ColumnIdent("a.b", Collections.singletonList("c"));
assertThat(ident.sqlFqn(), is("a.b['c']"));
}
}

0 comments on commit d4006c4

Please sign in to comment.