Java Code Examples for org.apache.calcite.sql.parser.SqlParser#Config

The following examples show how to use org.apache.calcite.sql.parser.SqlParser#Config . 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: SqlAdvisorTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void testSimpleParserQuotedIdImpl() {
  SqlParser.Config parserConfig = tester.getFactory().getParserConfig();
  String sql;
  String expected;

  // unclosed double-quote
  sql = replaceQuotes(parserConfig, "select * from t where [^");
  expected = replaceQuotes(parserConfig, "SELECT * FROM t WHERE _suggest_");
  assertSimplify(sql, expected);

  // closed double-quote
  sql = replaceQuotes(parserConfig, "select * from t where [^] and x = y");
  expected = replaceQuotes(parserConfig, "SELECT * FROM t WHERE _suggest_ and x = y");
  assertSimplify(sql, expected);

  // closed double-quote containing extra stuff
  sql = replaceQuotes(parserConfig, "select * from t where [^foo] and x = y");
  expected = replaceQuotes(parserConfig, "SELECT * FROM t WHERE _suggest_ and x = y");
  assertSimplify(sql, expected);

  // escaped double-quote containing extra stuff
  sql = replaceQuotes(parserConfig, "select * from t where [^f]]oo] and x = y");
  expected = replaceQuotes(parserConfig, "SELECT * FROM t WHERE _suggest_ and x = y");
  assertSimplify(sql, expected);
}
 
Example 2
Source File: GremlinCompiler.java    From sql-gremlin with Apache License 2.0 6 votes vote down vote up
public GremlinCompiler(Graph graph, SchemaConfig schemaConfig) {
    this.graph = graph;
    this.schemaConfig = schemaConfig;

    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelCollationTraitDef.INSTANCE);
    final SqlParser.Config parserConfig =
            SqlParser.configBuilder().setLex(Lex.MYSQL).build();

    frameworkConfig = Frameworks.newConfigBuilder()
            .parserConfig(parserConfig)
            .defaultSchema(rootSchema.add("gremlin", new GremlinSchema(graph, schemaConfig)))
            .traitDefs(traitDefs)
            .programs(Programs.sequence(Programs.ofRules(Programs.RULE_SET), Programs.CALC_PROGRAM))
            .build();
}
 
Example 3
Source File: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
private CatalogReader createCatalogReader(
		boolean lenientCaseSensitivity,
		String currentCatalog,
		String currentDatabase) {
	SqlParser.Config sqlParserConfig = getSqlParserConfig();
	final boolean caseSensitive;
	if (lenientCaseSensitivity) {
		caseSensitive = false;
	} else {
		caseSensitive = sqlParserConfig.caseSensitive();
	}

	SqlParser.Config parserConfig = SqlParser.configBuilder(sqlParserConfig)
		.setCaseSensitive(caseSensitive)
		.build();

	return new CatalogReader(
		rootSchema,
		asList(
			asList(currentCatalog, currentDatabase),
			singletonList(currentCatalog)
		),
		typeFactory,
		CalciteConfig.connectionConfig(parserConfig));
}
 
Example 4
Source File: QueryMetricsUtil.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    if (context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder()
                .input(input)
                .subject(subject)
                .valid(true)
                .explanation("Expression Language Present")
                .build();
    }

    final String substituted = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();

    final SqlParser.Config config = SqlParser.configBuilder()
            .setLex(Lex.MYSQL_ANSI)
            .build();

    final SqlParser parser = SqlParser.create(substituted, config);
    try {
        parser.parseStmt();
        return new ValidationResult.Builder()
                .subject(subject)
                .input(input)
                .valid(true)
                .build();
    } catch (final Exception e) {
        return new ValidationResult.Builder()
                .subject(subject)
                .input(input)
                .valid(false)
                .explanation("Not a valid SQL Statement: " + e.getMessage())
                .build();
    }
}
 
Example 5
Source File: KSqlParser.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
/** Parser sql mapper kafka tree. */
public static TopicPartitionSchema parserTopic(String sql) {
	TopicPartitionSchema tps = new TopicPartitionSchema();
	try {
		SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.JAVA).build();
		SqlParser sqlParser = SqlParser.create(sql, config);
		SqlNode sqlNode = sqlParser.parseStmt();
		parseNode(sqlNode, tps);
	} catch (Exception e) {
		ErrorUtils.print(KSqlParser.class).error("Parser kafka sql has error, msg is ", e);
	}
	return tps;
}
 
Example 6
Source File: TableEnv.java    From marble with Apache License 2.0 5 votes vote down vote up
private CalciteConnectionConfig createDefaultConnectionConfig(
    SqlParser.Config sqlParserConfig) {
  Properties prop = new Properties();
  prop.setProperty(CalciteConnectionProperty.CASE_SENSITIVE.camelName(),
      String.valueOf(sqlParserConfig.caseSensitive()));
  return new CalciteConnectionConfigImpl(prop);
}
 
Example 7
Source File: PlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-569">[CALCITE-569]
 * ArrayIndexOutOfBoundsException when deducing collation</a>. */
