Java Code Examples for org.apache.calcite.sql.parser.SqlAbstractParserImpl

The following examples show how to use org.apache.calcite.sql.parser.SqlAbstractParserImpl. These examples are extracted from open source projects. 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 Project: dremio-oss   Source File: SqlReservedKeywordGenerator.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  if (args.length != 1) {
    throw new IllegalArgumentException("Usage: java {cp} " + SqlReservedKeywordGenerator.class.getName() +
        " path/where/to/write/the/file");
  }

  final File outputFile = new File(args[0], RESERVED_KEYWORD_FILE_NAME);
  System.out.println("Writing reserved SQL keywords to file: " + outputFile.getAbsolutePath());

  try(PrintWriter outFile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), UTF_8))) {
    outFile.printf("# AUTO-GENERATED LIST OF SQL RESERVED KEYWORDS (generated by %s)",
        SqlReservedKeywordGenerator.class.getName());
    outFile.println();

    final SqlAbstractParserImpl.Metadata metadata = SqlParser.create("", new ParserConfig(Quoting.DOUBLE_QUOTE, 256)).getMetadata();
    for (String s : metadata.getTokens()) {
      if (metadata.isKeyword(s) && metadata.isReservedWord(s)) {
        outFile.println(s);
      }
    }
  }
}
 
Example 2
Source Project: flink   Source File: CalciteParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses a SQL string as an identifier into a {@link SqlIdentifier}.
 *
 * @param identifier a sql string to parse as an identifier
 * @return a parsed sql node
 * @throws SqlParserException if an exception is thrown when parsing the identifier
 */
public SqlIdentifier parseIdentifier(String identifier) {
	try {
		SqlAbstractParserImpl flinkParser = createFlinkParser(identifier);
		if (flinkParser instanceof FlinkSqlParserImpl) {
			return ((FlinkSqlParserImpl) flinkParser).TableApiIdentifier();
		} else if (flinkParser instanceof FlinkHiveSqlParserImpl) {
			return ((FlinkHiveSqlParserImpl) flinkParser).TableApiIdentifier();
		} else {
			throw new IllegalArgumentException("Unrecognized sql parser type " + flinkParser.getClass().getName());
		}
	} catch (Exception e) {
		throw new SqlParserException(String.format(
			"Invalid SQL identifier %s.", identifier), e);
	}
}
 
Example 3
Source Project: flink   Source File: CalciteParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Equivalent to {@link SqlParser#create(Reader, SqlParser.Config)}. The only
 * difference is we do not wrap the {@link FlinkSqlParserImpl} with {@link SqlParser}.
 *
 * <p>It is so that we can access specific parsing methods not accessible through the {@code SqlParser}.
 */
private SqlAbstractParserImpl createFlinkParser(String expr) {
	SourceStringReader reader = new SourceStringReader(expr);
	SqlAbstractParserImpl parser = config.parserFactory().getParser(reader);
	parser.setTabSize(1);
	parser.setQuotedCasing(config.quotedCasing());
	parser.setUnquotedCasing(config.unquotedCasing());
	parser.setIdentifierMaxLength(config.identifierMaxLength());
	parser.setConformance(config.conformance());
	switch (config.quoting()) {
		case DOUBLE_QUOTE:
			parser.switchTo("DQID");
			break;
		case BACK_TICK:
			parser.switchTo("BTID");
			break;
		case BRACKET:
			parser.switchTo("DEFAULT");
			break;
	}

	return parser;
}
 
Example 4
Source Project: Bats   Source File: SqlAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
private void ensureReservedAndKeyWords() {
  if (reservedWordsSet != null) {
    return;
  }
  Collection<String> c = SqlAbstractParserImpl.getSql92ReservedWords();
  List<String> l =
      Arrays.asList(
          getParserMetadata().getJdbcKeywords().split(","));
  List<String> al = new ArrayList<>();
  al.addAll(c);
  al.addAll(l);
  reservedWordsList = al;
  reservedWordsSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  reservedWordsSet.addAll(reservedWordsList);
}
 
Example 5
Source Project: calcite   Source File: BabelParserTest.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc}
 *
 * <p>Copy-pasted from base method, but with some key differences.
 */
@Override @Test protected void testMetadata() {
  SqlAbstractParserImpl.Metadata metadata = getSqlParser("").getMetadata();
  assertThat(metadata.isReservedFunctionName("ABS"), is(true));
  assertThat(metadata.isReservedFunctionName("FOO"), is(false));

  assertThat(metadata.isContextVariableName("CURRENT_USER"), is(true));
  assertThat(metadata.isContextVariableName("CURRENT_CATALOG"), is(true));
  assertThat(metadata.isContextVariableName("CURRENT_SCHEMA"), is(true));
  assertThat(metadata.isContextVariableName("ABS"), is(false));
  assertThat(metadata.isContextVariableName("FOO"), is(false));

  assertThat(metadata.isNonReservedKeyword("A"), is(true));
  assertThat(metadata.isNonReservedKeyword("KEY"), is(true));
  assertThat(metadata.isNonReservedKeyword("SELECT"), is(false));
  assertThat(metadata.isNonReservedKeyword("FOO"), is(false));
  assertThat(metadata.isNonReservedKeyword("ABS"), is(true)); // was false

  assertThat(metadata.isKeyword("ABS"), is(true));
  assertThat(metadata.isKeyword("CURRENT_USER"), is(true));
  assertThat(metadata.isKeyword("CURRENT_CATALOG"), is(true));
  assertThat(metadata.isKeyword("CURRENT_SCHEMA"), is(true));
  assertThat(metadata.isKeyword("KEY"), is(true));
  assertThat(metadata.isKeyword("SELECT"), is(true));
  assertThat(metadata.isKeyword("HAVING"), is(true));
  assertThat(metadata.isKeyword("A"), is(true));
  assertThat(metadata.isKeyword("BAR"), is(false));

  assertThat(metadata.isReservedWord("SELECT"), is(true));
  assertThat(metadata.isReservedWord("CURRENT_CATALOG"), is(false)); // was true
  assertThat(metadata.isReservedWord("CURRENT_SCHEMA"), is(false)); // was true
  assertThat(metadata.isReservedWord("KEY"), is(false));

  String jdbcKeywords = metadata.getJdbcKeywords();
  assertThat(jdbcKeywords.contains(",COLLECT,"), is(false)); // was true
  assertThat(!jdbcKeywords.contains(",SELECT,"), is(true));
}
 
Example 6
Source Project: calcite   Source File: SqlAdvisor.java    License: Apache License 2.0 5 votes vote down vote up
private void ensureReservedAndKeyWords() {
  if (reservedWordsSet != null) {
    return;
  }
  Collection<String> c = SqlAbstractParserImpl.getSql92ReservedWords();
  List<String> l =
      Arrays.asList(
          getParserMetadata().getJdbcKeywords().split(","));
  List<String> al = new ArrayList<>();
  al.addAll(c);
  al.addAll(l);
  reservedWordsList = al;
  reservedWordsSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  reservedWordsSet.addAll(reservedWordsList);
}
 
Example 7
Source Project: Bats   Source File: DrillParserWithCompoundIdConverter.java    License: Apache License 2.0 4 votes vote down vote up
public SqlAbstractParserImpl getParser(Reader stream) {
  SqlAbstractParserImpl parserImpl = new DrillParserWithCompoundIdConverter(stream);
  parserImpl.setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH);
  return parserImpl;
}
 
Example 8
Source Project: dremio-oss   Source File: ParserWithCompoundIdConverter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SqlAbstractParserImpl getParser(Reader stream) {
  SqlAbstractParserImpl parserImpl = new ParserWithCompoundIdConverter(stream);
  parserImpl.setIdentifierMaxLength(PlannerSettings.DEFAULT_IDENTIFIER_MAX_LENGTH);
  return parserImpl;
}
 
Example 9
Source Project: calcite   Source File: ServerDdlExecutor.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlAbstractParserImpl getParser(Reader stream) {
  return SqlDdlParserImpl.FACTORY.getParser(stream);
}
 
Example 10
Source Project: calcite   Source File: DocumentationTest.java    License: Apache License 2.0 4 votes vote down vote up
/** Generates a copy of {@code reference.md} with the current set of key
 * words. Fails if the copy is different from the original. */
@Test void testGenerateKeyWords() throws IOException {
  final FileFixture f = new FileFixture();
  f.outFile.getParentFile().mkdirs();
  try (BufferedReader r = Util.reader(f.inFile);
       FileOutputStream fos = new FileOutputStream(f.outFile);
       PrintWriter w = Util.printWriter(f.outFile)) {
    String line;
    int stage = 0;
    while ((line = r.readLine()) != null) {
      if (line.equals("{% comment %} end {% endcomment %}")) {
        ++stage;
      }
      if (stage != 1) {
        w.println(line);
      }
      if (line.equals("{% comment %} start {% endcomment %}")) {
        ++stage;
        SqlAbstractParserImpl.Metadata metadata =
            new SqlParserTest().getSqlParser("").getMetadata();
        int z = 0;
        for (String s : metadata.getTokens()) {
          if (z++ > 0) {
            w.println(",");
          }
          if (metadata.isKeyword(s)) {
            w.print(metadata.isReservedWord(s) ? ("**" + s + "**") : s);
          }
        }
        w.println(".");
      }
    }
    w.flush();
    fos.flush();
    fos.getFD().sync();
  }
  String diff = DiffTestCase.diff(f.outFile, f.inFile);
  if (!diff.isEmpty()) {
    throw new AssertionError("Mismatch between " + f.outFile
        + " and " + f.inFile + ":\n"
        + diff);
  }
}
 
Example 11
Source Project: calcite   Source File: ExtensionDdlExecutor.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlAbstractParserImpl getParser(Reader stream) {
  return ExtensionSqlParserImpl.FACTORY.getParser(stream);
}
 
Example 12
Source Project: Bats   Source File: SqlAdvisor.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns the underlying Parser metadata.
 *
 * <p>To use a different parser (recognizing a different dialect of SQL),
 * derived class should override.
 *
 * @return metadata
 */
protected SqlAbstractParserImpl.Metadata getParserMetadata() {
  SqlParser parser = SqlParser.create("", parserConfig);
  return parser.getMetadata();
}
 
Example 13
Source Project: calcite   Source File: SqlAdvisor.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns the underlying Parser metadata.
 *
 * <p>To use a different parser (recognizing a different dialect of SQL),
 * derived class should override.
 *
 * @return metadata
 */
protected SqlAbstractParserImpl.Metadata getParserMetadata() {
  SqlParser parser = SqlParser.create("", parserConfig);
  return parser.getMetadata();
}