You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2020/04/07 16:34:36 UTC

[incubator-nlpcraft] branch NLPCRAFT-30 updated: Fixes

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-30
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-30 by this push:
     new cf6c023  Fixes
cf6c023 is described below

commit cf6c02317360af50abde6b81c010a0a177619ec6
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Tue Apr 7 19:34:22 2020 +0300

    Fixes
---
 .../apache/nlpcraft/examples/sql/SqlModel.scala    | 36 +++++++++-
 .../nlpcraft/model/tools/sqlgen/NCSqlSort.java     | 13 ++--
 .../model/tools/sqlgen/impl/NCSqlBeans.scala       | 14 ++--
 .../tools/sqlgen/impl/NCSqlExtractorImpl.scala     | 78 +++++++++++++++-------
 4 files changed, 107 insertions(+), 34 deletions(-)

diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
index ff715f1..3a86cd3 100644
--- a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
+++ b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModel.scala
@@ -80,6 +80,40 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
         Condition(colTok, condTok)
     }
 
+//    private def findSchemaColumn(cols: Seq[NCSqlColumn], tab: String, col: String): NCSqlColumn =
+//        cols.find(_.getColumn == col).getOrElse(throw new IllegalArgumentException(s"Table not found: $tab.$col"))
+//
+//    private def getWithGroup(tok: NCToken, group: String): Seq[NCToken] =
+//        (Seq(tok) ++ tok.findPartTokens().asScala).flatMap(p ⇒ if (p.getGroups.contains(group)) Some(p) else None)
+
+//    private def findAnyTableTokenOpt(schema: NCSqlSchema, tok: NCToken): Option[NCSqlTable] = {
+//        val tabs = getWithGroup(tok, "table")
+//
+//        tabs.size match {
+//            case 1 ⇒ Some(findSchemaTable(schema, tabs.head.meta("sql:name")))
+//
+//            case 0 ⇒ None
+//            case _ ⇒ throw new IllegalArgumentException("Too many tables found")
+//        }
+//    }
+
+    private def getWithGroup(tok: NCToken, group: String): Seq[NCToken] =
+        (Seq(tok) ++ tok.findPartTokens().asScala).flatMap(p ⇒ if (p.getGroups.contains(group)) Some(p) else None)
+
+    private def findAnyColumnTokenOpt(tok: NCToken): Option[NCToken] = {
+        val cols = getWithGroup(tok, "column")
+
+        cols.size match {
+            case 1 ⇒ Some(cols.head)
+
+            case 0 ⇒ None
+            case _ ⇒ throw new IllegalArgumentException(s"Too many columns found for token: $tok")
+        }
+    }
+
+    private def findAnyColumnToken(tok: NCToken): NCToken =
+        findAnyColumnTokenOpt(tok).getOrElse(throw new IllegalArgumentException(s"No columns found for token: $tok"))
+
     def extractNumConditions(ext: NCSqlExtractor, colTok: NCToken, numTok: NCToken): Seq[SqlSimpleCondition] = {
         val col = ext.extractColumn(colTok)
 
@@ -168,7 +202,7 @@ class SqlModel extends NCModelFileAdapter("org/apache/nlpcraft/examples/sql/sql_
             query =
                 SqlBuilder(SCHEMA).
                     withTables(tabs.map(ext.extractTable): _*).
-                    withColumns(cols.map(ext.extractColumn): _*).
+                    withColumns(cols.map(col ⇒ (ext.extractColumn(findAnyColumnToken(col)))): _*).
                     withAndConditions(extractValuesConditions(ext, condVals): _*).
                     withAndConditions(
                         condDates.map(t ⇒ extractColumnAndCondition(t, "nlpcraft:date")).flatMap(h ⇒
diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java
index 319df8e..dc92395 100644
--- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java
+++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/NCSqlSort.java
@@ -19,6 +19,8 @@ package org.apache.nlpcraft.model.tools.sqlgen;
 
 import org.apache.nlpcraft.model.*;
 
+import java.util.List;
+
 /**
  * Object presentation of SQL sorting.
  * 
@@ -27,11 +29,14 @@ import org.apache.nlpcraft.model.*;
  */
 public interface NCSqlSort {
     /**
-     * Gets SQL column by which to sort.
-     *
-     * @return SQL column by which to sort.
+     * TODO:
+     */
+    List<NCSqlColumn> getSubj();
+
+    /**
+     * TODO:
      */
-    NCSqlColumn getColumn();
+    List<NCSqlColumn> getBy();
 
     /**
      * Gets sorting direction.
diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala
index 8dff725..adc77e4 100644
--- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala
+++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlBeans.scala
@@ -68,12 +68,14 @@ case class NCSqlJoinImpl(fromTable: String, toTable: String, fromColumns: Seq[St
 }
 
 /**
-  *
-  * @param column
-  * @param asc
-  */
-case class NCSqlSortImpl(column: NCSqlColumn, asc: Boolean) extends NCSqlSort {
-    override def getColumn: NCSqlColumn = column
+ *
+ * @param subj
+ * @param by
+ * @param asc
+ */
+case class NCSqlSortImpl(subj: Seq[NCSqlColumn], by: Seq[NCSqlColumn], asc: Boolean) extends NCSqlSort {
+    override def getSubj: util.List[NCSqlColumn] = subj.asJava
+    override def getBy: util.List[NCSqlColumn] = by.asJava
     override def isAscending: Boolean = asc
 }
 
diff --git a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala
index 96bbf84..e29648a 100644
--- a/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala
+++ b/src/main/scala/org/apache/nlpcraft/model/tools/sqlgen/impl/NCSqlExtractorImpl.scala
@@ -25,6 +25,7 @@ import org.apache.nlpcraft.model.tools.sqlgen._
 import org.apache.nlpcraft.model._
 
 import scala.collection.JavaConverters._
+import scala.compat.java8.OptionConverters._
 
 /**
   *
@@ -114,20 +115,29 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE
       * @return
       */
     private def getReference(refTok: NCToken): Option[NCSqlColumn] = {
-        val tok = getLinkBySingleIndex(variant, refTok)
-        
+        val tok = getLinks(variant, refTok)
+
         findAnyColumnTokenOpt(tok) match {
             // If reference is column - sort by column.
             case Some(t) ⇒ Some(extractColumn(t))
             case None ⇒
-                // If reference is table -  sort by any PK column of table.
+                // If reference is table - sort by any PK column of table.
                 findAnyTableTokenOpt(tok) match {
                     case Some(tab) ⇒ Some(tab.getColumns.asScala.minBy(col ⇒ if (col.isPk) 0 else 1))
                     case None ⇒ None
                 }
         }
     }
-    
+
+    /**
+     *
+     * @param refToks
+     * @return
+     */
+    private def getReferences(refToks: Seq[NCToken]): Seq[NCSqlColumn] = {
+        refToks
+    }
+
     /**
       *
       * @param limitTok
@@ -138,12 +148,33 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE
         
         // Skips indexes to simplify.
         val limit: Double = limitTok.metax("nlpcraft:limit:limit")
-        
-        NCSqlLimitImpl(
-            getReference(limitTok).getOrElse(throw new NCException(s"Limit not found for: $limitTok")),
-            limit.intValue(),
-            limitTok.metax("nlpcraft:limit:asc")
-        )
+
+        val links = getLinks(variant, limitTok, "indexes", "note", false)
+
+        val size = links.size
+
+        size match {
+            case 1 ⇒
+                val link = links.head
+
+                val res = findAnyColumnTokenOpt(link) match {
+                    // If reference is column - sort by column.
+                    case Some(t) ⇒ extractColumn(t)
+                    case None ⇒
+                        // If reference is table - sort by any PK column of table.
+                        findAnyTableTokenOpt(link) match {
+                            case Some(tab) ⇒ tab.getColumns.asScala.minBy(col ⇒ if (col.isPk) 0 else 1)
+                            case None ⇒ throw new NCException("TODO:")
+                        }
+                }
+
+                NCSqlLimitImpl(
+                    res,
+                    limit.intValue(),
+                    limitTok.metax("nlpcraft:limit:asc")
+                )
+            case  _ ⇒ throw new NCException(s"Unexpected LIMIT links count: $size")
+        }
     }
     
     /**
@@ -205,25 +236,26 @@ class NCSqlExtractorImpl(schema: NCSqlSchema, variant: NCVariant) extends NCSqlE
       * @param tok
       * @return
       */
-    private def getLinkBySingleIndex(variant: NCVariant, tok: NCToken): NCToken = {
-        val idxs: util.List[Integer] = tok.metax(s"${tok.getId}:indexes")
+    private def getLinks(variant: NCVariant, tok: NCToken, idxField: String, noteField: String, canBeEmpty: Boolean): Seq[NCToken] = {
+        val idxsOpt: Option[util.List[Integer]] = tok.metaOpt(s"${tok.getId}:$idxField").asScala
         
-        if (idxs.isEmpty)
+        if (idxsOpt.isEmpty && !canBeEmpty)
             throw new NCException(s"Empty indexes for: $tok")
 
-        val idx = idxs.get(0)
+        idxsOpt.get.asScala.map(idx ⇒ {
+            if (idx < variant.size) {
+                val note: String = tok.metax(s"${tok.getId}:$noteField")
 
-        if (idx < variant.size) {
-            val note: String = tok.metax(s"${tok.getId}:note")
+                val link = variant.get(idx)
 
-            val link = variant.get(idx)
+                if (link.getId != note)
+                    throw new NCException(s"Unexpected token with index: $idx, type: $note")
 
-            if (link.getId != note)
-                throw new NCException(s"Unexpected token with index: $idx, type: $note")
+                link
+            }
+            else
+                throw new NCException(s"Token not found with index: $idx")
+        })
 
-            link
-        }
-        else
-            throw new NCException(s"Token not found with index: $idx")
     }
 }