Skip to content

Commit

Permalink
[GIE Compiler] fix bugs of empty array or map
Browse files Browse the repository at this point in the history
  • Loading branch information
shirly121 committed Oct 18, 2023
1 parent 89c417b commit 9603ad5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFail
return (componentType == null)
? typeFactory.createSqlType(SqlTypeName.ANY)
: componentType;
} catch (AssertionError e) {
} catch (Throwable e) {
return typeFactory.createSqlType(SqlTypeName.ANY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
List<RelDataType> argTypes = opBinding.collectOperandTypes();
List<RelDataType> keyTypes = Util.quotientList(argTypes, 2, 0);
List<RelDataType> valueTypes = Util.quotientList(argTypes, 2, 1);
RelDataType keyType = typeFactory.leastRestrictive(keyTypes);
RelDataType keyType = getComponentType(typeFactory, keyTypes);
RelDataType valueType = getComponentType(typeFactory, valueTypes);
if (valueType != null && valueType.getSqlTypeName() != SqlTypeName.ANY) {
if (keyType != null
&& keyType.getSqlTypeName() != SqlTypeName.ANY
&& valueType != null
&& valueType.getSqlTypeName() != SqlTypeName.ANY) {
return SqlTypeUtil.createMapType(opBinding.getTypeFactory(), keyType, valueType, false);
} else {
return ((GraphTypeFactoryImpl) typeFactory)
.createArbitraryMapType(keyType, valueTypes, false);
.createArbitraryMapType(keyTypes, valueTypes, false);
}
}

Expand All @@ -74,7 +77,7 @@ public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFail
return (componentType == null)
? typeFactory.createSqlType(SqlTypeName.ANY)
: componentType;
} catch (AssertionError e) {
} catch (Throwable e) {
return typeFactory.createSqlType(SqlTypeName.ANY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.type.AbstractSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* introduce a new array type to allow different component types in a single array,
Expand All @@ -33,7 +33,7 @@ public class ArbitraryArrayType extends AbstractSqlType {

public ArbitraryArrayType(List<RelDataType> componentTypes, boolean isNullable) {
super(SqlTypeName.ARRAY, isNullable, null);
this.componentTypes = ObjectUtils.requireNonEmpty(componentTypes);
this.componentTypes = Objects.requireNonNull(componentTypes);
this.computeDigest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,34 @@
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.type.AbstractSqlType;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* introduce a new map type to allow different value types in a single map,
* introduce a new map type to allow different keys or value types in a single map,
* to support {@code MapLiteral} in cypher, i.e. [name: a.name, a: a, age: b.age]
*/
public class ArbitraryMapType extends AbstractSqlType {
private final RelDataType keyType;
private final List<RelDataType> keyTypes;
private final List<RelDataType> valueTypes;

protected ArbitraryMapType(
RelDataType keyType, List<RelDataType> valueTypes, boolean isNullable) {
List<RelDataType> keyTypes, List<RelDataType> valueTypes, boolean isNullable) {
super(SqlTypeName.MAP, isNullable, null);
this.keyType = Objects.requireNonNull(keyType);
this.valueTypes = ObjectUtils.requireNonEmpty(valueTypes);
this.keyTypes = Objects.requireNonNull(keyTypes);
this.valueTypes = Objects.requireNonNull(valueTypes);
this.computeDigest();
}

@Override
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
sb.append("(" + keyType.toString() + ", " + valueTypes.toString() + ") MAP");
sb.append("(" + keyTypes.toString() + ", " + valueTypes.toString() + ") MAP");
}

@Override
public RelDataType getKeyType() {
return this.keyType;
public List<RelDataType> getKeyTypes() {
return Collections.unmodifiableList(this.keyTypes);
}

public List<RelDataType> getValueTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public RelDataType createArbitraryArrayType(
}

public RelDataType createArbitraryMapType(
RelDataType keyType, List<RelDataType> valueTypes, boolean isNullable) {
return new ArbitraryMapType(keyType, valueTypes, isNullable);
List<RelDataType> keyTypes, List<RelDataType> valueTypes, boolean isNullable) {
return new ArbitraryMapType(keyTypes, valueTypes, isNullable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected AnyValue parseEntry(IrResult.Entry entry, @Nullable RelDataType dataTy
} else if (dataType instanceof ArbitraryMapType) {
return parseKeyValues(
entry.getMap(),
dataType.getKeyType(),
((ArbitraryMapType) dataType).getKeyTypes(),
((ArbitraryMapType) dataType).getValueTypes());
}
default:
Expand Down Expand Up @@ -190,7 +190,9 @@ protected AnyValue parseKeyValues(
}

protected AnyValue parseKeyValues(
IrResult.KeyValues keyValues, RelDataType keyType, List<RelDataType> valueTypes) {
IrResult.KeyValues keyValues,
List<RelDataType> keyTypes,
List<RelDataType> valueTypes) {
List<IrResult.KeyValues.KeyValue> entries = keyValues.getKeyValuesList();
Preconditions.checkArgument(
entries.size() == valueTypes.size(),
Expand Down

0 comments on commit 9603ad5

Please sign in to comment.