metadata.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. // Copyright (c) 2015 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. )
  13. // schema metadata for a keyspace
  14. type KeyspaceMetadata struct {
  15. Name string
  16. DurableWrites bool
  17. StrategyClass string
  18. StrategyOptions map[string]interface{}
  19. Tables map[string]*TableMetadata
  20. }
  21. // schema metadata for a table (a.k.a. column family)
  22. type TableMetadata struct {
  23. Keyspace string
  24. Name string
  25. KeyValidator string
  26. Comparator string
  27. DefaultValidator string
  28. KeyAliases []string
  29. ColumnAliases []string
  30. ValueAlias string
  31. PartitionKey []*ColumnMetadata
  32. ClusteringColumns []*ColumnMetadata
  33. Columns map[string]*ColumnMetadata
  34. OrderedColumns []string
  35. }
  36. // schema metadata for a column
  37. type ColumnMetadata struct {
  38. Keyspace string
  39. Table string
  40. Name string
  41. ComponentIndex int
  42. Kind ColumnKind
  43. Validator string
  44. Type TypeInfo
  45. ClusteringOrder string
  46. Order ColumnOrder
  47. Index ColumnIndexMetadata
  48. }
  49. // the ordering of the column with regard to its comparator
  50. type ColumnOrder bool
  51. const (
  52. ASC ColumnOrder = false
  53. DESC = true
  54. )
  55. type ColumnIndexMetadata struct {
  56. Name string
  57. Type string
  58. Options map[string]interface{}
  59. }
  60. type ColumnKind int
  61. const (
  62. ColumnUnkownKind ColumnKind = iota
  63. ColumnPartitionKey
  64. ColumnClusteringKey
  65. ColumnRegular
  66. ColumnCompact
  67. ColumnStatic
  68. )
  69. func (c ColumnKind) String() string {
  70. switch c {
  71. case ColumnPartitionKey:
  72. return "partition_key"
  73. case ColumnClusteringKey:
  74. return "clustering_key"
  75. case ColumnRegular:
  76. return "regular"
  77. case ColumnCompact:
  78. return "compact"
  79. case ColumnStatic:
  80. return "static"
  81. default:
  82. return fmt.Sprintf("unknown_column_%d", c)
  83. }
  84. }
  85. func (c *ColumnKind) UnmarshalCQL(typ TypeInfo, p []byte) error {
  86. if typ.Type() != TypeVarchar {
  87. return unmarshalErrorf("unable to marshall %s into ColumnKind, expected Varchar", typ)
  88. }
  89. kind, err := columnKindFromSchema(string(p))
  90. if err != nil {
  91. return err
  92. }
  93. *c = kind
  94. return nil
  95. }
  96. func columnKindFromSchema(kind string) (ColumnKind, error) {
  97. switch kind {
  98. case "partition_key":
  99. return ColumnPartitionKey, nil
  100. case "clustering_key", "clustering":
  101. return ColumnClusteringKey, nil
  102. case "regular":
  103. return ColumnRegular, nil
  104. case "compact_value":
  105. return ColumnCompact, nil
  106. case "static":
  107. return ColumnStatic, nil
  108. default:
  109. return -1, fmt.Errorf("unknown column kind: %q", kind)
  110. }
  111. }
  112. // default alias values
  113. const (
  114. DEFAULT_KEY_ALIAS = "key"
  115. DEFAULT_COLUMN_ALIAS = "column"
  116. DEFAULT_VALUE_ALIAS = "value"
  117. )
  118. // queries the cluster for schema information for a specific keyspace
  119. type schemaDescriber struct {
  120. session *Session
  121. mu sync.Mutex
  122. cache map[string]*KeyspaceMetadata
  123. }
  124. // creates a session bound schema describer which will query and cache
  125. // keyspace metadata
  126. func newSchemaDescriber(session *Session) *schemaDescriber {
  127. return &schemaDescriber{
  128. session: session,
  129. cache: map[string]*KeyspaceMetadata{},
  130. }
  131. }
  132. // returns the cached KeyspaceMetadata held by the describer for the named
  133. // keyspace.
  134. func (s *schemaDescriber) getSchema(keyspaceName string) (*KeyspaceMetadata, error) {
  135. s.mu.Lock()
  136. defer s.mu.Unlock()
  137. metadata, found := s.cache[keyspaceName]
  138. if !found {
  139. // refresh the cache for this keyspace
  140. err := s.refreshSchema(keyspaceName)
  141. if err != nil {
  142. return nil, err
  143. }
  144. metadata = s.cache[keyspaceName]
  145. }
  146. return metadata, nil
  147. }
  148. // clears the already cached keyspace metadata
  149. func (s *schemaDescriber) clearSchema(keyspaceName string) {
  150. s.mu.Lock()
  151. defer s.mu.Unlock()
  152. delete(s.cache, keyspaceName)
  153. }
  154. // forcibly updates the current KeyspaceMetadata held by the schema describer
  155. // for a given named keyspace.
  156. func (s *schemaDescriber) refreshSchema(keyspaceName string) error {
  157. var err error
  158. // query the system keyspace for schema data
  159. // TODO retrieve concurrently
  160. keyspace, err := getKeyspaceMetadata(s.session, keyspaceName)
  161. if err != nil {
  162. return err
  163. }
  164. tables, err := getTableMetadata(s.session, keyspaceName)
  165. if err != nil {
  166. return err
  167. }
  168. columns, err := getColumnMetadata(s.session, keyspaceName)
  169. if err != nil {
  170. return err
  171. }
  172. // organize the schema data
  173. compileMetadata(s.session.cfg.ProtoVersion, keyspace, tables, columns)
  174. // update the cache
  175. s.cache[keyspaceName] = keyspace
  176. return nil
  177. }
  178. // "compiles" derived information about keyspace, table, and column metadata
  179. // for a keyspace from the basic queried metadata objects returned by
  180. // getKeyspaceMetadata, getTableMetadata, and getColumnMetadata respectively;
  181. // Links the metadata objects together and derives the column composition of
  182. // the partition key and clustering key for a table.
  183. func compileMetadata(
  184. protoVersion int,
  185. keyspace *KeyspaceMetadata,
  186. tables []TableMetadata,
  187. columns []ColumnMetadata,
  188. ) {
  189. keyspace.Tables = make(map[string]*TableMetadata)
  190. for i := range tables {
  191. tables[i].Columns = make(map[string]*ColumnMetadata)
  192. keyspace.Tables[tables[i].Name] = &tables[i]
  193. }
  194. // add columns from the schema data
  195. for i := range columns {
  196. // decode the validator for TypeInfo and order
  197. if columns[i].ClusteringOrder != "" { // Cassandra 3.x+
  198. columns[i].Type = NativeType{typ: getCassandraType(columns[i].Validator)}
  199. columns[i].Order = ASC
  200. if columns[i].ClusteringOrder == "desc" {
  201. columns[i].Order = DESC
  202. }
  203. } else {
  204. validatorParsed := parseType(columns[i].Validator)
  205. columns[i].Type = validatorParsed.types[0]
  206. columns[i].Order = ASC
  207. if validatorParsed.reversed[0] {
  208. columns[i].Order = DESC
  209. }
  210. }
  211. table := keyspace.Tables[columns[i].Table]
  212. table.Columns[columns[i].Name] = &columns[i]
  213. table.OrderedColumns = append(table.OrderedColumns, columns[i].Name)
  214. }
  215. if protoVersion == 1 {
  216. compileV1Metadata(tables)
  217. } else {
  218. compileV2Metadata(tables)
  219. }
  220. }
  221. // Compiles derived information from TableMetadata which have had
  222. // ColumnMetadata added already. V1 protocol does not return as much
  223. // column metadata as V2+ (because V1 doesn't support the "type" column in the
  224. // system.schema_columns table) so determining PartitionKey and ClusterColumns
  225. // is more complex.
  226. func compileV1Metadata(tables []TableMetadata) {
  227. for i := range tables {
  228. table := &tables[i]
  229. // decode the key validator
  230. keyValidatorParsed := parseType(table.KeyValidator)
  231. // decode the comparator
  232. comparatorParsed := parseType(table.Comparator)
  233. // the partition key length is the same as the number of types in the
  234. // key validator
  235. table.PartitionKey = make([]*ColumnMetadata, len(keyValidatorParsed.types))
  236. // V1 protocol only returns "regular" columns from
  237. // system.schema_columns (there is no type field for columns)
  238. // so the alias information is used to
  239. // create the partition key and clustering columns
  240. // construct the partition key from the alias
  241. for i := range table.PartitionKey {
  242. var alias string
  243. if len(table.KeyAliases) > i {
  244. alias = table.KeyAliases[i]
  245. } else if i == 0 {
  246. alias = DEFAULT_KEY_ALIAS
  247. } else {
  248. alias = DEFAULT_KEY_ALIAS + strconv.Itoa(i+1)
  249. }
  250. column := &ColumnMetadata{
  251. Keyspace: table.Keyspace,
  252. Table: table.Name,
  253. Name: alias,
  254. Type: keyValidatorParsed.types[i],
  255. Kind: ColumnPartitionKey,
  256. ComponentIndex: i,
  257. }
  258. table.PartitionKey[i] = column
  259. table.Columns[alias] = column
  260. }
  261. // determine the number of clustering columns
  262. size := len(comparatorParsed.types)
  263. if comparatorParsed.isComposite {
  264. if len(comparatorParsed.collections) != 0 ||
  265. (len(table.ColumnAliases) == size-1 &&
  266. comparatorParsed.types[size-1].Type() == TypeVarchar) {
  267. size = size - 1
  268. }
  269. } else {
  270. if !(len(table.ColumnAliases) != 0 || len(table.Columns) == 0) {
  271. size = 0
  272. }
  273. }
  274. table.ClusteringColumns = make([]*ColumnMetadata, size)
  275. for i := range table.ClusteringColumns {
  276. var alias string
  277. if len(table.ColumnAliases) > i {
  278. alias = table.ColumnAliases[i]
  279. } else if i == 0 {
  280. alias = DEFAULT_COLUMN_ALIAS
  281. } else {
  282. alias = DEFAULT_COLUMN_ALIAS + strconv.Itoa(i+1)
  283. }
  284. order := ASC
  285. if comparatorParsed.reversed[i] {
  286. order = DESC
  287. }
  288. column := &ColumnMetadata{
  289. Keyspace: table.Keyspace,
  290. Table: table.Name,
  291. Name: alias,
  292. Type: comparatorParsed.types[i],
  293. Order: order,
  294. Kind: ColumnClusteringKey,
  295. ComponentIndex: i,
  296. }
  297. table.ClusteringColumns[i] = column
  298. table.Columns[alias] = column
  299. }
  300. if size != len(comparatorParsed.types)-1 {
  301. alias := DEFAULT_VALUE_ALIAS
  302. if len(table.ValueAlias) > 0 {
  303. alias = table.ValueAlias
  304. }
  305. // decode the default validator
  306. defaultValidatorParsed := parseType(table.DefaultValidator)
  307. column := &ColumnMetadata{
  308. Keyspace: table.Keyspace,
  309. Table: table.Name,
  310. Name: alias,
  311. Type: defaultValidatorParsed.types[0],
  312. Kind: ColumnRegular,
  313. }
  314. table.Columns[alias] = column
  315. }
  316. }
  317. }
  318. // The simpler compile case for V2+ protocol
  319. func compileV2Metadata(tables []TableMetadata) {
  320. for i := range tables {
  321. table := &tables[i]
  322. clusteringColumnCount := componentColumnCountOfType(table.Columns, ColumnClusteringKey)
  323. table.ClusteringColumns = make([]*ColumnMetadata, clusteringColumnCount)
  324. if table.KeyValidator != "" {
  325. keyValidatorParsed := parseType(table.KeyValidator)
  326. table.PartitionKey = make([]*ColumnMetadata, len(keyValidatorParsed.types))
  327. } else { // Cassandra 3.x+
  328. partitionKeyCount := componentColumnCountOfType(table.Columns, ColumnPartitionKey)
  329. table.PartitionKey = make([]*ColumnMetadata, partitionKeyCount)
  330. }
  331. for _, columnName := range table.OrderedColumns {
  332. column := table.Columns[columnName]
  333. if column.Kind == ColumnPartitionKey {
  334. table.PartitionKey[column.ComponentIndex] = column
  335. } else if column.Kind == ColumnClusteringKey {
  336. table.ClusteringColumns[column.ComponentIndex] = column
  337. }
  338. }
  339. }
  340. }
  341. // returns the count of coluns with the given "kind" value.
  342. func componentColumnCountOfType(columns map[string]*ColumnMetadata, kind ColumnKind) int {
  343. maxComponentIndex := -1
  344. for _, column := range columns {
  345. if column.Kind == kind && column.ComponentIndex > maxComponentIndex {
  346. maxComponentIndex = column.ComponentIndex
  347. }
  348. }
  349. return maxComponentIndex + 1
  350. }
  351. // query only for the keyspace metadata for the specified keyspace from system.schema_keyspace
  352. func getKeyspaceMetadata(session *Session, keyspaceName string) (*KeyspaceMetadata, error) {
  353. keyspace := &KeyspaceMetadata{Name: keyspaceName}
  354. if session.useSystemSchema { // Cassandra 3.x+
  355. const stmt = `
  356. SELECT durable_writes, replication
  357. FROM system_schema.keyspaces
  358. WHERE keyspace_name = ?`
  359. var replication map[string]string
  360. iter := session.control.query(stmt, keyspaceName)
  361. if iter.NumRows() == 0 {
  362. return nil, ErrKeyspaceDoesNotExist
  363. }
  364. iter.Scan(&keyspace.DurableWrites, &replication)
  365. err := iter.Close()
  366. if err != nil {
  367. return nil, fmt.Errorf("Error querying keyspace schema: %v", err)
  368. }
  369. keyspace.StrategyClass = replication["class"]
  370. keyspace.StrategyOptions = make(map[string]interface{})
  371. for k, v := range replication {
  372. keyspace.StrategyOptions[k] = v
  373. }
  374. } else {
  375. const stmt = `
  376. SELECT durable_writes, strategy_class, strategy_options
  377. FROM system.schema_keyspaces
  378. WHERE keyspace_name = ?`
  379. var strategyOptionsJSON []byte
  380. iter := session.control.query(stmt, keyspaceName)
  381. if iter.NumRows() == 0 {
  382. return nil, ErrKeyspaceDoesNotExist
  383. }
  384. iter.Scan(&keyspace.DurableWrites, &keyspace.StrategyClass, &strategyOptionsJSON)
  385. err := iter.Close()
  386. if err != nil {
  387. return nil, fmt.Errorf("Error querying keyspace schema: %v", err)
  388. }
  389. err = json.Unmarshal(strategyOptionsJSON, &keyspace.StrategyOptions)
  390. if err != nil {
  391. return nil, fmt.Errorf(
  392. "Invalid JSON value '%s' as strategy_options for in keyspace '%s': %v",
  393. strategyOptionsJSON, keyspace.Name, err,
  394. )
  395. }
  396. }
  397. return keyspace, nil
  398. }
  399. // query for only the table metadata in the specified keyspace from system.schema_columnfamilies
  400. func getTableMetadata(session *Session, keyspaceName string) ([]TableMetadata, error) {
  401. var (
  402. iter *Iter
  403. scan func(iter *Iter, table *TableMetadata) bool
  404. stmt string
  405. keyAliasesJSON []byte
  406. columnAliasesJSON []byte
  407. )
  408. if session.useSystemSchema { // Cassandra 3.x+
  409. stmt = `
  410. SELECT
  411. table_name
  412. FROM system_schema.tables
  413. WHERE keyspace_name = ?`
  414. switchIter := func() *Iter {
  415. iter.Close()
  416. stmt = `
  417. SELECT
  418. view_name
  419. FROM system_schema.views
  420. WHERE keyspace_name = ?`
  421. iter = session.control.query(stmt, keyspaceName)
  422. return iter
  423. }
  424. scan = func(iter *Iter, table *TableMetadata) bool {
  425. r := iter.Scan(
  426. &table.Name,
  427. )
  428. if !r {
  429. iter = switchIter()
  430. if iter != nil {
  431. switchIter = func() *Iter { return nil }
  432. r = iter.Scan(&table.Name)
  433. }
  434. }
  435. return r
  436. }
  437. } else if session.cfg.ProtoVersion < protoVersion4 {
  438. // we have key aliases
  439. // TODO: Do we need key_aliases?
  440. stmt = `
  441. SELECT
  442. columnfamily_name,
  443. key_validator,
  444. comparator,
  445. default_validator,
  446. key_aliases,
  447. column_aliases,
  448. value_alias
  449. FROM system.schema_columnfamilies
  450. WHERE keyspace_name = ?`
  451. scan = func(iter *Iter, table *TableMetadata) bool {
  452. return iter.Scan(
  453. &table.Name,
  454. &table.KeyValidator,
  455. &table.Comparator,
  456. &table.DefaultValidator,
  457. &keyAliasesJSON,
  458. &columnAliasesJSON,
  459. &table.ValueAlias,
  460. )
  461. }
  462. } else {
  463. stmt = `
  464. SELECT
  465. columnfamily_name,
  466. key_validator,
  467. comparator,
  468. default_validator
  469. FROM system.schema_columnfamilies
  470. WHERE keyspace_name = ?`
  471. scan = func(iter *Iter, table *TableMetadata) bool {
  472. return iter.Scan(
  473. &table.Name,
  474. &table.KeyValidator,
  475. &table.Comparator,
  476. &table.DefaultValidator,
  477. )
  478. }
  479. }
  480. iter = session.control.query(stmt, keyspaceName)
  481. tables := []TableMetadata{}
  482. table := TableMetadata{Keyspace: keyspaceName}
  483. for scan(iter, &table) {
  484. var err error
  485. // decode the key aliases
  486. if keyAliasesJSON != nil {
  487. table.KeyAliases = []string{}
  488. err = json.Unmarshal(keyAliasesJSON, &table.KeyAliases)
  489. if err != nil {
  490. iter.Close()
  491. return nil, fmt.Errorf(
  492. "Invalid JSON value '%s' as key_aliases for in table '%s': %v",
  493. keyAliasesJSON, table.Name, err,
  494. )
  495. }
  496. }
  497. // decode the column aliases
  498. if columnAliasesJSON != nil {
  499. table.ColumnAliases = []string{}
  500. err = json.Unmarshal(columnAliasesJSON, &table.ColumnAliases)
  501. if err != nil {
  502. iter.Close()
  503. return nil, fmt.Errorf(
  504. "Invalid JSON value '%s' as column_aliases for in table '%s': %v",
  505. columnAliasesJSON, table.Name, err,
  506. )
  507. }
  508. }
  509. tables = append(tables, table)
  510. table = TableMetadata{Keyspace: keyspaceName}
  511. }
  512. err := iter.Close()
  513. if err != nil && err != ErrNotFound {
  514. return nil, fmt.Errorf("Error querying table schema: %v", err)
  515. }
  516. return tables, nil
  517. }
  518. func (s *Session) scanColumnMetadataV1(keyspace string) ([]ColumnMetadata, error) {
  519. // V1 does not support the type column, and all returned rows are
  520. // of kind "regular".
  521. const stmt = `
  522. SELECT
  523. columnfamily_name,
  524. column_name,
  525. component_index,
  526. validator,
  527. index_name,
  528. index_type,
  529. index_options
  530. FROM system.schema_columns
  531. WHERE keyspace_name = ?`
  532. var columns []ColumnMetadata
  533. rows := s.control.query(stmt, keyspace).Scanner()
  534. for rows.Next() {
  535. var (
  536. column = ColumnMetadata{Keyspace: keyspace}
  537. indexOptionsJSON []byte
  538. )
  539. // all columns returned by V1 are regular
  540. column.Kind = ColumnRegular
  541. err := rows.Scan(&column.Table,
  542. &column.Name,
  543. &column.ComponentIndex,
  544. &column.Validator,
  545. &column.Index.Name,
  546. &column.Index.Type,
  547. &indexOptionsJSON)
  548. if err != nil {
  549. return nil, err
  550. }
  551. if len(indexOptionsJSON) > 0 {
  552. err := json.Unmarshal(indexOptionsJSON, &column.Index.Options)
  553. if err != nil {
  554. return nil, fmt.Errorf(
  555. "Invalid JSON value '%s' as index_options for column '%s' in table '%s': %v",
  556. indexOptionsJSON,
  557. column.Name,
  558. column.Table,
  559. err)
  560. }
  561. }
  562. columns = append(columns, column)
  563. }
  564. if err := rows.Err(); err != nil {
  565. return nil, err
  566. }
  567. return columns, nil
  568. }
  569. func (s *Session) scanColumnMetadataV2(keyspace string) ([]ColumnMetadata, error) {
  570. // V2+ supports the type column
  571. const stmt = `
  572. SELECT
  573. columnfamily_name,
  574. column_name,
  575. component_index,
  576. validator,
  577. index_name,
  578. index_type,
  579. index_options,
  580. type
  581. FROM system.schema_columns
  582. WHERE keyspace_name = ?`
  583. var columns []ColumnMetadata
  584. rows := s.control.query(stmt, keyspace).Scanner()
  585. for rows.Next() {
  586. var (
  587. column = ColumnMetadata{Keyspace: keyspace}
  588. indexOptionsJSON []byte
  589. )
  590. err := rows.Scan(&column.Table,
  591. &column.Name,
  592. &column.ComponentIndex,
  593. &column.Validator,
  594. &column.Index.Name,
  595. &column.Index.Type,
  596. &indexOptionsJSON,
  597. &column.Kind,
  598. )
  599. if err != nil {
  600. return nil, err
  601. }
  602. if len(indexOptionsJSON) > 0 {
  603. err := json.Unmarshal(indexOptionsJSON, &column.Index.Options)
  604. if err != nil {
  605. return nil, fmt.Errorf(
  606. "Invalid JSON value '%s' as index_options for column '%s' in table '%s': %v",
  607. indexOptionsJSON,
  608. column.Name,
  609. column.Table,
  610. err)
  611. }
  612. }
  613. columns = append(columns, column)
  614. }
  615. if err := rows.Err(); err != nil {
  616. return nil, err
  617. }
  618. return columns, nil
  619. }
  620. func (s *Session) scanColumnMetadataSystem(keyspace string) ([]ColumnMetadata, error) {
  621. const stmt = `
  622. SELECT
  623. table_name,
  624. column_name,
  625. clustering_order,
  626. type,
  627. kind,
  628. position
  629. FROM system_schema.columns
  630. WHERE keyspace_name = ?`
  631. var columns []ColumnMetadata
  632. rows := s.control.query(stmt, keyspace).Scanner()
  633. for rows.Next() {
  634. column := ColumnMetadata{Keyspace: keyspace}
  635. err := rows.Scan(&column.Table,
  636. &column.Name,
  637. &column.ClusteringOrder,
  638. &column.Validator,
  639. &column.Kind,
  640. &column.ComponentIndex,
  641. )
  642. if err != nil {
  643. return nil, err
  644. }
  645. columns = append(columns, column)
  646. }
  647. if err := rows.Err(); err != nil {
  648. return nil, err
  649. }
  650. // TODO(zariel): get column index info from system_schema.indexes
  651. return columns, nil
  652. }
  653. // query for only the column metadata in the specified keyspace from system.schema_columns
  654. func getColumnMetadata(session *Session, keyspaceName string) ([]ColumnMetadata, error) {
  655. var (
  656. columns []ColumnMetadata
  657. err error
  658. )
  659. // Deal with differences in protocol versions
  660. if session.cfg.ProtoVersion == 1 {
  661. columns, err = session.scanColumnMetadataV1(keyspaceName)
  662. } else if session.useSystemSchema { // Cassandra 3.x+
  663. columns, err = session.scanColumnMetadataSystem(keyspaceName)
  664. } else {
  665. columns, err = session.scanColumnMetadataV2(keyspaceName)
  666. }
  667. if err != nil && err != ErrNotFound {
  668. return nil, fmt.Errorf("Error querying column schema: %v", err)
  669. }
  670. return columns, nil
  671. }
  672. // type definition parser state
  673. type typeParser struct {
  674. input string
  675. index int
  676. }
  677. // the type definition parser result
  678. type typeParserResult struct {
  679. isComposite bool
  680. types []TypeInfo
  681. reversed []bool
  682. collections map[string]TypeInfo
  683. }
  684. // Parse the type definition used for validator and comparator schema data
  685. func parseType(def string) typeParserResult {
  686. parser := &typeParser{input: def}
  687. return parser.parse()
  688. }
  689. const (
  690. REVERSED_TYPE = "org.apache.cassandra.db.marshal.ReversedType"
  691. COMPOSITE_TYPE = "org.apache.cassandra.db.marshal.CompositeType"
  692. COLLECTION_TYPE = "org.apache.cassandra.db.marshal.ColumnToCollectionType"
  693. LIST_TYPE = "org.apache.cassandra.db.marshal.ListType"
  694. SET_TYPE = "org.apache.cassandra.db.marshal.SetType"
  695. MAP_TYPE = "org.apache.cassandra.db.marshal.MapType"
  696. )
  697. // represents a class specification in the type def AST
  698. type typeParserClassNode struct {
  699. name string
  700. params []typeParserParamNode
  701. // this is the segment of the input string that defined this node
  702. input string
  703. }
  704. // represents a class parameter in the type def AST
  705. type typeParserParamNode struct {
  706. name *string
  707. class typeParserClassNode
  708. }
  709. func (t *typeParser) parse() typeParserResult {
  710. // parse the AST
  711. ast, ok := t.parseClassNode()
  712. if !ok {
  713. // treat this is a custom type
  714. return typeParserResult{
  715. isComposite: false,
  716. types: []TypeInfo{
  717. NativeType{
  718. typ: TypeCustom,
  719. custom: t.input,
  720. },
  721. },
  722. reversed: []bool{false},
  723. collections: nil,
  724. }
  725. }
  726. // interpret the AST
  727. if strings.HasPrefix(ast.name, COMPOSITE_TYPE) {
  728. count := len(ast.params)
  729. // look for a collections param
  730. last := ast.params[count-1]
  731. collections := map[string]TypeInfo{}
  732. if strings.HasPrefix(last.class.name, COLLECTION_TYPE) {
  733. count--
  734. for _, param := range last.class.params {
  735. // decode the name
  736. var name string
  737. decoded, err := hex.DecodeString(*param.name)
  738. if err != nil {
  739. Logger.Printf(
  740. "Error parsing type '%s', contains collection name '%s' with an invalid format: %v",
  741. t.input,
  742. *param.name,
  743. err,
  744. )
  745. // just use the provided name
  746. name = *param.name
  747. } else {
  748. name = string(decoded)
  749. }
  750. collections[name] = param.class.asTypeInfo()
  751. }
  752. }
  753. types := make([]TypeInfo, count)
  754. reversed := make([]bool, count)
  755. for i, param := range ast.params[:count] {
  756. class := param.class
  757. reversed[i] = strings.HasPrefix(class.name, REVERSED_TYPE)
  758. if reversed[i] {
  759. class = class.params[0].class
  760. }
  761. types[i] = class.asTypeInfo()
  762. }
  763. return typeParserResult{
  764. isComposite: true,
  765. types: types,
  766. reversed: reversed,
  767. collections: collections,
  768. }
  769. } else {
  770. // not composite, so one type
  771. class := *ast
  772. reversed := strings.HasPrefix(class.name, REVERSED_TYPE)
  773. if reversed {
  774. class = class.params[0].class
  775. }
  776. typeInfo := class.asTypeInfo()
  777. return typeParserResult{
  778. isComposite: false,
  779. types: []TypeInfo{typeInfo},
  780. reversed: []bool{reversed},
  781. }
  782. }
  783. }
  784. func (class *typeParserClassNode) asTypeInfo() TypeInfo {
  785. if strings.HasPrefix(class.name, LIST_TYPE) {
  786. elem := class.params[0].class.asTypeInfo()
  787. return CollectionType{
  788. NativeType: NativeType{
  789. typ: TypeList,
  790. },
  791. Elem: elem,
  792. }
  793. }
  794. if strings.HasPrefix(class.name, SET_TYPE) {
  795. elem := class.params[0].class.asTypeInfo()
  796. return CollectionType{
  797. NativeType: NativeType{
  798. typ: TypeSet,
  799. },
  800. Elem: elem,
  801. }
  802. }
  803. if strings.HasPrefix(class.name, MAP_TYPE) {
  804. key := class.params[0].class.asTypeInfo()
  805. elem := class.params[1].class.asTypeInfo()
  806. return CollectionType{
  807. NativeType: NativeType{
  808. typ: TypeMap,
  809. },
  810. Key: key,
  811. Elem: elem,
  812. }
  813. }
  814. // must be a simple type or custom type
  815. info := NativeType{typ: getApacheCassandraType(class.name)}
  816. if info.typ == TypeCustom {
  817. // add the entire class definition
  818. info.custom = class.input
  819. }
  820. return info
  821. }
  822. // CLASS := ID [ PARAMS ]
  823. func (t *typeParser) parseClassNode() (node *typeParserClassNode, ok bool) {
  824. t.skipWhitespace()
  825. startIndex := t.index
  826. name, ok := t.nextIdentifier()
  827. if !ok {
  828. return nil, false
  829. }
  830. params, ok := t.parseParamNodes()
  831. if !ok {
  832. return nil, false
  833. }
  834. endIndex := t.index
  835. node = &typeParserClassNode{
  836. name: name,
  837. params: params,
  838. input: t.input[startIndex:endIndex],
  839. }
  840. return node, true
  841. }
  842. // PARAMS := "(" PARAM { "," PARAM } ")"
  843. // PARAM := [ PARAM_NAME ":" ] CLASS
  844. // PARAM_NAME := ID
  845. func (t *typeParser) parseParamNodes() (params []typeParserParamNode, ok bool) {
  846. t.skipWhitespace()
  847. // the params are optional
  848. if t.index == len(t.input) || t.input[t.index] != '(' {
  849. return nil, true
  850. }
  851. params = []typeParserParamNode{}
  852. // consume the '('
  853. t.index++
  854. t.skipWhitespace()
  855. for t.input[t.index] != ')' {
  856. // look for a named param, but if no colon, then we want to backup
  857. backupIndex := t.index
  858. // name will be a hex encoded version of a utf-8 string
  859. name, ok := t.nextIdentifier()
  860. if !ok {
  861. return nil, false
  862. }
  863. hasName := true
  864. // TODO handle '=>' used for DynamicCompositeType
  865. t.skipWhitespace()
  866. if t.input[t.index] == ':' {
  867. // there is a name for this parameter
  868. // consume the ':'
  869. t.index++
  870. t.skipWhitespace()
  871. } else {
  872. // no name, backup
  873. hasName = false
  874. t.index = backupIndex
  875. }
  876. // parse the next full parameter
  877. classNode, ok := t.parseClassNode()
  878. if !ok {
  879. return nil, false
  880. }
  881. if hasName {
  882. params = append(
  883. params,
  884. typeParserParamNode{name: &name, class: *classNode},
  885. )
  886. } else {
  887. params = append(
  888. params,
  889. typeParserParamNode{class: *classNode},
  890. )
  891. }
  892. t.skipWhitespace()
  893. if t.input[t.index] == ',' {
  894. // consume the comma
  895. t.index++
  896. t.skipWhitespace()
  897. }
  898. }
  899. // consume the ')'
  900. t.index++
  901. return params, true
  902. }
  903. func (t *typeParser) skipWhitespace() {
  904. for t.index < len(t.input) && isWhitespaceChar(t.input[t.index]) {
  905. t.index++
  906. }
  907. }
  908. func isWhitespaceChar(c byte) bool {
  909. return c == ' ' || c == '\n' || c == '\t'
  910. }
  911. // ID := LETTER { LETTER }
  912. // LETTER := "0"..."9" | "a"..."z" | "A"..."Z" | "-" | "+" | "." | "_" | "&"
  913. func (t *typeParser) nextIdentifier() (id string, found bool) {
  914. startIndex := t.index
  915. for t.index < len(t.input) && isIdentifierChar(t.input[t.index]) {
  916. t.index++
  917. }
  918. if startIndex == t.index {
  919. return "", false
  920. }
  921. return t.input[startIndex:t.index], true
  922. }
  923. func isIdentifierChar(c byte) bool {
  924. return (c >= '0' && c <= '9') ||
  925. (c >= 'a' && c <= 'z') ||
  926. (c >= 'A' && c <= 'Z') ||
  927. c == '-' ||
  928. c == '+' ||
  929. c == '.' ||
  930. c == '_' ||
  931. c == '&'
  932. }