org.apache.calcite.sql.validate.SqlValidatorImpl Java Examples

The following examples show how to use org.apache.calcite.sql.validate.SqlValidatorImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the operands of a call, inferring the return type in the
 * process.
 *
 * @param validator active validator
 * @param scope     validation scope
 * @param call      call to be validated
 * @return inferred type
 */
public final RelDataType validateOperands(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Let subclasses know what's up.
  preValidateCall(validator, scope, call);

  // Check the number of operands
  checkOperandCount(validator, operandTypeChecker, call);

  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);

  checkOperandTypes(
      opBinding,
      true);

  // Now infer the result type.
  RelDataType ret = inferReturnType(opBinding);
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  return ret;
}
 
Example #2
Source File: SqlCall.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void findValidOptions(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlParserPos pos,
    Collection<SqlMoniker> hintList) {
  for (SqlNode operand : getOperandList()) {
    if (operand instanceof SqlIdentifier) {
      SqlIdentifier id = (SqlIdentifier) operand;
      SqlParserPos idPos = id.getParserPosition();
      if (idPos.toString().equals(pos.toString())) {
        ((SqlValidatorImpl) validator).lookupNameCompletionHints(
            scope, id.names, pos, hintList);
        return;
      }
    }
  }
  // no valid options
}
 
Example #3
Source File: SqlCall.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void findValidOptions(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlParserPos pos,
    Collection<SqlMoniker> hintList) {
  for (SqlNode operand : getOperandList()) {
    if (operand instanceof SqlIdentifier) {
      SqlIdentifier id = (SqlIdentifier) operand;
      SqlParserPos idPos = id.getParserPosition();
      if (idPos.toString().equals(pos.toString())) {
        ((SqlValidatorImpl) validator).lookupNameCompletionHints(
            scope, id.names, pos, hintList);
        return;
      }
    }
  }
  // no valid options
}
 
Example #4
Source File: SqlOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the operands of a call, inferring the return type in the
 * process.
 *
 * @param validator active validator
 * @param scope     validation scope
 * @param call      call to be validated
 * @return inferred type
 */
public final RelDataType validateOperands(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Let subclasses know what's up.
  preValidateCall(validator, scope, call);

  // Check the number of operands
  checkOperandCount(validator, operandTypeChecker, call);

  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call);

  checkOperandTypes(
      opBinding,
      true);

  // Now infer the result type.
  RelDataType ret = inferReturnType(opBinding);
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  return ret;
}
 
Example #5
Source File: TestSQLAnalyzerFactory.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreationOfValidator() {
  SabotContext sabotContext = mock(SabotContext.class);
  FunctionImplementationRegistry functionImplementationRegistry = mock(FunctionImplementationRegistry.class);
  CatalogService catalogService = mock(CatalogService.class);
  Catalog catalog = mock(Catalog.class);
  ProjectOptionManager mockOptions = mock(ProjectOptionManager.class);
  when(mockOptions.getOptionValidatorListing()).thenReturn(mock(OptionValidatorListing.class));

  // Stub appropriate methods.
  when(sabotContext.getFunctionImplementationRegistry()).thenReturn(functionImplementationRegistry);
  when(sabotContext.getCatalogService()).thenReturn(catalogService);
  when(sabotContext.getCatalogService().getCatalog(any(MetadataRequestOptions.class))).thenReturn(catalog);

  OptionValue value1 = OptionValue.createBoolean(OptionValue.OptionType.SYSTEM, PlannerSettings.ENABLE_DECIMAL_V2_KEY, false);
  OptionValue value2 = OptionValue.createLong(OptionValue.OptionType.SYSTEM, UserSession.MAX_METADATA_COUNT.getOptionName(), 0);
  OptionList optionList = new OptionList();
  optionList.add(value1);
  optionList.add(value2);

  when(mockOptions.getOption(PlannerSettings.ENABLE_DECIMAL_V2_KEY)).thenReturn(value1);
  when(mockOptions.getOption(UserSession.MAX_METADATA_COUNT.getOptionName())).thenReturn(value2);
  when(mockOptions.getNonDefaultOptions()).thenReturn(optionList);

  // Test that the correct concrete implementation is created.
  SQLAnalyzer sqlAnalyzer = SQLAnalyzerFactory.createSQLAnalyzer(SystemUser.SYSTEM_USERNAME, sabotContext, null, true, mockOptions);
  SqlValidatorWithHints validator = sqlAnalyzer.validator;
  assertTrue(validator instanceof SqlAdvisorValidator);

  sqlAnalyzer = SQLAnalyzerFactory.createSQLAnalyzer(SystemUser.SYSTEM_USERNAME, sabotContext, null, false, mockOptions);
  validator = sqlAnalyzer.validator;
  assertTrue(validator instanceof SqlValidatorImpl);
}
 