@Test void testOrderByNonSelectColumn() throws Exception {
  final SchemaPlus schema = Frameworks.createRootSchema(true)
      .add("tpch", new ReflectiveSchema(new TpchSchema()));

  String query = "select t.psPartkey from\n"
      + "(select ps.psPartkey from `tpch`.`partsupp` ps\n"
      + "order by ps.psPartkey, ps.psSupplyCost) t\n"
      + "order by t.psPartkey";

  List<RelTraitDef> traitDefs = new ArrayList<>();
  traitDefs.add(ConventionTraitDef.INSTANCE);
  traitDefs.add(RelCollationTraitDef.INSTANCE);
  final SqlParser.Config parserConfig =
      SqlParser.configBuilder().setLex(Lex.MYSQL).build();
  FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(parserConfig)
      .defaultSchema(schema)
      .traitDefs(traitDefs)
      .programs(Programs.ofRules(Programs.RULE_SET))
      .build();
  String plan;
  try (Planner p = Frameworks.getPlanner(config)) {
    SqlNode n = p.parse(query);
    n = p.validate(n);
    RelNode r = p.rel(n).project();
    plan = RelOptUtil.toString(r);
    plan = Util.toLinux(plan);
  }
  assertThat(plan,
      equalTo("LogicalSort(sort0=[$0], dir0=[ASC])\n"
      + "  LogicalProject(psPartkey=[$0])\n"
      + "    LogicalTableScan(table=[[tpch, partsupp]])\n"));
}
 
Example 8
Source File: PlannerContext.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the SQL parser config for this environment including a custom Calcite configuration.
 */
private SqlParser.Config getSqlParserConfig() {
	return JavaScalaConversionUtil.toJava(getCalciteConfig(tableConfig).getSqlParserConfig()).orElseGet(
			// we use Java lex because back ticks are easier than double quotes in programming
			// and cases are preserved
			() -> SqlParser
					.configBuilder()
					.setParserFactory(FlinkSqlParserImpl.FACTORY)
					.setConformance(getSqlConformance())
					.setLex(Lex.JAVA)
					.setIdentifierMaxLength(256)
					.build());
}
 
Example 9
Source File: FlinkDDLDataTypeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static SqlParser.Config createParserConfig(Map<String, Object> options) {
	return SqlParser.configBuilder()
		.setQuoting((Quoting) options.get("quoting"))
		.setUnquotedCasing((Casing) options.get("unquotedCasing"))
		.setQuotedCasing((Casing) options.get("quotedCasing"))
		.setConformance((SqlConformance) options.get("conformance"))
		.setCaseSensitive((boolean) options.get("caseSensitive"))
		.setParserFactory((SqlParserImplFactory) options.get("parserFactory"))
		.build();
}
 
Example 10
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 11
Source File: CalciteParser.java    From flink with Apache License 2.0 4 votes vote down vote up
public CalciteParser(SqlParser.Config config) {
	this.config = config;
}
 
Example 12
Source File: BatsEngine.java    From Bats with Apache License 2.0 4 votes vote down vote up
public static SqlNode parse(String sql) throws SqlParseException {
    SqlParser.Config config = SqlParser.configBuilder().setUnquotedCasing(org.apache.calcite.util.Casing.TO_LOWER)
            .build();
    return parse(sql, config);
}
 
Example 13
Source File: TableConfig.java    From marble with Apache License 2.0 4 votes vote down vote up
public SqlParser.Config getSqlParserConfig() {
  return sqlParserConfig;
}
 
