Skip to content

Commit

Permalink
Fix sqls (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin authored Nov 29, 2023
1 parent 9b127a8 commit 69a3e99
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,27 @@ object StringEx {
line = line.concat(" ")
sb.append(line)
}
initSqlList = getInitSql(sb.toString)
initSqlList = getMultipleSqls(sb.toString)
catch
case e: IOException =>
throw BitlapSQLException("Invalid sql syntax in initFile", cause = Option(e))
finally if br != null then br.close()
initSqlList

def getSqlStmts(lines: List[String]): List[String] = {
val sb = new mutable.StringBuilder("")
lines.map(_.trim).foreach { line =>
def getSqlStmts(sqls: String): List[String] = {
val lines = List(if (sqls.endsWith(";")) sqls else sqls + ";")
val sb = new mutable.StringBuilder("")
lines.map(_.trim).filter(_.nonEmpty).foreach { line =>
if line.nonEmpty then
if !line.startsWith("#") && !line.startsWith("--") then {
sb.append(line.concat(" "))
}
}
getInitSql(sb.toString)
getMultipleSqls(sb.toString)

}

private def getInitSql(sbLine: String): List[String] =
private def getMultipleSqls(sbLine: String): List[String] =
val sqlArray = sbLine.toCharArray
val initSqlList = new JArrayList[String]
var index = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SqlLoadData(
extends BitlapSqlDdlNode(_pos, SqlLoadData.OPERATOR, List(filePath, tableName)) {

override val resultTypes: List[(String, SqlTypeName)] = List(
"result" -> SqlTypeName.VARCHAR
"result" -> SqlTypeName.BOOLEAN
)

override def operator(context: DataContext): List[Array[Any]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class BitlapSingleResult(
}
} catch
case NonFatal(e) =>
throw BitlapSQLException("Fetch metadata failed", cause = Some(e))
throw BitlapSQLException("Execute SQL failed", cause = Some(e))
}

private lazy val tableSchema: TableSchema = {
Expand All @@ -56,10 +56,22 @@ final class BitlapSingleResult(
} catch
case NonFatal(e) =>
throw BitlapSQLException("Fetch metadata failed", cause = Some(e))
}

private lazy val fetchResult: FetchResults = {

try {
if (operationId != null) {
sync.fetchResults(operationId, 1000, 1)
} else {
throw BitlapRuntimeException(s"Invalid operationId: $operationId")
}
} catch
case NonFatal(e) =>
throw BitlapSQLException("Fetch result failed", cause = Some(e))
}
private lazy val fetchResult: FetchResults = sync.fetchResults(operationId, 1000, 1)
private lazy val result: Result = Result(tableSchema, fetchResult)

private lazy val result: Result = Result(tableSchema, fetchResult)

override def iterator: Iterator[Result] = Iterator.single(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import enumeratum.values.*
sealed abstract class GetInfoType(val value: Int) extends IntEnumEntry

object GetInfoType extends IntEnum[GetInfoType] {

case object Unknown extends GetInfoType(0)

case object MaxDriverConnections extends GetInfoType(1)

case object MaxConcurrentActivities extends GetInfoType(10)
Expand All @@ -41,7 +44,7 @@ object GetInfoType extends IntEnum[GetInfoType] {
def toGetInfoType(getInfoType: BGetInfoType): GetInfoType =
GetInfoType
.withValueOpt(getInfoType.value)
.getOrElse(throw new IllegalArgumentException(s"Invalid GetInfoType: $getInfoType"))
.getOrElse(Unknown)

def toBGetInfoType(getInfoType: GetInfoType): BGetInfoType =
BGetInfoType.fromValue(getInfoType.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,36 @@
*/
package org.bitlap.server

import zio.Runtime
import zio.{ Runtime, Trace }
import zio.logging.{ LogColor, LogFilter, LogFormat }
import zio.logging.LogFormat.*
import zio.logging.LogFormat.{ quoted, * }
import zio.logging.backend.SLF4J

object BitlapLogFormat {

private val traceFormat = LogFormat.make {
(
builder,
trace,
_,
_,
_,
_,
_,
_,
_
) =>
val text = trace match
case Trace(location, file, line) => s" $location:$line"
case t => s" $t"
builder.appendText(text)
}

// we don't need to print the time and log level in zio anymore because they already exist in log4j2.
val colored: LogFormat =
label("thread", fiberId).color(LogColor.WHITE) |-|
label("message", quoted(line)).highlight +
label("message", quoted(line)).highlight |-|
label("trace", traceFormat).color(LogColor.BLUE) +
(space + label("cause", cause).highlight).filter(LogFilter.causeNonEmpty)

val slf4j = Runtime.removeDefaultLoggers >>> SLF4J.slf4j(colored)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,22 @@ class SqlService(context: BitlapGlobalContext) extends BitlapLogging {
private val conf = context.config.grpcConfig

def execute(sql: String): ZIO[Any, Throwable, SqlData] = {
context.getSyncConnection.map(
{ syncConnect =>
try {
syncConnect.use { conn =>
conn.open(ServerAddress(conf.host, conf.port))
val rss = StringEx.getSqlStmts(sql.split("\n").toList).map { sql =>
conn
.execute(sql)
.headOption
.map { result =>
SqlData.fromList(result.underlying)
}
.toList
ZIO.acquireReleaseWith(context.getSyncConnection)(c => ZIO.attempt(c.close()).ignoreLogged) { conn =>
conn.open(ServerAddress(conf.host, conf.port))
ZIO.attempt {
val rss = StringEx.getSqlStmts(sql).map { sql =>
conn
.execute(sql)
.headOption
.map { result =>
SqlData.fromList(result.underlying)
}
rss.flatten.lastOption.getOrElse(
SqlData.empty
)
}
} catch {
case NonFatal(e) =>
log.error("Execute sql failed", e)
throw BitlapExceptions.httpException(-1, e.getMessage)
.toList
}
rss.flatten.lastOption.getOrElse(
SqlData.empty
)
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ final class SimpleLocalSession(
if op.state.terminal then {
ZIO.logInfo(s"$operationHandle Operation is already aborted in state - ${op.state}")
} else {
op.setState(OperationState.CanceledState)
ZIO.logInfo(s"$operationHandle Attempting to cancel from state - ${op.state}") *> removeOperation(
operationHandle
).unit
ZIO.attemptBlocking {
op.setState(OperationState.CanceledState)
} *>
ZIO.logInfo(s"$operationHandle Attempting to cancel from state - ${op.state}") *> removeOperation(
operationHandle
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import java.sql.*

import scala.collection.mutable.ListBuffer

import org.bitlap.common.exception.DataFormatException
import org.bitlap.common.BitlapLogging
import org.bitlap.common.exception.{ BitlapException, DataFormatException }
import org.bitlap.core.*
import org.bitlap.core.sql.QueryExecution
import org.bitlap.network.enumeration.*
Expand All @@ -36,7 +37,8 @@ final class SimpleOperation(
opType: OperationType,
hasResultSet: Boolean = false
)(using globalConfig: BitlapConfiguration)
extends Operation(parentSession, opType, hasResultSet, globalConfig) {
extends Operation(parentSession, opType, hasResultSet, globalConfig)
with BitlapLogging {

override def run(): Task[Unit] = {
for {
Expand All @@ -53,7 +55,7 @@ final class SimpleOperation(
} catch {
case e: Exception =>
super.setState(OperationState.ErrorState)
throw e
logger.error("Simple operation run failed", e)
}
schema
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ abstract class BaseSpec extends AnyFunSuite with BeforeAndAfterAll with should.M
server.start()
Thread.sleep(5000L)
}

override protected def afterAll(): Unit = {
server.interrupt()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ClientOperationSpec extends BaseSpec {
.map(_.underlying)
.getOrElse(List.empty)

assert(cr == List(List(("Boolean", "true"))))
assert(ld == List(List(("String", "true"))))
assert(cr == List(List(("result", "true"))))
assert(ld == List(List(("result", "true"))))

val rs = sync
.execute(
Expand Down
18 changes: 17 additions & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ import org.bitlap.server.BitlapServer
}
```

这个`main`方法将读取测试的配置来启动服务
这个`main`方法将读取测试的配置来启动服务

## 初始化数据

```sql
create table if not exists bitlap_test_table;

load data 'classpath:simple_data.csv' overwrite table bitlap_test_table;
```

## 查询

```sql
select _time, sum(vv) as vv, sum(pv) as pv, count(distinct pv) as uv
from bitlap_test_table
where _time >= 0 group by _time;
```

0 comments on commit 69a3e99

Please sign in to comment.