Example #6
Source File: SqlOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected List<RelDataType> constructArgTypeList(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call,
    List<SqlNode> args,
    boolean convertRowArgToColumnList) {
  // Scope for operands. Usually the same as 'scope'.
  final SqlValidatorScope operandScope = scope.getOperandScope(call);

  final ImmutableList.Builder<RelDataType> argTypeBuilder =
          ImmutableList.builder();
  for (SqlNode operand : args) {
    RelDataType nodeType;
    // for row arguments that should be converted to ColumnList
    // types, set the nodeType to a ColumnList type but defer
    // validating the arguments of the row constructor until we know
    // for sure that the row argument maps to a ColumnList type
    if (operand.getKind() == SqlKind.ROW && convertRowArgToColumnList) {
      RelDataTypeFactory typeFactory = validator.getTypeFactory();
      nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
      ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
    } else {
      nodeType = validator.deriveType(operandScope, operand);
    }
    argTypeBuilder.add(nodeType);
  }

  return argTypeBuilder.build();
}
 
Example #7
Source File: SqlOverOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Validate type of the inner aggregate call
  validateOperands(validator, scope, call);

  // Assume the first operand is an aggregate call and derive its type.
  // When we are sure the window is not empty, pass that information to the
  // aggregate's operator return type inference as groupCount=1
  // Otherwise pass groupCount=0 so the agg operator understands the window
  // can be empty
  SqlNode agg = call.operand(0);

  if (!(agg instanceof SqlCall)) {
    throw new IllegalStateException("Argument to SqlOverOperator"
        + " should be SqlCall, got " + agg.getClass() + ": " + agg);
  }

  SqlNode window = call.operand(1);
  SqlWindow w = validator.resolveWindow(window, scope);

  final int groupCount = w.isAlwaysNonEmpty() ? 1 : 0;
  final SqlCall aggCall = (SqlCall) agg;

  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
    @Override public int getGroupCount() {
      return groupCount;
    }
  };

  RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);

  // Copied from validateOperands
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  ((SqlValidatorImpl) validator).setValidatedNodeType(agg, ret);
  return ret;
}
 
Example #8
Source File: SqlJsonObjectAggAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator validator,
    SqlValidatorScope scope, SqlCall call) {
  // To prevent operator rewriting by SqlFunction#deriveType.
  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #9
Source File: SqlJsonArrayAggAggFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator validator,
    SqlValidatorScope scope, SqlCall call) {
  // To prevent operator rewriting by SqlFunction#deriveType.
  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #10
Source File: SqlFilterOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Validate type of the inner aggregate call
  validateOperands(validator, scope, call);

  // Assume the first operand is an aggregate call and derive its type.
  final SqlCall aggCall = getAggCall(call);

  // Pretend that group-count is 0. This tells the aggregate function that it
  // might be invoked with 0 rows in a group. Most aggregate functions will
  // return NULL in this case.
  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
    @Override public int getGroupCount() {
      return 0;
    }
  };

  RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);

  // Copied from validateOperands
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  ((SqlValidatorImpl) validator).setValidatedNodeType(aggCall, ret);
  if (hasWithinGroupCall(call)) {
    ((SqlValidatorImpl) validator).setValidatedNodeType(getWithinGroupCall(call), ret);
  }
  return ret;
}
 
Example #11
Source File: SqlJdbcFunctionCall.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Override SqlFunction.deriveType, because function-resolution is
  // not relevant to a JDBC function call.
  // REVIEW: jhyde, 2006/4/18: Should SqlJdbcFunctionCall even be a
  // subclass of SqlFunction?

  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #12
Source File: SqlJdbcFunctionCall.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Override SqlFunction.deriveType, because function-resolution is
  // not relevant to a JDBC function call.
  // REVIEW: jhyde, 2006/4/18: Should SqlJdbcFunctionCall even be a
  // subclass of SqlFunction?

  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #13
