Commit 543f3a72 by Harish Butani

support navigation from an instance to a trait

parent 665165ed
......@@ -406,7 +406,15 @@ object Expressions {
val children = if (child.isDefined) List(child.get) else Nil
lazy val dataType = {
val t = if (!fieldInfo.isReverse) fieldInfo.attrInfo.dataType() else fieldInfo.reverseDataType
val t = {
if (fieldInfo.traitName != null ) {
typSystem.getDataType(classOf[TraitType], fieldInfo.traitName)
} else if (!fieldInfo.isReverse) {
fieldInfo.attrInfo.dataType()
} else {
fieldInfo.reverseDataType
}
}
elemType(t)
}
override lazy val resolved: Boolean = true
......
......@@ -67,8 +67,9 @@ trait GraphPersistenceStrategies {
def traitNames(v: TitanVertex): java.util.List[String]
def edgeLabel(fInfo: FieldInfo): String = fInfo match {
case FieldInfo(dataType, aInfo, null) => edgeLabel(dataType, aInfo)
case FieldInfo(dataType, aInfo, reverseDataType) => edgeLabel(reverseDataType, aInfo)
case FieldInfo(dataType, aInfo, null, null) => edgeLabel(dataType, aInfo)
case FieldInfo(dataType, aInfo, reverseDataType, null) => edgeLabel(reverseDataType, aInfo)
case FieldInfo(dataType, null, null, traitName) => traitLabel(dataType, traitName)
}
def fieldPrefixInSelect: String
......
......@@ -176,6 +176,16 @@ class GremlinTranslator(expr: Expression,
case None => step
}
}
case fe@FieldExpression(fieldName, fInfo, child)
if fInfo.traitName != null => {
val direction = "out"
val edgeLbl = gPersistenceBehavior.edgeLabel(fInfo)
val step = s"""$direction("$edgeLbl")"""
child match {
case Some(e) => s"${genQuery(e, inSelect)}.$step"
case None => step
}
}
case c@ComparisonExpression(symb, f@FieldExpression(fieldName, fInfo, ch), l) => {
ch match {
case Some(child) =>
......
......@@ -65,6 +65,10 @@ class Resolver(srcExpr: Option[Expression] = None, aliases: Map[String, Expressi
if (fInfo.isDefined) {
return new FieldExpression(fieldName, fInfo.get, Some(child))
}
val tType = resolveAsTraitType(fieldName)
if (tType.isDefined) {
return new FieldExpression(fieldName, FieldInfo(child.dataType, null, null, fieldName), Some(child))
}
f
}
case isTraitLeafExpression(traitName, classExpression)
......
......@@ -77,10 +77,16 @@ object TypeUtils {
}
import scala.language.existentials
case class FieldInfo(dataType : IDataType[_], attrInfo : AttributeInfo, reverseDataType : IDataType[_] = null) {
case class FieldInfo(dataType : IDataType[_],
attrInfo : AttributeInfo,
reverseDataType : IDataType[_] = null,
traitName : String = null) {
def isReverse = reverseDataType != null
override def toString : String = {
if ( reverseDataType == null ) {
if ( traitName != null ) {
s"""FieldInfo("${dataType.getName}", "$traitName")"""
}
else if ( reverseDataType == null ) {
s"""FieldInfo("${dataType.getName}", "${attrInfo.name}")"""
} else {
s"""FieldInfo("${dataType.getName}", "${attrInfo.name}", "${reverseDataType.getName}")"""
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.metadata.query
import com.thinkaurelius.titan.core.TitanGraph
import org.apache.hadoop.metadata.query.Expressions._
import org.apache.hadoop.metadata.typesystem.types.TypeSystem
import org.junit.runner.RunWith
import org.scalatest._
import Matchers._
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class GremlinTest2 extends FunSuite with BeforeAndAfterAll {
var g: TitanGraph = null
override def beforeAll() {
TypeSystem.getInstance().reset()
QueryTestsUtils.setupTypes
g = QueryTestsUtils.setupTestGraph
}
override def afterAll() {
g.shutdown()
}
val STRUCT_NAME_REGEX = (TypeUtils.TEMP_STRUCT_NAME_PREFIX + "\\d+").r
def validateJson(r: GremlinQueryResult, expected: String = null): Unit = {
val rJ = r.toJson
if (expected != null) {
val a = STRUCT_NAME_REGEX.replaceAllIn(rJ, "")
val b = STRUCT_NAME_REGEX.replaceAllIn(expected, "")
Assertions.assert(a == b)
} else {
println(rJ)
}
}
test("testTraitSelect") {
val r = QueryProcessor.evaluate(_class("Table").as("t").join("Dimension").as("dim").select(id("t"), id("dim")), g)
validateJson(r, "{\n \"query\":\"Table as t.Dimension as dim select t as _col_0, dim as _col_1\",\n \"dataType\":{\n \"typeName\":\"\",\n \"attributeDefinitions\":[\n {\n \"name\":\"_col_0\",\n \"dataTypeName\":\"Table\",\n \"multiplicity\":{\n \"lower\":0,\n \"upper\":1,\n \"isUnique\":false\n },\n \"isComposite\":false,\n \"isUnique\":false,\n \"isIndexable\":true,\n \"reverseAttributeName\":null\n },\n {\n \"name\":\"_col_1\",\n \"dataTypeName\":\"Dimension\",\n \"multiplicity\":{\n \"lower\":0,\n \"upper\":1,\n \"isUnique\":false\n },\n \"isComposite\":false,\n \"isUnique\":false,\n \"isIndexable\":true,\n \"reverseAttributeName\":null\n }\n ]\n },\n \"rows\":[\n {\n \"$typeName$\":\"\",\n \"_col_1\":{\n \"$typeName$\":\"Dimension\"\n },\n \"_col_0\":{\n \"id\":\"3328\",\n \"$typeName$\":\"Table\",\n \"version\":0\n }\n },\n {\n \"$typeName$\":\"\",\n \"_col_1\":{\n \"$typeName$\":\"Dimension\"\n },\n \"_col_0\":{\n \"id\":\"4864\",\n \"$typeName$\":\"Table\",\n \"version\":0\n }\n },\n {\n \"$typeName$\":\"\",\n \"_col_1\":{\n \"$typeName$\":\"Dimension\"\n },\n \"_col_0\":{\n \"id\":\"6656\",\n \"$typeName$\":\"Table\",\n \"version\":0\n }\n }\n ]\n}")
}
}
\ No newline at end of file
......@@ -87,7 +87,11 @@ public class TypeSystem {
public <T> T getDataType(Class<T> cls, String name) throws MetadataException {
if (types.containsKey(name)) {
try {
return cls.cast(types.get(name));
} catch(ClassCastException cce) {
throw new MetadataException(cce);
}
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment