Java Code Examples for org.apache.flink.table.api.SqlParserException

The following examples show how to use org.apache.flink.table.api.SqlParserException. 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: AthenaX   Source File: ProcessExecutorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSql() throws IOException {
  RowTypeInfo schema = new RowTypeInfo(new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO}, new String[] {"id"});
  MockExternalCatalogTable inputTable = new MockExternalCatalogTable(schema, Collections.singletonList(Row.of(1)));
  MockExternalCatalogTable outputTable = new MockExternalCatalogTable(schema, new ArrayList<>());
  SingleLevelMemoryCatalog input = new SingleLevelMemoryCatalog("input",
      Collections.singletonMap("foo", inputTable));
  SingleLevelMemoryCatalog output = new SingleLevelMemoryCatalog("output",
      Collections.singletonMap("bar", outputTable));
  JobDescriptor job = new JobDescriptor(
      Collections.singletonMap("input", input),
      Collections.emptyMap(),
      output,
      1,
      "SELECT2 * FROM input.foo");
  CompilationResult res = new ContainedExecutor().run(job);
  assertNull(res.jobGraph());
  assertTrue(res.remoteThrowable() instanceof SqlParserException);
}
 
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-sql-submit   Source File: SqlSubmit.java    License: Apache License 2.0 5 votes vote down vote up
private void callCreateTable(SqlCommandCall cmdCall) {
    String ddl = cmdCall.operands[0];
    try {
        tEnv.sqlUpdate(ddl);
    } catch (SqlParserException e) {
        throw new RuntimeException("SQL parse failed:\n" + ddl + "\n", e);
    }
}
 
Example 4
Source Project: flink-sql-submit   Source File: SqlSubmit.java    License: Apache License 2.0 5 votes vote down vote up
private void callInsertInto(SqlCommandCall cmdCall) {
    String dml = cmdCall.operands[0];
    try {
        tEnv.sqlUpdate(dml);
    } catch (SqlParserException e) {
        throw new RuntimeException("SQL parse failed:\n" + dml + "\n", e);
    }
}
 
Example 5
Source Project: flink   Source File: CalciteParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parses a SQL statement into a {@link SqlNode}. The {@link SqlNode} is not yet validated.
 *
 * @param sql a sql string to parse
 * @return a parsed sql node
 * @throws SqlParserException if an exception is thrown when parsing the statement
 */
public SqlNode parse(String sql) {
	try {
		SqlParser parser = SqlParser.create(sql, config);
		return parser.parseStmt();
	} catch (SqlParseException e) {
		throw new SqlParserException("SQL parse failed. " + e.getMessage(), e);
	}
}
 
Example 6
Source Project: flink   Source File: CalciteParser.java    License: Apache License 2.0 5 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 {
		return createFlinkParser(identifier).TableApiIdentifier();
	} catch (Exception e) {
		throw new SqlParserException(String.format(
			"Invalid SQL identifier %s.", identifier));
	}
}
 
Example 7
Source Project: flink   Source File: TableEnvironmentImpl.java    License: Apache License 2.0 5 votes vote down vote up
protected TableEnvironmentImpl(
		CatalogManager catalogManager,
		ModuleManager moduleManager,
		TableConfig tableConfig,
		Executor executor,
		FunctionCatalog functionCatalog,
		Planner planner,
		boolean isStreamingMode) {
	this.catalogManager = catalogManager;
	this.catalogManager.setCatalogTableSchemaResolver(
			new CatalogTableSchemaResolver(planner.getParser(), isStreamingMode));
	this.moduleManager = moduleManager;
	this.execEnv = executor;

	this.tableConfig = tableConfig;

	this.functionCatalog = functionCatalog;
	this.planner = planner;
	this.parser = planner.getParser();
	this.isStreamingMode = isStreamingMode;
	this.operationTreeBuilder = OperationTreeBuilder.create(
		tableConfig,
		functionCatalog.asLookup(parser::parseIdentifier),
		catalogManager.getDataTypeFactory(),
		path -> {
			try {
				UnresolvedIdentifier unresolvedIdentifier = parser.parseIdentifier(path);
				Optional<CatalogQueryOperation> catalogQueryOperation = scanInternal(unresolvedIdentifier);
				return catalogQueryOperation.map(t -> ApiExpressionUtils.tableRef(path, t));
			} catch (SqlParserException ex) {
				// The TableLookup is used during resolution of expressions and it actually might not be an
				// identifier of a table. It might be a reference to some other object such as column, local
				// reference etc. This method should return empty optional in such cases to fallback for other
				// identifiers resolution.
				return Optional.empty();
			}
		},
		isStreamingMode
	);
}
 
Example 8
Source Project: flink   Source File: CalciteParser.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parses a SQL statement into a {@link SqlNode}. The {@link SqlNode} is not yet validated.
 *
 * @param sql a sql string to parse
 * @return a parsed sql node
 * @throws SqlParserException if an exception is thrown when parsing the statement
 */
public SqlNode parse(String sql) {
	try {
		SqlParser parser = SqlParser.create(sql, config);
		return parser.parseStmt();
	} catch (SqlParseException e) {
		throw new SqlParserException("SQL parse failed. " + e.getMessage(), e);
	}
}