fully parsing the example file now

This commit is contained in:
Jonathan Shook 2022-07-11 18:44:38 -05:00
parent 454412aa8f
commit 2df9e6b044
7 changed files with 74 additions and 39 deletions

View File

@ -20,12 +20,15 @@ import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer; import org.antlr.v4.runtime.Recognizer;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.function.Supplier;
public class CQBErrorListener extends BaseErrorListener { public class CQBErrorListener extends BaseErrorListener implements Supplier<List<String>> {
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>();
private final Path origin; private final Path origin;
@ -37,20 +40,35 @@ public class CQBErrorListener extends BaseErrorListener {
@Override @Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
String locref= getLocationRef(origin,line, charPositionInLine); String locref= getLocationRef(origin,line, charPositionInLine);
String errmsg = "Error in " + origin.toString()+ "\n:"+locref; String errmsg = "Error in " + origin.toString()+ ":\n" +
e.toString() + ":\n"+locref;
errors.add(errmsg); errors.add(errmsg);
} }
public String getLocationRef(Path srcpath, int line, int charPositionInLine) { public String getLocationRef(Path srcpath, int line, int charPositionInLine) {
Path cwd = Path.of(".").toAbsolutePath().normalize();
Path vcwd = cwd;
while (vcwd!=null) {
if (Files.exists(Path.of(vcwd + File.separator + ".git"))) {
break;
}
vcwd = vcwd.getParent();
}
boolean inij = System.getProperty("sun.java.command","").toLowerCase(Locale.ROOT).contains("intellij"); boolean inij = System.getProperty("sun.java.command","").toLowerCase(Locale.ROOT).contains("intellij");
Path vcwd = Path.of(".").toAbsolutePath().normalize(); vcwd = inij ? cwd.getParent().normalize() : vcwd;
vcwd = inij ? vcwd.getParent().normalize() : vcwd;
Path relpath = vcwd.relativize(srcpath.toAbsolutePath()); Path relpath = vcwd.relativize(srcpath.toAbsolutePath());
if (inij) { if (inij) {
relpath = Path.of("local/verizon/"); relpath = Path.of(relpath.toString().replace("target/classes/","src/main/resources/"));
// relpath = Path.of(relpath.toString().replace("target/classes/","src/main/resources/"));
} }
return "\tat (" + relpath + ":" + line+":"+charPositionInLine + ")"; return "\tat (" + relpath + ":" + line+":"+charPositionInLine + ")";
} }
@Override
public List<String> get() {
return this.errors;
}
} }

View File

