danny0405 commented on a change in pull request #2230:
URL: https://github.com/apache/calcite/pull/2230#discussion_r511865634
##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java
##########
@@ -129,18 +129,34 @@ private SqlSingleOperandTypeChecker
getChecker(SqlCallBinding callBinding) {
return typeFactory.createTypeWithNullability(operandType.getValueType(),
true);
case ROW:
- String fieldName = opBinding.getOperandLiteralValue(1, String.class);
- RelDataTypeField field = operandType.getField(fieldName, false, false);
- if (field == null) {
- throw new AssertionError("Cannot infer type of field '"
- + fieldName + "' within ROW type: " + operandType);
- } else {
- RelDataType fieldType = field.getType();
- if (operandType.isNullable()) {
- fieldType = typeFactory.createTypeWithNullability(fieldType, true);
+ RelDataType fieldType = null;
+ RelDataType indexType = opBinding.getOperandType(1);
+
+ if (SqlTypeFamily.STRING.contains(indexType)) {
+ String fieldName = opBinding.getOperandLiteralValue(1, String.class);
+ RelDataTypeField field = operandType.getField(fieldName, false, false);
+ if (field == null) {
+ throw new AssertionError("Cannot infer type of field '"
+ + fieldName + "' within ROW type: " + operandType);
+ } else {
+ fieldType = field.getType();
+ }
+ } else if (SqlTypeFamily.INTEGER.contains(indexType)) {
+ Integer index = opBinding.getOperandLiteralValue(1, Integer.class);
Review comment:
You can use the `SqlTypeUtil` to decide the type category.
##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlItemOperator.java
##########
@@ -129,18 +129,34 @@ private SqlSingleOperandTypeChecker
getChecker(SqlCallBinding callBinding) {
return typeFactory.createTypeWithNullability(operandType.getValueType(),
true);
case ROW:
- String fieldName = opBinding.getOperandLiteralValue(1, String.class);
- RelDataTypeField field = operandType.getField(fieldName, false, false);
- if (field == null) {
- throw new AssertionError("Cannot infer type of field '"
- + fieldName + "' within ROW type: " + operandType);
- } else {
- RelDataType fieldType = field.getType();
- if (operandType.isNullable()) {
- fieldType = typeFactory.createTypeWithNullability(fieldType, true);
+ RelDataType fieldType = null;
+ RelDataType indexType = opBinding.getOperandType(1);
+
+ if (SqlTypeFamily.STRING.contains(indexType)) {
+ String fieldName = opBinding.getOperandLiteralValue(1, String.class);
+ RelDataTypeField field = operandType.getField(fieldName, false, false);
+ if (field == null) {
+ throw new AssertionError("Cannot infer type of field '"
+ + fieldName + "' within ROW type: " + operandType);
+ } else {
+ fieldType = field.getType();
+ }
+ } else if (SqlTypeFamily.INTEGER.contains(indexType)) {
+ Integer index = opBinding.getOperandLiteralValue(1, Integer.class);
+ if (index == null || index < 1 || index > operandType.getFieldCount())
{
+ throw new AssertionError("Cannot infer type of field at position "
+ + index + " within ROW type: " + operandType);
+ } else {
+ fieldType = operandType.getFieldList().get(index - 1).getType(); //
1 indexed
}
- return fieldType;
+ } else {
+ throw new AssertionError("Unsupported field identifier type: '"
+ + indexType + "'");
+ }
+ if (fieldType != null && operandType.isNullable()) {
+ fieldType = typeFactory.createTypeWithNullability(fieldType, true);
Review comment:
Is `fieldType ` never null here ?
##########
File path:
core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
##########
@@ -8093,6 +8094,23 @@ void checkArrayConcatAggFuncFails(SqlTester t) {
tester.checkColumnType(
"select cast(null as any)['x'] from (values(1))",
"ANY");
+
+ // Row item
+ final String intStructQuery = "select \"T\".\"X\"[1] "
+ + "from (VALUES (ROW(ROW(3, 7), ROW(4, 8)))) as T(x, y)";
+ tester.check(intStructQuery, SqlTests.INTEGER_TYPE_CHECKER, 3, 0);
Review comment:
Does SQL standard allows access row fields with indices ?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]