Example 14
Source File: TableEnv.java    From marble with Apache License 2.0 4 votes vote down vote up
public TableEnv(TableConfig tableConfig) {
  try {
    this.tableConfig = tableConfig;
    SqlParser.Config sqlParserConfig = tableConfig.getSqlParserConfig()
        != null ? tableConfig.getSqlParserConfig() : SqlParser
        .configBuilder().setCaseSensitive(false)
        .build();
    SqlOperatorTable sqlStdOperatorTable = tableConfig
        .getSqlOperatorTable()
        != null
        ? tableConfig.getSqlOperatorTable()
        : ChainedSqlOperatorTable.of(SqlStdOperatorTable.instance());
    CalciteConnectionConfig calciteConnectionConfig = tableConfig
        .getCalciteConnectionConfig()
        != null
        ? tableConfig.getCalciteConnectionConfig()
        : createDefaultConnectionConfig(sqlParserConfig);
    RelDataTypeSystem typeSystem = tableConfig.getRelDataTypeSystem() != null
        ? tableConfig.getRelDataTypeSystem()
        : calciteConnectionConfig.typeSystem(RelDataTypeSystem.class,
            RelDataTypeSystem.DEFAULT);
    SqlRexConvertletTable convertletTable = tableConfig
        .getConvertletTable()
        != null
        ? tableConfig
        .getConvertletTable()
        : StandardConvertletTable.INSTANCE;
    RexExecutor rexExecutor = tableConfig.getRexExecutor() != null
        ? tableConfig.getRexExecutor()
        : RexUtil.EXECUTOR;
    this.calciteCatalogReader = new CalciteCatalogReader(
        CalciteSchema.from(rootSchema),
        CalciteSchema.from(rootSchema).path(null),
        new JavaTypeFactoryImpl(typeSystem),
        calciteConnectionConfig);
    this.frameworkConfig = createFrameworkConfig(sqlParserConfig,
        ChainedSqlOperatorTable.of(sqlStdOperatorTable,
            calciteCatalogReader), convertletTable,
        calciteConnectionConfig, typeSystem, rexExecutor);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 15
Source File: SqlHandlerUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * When enabled, add a writer rel on top of the given rel to catch the output and write to configured store table.
 * @param inputRel
 * @return
 */
public static Rel storeQueryResultsIfNeeded(final SqlParser.Config config, final QueryContext context,
                                            final Rel inputRel) {
  final OptionManager options = context.getOptions();
  final StoreQueryResultsPolicy storeQueryResultsPolicy = Optional
      .ofNullable(options.getOption(STORE_QUERY_RESULTS.getOptionName()))
      .map(o -> StoreQueryResultsPolicy.valueOf(o.getStringVal().toUpperCase(Locale.ROOT)))
      .orElse(StoreQueryResultsPolicy.NO);

  switch (storeQueryResultsPolicy) {
  case NO:
    return inputRel;

  case DIRECT_PATH:
  case PATH_AND_ATTEMPT_ID:
    // supported cases
    break;

  default:
    logger.warn("Unknown query result store policy {}. Query results won't be saved", storeQueryResultsPolicy);
    return inputRel;
  }

  final String storeTablePath = options.getOption(QUERY_RESULTS_STORE_TABLE.getOptionName()).getStringVal();
  final List<String> storeTable =
      new StrTokenizer(storeTablePath, '.', config.quoting().string.charAt(0))
          .setIgnoreEmptyTokens(true)
          .getTokenList();

  if (storeQueryResultsPolicy == StoreQueryResultsPolicy.PATH_AND_ATTEMPT_ID) {
    // QueryId is same as attempt id. Using its string form for the table name
    storeTable.add(QueryIdHelper.getQueryId(context.getQueryId()));
  }

  // Query results are stored in arrow format. If need arises, we can change this to a configuration option.
  final Map<String, Object> storageOptions = ImmutableMap.<String, Object>of("type", ArrowFormatPlugin.ARROW_DEFAULT_NAME);

  WriterOptions writerOptions =
    options.getOption(PlannerSettings.ENABLE_OUTPUT_LIMITS)
    ? WriterOptions.DEFAULT.withRecordLimit(options.getOption(PlannerSettings.OUTPUT_LIMIT_SIZE))
    : WriterOptions.DEFAULT;

  // store table as system user.
  final CreateTableEntry createTableEntry = context.getCatalog()
      .resolveCatalog(SystemUser.SYSTEM_USERNAME)
      .createNewTable(new NamespaceKey(storeTable), null, writerOptions, storageOptions);

  final RelTraitSet traits = inputRel.getCluster().traitSet().plus(Rel.LOGICAL);
  return new WriterRel(inputRel.getCluster(), traits, inputRel, createTableEntry, inputRel.getRowType());
}
 
Example 16
Source File: MycatCalciteDataContext.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public SqlParser.Config getParserConfig() {
    return MycatCalciteSupport.INSTANCE.config.getParserConfig();
}
 
Example 17
Source File: InitializerContext.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Parse a column computation expression for a table. Usually this expression is declared
 * in the create table statement, i.e.
 * <pre>
 *   create table t(
 *     a int not null,
 *     b varchar(5) as (my_udf(a)) virtual,
 *     c int not null as (a + 1)
 *   );
 * </pre>
 *
 * <p>You can use the string format expression "my_udf(a)" and "a + 1"
 * as the initializer expression of column b and c.
 *
 * <p>Calcite doesn't really need this now because the DDL nodes
 * can be executed directly from {@code SqlNode}s, but we still provide the way
 * to initialize from a SQL-like string, because a string can be used to persist easily and
 * the column expressions are important part of the table metadata.
 *
 * @param config parse config
 * @param expr   the SQL-style column expression
 * @return a {@code SqlNode} instance
 */
default SqlNode parseExpression(SqlParser.Config config, String expr) {
  SqlParser parser = SqlParser.create(expr, config);
  try {
    return parser.parseExpression();
  } catch (SqlParseException e) {
    throw new RuntimeException("Failed to parse expression " + expr, e);
  }
}
 
Example 18
Source File: FrameworkConfig.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * The configuration of SQL parser.
 */
SqlParser.Config getParserConfig();
 
Example 19
Source File: SqlSimpleParser.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a SqlSimpleParser
 *
 * @param hintToken Hint token
 * @param parserConfig parser configuration
 */
public SqlSimpleParser(String hintToken,
    SqlParser.Config parserConfig) {
  this.hintToken = hintToken;
  this.parserConfig = parserConfig;
}
 
Example 20
Source File: FrameworkConfig.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * The configuration of SQL parser.
 */
SqlParser.Config getParserConfig();