@ -16,32 +16,32 @@
package io.nosqlbench.converters.cql.cqlast; package io.nosqlbench.converters.cql.cqlast;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
public class CqlModel { public class CqlModel {
private final Supplier<List<String>> errors;
Map<String, CqlKeyspace> keyspaces = new LinkedHashMap<>(); Map<String, CqlKeyspace> keyspaces = new LinkedHashMap<>();
Map<String, Map<String, CqlTable>> tables = new LinkedHashMap<>(); Map<String, Map<String, CqlTable>> tables = new LinkedHashMap<>();
public List<String> getErrors() {
return errors;
}
public void setErrors(List<String> errors) {
this.errors = errors;
}
List<String> errors = new ArrayList<>();
transient transient
CqlKeyspace keyspace = null; CqlKeyspace keyspace = null;
transient transient
CqlTable table; CqlTable table;
public CqlModel(Supplier<List<String>> errorSource) {
this.errors = errorSource;
}
public List<String> getErrors() {
return errors.get();
}
public void newKeyspace() { public void newKeyspace() {
keyspace = new CqlKeyspace(); keyspace = new CqlKeyspace();
} }
@ -87,12 +87,12 @@ public class CqlModel {
for (String ks : keyspaces.keySet()) { for (String ks : keyspaces.keySet()) {
CqlKeyspace keyspace = keyspaces.get(ks); CqlKeyspace keyspace = keyspaces.get(ks);
sb.append("keyspace '").append(keyspace.getKeyspaceName()).append("':\n"); sb.append("keyspace '").append(keyspace.getKeyspaceName()).append("':\n");
sb.append(keyspace.toString()).append("\n"); sb.append(keyspace).append("\n");
tables.getOrDefault(ks,Map.of()).values().stream() tables.getOrDefault(ks,Map.of()).values().stream()
.forEach(table -> { .forEach(table -> {
sb.append("table '").append(table.getTableName()).append("':\n"); sb.append("table '").append(table.getTableName()).append("':\n");
sb.append(table.toString()); sb.append(table);
}); });
} }
return sb.toString(); return sb.toString();

View File

@ -28,10 +28,11 @@ import java.util.List;
public class CqlModelBuilder extends CqlParserBaseListener { public class CqlModelBuilder extends CqlParserBaseListener {
private final CQBErrorListener errorListener; private final CQBErrorListener errorListener;
CqlModel model = new CqlModel(); private final CqlModel model;
public CqlModelBuilder(CQBErrorListener errorListener) { public CqlModelBuilder(CQBErrorListener errorListener) {
this.errorListener = errorListener; this.errorListener = errorListener;
this.model = new CqlModel(errorListener);
} }
@Override @Override

View File

@ -77,10 +77,10 @@ public class CqlWorkloadExporter {
} }
Path srcpath = Path.of(args[0]); Path srcpath = Path.of(args[0]);
if (!srcpath.toString().endsWith(".cql")) { if (!srcpath.toString().endsWith(".cql")) {
throw new RuntimeException("File '" + srcpath.toString() + "' must end in .cql"); throw new RuntimeException("File '" + srcpath + "' must end in .cql");
} }
if (!Files.exists(srcpath)) { if (!Files.exists(srcpath)) {
throw new RuntimeException("File '" + srcpath.toString() + "' does not exist."); throw new RuntimeException("File '" + srcpath + "' does not exist.");
} }
Path target = Path.of(srcpath.toString().replace("\\.cql", "\\.yaml")); Path target = Path.of(srcpath.toString().replace("\\.cql", "\\.yaml"));
@ -91,7 +91,7 @@ public class CqlWorkloadExporter {
throw new RuntimeException("Target file must end in .yaml"); throw new RuntimeException("Target file must end in .yaml");
} }
if (Files.exists(target) && !target.toString().startsWith("_")) { if (Files.exists(target) && !target.toString().startsWith("_")) {
throw new RuntimeException("Target file '" + target.toString() + "' exists. Please remove it first or use a different target file name."); throw new RuntimeException("Target file '" + target + "' exists. Please remove it first or use a different target file name.");
} }
CqlWorkloadExporter exporter = new CqlWorkloadExporter(srcpath); CqlWorkloadExporter exporter = new CqlWorkloadExporter(srcpath);
@ -128,7 +128,7 @@ public class CqlWorkloadExporter {
model.getAllTables() model.getAllTables()
.stream() .stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
t -> "insert-" + t.getTableName(), t -> "insert-" + t.getKeySpace()+"__"+t.getTableName(),
this::genUpsertTemplate) this::genUpsertTemplate)
) )
); );
@ -137,7 +137,7 @@ public class CqlWorkloadExporter {
model.getAllTables() model.getAllTables()
.stream() .stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
t -> "select-" + t.getTableName(), t -> "select-" + t.getKeySpace()+"__"+t.getTableName(),
this::genSelectTemplate) this::genSelectTemplate)
)); ));
@ -150,7 +150,7 @@ public class CqlWorkloadExporter {
Map<String, String> rampupOpTemplates = model.getAllTables() Map<String, String> rampupOpTemplates = model.getAllTables()
.stream() .stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
t -> "insert-" + t.getTableName(), t -> "insert-" + t.getKeySpace()+"__"+t.getTableName(),
this::genUpsertTemplate) this::genUpsertTemplate)
); );

View File

@ -87,7 +87,8 @@ K_KEYS: 'KEYS';
K_KEYSPACE: 'KEYSPACE'; K_KEYSPACE: 'KEYSPACE';
K_KEYSPACES: 'KEYSPACES'; K_KEYSPACES: 'KEYSPACES';
K_LANGUAGE: 'LANGUAGE'; K_LANGUAGE: 'LANGUAGE';
K_LEVEL: 'LEVEL'; // Disabled because there was no definitive reference to this as a bare keyword in the specs
//K_LEVEL: 'LEVEL';
K_LIMIT: 'LIMIT'; K_LIMIT: 'LIMIT';
K_LOCAL_ONE: 'LOCAL_ONE'; K_LOCAL_ONE: 'LOCAL_ONE';
K_LOCAL_QUORUM: 'LOCAL_QUORUM'; K_LOCAL_QUORUM: 'LOCAL_QUORUM';

