use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.
the class CassandraSchema method addMaterializedViews.
/**
* Add all materialized views defined in the schema to this column family
*/
private void addMaterializedViews() {
// Close the hook use to get us here
hook.close();
for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
String tableName = view.getBaseTable().getName();
StringBuilder queryBuilder = new StringBuilder("SELECT ");
// Add all the selected columns to the query
List<String> columnNames = new ArrayList<String>();
for (ColumnMetadata column : view.getColumns()) {
columnNames.add("\"" + column.getName() + "\"");
}
queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
queryBuilder.append(" FROM \"" + tableName + "\"");
// Get the where clause from the system schema
String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
// Parse and unparse the view query to get properly quoted field names
String query = queryBuilder.toString();
SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
configBuilder.setUnquotedCasing(Casing.UNCHANGED);
SqlSelect parsedQuery;
try {
parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
} catch (SqlParseException e) {
LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
continue;
}
StringWriter stringWriter = new StringWriter(query.length());
PrintWriter printWriter = new PrintWriter(stringWriter);
SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
parsedQuery.unparse(writer, 0, 0);
query = stringWriter.toString();
// Add the view for this query
String viewName = "$" + getTableNames().size();
SchemaPlus schema = parentSchema.getSubSchema(name);
CalciteSchema calciteSchema = CalciteSchema.from(schema);
List<String> viewPath = calciteSchema.path(viewName);
schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.
the class SqlTesterImpl method buildQuery2.
/**
* Builds a query that extracts all literals as columns in an underlying
* select.
*
* <p>For example,</p>
*
* <blockquote>{@code 1 < 5}</blockquote>
*
* <p>becomes</p>
*
* <blockquote>{@code SELECT p0 < p1
* FROM (VALUES (1, 5)) AS t(p0, p1)}</blockquote>
*
* <p>Null literals don't have enough type information to be extracted.
* We push down {@code CAST(NULL AS type)} but raw nulls such as
* {@code CASE 1 WHEN 2 THEN 'a' ELSE NULL END} are left as is.</p>
*
* @param expression Scalar expression
* @return Query that evaluates a scalar expression
*/
private String buildQuery2(String expression) {
// "values (1 < 5)"
// becomes
// "select p0 < p1 from (values (1, 5)) as t(p0, p1)"
SqlNode x;
final String sql = "values (" + expression + ")";
try {
x = parseQuery(sql);
} catch (SqlParseException e) {
throw new RuntimeException(e);
}
final Collection<SqlNode> literalSet = new LinkedHashSet<>();
x.accept(new SqlShuttle() {
private final List<SqlOperator> ops = ImmutableList.of(SqlStdOperatorTable.LITERAL_CHAIN, SqlStdOperatorTable.LOCALTIME, SqlStdOperatorTable.LOCALTIMESTAMP, SqlStdOperatorTable.CURRENT_TIME, SqlStdOperatorTable.CURRENT_TIMESTAMP);
@Override
public SqlNode visit(SqlLiteral literal) {
if (!isNull(literal) && literal.getTypeName() != SqlTypeName.SYMBOL) {
literalSet.add(literal);
}
return literal;
}
@Override
public SqlNode visit(SqlCall call) {
final SqlOperator operator = call.getOperator();
if (operator == SqlStdOperatorTable.CAST && isNull(call.operand(0))) {
literalSet.add(call);
return call;
} else if (ops.contains(operator)) {
// literal"
return call;
} else {
return super.visit(call);
}
}
private boolean isNull(SqlNode sqlNode) {
return sqlNode instanceof SqlLiteral && ((SqlLiteral) sqlNode).getTypeName() == SqlTypeName.NULL;
}
});
final List<SqlNode> nodes = new ArrayList<>(literalSet);
Collections.sort(nodes, new Comparator<SqlNode>() {
public int compare(SqlNode o1, SqlNode o2) {
final SqlParserPos pos0 = o1.getParserPosition();
final SqlParserPos pos1 = o2.getParserPosition();
int c = -Utilities.compare(pos0.getLineNum(), pos1.getLineNum());
if (c != 0) {
return c;
}
return -Utilities.compare(pos0.getColumnNum(), pos1.getColumnNum());
}
});
String sql2 = sql;
final List<Pair<String, String>> values = new ArrayList<>();
int p = 0;
for (SqlNode literal : nodes) {
final SqlParserPos pos = literal.getParserPosition();
final int start = SqlParserUtil.lineColToIndex(sql, pos.getLineNum(), pos.getColumnNum());
final int end = SqlParserUtil.lineColToIndex(sql, pos.getEndLineNum(), pos.getEndColumnNum()) + 1;
String param = "p" + (p++);
values.add(Pair.of(sql2.substring(start, end), param));
sql2 = sql2.substring(0, start) + param + sql2.substring(end);
}
if (values.isEmpty()) {
values.add(Pair.of("1", "p0"));
}
return "select " + sql2.substring("values (".length(), sql2.length() - 1) + " from (values (" + Util.commaList(Pair.left(values)) + ")) as t(" + Util.commaList(Pair.right(values)) + ")";
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.
the class SqlTesterImpl method assertExceptionIsThrown.
public void assertExceptionIsThrown(String sql, String expectedMsgPattern) {
SqlValidator validator;
SqlNode sqlNode;
SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
try {
sqlNode = parseQuery(sap.sql);
validator = getValidator();
} catch (SqlParseException e) {
String errMessage = e.getMessage();
if (expectedMsgPattern == null) {
throw new RuntimeException("Error while parsing query:" + sap.sql, e);
} else if (errMessage == null || !errMessage.matches(expectedMsgPattern)) {
throw new RuntimeException("Error did not match expected [" + expectedMsgPattern + "] while parsing query [" + sap.sql + "]", e);
}
return;
} catch (Throwable e) {
throw new RuntimeException("Error while parsing query: " + sap.sql, e);
}
Throwable thrown = null;
try {
validator.validate(sqlNode);
} catch (Throwable ex) {
thrown = ex;
}
SqlValidatorTestCase.checkEx(thrown, expectedMsgPattern, sap);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.
the class SqlValidatorTestCase method checkEx.
/**
* Checks whether an exception matches the expected pattern. If <code>
* sap</code> contains an error location, checks this too.
*
* @param ex Exception thrown
* @param expectedMsgPattern Expected pattern
* @param sap Query and (optional) position in query
*/
public static void checkEx(Throwable ex, String expectedMsgPattern, SqlParserUtil.StringAndPos sap) {
if (null == ex) {
if (expectedMsgPattern == null) {
// No error expected, and no error happened.
return;
} else {
throw new AssertionError("Expected query to throw exception, " + "but it did not; query [" + sap.sql + "]; expected [" + expectedMsgPattern + "]");
}
}
Throwable actualException = ex;
String actualMessage = actualException.getMessage();
int actualLine = -1;
int actualColumn = -1;
int actualEndLine = 100;
int actualEndColumn = 99;
// Search for an CalciteContextException somewhere in the stack.
CalciteContextException ece = null;
for (Throwable x = ex; x != null; x = x.getCause()) {
if (x instanceof CalciteContextException) {
ece = (CalciteContextException) x;
break;
}
if (x.getCause() == x) {
break;
}
}
// Search for a SqlParseException -- with its position set -- somewhere
// in the stack.
SqlParseException spe = null;
for (Throwable x = ex; x != null; x = x.getCause()) {
if ((x instanceof SqlParseException) && (((SqlParseException) x).getPos() != null)) {
spe = (SqlParseException) x;
break;
}
if (x.getCause() == x) {
break;
}
}
if (ece != null) {
actualLine = ece.getPosLine();
actualColumn = ece.getPosColumn();
actualEndLine = ece.getEndPosLine();
actualEndColumn = ece.getEndPosColumn();
if (ece.getCause() != null) {
actualException = ece.getCause();
actualMessage = actualException.getMessage();
}
} else if (spe != null) {
actualLine = spe.getPos().getLineNum();
actualColumn = spe.getPos().getColumnNum();
actualEndLine = spe.getPos().getEndLineNum();
actualEndColumn = spe.getPos().getEndColumnNum();
if (spe.getCause() != null) {
actualException = spe.getCause();
actualMessage = actualException.getMessage();
}
} else {
final String message = ex.getMessage();
if (message != null) {
Matcher matcher = LINE_COL_TWICE_PATTERN.matcher(message);
if (matcher.matches()) {
actualLine = Integer.parseInt(matcher.group(1));
actualColumn = Integer.parseInt(matcher.group(2));
actualEndLine = Integer.parseInt(matcher.group(3));
actualEndColumn = Integer.parseInt(matcher.group(4));
actualMessage = matcher.group(5);
} else {
matcher = LINE_COL_PATTERN.matcher(message);
if (matcher.matches()) {
actualLine = Integer.parseInt(matcher.group(1));
actualColumn = Integer.parseInt(matcher.group(2));
}
}
}
}
if (null == expectedMsgPattern) {
actualException.printStackTrace();
fail("Validator threw unexpected exception" + "; query [" + sap.sql + "]; exception [" + actualMessage + "]; class [" + actualException.getClass() + "]; pos [line " + actualLine + " col " + actualColumn + " thru line " + actualLine + " col " + actualColumn + "]");
}
String sqlWithCarets;
if (actualColumn <= 0 || actualLine <= 0 || actualEndColumn <= 0 || actualEndLine <= 0) {
if (sap.pos != null) {
AssertionError e = new AssertionError("Expected error to have position," + " but actual error did not: " + " actual pos [line " + actualLine + " col " + actualColumn + " thru line " + actualEndLine + " col " + actualEndColumn + "]");
e.initCause(actualException);
throw e;
}
sqlWithCarets = sap.sql;
} else {
sqlWithCarets = SqlParserUtil.addCarets(sap.sql, actualLine, actualColumn, actualEndLine, actualEndColumn + 1);
if (sap.pos == null) {
throw new AssertionError("Actual error had a position, but expected " + "error did not. Add error position carets to sql:\n" + sqlWithCarets);
}
}
if (actualMessage != null) {
actualMessage = Util.toLinux(actualMessage);
}
if (actualMessage == null || !actualMessage.matches(expectedMsgPattern)) {
actualException.printStackTrace();
final String actualJavaRegexp = (actualMessage == null) ? "null" : TestUtil.quoteForJava(TestUtil.quotePattern(actualMessage));
fail("Validator threw different " + "exception than expected; query [" + sap.sql + "];\n" + " expected pattern [" + expectedMsgPattern + "];\n" + " actual [" + actualMessage + "];\n" + " actual as java regexp [" + actualJavaRegexp + "]; pos [" + actualLine + " col " + actualColumn + " thru line " + actualEndLine + " col " + actualEndColumn + "]; sql [" + sqlWithCarets + "]");
} else if (sap.pos != null && (actualLine != sap.pos.getLineNum() || actualColumn != sap.pos.getColumnNum() || actualEndLine != sap.pos.getEndLineNum() || actualEndColumn != sap.pos.getEndColumnNum())) {
fail("Validator threw expected " + "exception [" + actualMessage + "];\nbut at pos [line " + actualLine + " col " + actualColumn + " thru line " + actualEndLine + " col " + actualEndColumn + "];\nsql [" + sqlWithCarets + "]");
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.
the class PlannerTest method testParseFails.
@Test
public void testParseFails() throws SqlParseException {
Planner planner = getPlanner(null);
try {
SqlNode parse = planner.parse("select * * from \"emps\"");
fail("expected error, got " + parse);
} catch (SqlParseException e) {
assertThat(e.getMessage(), containsString("Encountered \"*\" at line 1, column 10."));
}
}
Aggregations