Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #122 support CQL comments #139

Merged
merged 6 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cql-ast/src/main/scala/troy/cql/ast/Statements.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ final case class CreateType(
ifNotExists: Boolean,
typeName: TypeName,
fields: Seq[Field]
) extends DataDefinition
) extends DataDefinition
5 changes: 4 additions & 1 deletion cql-parser/src/main/scala/troy/cql/parser/CqlParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ object CqlParser extends JavaTokenParsers

str | blob | uuid | floats | int | boolean | nullConst
}

// Ignores comments
protected override val whiteSpace = """(\s|//.*|--.*|(?m)/\*(\*(?!/)|[^*])*\*/)+""".r

def identifier: Parser[Identifier] = "[a-zA-Z0-9_]+".r.filter(k => !Keywords.contains(k.toUpperCase))

def optionInstruction: Parser[OptionInstruction] = {
def identifierOption = identifier ~ ("=".i ~> identifier) ^^^^ IdentifierOption

def constantOption = identifier ~ ("=".i ~> constant) ^^^^ ConstantOption
def mapLiteralOption = identifier ~ ("=".i ~> mapLiteral) ^^^^ MapLiteralOption

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class CreateTableParserTest extends FlatSpec with Matchers {
user_age int static,
user_weight float,
user_gender smallint,
user_ip inet
user_ip inet,
user_time time,
user_status boolean,
user_balance double,
user_trans_id bigint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding test for comments similar to those?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the same file CreateTableParserTest.scala ??

);
"""
)
Expand All @@ -126,7 +130,39 @@ class CreateTableParserTest extends FlatSpec with Matchers {
Table.Column("user_age", DataType.Int, true, false),
Table.Column("user_weight", DataType.Float, false, false),
Table.Column("user_gender", DataType.Smallint, false, false),
Table.Column("user_ip", DataType.Inet, false, false)
Table.Column("user_ip", DataType.Inet, false, false),
Table.Column("user_time", DataType.Time, false, false),
Table.Column("user_status", DataType.Boolean, false, false),
Table.Column("user_balance", DataType.Double, false, false),
Table.Column("user_trans_id", DataType.BigInt, false, false)
)
statement.primaryKey.isEmpty shouldBe true // Primary is defined inline instead
statement.options.isEmpty shouldBe true
}

it should "parse multi-fields create table with comments" in {
val statement = parseSchemaAs[CreateTable](
"""
/*
Test
multiline
comments
*/

CREATE TABLE test.users (
user_id text PRIMARY KEY, -- Singleline dash comment
user_name text static,
user_age int static,
user_weight float // Singleline slash comment
);
"""
)
statement.ifNotExists shouldBe false
statement.columns shouldBe Seq(
Table.Column("user_id", DataType.Text, false, true),
Table.Column("user_name", DataType.Text, true, false),
Table.Column("user_age", DataType.Int, true, false),
Table.Column("user_weight", DataType.Float, false, false)
)
statement.primaryKey.isEmpty shouldBe true // Primary is defined inline instead
statement.options.isEmpty shouldBe true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ class SelectStatementParserTest extends FlatSpec with Matchers {
selection.items(1).as.isEmpty shouldBe true
}

it should "parse simple select statements with order by ASC" in {
val statement = parseQuery("SELECT name, occupation FROM test.users ORDER BY name ASC;").asInstanceOf[SelectStatement]
statement.from.keyspace.get.name shouldBe "test"
statement.from.table shouldBe "users"
statement.mod.isEmpty shouldBe true
statement.where.isEmpty shouldBe true
statement.orderBy.isDefined shouldBe true
statement.orderBy.get.orderings.size shouldBe 1

val orderings = statement.orderBy.get.orderings(0)
orderings.columnName.name shouldBe "name"
orderings.direction.isDefined shouldBe true
orderings.direction.get shouldBe OrderBy.Ascending

statement.perPartitionLimit.isEmpty shouldBe true
statement.limit.isEmpty shouldBe true
statement.allowFiltering shouldBe false

val selection = statement.selection.asInstanceOf[Select.SelectClause]
selection.items.size shouldBe 2

selection.items(0).selector shouldBe Select.ColumnName("name")
selection.items(0).as.isEmpty shouldBe true

selection.items(1).selector shouldBe Select.ColumnName("occupation")
selection.items(1).as.isEmpty shouldBe true
}

it should "parse simple select statements with JSON" in {
val statement = parseQuery("SELECT JSON name, occupation FROM users;").asInstanceOf[SelectStatement]
statement.from.table shouldBe "users"
Expand Down
10 changes: 8 additions & 2 deletions troy-schema/src/test/resources/test.cql
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/*
This is
Multiline
Comments test
*/

CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy' , 'replication_factor': '1'};

CREATE TABLE test.posts (
author_id uuid,
author_id uuid, // Single line comment test with slash
post_id uuid,
author_name text static,
post_title text,
Expand All @@ -12,7 +18,7 @@ CREATE TABLE test.post_details (
author_id uuid,
id uuid,
rating int,
title text,
title text, -- Single line comment test with dash
someField text,
tags set<text>,
comment_ids set<int>,
Expand Down