View File

@ -367,8 +367,9 @@ dropIndex
; ;
// TODO: update to https://docs.datastax.com/en/dse/6.8/cql/cql/cql_reference/cql_commands/cqlCreateTable.html // TODO: update to https://docs.datastax.com/en/dse/6.8/cql/cql/cql_reference/cql_commands/cqlCreateTable.html
// TODO: Update optional sequence for compact storage [options] clustering order [options] as documented above
createTable createTable
: kwCreate kwTable ifNotExist? (keyspace DOT)? table syntaxBracketLr columnDefinitionList syntaxBracketRr (withElement (kwAnd tableOptions)*)? : kwCreate kwTable ifNotExist? (keyspace DOT)? table syntaxBracketLr columnDefinitionList syntaxBracketRr (kwWith tableOptions (kwAnd tableOptions)*)?
; ;
withElement withElement
@ -386,15 +387,22 @@ tableOptions
tableOptionItem tableOptionItem
: tableOptionName OPERATOR_EQ tableOptionValue : tableOptionName OPERATOR_EQ tableOptionValue
| tableOptionName OPERATOR_EQ optionHash | tableOptionName OPERATOR_EQ optionHash
| kwCompactStorage
| clusteringOrder
; ;
tableOptionName tableOptionName
: OBJECT_NAME : OBJECT_NAME
; ;
kwCompactStorage
: K_COMPACT K_STORAGE
;
tableOptionValue tableOptionValue
: stringLiteral : stringLiteral
| floatLiteral | floatLiteral
| booleanLiteral
; ;
optionHash optionHash
@ -447,7 +455,7 @@ compoundKey
; ;
compositeKey compositeKey
: syntaxBracketLr partitionKeyList syntaxBracketRr (syntaxComma clusteringKeyList) : syntaxBracketLr partitionKeyList syntaxBracketRr (syntaxComma clusteringKeyList)?
; ;
partitionKeyList partitionKeyList
@ -778,11 +786,15 @@ column
; ;
dataType dataType
: dataTypeName dataTypeDefinition? : dataTypeName
| K_FROZEN syntaxBracketLa dataType syntaxBracketRa
| K_SET syntaxBracketLa dataType syntaxBracketRa
| K_LIST syntaxBracketLa dataType syntaxBracketRa
| K_MAP syntaxBracketLa dataType syntaxComma dataType syntaxBracketRa
; ;
dataTypeName dataTypeName
: OBJECT_NAME : (keyspace DOT)? OBJECT_NAME
| K_TIMESTAMP | K_TIMESTAMP
| K_SET | K_SET
| K_ASCII | K_ASCII
@ -794,7 +806,7 @@ dataTypeName
| K_DECIMAL | K_DECIMAL
| K_DOUBLE | K_DOUBLE
| K_FLOAT | K_FLOAT
| K_FROZEN // | K_FROZEN
| K_INET | K_INET
| K_INT | K_INT
| K_LIST | K_LIST
@ -812,8 +824,11 @@ dataTypeName
; ;
dataTypeDefinition dataTypeDefinition
: syntaxBracketLa dataTypeName (syntaxComma dataTypeName)* syntaxBracketRa : syntaxBracketLa dataType syntaxBracketRa
| syntaxBracketLa dataType syntaxBracketRa | syntaxBracketLa dataTypeName (syntaxBracketLa dataTypeName syntaxBracketRa) syntaxBracketRa
| syntaxBracketLa dataTypeName (syntaxComma dataTypeName)* syntaxBracketRa
| syntaxBracketLa dataTypeName (syntaxComma syntaxBracketLa dataTypeName syntaxBracketRa)* syntaxBracketRa
// | syntaxBracketLa dataTypeName (syntaxComma dataTypeDefinition)* syntaxBracketRa
; ;
orderDirection orderDirection

View File

@ -65,7 +65,7 @@ public class CqlModelParser {
CqlModel model = cqlModelBuilder.getModel(); CqlModel model = cqlModelBuilder.getModel();
if (model.getErrors().size()>0) { if (model.getErrors().size()>0) {
System.out.println(model.getErrors()); System.out.println(model.getErrors());
throw new RuntimeException("Unable to render model for unparsable input with " + model.getErrors() + " errors"); throw new RuntimeException("Unable to render model for unparsable input with errors:\n" + model.getErrors());
} else { } else {
return model; return model;
} }