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

The following examples show how to use org.apache.calcite.sql.validate.SqlValidatorWithHints. 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: TestSQLAnalyzer.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
  // Create and Mock dependencies
  final SabotContext sabotContext = mock(SabotContext.class);
  final FunctionImplementationRegistry functionImplementationRegistry = mock(FunctionImplementationRegistry.class);
  final JavaTypeFactory typeFactory = JavaTypeFactoryImpl.INSTANCE;

  // Stub necessary methods
  when(sabotContext.getFunctionImplementationRegistry()).thenReturn(functionImplementationRegistry);

  // Utilize custom catalog reader implementation to return specific suggestions,
  // without requiring server startup
  MockCatalogReader mockCatalogReader = new MockCatalogReader(typeFactory, false);
  SqlValidatorWithHints validator =  new SqlAdvisorValidator(new OperatorTable(sabotContext.getFunctionImplementationRegistry()), mockCatalogReader, typeFactory, DremioSqlConformance.INSTANCE);
  sqlAnalyzer = new SQLAnalyzer(validator);
}
 
Example #2
Source File: SqlAdvisor.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SqlAdvisor with a validator instance and given parser configuration
 *
 * @param validator Validator
 * @param parserConfig parser config
 */
public SqlAdvisor(
    SqlValidatorWithHints validator,
    SqlParser.Config parserConfig) {
  this.validator = validator;
  this.parserConfig = parserConfig;
}
 
Example #3
Source File: SQLAnalyzerFactory.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method to create the SQLAnalyzer using the appropriate implementation of SqlValidatorWithHints.
 *
 * If createForSqlSuggestions is true, construct a SqlAdvisorValidator instance,
 * otherwise construct a SqlValidatorImpl instance. Inject this into the constructor
 * for a SQLAnalyzer object.
 *
 * @param username
 * @param sabotContext
 * @param context
 * @param createForSqlSuggestions
 * @return SQLAnalyzer instance
 */
public static SQLAnalyzer createSQLAnalyzer(final String username,
                                            final SabotContext sabotContext,
                                            final List<String> context,
                                            final boolean createForSqlSuggestions,
                                            ProjectOptionManager projectOptionManager) {
  final ViewExpansionContext viewExpansionContext = new ViewExpansionContext(username);
  final OptionManager optionManager = OptionManagerWrapper.Builder.newBuilder()
    .withOptionManager(new DefaultOptionManager(sabotContext.getOptionValidatorListing()))
    .withOptionManager(new EagerCachingOptionManager(projectOptionManager))
    .withOptionManager(new QueryOptionManager(sabotContext.getOptionValidatorListing()))
    .build();
  final NamespaceKey defaultSchemaPath = context == null ? null : new NamespaceKey(context);

  final SchemaConfig newSchemaConfig = SchemaConfig.newBuilder(username)
    .defaultSchema(defaultSchemaPath)
    .optionManager(optionManager)
    .setViewExpansionContext(viewExpansionContext)
    .build();

  Catalog catalog = sabotContext.getCatalogService()
      .getCatalog(MetadataRequestOptions.of(newSchemaConfig));
  JavaTypeFactory typeFactory = JavaTypeFactoryImpl.INSTANCE;
  DremioCatalogReader catalogReader = new DremioCatalogReader(catalog, typeFactory);

  FunctionImplementationRegistry functionImplementationRegistry = optionManager.getOption
    (PlannerSettings.ENABLE_DECIMAL_V2_KEY).getBoolVal() ? sabotContext.getDecimalFunctionImplementationRegistry()
      : sabotContext.getFunctionImplementationRegistry();
  OperatorTable opTable = new OperatorTable(functionImplementationRegistry);
  SqlOperatorTable chainedOpTable =  new ChainedSqlOperatorTable(ImmutableList.<SqlOperatorTable>of(opTable, catalogReader));

  // Create the appropriate implementation depending on intended use of the validator.
  SqlValidatorWithHints validator =
    createForSqlSuggestions ?
      new SqlAdvisorValidator(chainedOpTable, catalogReader, typeFactory, DremioSqlConformance.INSTANCE) :
      SqlValidatorUtil.newValidator(chainedOpTable, catalogReader, typeFactory, DremioSqlConformance.INSTANCE);

  return new SQLAnalyzer(validator);
}
 
Example #4
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 #5
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SqlAdvisor with a validator instance and given parser configuration
 *
 * @param validator Validator
 * @param parserConfig parser config
 */
public SqlAdvisor(
    SqlValidatorWithHints validator,
    SqlParser.Config parserConfig) {
  this.validator = validator;
  this.parserConfig = parserConfig;
}
 
Example #6
Source File: SqlTestFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlAdvisor createAdvisor() {
  SqlValidator validator = getValidator();
  if (validator instanceof SqlValidatorWithHints) {
    return new SqlAdvisor((SqlValidatorWithHints) validator, parserConfig.get());
  }
  throw new UnsupportedOperationException(
      "Validator should implement SqlValidatorWithHints, actual validator is " + validator);
}
 
Example #7
Source File: SQLAnalyzer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
protected SQLAnalyzer(final SqlValidatorWithHints validator) {
  this.validator = validator;
}
 
Example #8
Source File: SqlAdvisor.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a SqlAdvisor with a validator instance
 *
 * @param validator Validator
 * @deprecated use {@link #SqlAdvisor(SqlValidatorWithHints, SqlParser.Config)}
 */
@Deprecated
public SqlAdvisor(
    SqlValidatorWithHints validator) {
  this(validator, SqlParser.Config.DEFAULT);
}
 
Example #9
Source File: SqlAdvisor.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a SqlAdvisor with a validator instance
 *
 * @param validator Validator
 * @deprecated use {@link #SqlAdvisor(SqlValidatorWithHints, SqlParser.Config)}
 */
@Deprecated // to be removed before 2.0
public SqlAdvisor(
    SqlValidatorWithHints validator) {
  this(validator, SqlParser.Config.DEFAULT);
}