Source File: SqlOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected List<RelDataType> constructArgTypeList(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call,
    List<SqlNode> args,
    boolean convertRowArgToColumnList) {
  // Scope for operands. Usually the same as 'scope'.
  final SqlValidatorScope operandScope = scope.getOperandScope(call);

  final ImmutableList.Builder<RelDataType> argTypeBuilder =
          ImmutableList.builder();
  for (SqlNode operand : args) {
    RelDataType nodeType;
    // for row arguments that should be converted to ColumnList
    // types, set the nodeType to a ColumnList type but defer
    // validating the arguments of the row constructor until we know
    // for sure that the row argument maps to a ColumnList type
    if (operand.getKind() == SqlKind.ROW && convertRowArgToColumnList) {
      RelDataTypeFactory typeFactory = validator.getTypeFactory();
      nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
      ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
    } else {
      nodeType = validator.deriveType(operandScope, operand);
    }
    argTypeBuilder.add(nodeType);
  }

  return argTypeBuilder.build();
}
 
Example #14
Source File: SqlOverOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Validate type of the inner aggregate call
  validateOperands(validator, scope, call);

  // Assume the first operand is an aggregate call and derive its type.
  // When we are sure the window is not empty, pass that information to the
  // aggregate's operator return type inference as groupCount=1
  // Otherwise pass groupCount=0 so the agg operator understands the window
  // can be empty
  SqlNode agg = call.operand(0);

  if (!(agg instanceof SqlCall)) {
    throw new IllegalStateException("Argument to SqlOverOperator"
        + " should be SqlCall, got " + agg.getClass() + ": " + agg);
  }

  SqlNode window = call.operand(1);
  SqlWindow w = validator.resolveWindow(window, scope, false);

  final int groupCount = w.isAlwaysNonEmpty() ? 1 : 0;
  final SqlCall aggCall = (SqlCall) agg;

  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
    @Override public int getGroupCount() {
      return groupCount;
    }
  };

  RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);

  // Copied from validateOperands
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  ((SqlValidatorImpl) validator).setValidatedNodeType(agg, ret);
  return ret;
}
 
Example #15
Source File: SqlJsonObjectAggAggFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator validator,
    SqlValidatorScope scope, SqlCall call) {
  // To prevent operator rewriting by SqlFunction#deriveType.
  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #16
Source File: SqlJsonArrayAggAggFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveType(SqlValidator validator,
    SqlValidatorScope scope, SqlCall call) {
  // To prevent operator rewriting by SqlFunction#deriveType.
  for (SqlNode operand : call.getOperandList()) {
    RelDataType nodeType = validator.deriveType(scope, operand);
    ((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
  }
  return validateOperands(validator, scope, call);
}
 
Example #17
Source File: SqlFilterOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType deriveType(
    SqlValidator validator,
    SqlValidatorScope scope,
    SqlCall call) {
  // Validate type of the inner aggregate call
  validateOperands(validator, scope, call);

  // Assume the first operand is an aggregate call and derive its type.
  final SqlCall aggCall = getAggCall(call);

  // Pretend that group-count is 0. This tells the aggregate function that it
  // might be invoked with 0 rows in a group. Most aggregate functions will
  // return NULL in this case.
  SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
    @Override public int getGroupCount() {
      return 0;
    }
  };

  RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);

  // Copied from validateOperands
  ((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
  ((SqlValidatorImpl) validator).setValidatedNodeType(aggCall, ret);
  if (hasWithinGroupCall(call)) {
    ((SqlValidatorImpl) validator).setValidatedNodeType(getWithinGroupCall(call), ret);
  }
  return ret;
}
 
Example #18
Source File: MycatCalcitePlanner.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public SqlValidatorImpl getSqlValidator() {
    SqlOperatorTable opTab = MycatCalciteSupport.INSTANCE.config.getOperatorTable();
    SqlValidatorCatalogReader catalogReader = createCalciteCatalogReader();
    RelDataTypeFactory typeFactory = MycatCalciteSupport.INSTANCE.TypeFactory;
    return (SqlValidatorImpl) SqlValidatorUtil.newValidator(opTab, catalogReader, typeFactory, MycatCalciteSupport.INSTANCE.getValidatorConfig());
}