-
Notifications
You must be signed in to change notification settings - Fork 929
SPARKC-403: Add CLUSTERING ORDER in cql statement #981
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
base: trunk
Are you sure you want to change the base?
Changes from 5 commits
4ae3ff9
5bc7cc6
4dd085c
bae67de
697dd0e
0aa10be
dfa0382
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| package com.datastax.spark.connector.cql | ||
|
|
||
| import java.io.IOException | ||
|
|
||
| import com.datastax.spark.connector._ | ||
| import com.datastax.spark.connector.mapper.{DataFrameColumnMapper, ColumnMapper} | ||
| import org.apache.spark.Logging | ||
|
|
@@ -92,7 +94,8 @@ case object RegularColumn extends ColumnRole | |
| case class ColumnDef( | ||
| columnName: String, | ||
| columnRole: ColumnRole, | ||
| columnType: ColumnType[_]) extends FieldDef { | ||
| columnType: ColumnType[_], | ||
| clusteringOrder: ClusteringOrder = ClusteringOrder.ASC) extends FieldDef { | ||
|
|
||
| def ref: ColumnRef = ColumnName(columnName) | ||
| def isStatic = columnRole == StaticColumn | ||
|
|
@@ -128,6 +131,15 @@ object ColumnDef { | |
| val columnType = ColumnType.fromDriverType(column.getType) | ||
| ColumnDef(column.getName, columnRole, columnType) | ||
| } | ||
|
|
||
| def apply( | ||
| column: ColumnMetadata, | ||
| columnRole: ColumnRole, | ||
| clusteringOrder: ClusteringOrder): ColumnDef = { | ||
|
|
||
| val columnType = ColumnType.fromDriverType(column.getType) | ||
| ColumnDef(column.getName, columnRole, columnType, clusteringOrder) | ||
| } | ||
| } | ||
|
|
||
| /** A Cassandra table metadata that can be serialized. */ | ||
|
|
@@ -138,11 +150,13 @@ case class TableDef( | |
| clusteringColumns: Seq[ColumnDef], | ||
| regularColumns: Seq[ColumnDef], | ||
| indexes: Seq[IndexDef] = Seq.empty, | ||
| isView: Boolean = false) extends StructDef { | ||
| isView: Boolean = false, | ||
| options: Seq[String] = Seq.empty) extends StructDef { | ||
|
|
||
| require(partitionKey.forall(_.isPartitionKeyColumn), "All partition key columns must have role PartitionKeyColumn") | ||
| require(clusteringColumns.forall(_.isClusteringColumn), "All clustering columns must have role ClusteringColumn") | ||
| require(regularColumns.forall(!_.isPrimaryKeyColumn), "Regular columns cannot have role PrimaryKeyColumn") | ||
| require(options.forall( option => !(option.toLowerCase.contains("and") && !(option.toLowerCase.contains("with")))), "Table options must not contain WITH OR AND") | ||
|
||
|
|
||
| val allColumns = regularColumns ++ clusteringColumns ++ partitionKey | ||
|
|
||
|
|
@@ -185,10 +199,15 @@ case class TableDef( | |
| val clusteringColumnNames = clusteringColumns.map(_.columnName).map(quote) | ||
| val primaryKeyClause = (partitionKeyClause +: clusteringColumnNames).mkString(", ") | ||
|
|
||
| s"""CREATE TABLE ${quote(keyspaceName)}.${quote(tableName)} ( | ||
| val stmt = s"""CREATE TABLE ${quote(keyspaceName)}.${quote(tableName)} ( | ||
| | $columnList, | ||
| | PRIMARY KEY ($primaryKeyClause) | ||
| |)""".stripMargin | ||
| val ordered = clusteringColumns.map( col => s"${quote(col.columnName)} ${col.clusteringOrder}") | ||
|
||
| .mkString("CLUSTERING ORDER BY (", ", ",")") | ||
|
|
||
| val orderWithOptions:Seq[String] = if (clusteringColumns.size > 0) options.+:(ordered) else options | ||
|
||
| if (orderWithOptions.size > 0) s"""$stmt${Properties.lineSeparator}WITH ${orderWithOptions.mkString(s"${Properties.lineSeparator} AND ")}""" else stmt | ||
| } | ||
|
|
||
| type ValueRepr = CassandraRow | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be tableOptions (matching the cql docs), and also now that I think about this perhaps it fits a Map better than a sequence? This would make it much clearer that we are looking for a set of key-value pairs.